001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.actions;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.net.*;
024: import java.rmi.RemoteException;
025: import java.util.*;
026:
027: import javax.swing.*;
028: import javax.xml.rpc.ServiceException;
029:
030: import org.openharmonise.him.actions.rules.*;
031: import org.openharmonise.him.harmonise.UserConfigClient;
032: import org.openharmonise.him.window.session.SessionEventData;
033: import org.openharmonise.vfs.*;
034: import org.openharmonise.vfs.authentication.AuthInfo;
035: import org.openharmonise.vfs.context.*;
036: import org.openharmonise.vfs.servers.*;
037:
038: /**
039: * Class to make development of Actions within Harmonise Information Manager easier.
040: *
041: * @author Matthew Large
042: * @version $Revision: 1.4 $
043: *
044: */
045: public abstract class AbstractHIMAction implements ActionListener {
046:
047: /**
048: * Virtual file that is the focus of the action.
049: */
050: private VirtualFile m_vfFile = null;
051:
052: /**
053: * Button that triggers action.
054: */
055: private JButton m_button = null;
056:
057: /**
058: * Menu item that triggers action.
059: */
060: private JMenuItem m_menuItem = null;
061:
062: /**
063: * List of {@link EnableRule} objects.
064: */
065: private ArrayList m_aEnableRules = new ArrayList();
066:
067: /**
068: * true if the current user has permissions to see this action.
069: */
070: protected boolean m_bShow = false;
071:
072: /**
073: * true if the current user's persmissions have been checked.
074: */
075: protected boolean m_bUserChecked = false;
076:
077: /**
078: *
079: */
080: public AbstractHIMAction() {
081: super ();
082: }
083:
084: /**
085: * Constructs a new abstract action.
086: *
087: * @param vfFile Virtual file that action will focus on
088: */
089: public AbstractHIMAction(VirtualFile vfFile) {
090: super ();
091: m_vfFile = vfFile;
092: }
093:
094: /* (non-Javadoc)
095: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
096: */
097: public abstract void actionPerformed(ActionEvent arg0);
098:
099: /**
100: * Returns the virtual file that the action is focused on.
101: *
102: * @return Virtual file
103: */
104: protected VirtualFile getPrimaryFile() {
105: VirtualFile vfFile = null;
106:
107: if (this .m_vfFile != null) {
108: vfFile = this .m_vfFile;
109: } else {
110: vfFile = this .getLastContextFile();
111: }
112:
113: return vfFile;
114: }
115:
116: /**
117: * Returns the virtual file from the last file context event.
118: *
119: * @return Last file context virtual file
120: */
121: protected VirtualFile getLastContextFile() {
122: ContextEvent ce = ContextHandler.getInstance().getLastEvent(
123: ContextType.CONTEXT_FILES);
124: return ce.getVFS().getVirtualFile(ce.getPath()).getResource();
125: }
126:
127: /**
128: * Returns the collection that the action is focused on.
129: *
130: * @return Collection
131: */
132: protected VirtualFile getPrimaryDirectory() {
133: VirtualFile vfFile = null;
134:
135: if (this .m_vfFile != null) {
136: vfFile = this .m_vfFile;
137: } else {
138: vfFile = this .getLastContextDirectory();
139: }
140:
141: return vfFile;
142: }
143:
144: /**
145: * Returns the virtual file from the last collection context event.
146: *
147: * @return Last collection context virtual file
148: */
149: protected VirtualFile getLastContextDirectory() {
150: ContextEvent ce = ContextHandler.getInstance().getLastEvent(
151: ContextType.CONTEXT_DIRS);
152: if (ce != null && ce.getVFS() != null) {
153: return ce.getVFS().getVirtualFile(ce.getPath())
154: .getResource();
155: } else {
156: return null;
157: }
158: }
159:
160: /* (non-Javadoc)
161: * @see com.simulacramedia.contentmanager.actions.CMAction#getButton()
162: */
163: public JButton getButton() {
164: if (this .m_button == null) {
165: this .m_button = new JButton(this .getText());
166: this .m_button.setToolTipText(this .getToolTip());
167: this .m_button.setIcon(this .getIcon());
168: this .m_button.addActionListener(this );
169:
170: String fontName = "Dialog";
171: int fontSize = 11;
172: Font font = new Font(fontName, Font.PLAIN, fontSize);
173: this .m_button.setFont(font);
174: Dimension dim = new Dimension(this .m_button
175: .getPreferredSize().width, 25);
176: this .m_button.setPreferredSize(dim);
177: }
178:
179: return this .m_button;
180: }
181:
182: /* (non-Javadoc)
183: * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
184: */
185: public JMenuItem getMenuItem() {
186: if (this .m_menuItem == null) {
187: this .m_menuItem = new JMenuItem(this .getText());
188: this .m_menuItem.setToolTipText(this .getToolTip());
189: this .m_menuItem.setIcon(this .getIcon());
190: this .m_menuItem.addActionListener(this );
191: this .m_menuItem.setMnemonic(this .getMnemonic().charAt(0));
192:
193: String fontName = "Dialog";
194: int fontSize = 11;
195: Font font = new Font(fontName, Font.PLAIN, fontSize);
196: this .m_menuItem.setFont(font);
197: }
198:
199: return this .m_menuItem;
200: }
201:
202: /**
203: * Adds a rule to action.
204: *
205: * @param rule Rule to add
206: */
207: protected void addEnableRule(EnableRule rule) {
208: this .m_aEnableRules.add(rule);
209: }
210:
211: /**
212: * Checks if an action, and therefore its buttons and menu items,
213: * is enabled. This is based on the file that the action is
214: * focuesed on, the context and the enable rules.
215: *
216: * @param ce Context event to check against
217: * @return true if the action is enabled
218: */
219: public boolean isEnabled(ContextEvent ce) {
220: boolean bEnabled = true;
221:
222: if (ce.CONTEXT_TYPE != ContextType.CONTEXT_TABS) {
223: VirtualFile vfFile = ce.getVFS().getVirtualFile(
224: ce.getPath()).getResource();
225: if (this .m_aEnableRules.size() > 0) {
226: bEnabled = false;
227: Iterator itor = this .m_aEnableRules.iterator();
228: while (itor.hasNext()) {
229: if (((EnableRule) itor.next()).isEnabled(vfFile)) {
230: bEnabled = true;
231: }
232: }
233: }
234: } else if (ce.CONTEXT_TYPE == ContextType.CONTEXT_TABS) {
235: if (this .m_aEnableRules.size() > 0) {
236: bEnabled = false;
237: Iterator itor = this .m_aEnableRules.iterator();
238: while (itor.hasNext()) {
239: EnableRule tempRule = (EnableRule) itor.next();
240: if (tempRule instanceof TabRule
241: && ((TabRule) tempRule).isEnabled(ce
242: .getMessage().trim())) {
243: bEnabled = true;
244: }
245: }
246: }
247: } else {
248: bEnabled = true;
249: }
250:
251: this .setEnabled(bEnabled);
252:
253: return bEnabled;
254: }
255:
256: /**
257: * Sets whether the action is enabled or not.
258: *
259: * @param bEnabled true to set the action to be enabled
260: */
261: public void setEnabled(boolean bEnabled) {
262: if (this .m_button != null) {
263: this .m_button.setEnabled(bEnabled);
264: }
265: if (this .m_menuItem != null) {
266: this .m_menuItem.setEnabled(bEnabled);
267: }
268: }
269:
270: /**
271: * Returns the text name of the action.
272: *
273: * @return Name
274: */
275: public abstract String getText();
276:
277: /**
278: * Returns the tooltip text for the action.
279: *
280: * @return Tooltip text
281: */
282: public abstract String getToolTip();
283:
284: /**
285: * Returns the icon for the action.
286: *
287: * @return Icon
288: */
289: public abstract Icon getIcon();
290:
291: /**
292: * Returns the Mnemonic for the action.
293: *
294: * @return Mnemonic
295: */
296: public abstract String getMnemonic();
297:
298: /**
299: * Utility method to help implementors fire session events.
300: *
301: * @param sMessage Message
302: * @param vfs Virtual file system
303: * @param sPath Full path
304: * @param sSessionEventType Event type
305: */
306: protected void fireSessionEvent(String sMessage,
307: AbstractVirtualFileSystem vfs, String sPath,
308: String sSessionEventType) {
309: try {
310: SessionEventData sed = new SessionEventData(
311: sSessionEventType);
312: ContextEvent ce = new ContextEvent(
313: ContextType.CONTEXT_SESSION_EVENT, sMessage, vfs,
314: sPath);
315: ce.setContextData(sed);
316: ContextHandler.getInstance().fireContextEvent(ce);
317: } catch (Exception e) {
318: e.printStackTrace();
319: }
320: }
321:
322: /**
323: * Checks the current user's permissions to see if they are allowed
324: * to see this action.
325: *
326: */
327: protected void checkUser() {
328: Server server = null;
329: server = ServerList.getInstance().getHarmoniseServer();
330: URI uri = server.getURI();
331:
332: String sURI = uri.getScheme() + "://" + uri.getHost() + ":"
333: + uri.getPort() + "/webdav/services/HarmoniseService";
334: URL url = null;
335: try {
336: url = new URL(sURI);
337: } catch (MalformedURLException e2) {
338: e2.printStackTrace();
339: System.exit(1);
340: }
341:
342: AuthInfo auth = server.getVFS().getAuthentication();
343:
344: try {
345: if (UserConfigClient.isSuperUser(url, auth.getUsername(),
346: auth.getPassword())) {
347: this .m_bShow = true;
348: }
349: } catch (RemoteException e) {
350: e.printStackTrace();
351: } catch (ServiceException e) {
352: e.printStackTrace();
353: }
354: this .m_bUserChecked = true;
355: }
356: }
|