KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > AbstractHIMAction


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.actions;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.net.*;
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.*;
26
27 import javax.swing.*;
28 import javax.xml.rpc.ServiceException JavaDoc;
29
30 import org.openharmonise.him.actions.rules.*;
31 import org.openharmonise.him.harmonise.UserConfigClient;
32 import org.openharmonise.him.window.session.SessionEventData;
33 import org.openharmonise.vfs.*;
34 import org.openharmonise.vfs.authentication.AuthInfo;
35 import org.openharmonise.vfs.context.*;
36 import org.openharmonise.vfs.servers.*;
37
38
39 /**
40  * Class to make development of Actions within Harmonise Information Manager easier.
41  *
42  * @author Matthew Large
43  * @version $Revision: 1.4 $
44  *
45  */

46 public abstract class AbstractHIMAction implements ActionListener {
47
48     /**
49      * Virtual file that is the focus of the action.
50      */

51     private VirtualFile m_vfFile = null;
52     
53     /**
54      * Button that triggers action.
55      */

56     private JButton m_button = null;
57     
58     /**
59      * Menu item that triggers action.
60      */

61     private JMenuItem m_menuItem = null;
62     
63     /**
64      * List of {@link EnableRule} objects.
65      */

66     private ArrayList m_aEnableRules = new ArrayList();
67
68     /**
69      * true if the current user has permissions to see this action.
70      */

71     protected boolean m_bShow = false;
72     
73     /**
74      * true if the current user's persmissions have been checked.
75      */

76     protected boolean m_bUserChecked = false;
77
78     /**
79      *
80      */

81     public AbstractHIMAction() {
82         super();
83     }
84     
85     /**
86      * Constructs a new abstract action.
87      *
88      * @param vfFile Virtual file that action will focus on
89      */

90     public AbstractHIMAction(VirtualFile vfFile) {
91         super();
92         m_vfFile = vfFile;
93     }
94
95     /* (non-Javadoc)
96      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
97      */

98     public abstract void actionPerformed(ActionEvent arg0);
99
100     /**
101      * Returns the virtual file that the action is focused on.
102      *
103      * @return Virtual file
104      */

105     protected VirtualFile getPrimaryFile() {
106         VirtualFile vfFile = null;
107         
108         if(this.m_vfFile!=null) {
109             vfFile = this.m_vfFile;
110         } else {
111             vfFile = this.getLastContextFile();
112         }
113         
114         return vfFile;
115     }
116     
117     /**
118      * Returns the virtual file from the last file context event.
119      *
120      * @return Last file context virtual file
121      */

122     protected VirtualFile getLastContextFile() {
123         ContextEvent ce = ContextHandler.getInstance().getLastEvent(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(ContextType.CONTEXT_DIRS);
151         if(ce!=null && ce.getVFS()!=null) {
152             return ce.getVFS().getVirtualFile(ce.getPath()).getResource();
153         } else {
154             return null;
155         }
156     }
157
158     /* (non-Javadoc)
159      * @see com.simulacramedia.contentmanager.actions.CMAction#getButton()
160      */

161     public JButton getButton() {
162         if(this.m_button==null) {
163             this.m_button = new JButton(this.getText());
164             this.m_button.setToolTipText(this.getToolTip());
165             this.m_button.setIcon(this.getIcon());
166             this.m_button.addActionListener(this);
167         
168             String JavaDoc fontName = "Dialog";
169             int fontSize = 11;
170             Font font = new Font(fontName, Font.PLAIN, fontSize);
171             this.m_button.setFont(font);
172             Dimension dim = new Dimension(this.m_button.getPreferredSize().width, 25);
173             this.m_button.setPreferredSize(dim);
174         }
175         
176         return this.m_button;
177     }
178
179     /* (non-Javadoc)
180      * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
181      */

182     public JMenuItem getMenuItem() {
183         if(this.m_menuItem==null) {
184             this.m_menuItem = new JMenuItem(this.getText());
185             this.m_menuItem.setToolTipText(this.getToolTip());
186             this.m_menuItem.setIcon(this.getIcon());
187             this.m_menuItem.addActionListener(this);
188             this.m_menuItem.setMnemonic(this.getMnemonic().charAt(0));
189         
190             String JavaDoc fontName = "Dialog";
191             int fontSize = 11;
192             Font font = new Font(fontName, Font.PLAIN, fontSize);
193             this.m_menuItem.setFont(font);
194         }
195         
196         return this.m_menuItem;
197     }
198     
199     /**
200      * Adds a rule to action.
201      *
202      * @param rule Rule to add
203      */

204     protected void addEnableRule(EnableRule rule) {
205         this.m_aEnableRules.add(rule);
206     }
207     
208     /**
209      * Checks if an action, and therefore its buttons and menu items,
210      * is enabled. This is based on the file that the action is
211      * focuesed on, the context and the enable rules.
212      *
213      * @param ce Context event to check against
214      * @return true if the action is enabled
215      */

216     public boolean isEnabled(ContextEvent ce) {
217         boolean bEnabled = true;
218         
219         if(ce.CONTEXT_TYPE!=ContextType.CONTEXT_TABS) {
220             VirtualFile vfFile = ce.getVFS().getVirtualFile(ce.getPath()).getResource();
221             if(this.m_aEnableRules.size()>0) {
222                 bEnabled = false;
223                 Iterator itor = this.m_aEnableRules.iterator();
224                 while(itor.hasNext()) {
225                     if( ((EnableRule)itor.next()).isEnabled(vfFile) ) {
226                         bEnabled = true;
227                     }
228                 }
229             }
230         } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS) {
231             if(this.m_aEnableRules.size()>0) {
232                 bEnabled = false;
233                 Iterator itor = this.m_aEnableRules.iterator();
234                 while(itor.hasNext()) {
235                     EnableRule tempRule = (EnableRule)itor.next();
236                     if( tempRule instanceof TabRule && ((TabRule)tempRule).isEnabled(ce.getMessage().trim()) ) {
237                         bEnabled = true;
238                     }
239                 }
240             }
241         } else {
242             bEnabled=true;
243         }
244         
245         this.setEnabled(bEnabled);
246         
247         return bEnabled;
248     }
249     
250     /**
251      * Sets whether the action is enabled or not.
252      *
253      * @param bEnabled true to set the action to be enabled
254      */

255     public void setEnabled(boolean bEnabled) {
256         if(this.m_button!=null) {
257             this.m_button.setEnabled(bEnabled);
258         }
259         if(this.m_menuItem!=null) {
260             this.m_menuItem.setEnabled(bEnabled);
261         }
262     }
263     
264     /**
265      * Returns the text name of the action.
266      *
267      * @return Name
268      */

269     public abstract String JavaDoc getText();
270     
271     /**
272      * Returns the tooltip text for the action.
273      *
274      * @return Tooltip text
275      */

276     public abstract String JavaDoc getToolTip();
277     
278     /**
279      * Returns the icon for the action.
280      *
281      * @return Icon
282      */

283     public abstract Icon getIcon();
284     
285     /**
286      * Returns the Mnemonic for the action.
287      *
288      * @return Mnemonic
289      */

290     public abstract String JavaDoc getMnemonic();
291     
292     /**
293      * Utility method to help implementors fire session events.
294      *
295      * @param sMessage Message
296      * @param vfs Virtual file system
297      * @param sPath Full path
298      * @param sSessionEventType Event type
299      */

300     protected void fireSessionEvent(String JavaDoc sMessage, AbstractVirtualFileSystem vfs, String JavaDoc sPath, String JavaDoc sSessionEventType) {
301         try {
302             SessionEventData sed = new SessionEventData(sSessionEventType);
303             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SESSION_EVENT, sMessage, vfs, sPath);
304             ce.setContextData(sed);
305             ContextHandler.getInstance().fireContextEvent(ce);
306         } catch (Exception JavaDoc e) {
307             e.printStackTrace();
308         }
309     }
310     /**
311      * Checks the current user's permissions to see if they are allowed
312      * to see this action.
313      *
314      */

315     protected void checkUser() {
316         Server server = null;
317         server = ServerList.getInstance().getHarmoniseServer();
318         URI uri = server.getURI();
319         
320         String JavaDoc sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService";
321         URL url = null;
322         try {
323             url = new URL(sURI);
324         } catch (MalformedURLException e2) {
325             e2.printStackTrace();
326             System.exit(1);
327         }
328         
329         AuthInfo auth = server.getVFS().getAuthentication();
330
331         try {
332             if( UserConfigClient.isSuperUser(url, auth.getUsername(), auth.getPassword()) ) {
333                 this.m_bShow=true;
334             }
335         } catch (RemoteException JavaDoc e) {
336             e.printStackTrace();
337         } catch (ServiceException JavaDoc e) {
338             e.printStackTrace();
339         }
340         this.m_bUserChecked=true;
341     }
342 }
343
Popular Tags