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.file;
020:
021: import java.awt.event.*;
022: import java.util.*;
023:
024: import javax.swing.*;
025:
026: import org.openharmonise.him.actions.*;
027: import org.openharmonise.him.actions.rules.*;
028: import org.openharmonise.him.configuration.*;
029: import org.openharmonise.him.context.StateHandler;
030: import org.openharmonise.him.window.messages.builders.*;
031: import org.openharmonise.him.window.session.*;
032: import org.openharmonise.swing.*;
033: import org.openharmonise.vfs.*;
034: import org.openharmonise.vfs.context.*;
035: import org.openharmonise.vfs.gui.*;
036: import org.openharmonise.vfs.status.*;
037:
038: /**
039: * Action to rename a virtual file.
040: *
041: * @author Matthew Large
042: * @version $Revision: 1.1 $
043: *
044: */
045: public class ActionRename extends AbstractHIMAction implements
046: HIMAction {
047:
048: public static String ACTION_NAME = "RENAME";
049:
050: /**
051: *
052: */
053: public ActionRename() {
054: super ();
055: this .setup();
056: }
057:
058: /**
059: * @param vfFile
060: */
061: public ActionRename(VirtualFile vfFile) {
062: super (vfFile);
063: this .setup();
064: }
065:
066: /**
067: * Configures this action.
068: *
069: */
070: private void setup() {
071: RuleGroup andGroup = new RuleGroup();
072: andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
073: IsPublishedRule pubRule = new IsPublishedRule();
074: andGroup.addEnableRule(pubRule);
075: PathRule pathRule = new PathRule("/webdav/Archive/");
076: pathRule.setResultComparator(false);
077: andGroup.addEnableRule(pathRule);
078: IsHistoricalRule histRule = new IsHistoricalRule();
079: histRule.setResultComparator(false);
080: andGroup.addEnableRule(histRule);
081: SecurityRule secRule = new SecurityRule(VirtualFile.METHOD_MOVE);
082: andGroup.addEnableRule(secRule);
083: this .addEnableRule(andGroup);
084: }
085:
086: /* (non-Javadoc)
087: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
088: */
089: public void actionPerformed(ActionEvent arg0) {
090: boolean bChange = (ConfigStore.getInstance().getPropertyValue(
091: "FILENAME_DISPLAY") != null && ConfigStore
092: .getInstance().getPropertyValue("FILENAME_DISPLAY")
093: .equals("DISPLAYNAME"));
094:
095: StatusData statusOverall = new VFSStatus();
096:
097: if (bChange) {
098: ConfigStore.getInstance().setProperty("FILENAME_DISPLAY",
099: "FILENAME");
100: ContextEvent ce = new ContextEvent(
101: ContextType.CONTEXT_FILENAME_DISPLAY);
102: ContextHandler.getInstance().fireContextEvent(ce);
103: }
104:
105: VirtualFile vfFile = this .getLastContextFile();
106: if (vfFile.isVersionable()
107: && vfFile.getState().equals(VirtualFile.STATE_PENDING)
108: && ((VersionedVirtualFile) vfFile).getLiveVersionPath() != null) {
109: vfFile = vfFile.getVFS().getVirtualFile(
110: ((VersionedVirtualFile) vfFile)
111: .getLiveVersionPath()).getResource();
112: }
113:
114: String sOriginalName = vfFile.getFileName();
115:
116: String sNewName = this .getNewName(sOriginalName);
117:
118: StateHandler.getInstance().addWait("RENAME-ACTION");
119: try {
120: if (!sNewName.equals("")) {
121: StatusData status = vfFile.rename(sNewName);
122: statusOverall.addStatusData(status);
123: if (status.isOK()) {
124: super .fireSessionEvent("Renamed", vfFile.getVFS(),
125: vfFile.getFullPath(),
126: SessionEventData.RESOURCE_RENAMED);
127: } else {
128: }
129: }
130: } catch (Exception e) {
131: e.printStackTrace(System.err);
132: statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
133: } finally {
134: if (bChange) {
135: ConfigStore.getInstance().setProperty(
136: "FILENAME_DISPLAY", "DISPLAYNAME");
137: ContextEvent ce = new ContextEvent(
138: ContextType.CONTEXT_FILENAME_DISPLAY);
139: ContextHandler.getInstance().fireContextEvent(ce);
140: }
141: VFSMessageBuilder.getInstance().fireMessage(
142: ActionRename.ACTION_NAME, statusOverall,
143: sOriginalName, sNewName);
144: StateHandler.getInstance().removeWait("RENAME-ACTION");
145: }
146: }
147:
148: /**
149: * Opens a diaglog to get a name.
150: *
151: * @param sOriginalName Original name
152: * @return Name or null if cancel was pressed
153: */
154: private String getNewName(String sOriginalName) {
155: JFrame frame = new JFrame();
156: frame.setIconImage(((ImageIcon) IconManager.getInstance()
157: .getIcon("32-sim-logo.gif")).getImage());
158:
159: SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame,
160: "Rename");
161: dialog.setTextValue(sOriginalName);
162: dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
163: dialog.show();
164: String sName = dialog.getTextValue();
165:
166: while (!sName.equals("") && !this .checkName(sName)) {
167: dialog = new SingleTextEntryDialog(frame, "Rename");
168: dialog.setTextValue(sName);
169: dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
170: dialog.show();
171: sName = dialog.getTextValue();
172: }
173:
174: return sName;
175:
176: }
177:
178: /**
179: * Checks if a name is valid.
180: *
181: * @param sName Name to check
182: * @return true if the name is valid
183: */
184: private boolean checkName(String sName) {
185: boolean bRetn = true;
186: String[] validChars = new String[] { "a", "b", "c", "d", "e",
187: "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
188: "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A",
189: "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
190: "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
191: "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7",
192: "8", "0" };
193: List aValidChars = Arrays.asList(validChars);
194:
195: char[] chars = sName.toCharArray();
196: for (int i = 0; i < chars.length; i++) {
197: char c = chars[i];
198: if (!aValidChars.contains(new String(new char[] { c }))) {
199: bRetn = false;
200: break;
201: }
202: }
203:
204: return bRetn;
205: }
206:
207: /* (non-Javadoc)
208: * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
209: */
210: public String getDescription() {
211: return "Renames the currently selected resource";
212: }
213:
214: /* (non-Javadoc)
215: * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
216: */
217: public String getText() {
218: return "Rename...";
219: }
220:
221: /* (non-Javadoc)
222: * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
223: */
224: public String getToolTip() {
225: return this .getDescription();
226: }
227:
228: /* (non-Javadoc)
229: * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
230: */
231: public Icon getIcon() {
232: return IconManager.getInstance().getIcon("16-blank.gif");
233: }
234:
235: /* (non-Javadoc)
236: * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
237: */
238: public int getAcceleratorKeycode() {
239: return 0;
240: }
241:
242: /* (non-Javadoc)
243: * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
244: */
245: public String getMnemonic() {
246: return "R";
247: }
248:
249: /* (non-Javadoc)
250: * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
251: */
252: public int getAcceleratorMask() {
253: return InputEvent.CTRL_MASK;
254: }
255:
256: }
|