KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public class ActionRename extends AbstractHIMAction implements HIMAction {
47
48     public static String JavaDoc ACTION_NAME = "RENAME";
49
50     /**
51      *
52      */

53     public ActionRename() {
54         super();
55         this.setup();
56     }
57
58     /**
59      * @param vfFile
60      */

61     public ActionRename(VirtualFile vfFile) {
62         super(vfFile);
63         this.setup();
64     }
65     
66     /**
67      * Configures this action.
68      *
69      */

70     private void setup() {
71         RuleGroup andGroup = new RuleGroup();
72         andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
73         IsPublishedRule pubRule = new IsPublishedRule();
74         andGroup.addEnableRule(pubRule);
75         PathRule pathRule = new PathRule("/webdav/Archive/");
76         pathRule.setResultComparator(false);
77         andGroup.addEnableRule(pathRule);
78         IsHistoricalRule histRule = new IsHistoricalRule();
79         histRule.setResultComparator(false);
80         andGroup.addEnableRule(histRule);
81         SecurityRule secRule = new SecurityRule(VirtualFile.METHOD_MOVE);
82         andGroup.addEnableRule(secRule);
83         this.addEnableRule(andGroup);
84     }
85
86     /* (non-Javadoc)
87      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
88      */

89     public void actionPerformed(ActionEvent arg0) {
90         boolean bChange = (ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY")!=null && ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY").equals("DISPLAYNAME"));
91
92         StatusData statusOverall = new VFSStatus();
93         
94         
95         if(bChange) {
96             ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "FILENAME");
97             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
98             ContextHandler.getInstance().fireContextEvent(ce);
99         }
100
101         VirtualFile vfFile = this.getLastContextFile();
102         if(vfFile.isVersionable() && vfFile.getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)vfFile).getLiveVersionPath()!=null) {
103             vfFile = vfFile.getVFS().getVirtualFile( ((VersionedVirtualFile)vfFile).getLiveVersionPath() ).getResource();
104         }
105         
106         String JavaDoc sOriginalName = vfFile.getFileName();
107         
108         String JavaDoc sNewName = this.getNewName(sOriginalName);
109     
110         StateHandler.getInstance().addWait("RENAME-ACTION");
111         try {
112             if(!sNewName.equals("")) {
113                 StatusData status = vfFile.rename(sNewName);
114                 statusOverall.addStatusData(status);
115                 if( status.isOK() ) {
116                     super.fireSessionEvent("Renamed", vfFile.getVFS(), vfFile.getFullPath(), SessionEventData.RESOURCE_RENAMED);
117                 } else {
118                 }
119             }
120         } catch (Exception JavaDoc e) {
121             e.printStackTrace(System.err);
122             statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
123         } finally {
124             if(bChange) {
125                 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME");
126                 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY);
127                 ContextHandler.getInstance().fireContextEvent(ce);
128             }
129             VFSMessageBuilder.getInstance().fireMessage(ActionRename.ACTION_NAME, statusOverall, sOriginalName, sNewName);
130             StateHandler.getInstance().removeWait("RENAME-ACTION");
131         }
132     }
133     
134     /**
135      * Opens a diaglog to get a name.
136      *
137      * @param sOriginalName Original name
138      * @return Name or null if cancel was pressed
139      */

140     private String JavaDoc getNewName(String JavaDoc sOriginalName) {
141         JFrame frame = new JFrame();
142         frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
143         
144         SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Rename");
145         dialog.setTextValue(sOriginalName);
146         dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
147         dialog.show();
148         String JavaDoc sName = dialog.getTextValue();
149         
150         while(!sName.equals("") && !this.checkName(sName)) {
151             dialog = new SingleTextEntryDialog(frame, "Rename");
152             dialog.setTextValue(sName);
153             dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
154             dialog.show();
155             sName = dialog.getTextValue();
156         }
157         
158         return sName;
159         
160     }
161     
162     /**
163      * Checks if a name is valid.
164      *
165      * @param sName Name to check
166      * @return true if the name is valid
167      */

168     private boolean checkName(String JavaDoc sName) {
169         boolean bRetn = true;
170         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"};
171         List aValidChars = Arrays.asList(validChars);
172         
173         char[] chars = sName.toCharArray();
174         for (int i = 0; i < chars.length; i++) {
175             char c = chars[i];
176             if(!aValidChars.contains(new String JavaDoc(new char[]{c}))) {
177                 bRetn=false;
178                 break;
179             }
180         }
181         
182         return bRetn;
183     }
184
185     /* (non-Javadoc)
186      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
187      */

188     public String JavaDoc getDescription() {
189         return "Renames the currently selected resource";
190     }
191
192     /* (non-Javadoc)
193      * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
194      */

195     public String JavaDoc getText() {
196         return "Rename...";
197     }
198
199     /* (non-Javadoc)
200      * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
201      */

202     public String JavaDoc getToolTip() {
203         return this.getDescription();
204     }
205
206     /* (non-Javadoc)
207      * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
208      */

209     public Icon getIcon() {
210         return IconManager.getInstance().getIcon("16-blank.gif");
211     }
212
213     /* (non-Javadoc)
214      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
215      */

216     public int getAcceleratorKeycode() {
217         return 0;
218     }
219
220     /* (non-Javadoc)
221      * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
222      */

223     public String JavaDoc getMnemonic() {
224         return "R";
225     }
226
227     /* (non-Javadoc)
228      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
229      */

230     public int getAcceleratorMask() {
231         return InputEvent.CTRL_MASK;
232     }
233
234 }
235
Popular Tags