KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > file > ActionNewCollection


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.file;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.him.actions.*;
29 import org.openharmonise.him.actions.rules.*;
30 import org.openharmonise.him.configuration.*;
31 import org.openharmonise.him.context.StateHandler;
32 import org.openharmonise.him.harmonise.*;
33 import org.openharmonise.him.window.messages.builders.*;
34 import org.openharmonise.him.window.session.*;
35 import org.openharmonise.swing.*;
36 import org.openharmonise.vfs.*;
37 import org.openharmonise.vfs.context.*;
38 import org.openharmonise.vfs.gui.*;
39 import org.openharmonise.vfs.status.*;
40
41
42 /**
43  * Action to create a new collection in the current collection.
44  *
45  * @author Matthew Large
46  * @version $Revision: 1.1 $
47  *
48  */

49 public class ActionNewCollection extends AbstractHIMAction implements HIMAction {
50
51     public static String JavaDoc ACTION_NAME = "NEW_COLLECTION";
52     
53     /**
54      * true if this action is enabled.
55      */

56     boolean m_bIsEnabled = false;
57     
58     /**
59      * Map of path to icon.
60      */

61     private HashMap m_nameIconMapping = new HashMap();
62     
63     /**
64      * Current collection full path.
65      */

66     private String JavaDoc m_sCurrentPath = "";
67
68     /**
69      *
70      */

71     public ActionNewCollection() {
72         super();
73         this.setup();
74     }
75
76     /**
77      * @param vfFile
78      */

79     public ActionNewCollection(VirtualFile vfFile) {
80         super(vfFile);
81         this.setup();
82     }
83     
84     /**
85      * Configures this action.
86      *
87      */

88     private void setup() {
89         RuleGroup andGroup = new RuleGroup();
90         andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
91         
92         RuleGroup orGroup = new RuleGroup();
93         orGroup.setGroupType(RuleGroup.GROUPTYPE_OR);
94         
95         orGroup.addEnableRule( new PathRule("/webdav/Content/Documents") );
96         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/flash") );
97         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/movies") );
98         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/office") );
99         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/audio") );
100         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/Image") );
101         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/image") );
102         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/pdf") );
103         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/e-mail") );
104         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/links") );
105         orGroup.addEnableRule( new PathRule("/webdav/Metadata/Properties") );
106         orGroup.addEnableRule( new PathRule("/webdav/Metadata/Values") );
107         orGroup.addEnableRule( new PathRule("/webdav/Newsletter/draft") );
108         orGroup.addEnableRule( new PathRule("/webdav/Reports/Queries") );
109         orGroup.addEnableRule( new PathRule("/webdav/Users") );
110         orGroup.addEnableRule( new PathRule("/webdav/Templates/Page Definition") );
111         orGroup.addEnableRule( new PathRule("/webdav/Templates/Composition/Page Templates") );
112         orGroup.addEnableRule( new PathRule("/webdav/Templates/Composition/Object Templates") );
113         orGroup.addEnableRule( new PathRule("/webdav/Templates/Display/XSLT") );
114         orGroup.addEnableRule( new PathRule("/webdav/Templates/Site Assets/Flash Navigation") );
115         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WEBSITE ) );
116         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_DEFINITIONS ) );
117         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_PROPS ) );
118         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_STAGES ) );
119         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_REPORTS_QUERIES ) );
120         
121         andGroup.addEnableRule(orGroup);
122         
123         SecurityRule secRule = new SecurityRule(VirtualFile.METHOD_MKDIR);
124         andGroup.addEnableRule(secRule);
125         
126         super.addEnableRule(andGroup);
127         
128         this.m_nameIconMapping.put("/webdav/Content/Assets/flash", "16-flash-folder.gif");
129         this.m_nameIconMapping.put("/webdav/Content/Assets/movies", "16-movie-folder.gif");
130         this.m_nameIconMapping.put("/webdav/Content/Assets/office", "16-assets-folder.gif");
131         this.m_nameIconMapping.put("/webdav/Content/Assets/audio", "16-assets-folder.gif");
132         this.m_nameIconMapping.put("/webdav/Content/Assets/image", "16-image-folder.gif");
133         this.m_nameIconMapping.put("/webdav/Content/Assets/Web Resources", "16-link-folder.gif");
134         this.m_nameIconMapping.put("/webdav/Content/Assets/e-mail", "16-email-folder.gif");
135         this.m_nameIconMapping.put("/webdav/Content/Assets/links", "16-link-folder.gif");
136         this.m_nameIconMapping.put("/webdav/Content/Assets/pdf", "16-pdf-folder.gif");
137         this.m_nameIconMapping.put("/webdav/Content/Documents", "16-section.gif");
138         this.m_nameIconMapping.put("/webdav/Metadata/Properties", "16-property-container.gif");
139         this.m_nameIconMapping.put("/webdav/Metadata/Values", "16-value-container.gif");
140         this.m_nameIconMapping.put("/webdav/Newsletter/draft", "16-newsletter-draft.gif");
141         this.m_nameIconMapping.put("/webdav/Reports/Queries", "16-report-folder.gif");
142         this.m_nameIconMapping.put("/webdav/Users", "16-user-container.gif");
143         this.m_nameIconMapping.put("/webdav/Website/Page Definition", "16-page-definition-folder.gif");
144         this.m_nameIconMapping.put("/webdav/Website/Composition/Page Templates", "16-template-folder.gif");
145         this.m_nameIconMapping.put("/webdav/Website/Composition/Object Templates", "16-object-template-folder.gif");
146         this.m_nameIconMapping.put("/webdav/Website/Composition/Includes", "16-includes-folder.gif");
147         this.m_nameIconMapping.put("/webdav/Website/Display/XSLT", "16-xslt-folder.gif");
148         this.m_nameIconMapping.put("/webdav/Website/Site Assets/FlashNav", "16-flash-folder.gif");
149     }
150
151     /* (non-Javadoc)
152      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
153      */

154     public void actionPerformed(ActionEvent arg0) {
155         boolean bChange = (ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY")!=null && ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY").equals("DISPLAYNAME"));
156
157         if(bChange) {
158             ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "FILENAME");
159             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
160             ContextHandler.getInstance().fireContextEvent(ce);
161         }
162         
163         String JavaDoc sDirPath = null;
164         
165         String JavaDoc sDirName = this.getNewName();
166         StateHandler.getInstance().addWait("NEW-COL-ACTION");
167         StatusData statusOverall = new VFSStatus();
168         
169         try {
170             if(!sDirName.equals("")) {
171                 sDirPath = this.getLastContextDirectory().getFullPath() + "/" + sDirName;
172                 StatusData status = this.getLastContextDirectory().mkDir(sDirName);
173                 statusOverall.addStatusData(status);
174                 if( status.isOK() ) {
175                     super.fireSessionEvent("Created", this.getLastContextDirectory().getVFS(), this.getLastContextDirectory().getFullPath() + "/" + sDirName, SessionEventData.RESOURCE_CREATED);
176                 } else {
177                 }
178             }
179         } catch (Exception JavaDoc e) {
180             e.printStackTrace(System.err);
181             statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
182         } finally {
183             if(bChange) {
184                 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME");
185                 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
186                 ContextHandler.getInstance().fireContextEvent(ce);
187             }
188             StatusMessage statusMessage = new StatusMessage(ActionNewCollection.ACTION_NAME, statusOverall);
189             statusMessage.setResourceTitle(sDirName);
190             statusMessage.setPath(sDirPath);
191             VFSMessageBuilder.getInstance().fireMessage( statusMessage );
192             //VFSMessageBuilder.getInstance().fireMessage(ActionNewCollection.ACTION_NAME, statusOverall, sDirName);
193
StateHandler.getInstance().removeWait("NEW-COL-ACTION");
194         }
195     }
196     
197     /**
198      * Opens a dialog to get a name for the collection.
199      *
200      * @return Name or null if cancel was pressed
201      */

202     private String JavaDoc getNewName() {
203         JFrame frame = new JFrame();
204         frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
205         
206         SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Collection name");
207         dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name.");
208         dialog.show();
209         String JavaDoc sName = dialog.getTextValue();
210         
211         while(!sName.equals("") && !this.checkName(sName)) {
212             dialog = new SingleTextEntryDialog(frame, "Collection name");
213             dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name.");
214             dialog.setLabelColor(Color.RED);
215             dialog.show();
216             sName = dialog.getTextValue();
217         }
218         
219         return sName;
220         
221     }
222     
223     /**
224      * Checks if a name is valid.
225      *
226      * @param sName Name to check
227      * @return true if the name if valid
228      */

229     private boolean checkName(String JavaDoc sName) {
230         boolean bRetn = true;
231         String JavaDoc[] validChars = new String JavaDoc[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","0","_"};
232         List JavaDoc aValidChars = Arrays.asList(validChars);
233         
234         char[] chars = sName.toCharArray();
235         for (int i = 0; i < chars.length; i++) {
236             char c = chars[i];
237             if(!aValidChars.contains(new String JavaDoc(new char[]{c}))) {
238                 bRetn=false;
239                 break;
240             }
241         }
242         
243         return bRetn;
244     }
245
246     /* (non-Javadoc)
247      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
248      */

249     public String JavaDoc getDescription() {
250         return "Creates a new collection under the currently selected collection";
251     }
252
253     /* (non-Javadoc)
254      * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
255      */

256     public String JavaDoc getText() {
257         return "New Collection";
258     }
259
260     /* (non-Javadoc)
261      * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
262      */

263     public String JavaDoc getToolTip() {
264         return this.getDescription();
265     }
266
267     /* (non-Javadoc)
268      * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
269      */

270     public Icon getIcon() {
271         return IconManager.getInstance().getIcon("16-section.gif");
272     }
273
274     /* (non-Javadoc)
275      * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
276      */

277     public JMenuItem getMenuItem() {
278         JMenuItem menuItem = super.getMenuItem();
279         menuItem.setAccelerator( KeyStroke.getKeyStroke(this.getAcceleratorKeycode(), this.getAcceleratorMask()) );
280         
281         return menuItem;
282     }
283
284     /* (non-Javadoc)
285      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
286      */

287     public int getAcceleratorKeycode() {
288         return KeyEvent.VK_N;
289     }
290
291     /* (non-Javadoc)
292      * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
293      */

294     public String JavaDoc getMnemonic() {
295         return "C";
296     }
297
298     /* (non-Javadoc)
299      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
300      */

301     public int getAcceleratorMask() {
302         return InputEvent.CTRL_MASK + InputEvent.SHIFT_MASK;
303     }
304
305     /* (non-Javadoc)
306      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.vfs.VirtualFile)
307      */

308     public boolean isEnabled(ContextEvent ce) {
309         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_DIRS) {
310             m_bIsEnabled = super.isEnabled(ce);
311             
312             renameButton(ce.getPath());
313             return m_bIsEnabled;
314         } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS) {
315             m_bIsEnabled = false;
316             this.setEnabled(m_bIsEnabled);
317             this.setEnabled(false);
318             return m_bIsEnabled;
319         } else {
320             return m_bIsEnabled;
321         }
322     }
323     
324     /**
325      * Changes the action's icon and name based on a given path.
326      *
327      * @param sPath Full path
328      */

329     private void renameButton(String JavaDoc sPath) {
330         if(!sPath.equals(this.m_sCurrentPath)) {
331             Iterator itor = this.m_nameIconMapping.keySet().iterator();
332             boolean bFound=false;
333             while(itor.hasNext()) {
334                 String JavaDoc sTestPath = (String JavaDoc)itor.next();
335                 if(sPath.startsWith(sTestPath)) {
336                     try {
337                         this.getButton().setIcon( IconManager.getInstance().getIcon((String JavaDoc) this.m_nameIconMapping.get(sTestPath)) );
338                         this.getMenuItem().setIcon( IconManager.getInstance().getIcon((String JavaDoc) this.m_nameIconMapping.get(sTestPath)) );
339                     } catch(Exception JavaDoc e) {
340
341                     }
342                     bFound=true;
343                 }
344             }
345             
346             if(!bFound) {
347                 this.getButton().setText("New collection");
348                 this.getButton().setIcon( IconManager.getInstance().getIcon("16-section.gif") );
349                 this.getMenuItem().setText("New collection");
350                 this.getMenuItem().setIcon( IconManager.getInstance().getIcon("16-section.gif") );
351                 this.m_sCurrentPath = "";
352             } else {
353                 this.m_sCurrentPath = sPath;
354             }
355         }
356     }
357
358 }
359
Popular Tags