KickJava   Java API By Example, From Geeks To Geeks.

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


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.editors.*;
33 import org.openharmonise.him.harmonise.*;
34 import org.openharmonise.him.window.messages.builders.*;
35 import org.openharmonise.him.window.session.*;
36 import org.openharmonise.swing.*;
37 import org.openharmonise.vfs.*;
38 import org.openharmonise.vfs.context.*;
39 import org.openharmonise.vfs.gui.*;
40 import org.openharmonise.vfs.status.*;
41
42
43 /**
44  * Action to create a new virtual file in the current collection.
45  *
46  * @author Matthew Large
47  * @version $Revision: 1.1 $
48  *
49  */

50 public class ActionNewFile extends AbstractHIMAction implements HIMAction {
51
52     public static String JavaDoc ACTION_NAME = "NEW_FILE";
53     
54     /**
55      * true if the action is enabled.
56      */

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

62     private HashMap m_namePathMapping = new HashMap();
63     
64     /**
65      * Map of path to icon.
66      */

67     private HashMap m_nameIconMapping = new HashMap();
68     
69     /**
70      * Current path.
71      */

72     private String JavaDoc m_sCurrentPath = "";
73
74     /**
75      *
76      */

77     public ActionNewFile() {
78         super();
79         this.setup();
80     }
81
82     /**
83      * @param vfFile
84      */

85     public ActionNewFile(VirtualFile vfFile) {
86         super(vfFile);
87         this.setup();
88     }
89     
90     /**
91      * Configures this action.
92      *
93      */

94     private void setup() {
95         RuleGroup andGroup = new RuleGroup();
96         andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
97         super.addEnableRule(andGroup);
98         
99         RuleGroup orGroup = new RuleGroup();
100         orGroup.setGroupType(RuleGroup.GROUPTYPE_OR);
101         
102         orGroup.addEnableRule( new PathRule("/webdav/Content/Documents") );
103         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/flash") );
104         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/audio") );
105         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/movies") );
106         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/office") );
107         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/image") );
108         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/Web Resources") );
109         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/e-mail") );
110         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/links") );
111         orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/pdf") );
112         orGroup.addEnableRule( new PathRule("/webdav/Metadata/Properties") );
113         orGroup.addEnableRule( new PathRule("/webdav/Metadata/Values") );
114         orGroup.addEnableRule( new PathRule("/webdav/Newsletter/draft") );
115         orGroup.addEnableRule( new PathRule("/webdav/Reports/Queries") );
116         orGroup.addEnableRule( new PathRule("/webdav/Users") );
117         orGroup.addEnableRule( new PathRule("/webdav/Users/admin") );
118         orGroup.addEnableRule( new PathRule("/webdav/Users/public") );
119         orGroup.addEnableRule( new PathRule("/webdav/Website/Page Definition") );
120         orGroup.addEnableRule( new PathRule("/webdav/Website/Composition/Page Templates") );
121         orGroup.addEnableRule( new PathRule("/webdav/Website/Composition/Object Templates") );
122         orGroup.addEnableRule( new PathRule("/webdav/Website/Composition/Includes") );
123         orGroup.addEnableRule( new PathRule("/webdav/Website/Display/XSLT") );
124         orGroup.addEnableRule( new PathRule("/webdav/Website/Site Assets/FlashNav") );
125         //orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WEBSITE ) );
126
orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_DEFINITIONS ) );
127         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_PROPS ) );
128         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_STAGES ) );
129         orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_REPORTS_QUERIES ) );
130         
131         andGroup.addEnableRule(orGroup);
132         
133         EnableRule rule = new IsDirectoryRule();
134         andGroup.addEnableRule( rule );
135         
136         this.m_namePathMapping.put("/webdav/Content/Assets/flash", "New Flash Movie");
137         this.m_namePathMapping.put("/webdav/Content/Assets/movies", "New Movie");
138         this.m_namePathMapping.put("/webdav/Content/Assets/office", "New Office Document");
139         this.m_namePathMapping.put("/webdav/Content/Assets/image", "New Image");
140         this.m_namePathMapping.put("/webdav/Content/Assets/Web Resources", "New Web Resource");
141         this.m_namePathMapping.put("/webdav/Content/Assets/e-mail", "New E-mail Address");
142         this.m_namePathMapping.put("/webdav/Content/Assets/links", "New Web Link");
143         this.m_namePathMapping.put("/webdav/Content/Assets/pdf", "New PDF");
144         this.m_namePathMapping.put("/webdav/Content/Documents", "New Document");
145         this.m_namePathMapping.put("/webdav/Metadata/Properties", "New Property");
146         this.m_namePathMapping.put("/webdav/Metadata/Values", "New Value");
147         this.m_namePathMapping.put("/webdav/Newsletter/draft", "New Newsletter");
148         this.m_namePathMapping.put("/webdav/Reports/Queries", "New Report");
149         this.m_namePathMapping.put("/webdav/Users", "New User");
150         this.m_namePathMapping.put( "/webdav/Website/Page Definition", "New Page Definition" );
151         this.m_namePathMapping.put( "/webdav/Website/Composition/Page Templates", "New Page Template" );
152         this.m_namePathMapping.put( "/webdav/Website/Composition/Object Templates", "New Object Template" );
153         this.m_namePathMapping.put( "/webdav/Website/Composition/Includes", "New Include" );
154         this.m_namePathMapping.put( "/webdav/Website/Display/XSLT", "New XSLT" );
155         this.m_namePathMapping.put( "/webdav/Website/Site Assets/FlashNav", "New Flash Navigation" );
156         this.m_namePathMapping.put( HarmonisePaths.PATH_REPORTS_QUERIES , "New Report Query");
157         this.m_namePathMapping.put( HarmonisePaths.PATH_WORKFLOW_PROPS , "New Workflow");
158         this.m_namePathMapping.put( HarmonisePaths.PATH_WORKFLOW_STAGES , "New WorkflowStage");
159
160         
161         this.m_nameIconMapping.put("/webdav/Content/Assets/flash", "16-flash.gif");
162         this.m_nameIconMapping.put("/webdav/Content/Assets/movies", "16-movie.gif");
163         this.m_nameIconMapping.put("/webdav/Content/Assets/office", "16-document.gif");
164         this.m_nameIconMapping.put("/webdav/Content/Assets/image", "16-image.gif");
165         this.m_nameIconMapping.put("/webdav/Content/Assets/Web Resources", "16-link.gif");
166         this.m_nameIconMapping.put("/webdav/Content/Assets/e-mail", "16-email.gif");
167         this.m_nameIconMapping.put("/webdav/Content/Assets/links", "16-link.gif");
168         this.m_nameIconMapping.put("/webdav/Content/Assets/pdf", "16-pdf.gif");
169         this.m_nameIconMapping.put("/webdav/Content/Documents", "16-document.gif");
170         this.m_nameIconMapping.put("/webdav/Metadata/Properties", "16-property.gif");
171         this.m_nameIconMapping.put("/webdav/Metadata/Values", "16-value.gif");
172         this.m_nameIconMapping.put("/webdav/Newsletter/draft", "16-newsletter.gif");
173         this.m_nameIconMapping.put("/webdav/Reports/Queries", "16-report.gif");
174         this.m_nameIconMapping.put("/webdav/Users", "16-user.gif");
175         this.m_nameIconMapping.put("/webdav/Website/Page Definition", "16-page-definition.gif");
176         this.m_nameIconMapping.put("/webdav/Website/Composition/Page Templates", "16-template.gif");
177         this.m_nameIconMapping.put("/webdav/Website/Composition/Object Templates", "16-object-template.gif");
178         this.m_nameIconMapping.put( "/webdav/Website/Composition/Includes", "16-includes.gif" );
179         this.m_nameIconMapping.put("/webdav/Website/Display/XSLT", "16-xslt.gif");
180         this.m_nameIconMapping.put("/webdav/Website/Site Assets/FlashNav", "16-flash.gif");
181         this.m_nameIconMapping.put( HarmonisePaths.PATH_REPORTS_QUERIES , "16-query.gif");
182         this.m_nameIconMapping.put( HarmonisePaths.PATH_WORKFLOW_PROPS , "16-command-change-status.gif");
183         this.m_nameIconMapping.put( HarmonisePaths.PATH_WORKFLOW_STAGES , "16-workflow-container.gif");
184         
185     }
186
187     /* (non-Javadoc)
188      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
189      */

190     public void actionPerformed(ActionEvent arg0) {
191         boolean bChange = (ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY")!=null && ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY").equals("DISPLAYNAME"));
192
193         StatusData statusOverall = new VFSStatus();
194         
195         if(bChange) {
196             ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "FILENAME");
197             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
198             ContextHandler.getInstance().fireContextEvent(ce);
199         }
200         String JavaDoc sName = "";
201         if(this.getLastContextDirectory().getFullPath().startsWith(HarmonisePaths.PATH_LINKS) || this.getLastContextDirectory().getFullPath().startsWith(HarmonisePaths.PATH_EMAIL)) {
202
203             String JavaDoc sLink = "";
204             String JavaDoc sTitle = "New Web Address";
205             String JavaDoc sPath=null;
206             
207             if(this.getLastContextDirectory().getFullPath().startsWith(HarmonisePaths.PATH_EMAIL)) {
208                 sTitle = "New E-mail Address";
209             }
210             
211             JFrame frame = new JFrame();
212             frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
213         
214             DualTextEntryDialog dialog = new DualTextEntryDialog(frame, sTitle);
215             dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
216             dialog.setCaption1("Resource name");
217             dialog.setCaption2("Address");
218             dialog.show();
219             sName = dialog.getText1Value();
220             sLink = dialog.getText2Value();
221         
222             while(!sName.equals("") && !this.checkName(sName)) {
223                 dialog = new DualTextEntryDialog(frame, "Resource name");
224                 dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
225                 dialog.setLabelColor(Color.RED);
226                 dialog.setCaption1("Resource name");
227                 dialog.setCaption2("Address");
228                 dialog.show();
229                 sName = dialog.getText1Value();
230                 sLink = dialog.getText2Value();
231             }
232             
233             StateHandler.getInstance().addWait("NEW-FILE-ACTION");
234             try {
235                 if(!sName.equals("") && !sLink.equals("")) {
236                     sPath = this.getLastContextDirectory().getFullPath() + "/" + sName;
237                     
238                     StatusData status = EditorController.getInstance().createNew(sPath, sLink.getBytes(), this.getLastContextDirectory().getVFS());
239                     statusOverall.addStatusData(status);
240                     if( status.isOK() ) {
241                         VirtualFile vfNewFile = this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource();
242                         super.fireSessionEvent("Created", this.getLastContextDirectory().getVFS(), sPath, SessionEventData.RESOURCE_CREATED);
243                     } else {
244                     }
245                 }
246             } catch (Exception JavaDoc e) {
247                 e.printStackTrace(System.err);
248                 statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
249             } finally {
250                 if(bChange) {
251                     ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME");
252                     ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
253                     ContextHandler.getInstance().fireContextEvent(ce);
254                 }
255                 StateHandler.getInstance().removeWait("NEW-FILE-ACTION");
256                 if(!sName.equals("") && !sLink.equals("")) {
257                     StatusMessage statusMessage = new StatusMessage(ActionNewFile.ACTION_NAME, statusOverall);
258                     statusMessage.setResourceTitle(sName);
259                     statusMessage.setPath(sPath);
260                     VFSMessageBuilder.getInstance().fireMessage( statusMessage );
261                 }
262             }
263         } else {
264             sName = this.getNewName();
265             String JavaDoc sPath=null;
266             
267             StateHandler.getInstance().addWait("NEW-FILE-ACTION");
268             try {
269                 if(!sName.equals("")) {
270                     sPath = this.getLastContextDirectory().getFullPath() + "/" + sName;
271                     
272                     StatusData status = EditorController.getInstance().createNew(sPath, this.getLastContextDirectory().getVFS());
273                     statusOverall.addStatusData(status);
274                     if( status.isOK() ) {
275                         VirtualFile vfNewFile = this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource();
276                         super.fireSessionEvent("Created", this.getLastContextDirectory().getVFS(), sPath, SessionEventData.RESOURCE_CREATED);
277                     
278                         if(sPath.startsWith(HarmonisePaths.PATH_USERS)) {
279                             VirtualFile vfFile = this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource();
280                             if(vfFile!=null) {
281                                 vfFile.lock();
282                                 ActionChangeUserPassword action = new ActionChangeUserPassword();
283                                 action.changePassword("Set the user's password.", this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource());
284                             }
285                         }
286                     } else {
287                     }
288                 }
289             } catch (Exception JavaDoc e) {
290                 e.printStackTrace(System.err);
291                 statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
292             } finally {
293                 if(bChange) {
294                     ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME");
295                     ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
296                     ContextHandler.getInstance().fireContextEvent(ce);
297                 }
298                 StateHandler.getInstance().removeWait("NEW-FILE-ACTION");
299                 if(!sName.equals("")) {
300                     StatusMessage statusMessage = new StatusMessage(ActionNewFile.ACTION_NAME, statusOverall);
301                     statusMessage.setResourceTitle(sName);
302                     statusMessage.setPath(sPath);
303                     VFSMessageBuilder.getInstance().fireMessage( statusMessage );
304                 }
305             }
306         }
307     }
308     
309     /**
310      * Opens a dialog to get a name.
311      *
312      * @return Name or null if cancel was pressed
313      */

314     private String JavaDoc getNewName() {
315         JFrame frame = new JFrame();
316         frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
317         
318         SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Resource name");
319         dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name, start with a letter");
320         dialog.show();
321         String JavaDoc sName = dialog.getTextValue();
322         
323         while(!sName.equals("") && !this.checkName(sName)) {
324             dialog = new SingleTextEntryDialog(frame, "Resource name");
325             dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name, start with a letter");
326             dialog.setLabelColor(Color.RED);
327             dialog.show();
328             sName = dialog.getTextValue();
329         }
330         
331         return sName;
332         
333     }
334     
335     /**
336      * Checks if a name if valid.
337      *
338      * @param sName Name to check
339      * @return true if the name is valid
340      */

341     private boolean checkName(String JavaDoc sName) {
342         boolean bRetn = true;
343         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","9","_"};
344         String JavaDoc[] numbers = new String JavaDoc[]{"0","1","2","3","4","5","6","7","8","9"};
345         List JavaDoc aNumbers = Arrays.asList(numbers);
346         List JavaDoc aValidChars = Arrays.asList(validChars);
347         
348         char[] chars = sName.toCharArray();
349         for (int i = 0; i < chars.length; i++) {
350             char c = chars[i];
351             if(i==0 && aNumbers.contains(new String JavaDoc(new char[]{c})) ) {
352                 bRetn=false;
353                 break;
354             }
355             if(!aValidChars.contains(new String JavaDoc(new char[]{c}))) {
356                 bRetn=false;
357                 break;
358             }
359         }
360         
361         return bRetn;
362     }
363
364     /* (non-Javadoc)
365      * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
366      */

367     public JMenuItem getMenuItem() {
368         JMenuItem menuItem = super.getMenuItem();
369         menuItem.setAccelerator( KeyStroke.getKeyStroke(this.getAcceleratorKeycode(), this.getAcceleratorMask()) );
370         
371         return menuItem;
372     }
373
374     /* (non-Javadoc)
375      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
376      */

377     public String JavaDoc getDescription() {
378         return "Creates a new resource under the currently selected collection";
379     }
380
381     /* (non-Javadoc)
382      * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
383      */

384     public String JavaDoc getText() {
385         return "New ...";
386     }
387
388     /* (non-Javadoc)
389      * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
390      */

391     public String JavaDoc getToolTip() {
392         return this.getDescription();
393     }
394
395     /* (non-Javadoc)
396      * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
397      */

398     public Icon getIcon() {
399         return IconManager.getInstance().getIcon("16-document.gif");
400     }
401
402     /* (non-Javadoc)
403      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
404      */

405     public int getAcceleratorKeycode() {
406         return KeyEvent.VK_N;
407     }
408
409     /* (non-Javadoc)
410      * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
411      */

412     public String JavaDoc getMnemonic() {
413         return "N";
414     }
415
416     /* (non-Javadoc)
417      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
418      */

419     public int getAcceleratorMask() {
420         return InputEvent.CTRL_MASK;
421     }
422
423     /* (non-Javadoc)
424      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.vfs.VirtualFile)
425      */

426     public boolean isEnabled(ContextEvent ce) {
427         //System.out.println("NEW FILE - got ContextEvent[" + ce.CONTEXT_TYPE + "] for path[" + ce.getPath());
428
if(ce.CONTEXT_TYPE==ContextType.CONTEXT_DIRS) {
429             
430             renameButton(ce.getPath());
431             
432             m_bIsEnabled = super.isEnabled(ce);
433             return m_bIsEnabled;
434         } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS) {
435             m_bIsEnabled = false;
436             this.setEnabled(false);
437             return m_bIsEnabled;
438         } else {
439             this.setEnabled(m_bIsEnabled);
440             return m_bIsEnabled;
441         }
442     }
443     
444     /**
445      * Changes the name and icon of the action based on a given path.
446      *
447      * @param sPath Full path
448      */

449     private void renameButton(String JavaDoc sPath) {
450         if(!sPath.equals(this.m_sCurrentPath)) {
451             Iterator itor = this.m_namePathMapping.keySet().iterator();
452             boolean bFound=false;
453             while(itor.hasNext()) {
454                 String JavaDoc sTestPath = (String JavaDoc)itor.next();
455                 if(sPath.startsWith(sTestPath)) {
456                     try {
457                         this.getButton().setText( (String JavaDoc)this.m_namePathMapping.get(sTestPath) );
458                         this.getButton().setIcon( IconManager.getInstance().getIcon((String JavaDoc) this.m_nameIconMapping.get(sTestPath)) );
459                         this.getMenuItem().setText( (String JavaDoc)this.m_namePathMapping.get(sTestPath) );
460                         this.getMenuItem().setIcon( IconManager.getInstance().getIcon((String JavaDoc) this.m_nameIconMapping.get(sTestPath)) );
461                     } catch(Exception JavaDoc e) {
462
463                     }
464                     bFound=true;
465                 }
466             }
467             
468             if(!bFound) {
469                 this.getButton().setText("New ...");
470                 this.getButton().setIcon( IconManager.getInstance().getIcon("16-document.gif") );
471                 this.getMenuItem().setText("New ...");
472                 this.getMenuItem().setIcon( IconManager.getInstance().getIcon("16-document.gif") );
473                 this.m_sCurrentPath = "";
474             } else {
475                 this.m_sCurrentPath = sPath;
476             }
477         }
478     }
479
480 }
481
Popular Tags