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.*;
022: import java.awt.event.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027:
028: import org.openharmonise.him.actions.*;
029: import org.openharmonise.him.actions.rules.*;
030: import org.openharmonise.him.configuration.*;
031: import org.openharmonise.him.context.StateHandler;
032: import org.openharmonise.him.harmonise.*;
033: import org.openharmonise.him.window.messages.builders.*;
034: import org.openharmonise.him.window.session.*;
035: import org.openharmonise.swing.*;
036: import org.openharmonise.vfs.*;
037: import org.openharmonise.vfs.context.*;
038: import org.openharmonise.vfs.gui.*;
039: import org.openharmonise.vfs.status.*;
040:
041: /**
042: * Action to create a new collection in the current collection.
043: *
044: * @author Matthew Large
045: * @version $Revision: 1.1 $
046: *
047: */
048: public class ActionNewCollection extends AbstractHIMAction implements
049: HIMAction {
050:
051: public static String ACTION_NAME = "NEW_COLLECTION";
052:
053: /**
054: * true if this action is enabled.
055: */
056: boolean m_bIsEnabled = false;
057:
058: /**
059: * Map of path to icon.
060: */
061: private HashMap m_nameIconMapping = new HashMap();
062:
063: /**
064: * Current collection full path.
065: */
066: private String m_sCurrentPath = "";
067:
068: /**
069: *
070: */
071: public ActionNewCollection() {
072: super ();
073: this .setup();
074: }
075:
076: /**
077: * @param vfFile
078: */
079: public ActionNewCollection(VirtualFile vfFile) {
080: super (vfFile);
081: this .setup();
082: }
083:
084: /**
085: * Configures this action.
086: *
087: */
088: private void setup() {
089: RuleGroup andGroup = new RuleGroup();
090: andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
091:
092: RuleGroup orGroup = new RuleGroup();
093: orGroup.setGroupType(RuleGroup.GROUPTYPE_OR);
094:
095: orGroup
096: .addEnableRule(new PathRule("/webdav/Content/Documents"));
097: orGroup.addEnableRule(new PathRule(
098: "/webdav/Content/Assets/flash"));
099: orGroup.addEnableRule(new PathRule(
100: "/webdav/Content/Assets/movies"));
101: orGroup.addEnableRule(new PathRule(
102: "/webdav/Content/Assets/office"));
103: orGroup.addEnableRule(new PathRule(
104: "/webdav/Content/Assets/audio"));
105: orGroup.addEnableRule(new PathRule(
106: "/webdav/Content/Assets/Image"));
107: orGroup.addEnableRule(new PathRule(
108: "/webdav/Content/Assets/image"));
109: orGroup
110: .addEnableRule(new PathRule(
111: "/webdav/Content/Assets/pdf"));
112: orGroup.addEnableRule(new PathRule(
113: "/webdav/Content/Assets/e-mail"));
114: orGroup.addEnableRule(new PathRule(
115: "/webdav/Content/Assets/links"));
116: orGroup.addEnableRule(new PathRule(
117: "/webdav/Metadata/Properties"));
118: orGroup.addEnableRule(new PathRule("/webdav/Metadata/Values"));
119: orGroup.addEnableRule(new PathRule("/webdav/Newsletter/draft"));
120: orGroup.addEnableRule(new PathRule("/webdav/Reports/Queries"));
121: orGroup.addEnableRule(new PathRule("/webdav/Users"));
122: orGroup.addEnableRule(new PathRule(
123: "/webdav/Templates/Page Definition"));
124: orGroup.addEnableRule(new PathRule(
125: "/webdav/Templates/Composition/Page Templates"));
126: orGroup.addEnableRule(new PathRule(
127: "/webdav/Templates/Composition/Object Templates"));
128: orGroup.addEnableRule(new PathRule(
129: "/webdav/Templates/Display/XSLT"));
130: orGroup.addEnableRule(new PathRule(
131: "/webdav/Templates/Site Assets/Flash Navigation"));
132: orGroup
133: .addEnableRule(new PathRule(HarmonisePaths.PATH_WEBSITE));
134: orGroup.addEnableRule(new PathRule(
135: HarmonisePaths.PATH_WORKFLOW_DEFINITIONS));
136: orGroup.addEnableRule(new PathRule(
137: HarmonisePaths.PATH_WORKFLOW_PROPS));
138: orGroup.addEnableRule(new PathRule(
139: HarmonisePaths.PATH_WORKFLOW_STAGES));
140: orGroup.addEnableRule(new PathRule(
141: HarmonisePaths.PATH_REPORTS_QUERIES));
142:
143: andGroup.addEnableRule(orGroup);
144:
145: SecurityRule secRule = new SecurityRule(
146: VirtualFile.METHOD_MKDIR);
147: andGroup.addEnableRule(secRule);
148:
149: super .addEnableRule(andGroup);
150:
151: this .m_nameIconMapping.put("/webdav/Content/Assets/flash",
152: "16-flash-folder.gif");
153: this .m_nameIconMapping.put("/webdav/Content/Assets/movies",
154: "16-movie-folder.gif");
155: this .m_nameIconMapping.put("/webdav/Content/Assets/office",
156: "16-assets-folder.gif");
157: this .m_nameIconMapping.put("/webdav/Content/Assets/audio",
158: "16-assets-folder.gif");
159: this .m_nameIconMapping.put("/webdav/Content/Assets/image",
160: "16-image-folder.gif");
161: this .m_nameIconMapping.put(
162: "/webdav/Content/Assets/Web Resources",
163: "16-link-folder.gif");
164: this .m_nameIconMapping.put("/webdav/Content/Assets/e-mail",
165: "16-email-folder.gif");
166: this .m_nameIconMapping.put("/webdav/Content/Assets/links",
167: "16-link-folder.gif");
168: this .m_nameIconMapping.put("/webdav/Content/Assets/pdf",
169: "16-pdf-folder.gif");
170: this .m_nameIconMapping.put("/webdav/Content/Documents",
171: "16-section.gif");
172: this .m_nameIconMapping.put("/webdav/Metadata/Properties",
173: "16-property-container.gif");
174: this .m_nameIconMapping.put("/webdav/Metadata/Values",
175: "16-value-container.gif");
176: this .m_nameIconMapping.put("/webdav/Newsletter/draft",
177: "16-newsletter-draft.gif");
178: this .m_nameIconMapping.put("/webdav/Reports/Queries",
179: "16-report-folder.gif");
180: this .m_nameIconMapping.put("/webdav/Users",
181: "16-user-container.gif");
182: this .m_nameIconMapping.put("/webdav/Website/Page Definition",
183: "16-page-definition-folder.gif");
184: this .m_nameIconMapping.put(
185: "/webdav/Website/Composition/Page Templates",
186: "16-template-folder.gif");
187: this .m_nameIconMapping.put(
188: "/webdav/Website/Composition/Object Templates",
189: "16-object-template-folder.gif");
190: this .m_nameIconMapping.put(
191: "/webdav/Website/Composition/Includes",
192: "16-includes-folder.gif");
193: this .m_nameIconMapping.put("/webdav/Website/Display/XSLT",
194: "16-xslt-folder.gif");
195: this .m_nameIconMapping.put(
196: "/webdav/Website/Site Assets/FlashNav",
197: "16-flash-folder.gif");
198: }
199:
200: /* (non-Javadoc)
201: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
202: */
203: public void actionPerformed(ActionEvent arg0) {
204: boolean bChange = (ConfigStore.getInstance().getPropertyValue(
205: "FILENAME_DISPLAY") != null && ConfigStore
206: .getInstance().getPropertyValue("FILENAME_DISPLAY")
207: .equals("DISPLAYNAME"));
208:
209: if (bChange) {
210: ConfigStore.getInstance().setProperty("FILENAME_DISPLAY",
211: "FILENAME");
212: ContextEvent ce = new ContextEvent(
213: ContextType.CONTEXT_FILENAME_DISPLAY);
214: ContextHandler.getInstance().fireContextEvent(ce);
215: }
216:
217: String sDirPath = null;
218:
219: String sDirName = this .getNewName();
220: StateHandler.getInstance().addWait("NEW-COL-ACTION");
221: StatusData statusOverall = new VFSStatus();
222:
223: try {
224: if (!sDirName.equals("")) {
225: sDirPath = this .getLastContextDirectory().getFullPath()
226: + "/" + sDirName;
227: StatusData status = this .getLastContextDirectory()
228: .mkDir(sDirName);
229: statusOverall.addStatusData(status);
230: if (status.isOK()) {
231: super .fireSessionEvent("Created", this
232: .getLastContextDirectory().getVFS(), this
233: .getLastContextDirectory().getFullPath()
234: + "/" + sDirName,
235: SessionEventData.RESOURCE_CREATED);
236: } else {
237: }
238: }
239: } catch (Exception e) {
240: e.printStackTrace(System.err);
241: statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
242: } finally {
243: if (bChange) {
244: ConfigStore.getInstance().setProperty(
245: "FILENAME_DISPLAY", "DISPLAYNAME");
246: ContextEvent ce = new ContextEvent(
247: ContextType.CONTEXT_FILENAME_DISPLAY);
248: ContextHandler.getInstance().fireContextEvent(ce);
249: }
250: StatusMessage statusMessage = new StatusMessage(
251: ActionNewCollection.ACTION_NAME, statusOverall);
252: statusMessage.setResourceTitle(sDirName);
253: statusMessage.setPath(sDirPath);
254: VFSMessageBuilder.getInstance().fireMessage(statusMessage);
255: //VFSMessageBuilder.getInstance().fireMessage(ActionNewCollection.ACTION_NAME, statusOverall, sDirName);
256: StateHandler.getInstance().removeWait("NEW-COL-ACTION");
257: }
258: }
259:
260: /**
261: * Opens a dialog to get a name for the collection.
262: *
263: * @return Name or null if cancel was pressed
264: */
265: private String getNewName() {
266: JFrame frame = new JFrame();
267: frame.setIconImage(((ImageIcon) IconManager.getInstance()
268: .getIcon("32-sim-logo.gif")).getImage());
269:
270: SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame,
271: "Collection name");
272: dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name.");
273: dialog.show();
274: String sName = dialog.getTextValue();
275:
276: while (!sName.equals("") && !this .checkName(sName)) {
277: dialog = new SingleTextEntryDialog(frame, "Collection name");
278: dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name.");
279: dialog.setLabelColor(Color.RED);
280: dialog.show();
281: sName = dialog.getTextValue();
282: }
283:
284: return sName;
285:
286: }
287:
288: /**
289: * Checks if a name is valid.
290: *
291: * @param sName Name to check
292: * @return true if the name if valid
293: */
294: private boolean checkName(String sName) {
295: boolean bRetn = true;
296: String[] validChars = new String[] { "a", "b", "c", "d", "e",
297: "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
298: "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A",
299: "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
300: "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
301: "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7",
302: "8", "0", "_" };
303: List aValidChars = Arrays.asList(validChars);
304:
305: char[] chars = sName.toCharArray();
306: for (int i = 0; i < chars.length; i++) {
307: char c = chars[i];
308: if (!aValidChars.contains(new String(new char[] { c }))) {
309: bRetn = false;
310: break;
311: }
312: }
313:
314: return bRetn;
315: }
316:
317: /* (non-Javadoc)
318: * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
319: */
320: public String getDescription() {
321: return "Creates a new collection under the currently selected collection";
322: }
323:
324: /* (non-Javadoc)
325: * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
326: */
327: public String getText() {
328: return "New Collection";
329: }
330:
331: /* (non-Javadoc)
332: * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
333: */
334: public String getToolTip() {
335: return this .getDescription();
336: }
337:
338: /* (non-Javadoc)
339: * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
340: */
341: public Icon getIcon() {
342: return IconManager.getInstance().getIcon("16-section.gif");
343: }
344:
345: /* (non-Javadoc)
346: * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
347: */
348: public JMenuItem getMenuItem() {
349: JMenuItem menuItem = super .getMenuItem();
350: menuItem.setAccelerator(KeyStroke.getKeyStroke(this
351: .getAcceleratorKeycode(), this .getAcceleratorMask()));
352:
353: return menuItem;
354: }
355:
356: /* (non-Javadoc)
357: * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
358: */
359: public int getAcceleratorKeycode() {
360: return KeyEvent.VK_N;
361: }
362:
363: /* (non-Javadoc)
364: * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
365: */
366: public String getMnemonic() {
367: return "C";
368: }
369:
370: /* (non-Javadoc)
371: * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
372: */
373: public int getAcceleratorMask() {
374: return InputEvent.CTRL_MASK + InputEvent.SHIFT_MASK;
375: }
376:
377: /* (non-Javadoc)
378: * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.vfs.VirtualFile)
379: */
380: public boolean isEnabled(ContextEvent ce) {
381: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_DIRS) {
382: m_bIsEnabled = super .isEnabled(ce);
383:
384: renameButton(ce.getPath());
385: return m_bIsEnabled;
386: } else if (ce.CONTEXT_TYPE == ContextType.CONTEXT_TABS) {
387: m_bIsEnabled = false;
388: this .setEnabled(m_bIsEnabled);
389: this .setEnabled(false);
390: return m_bIsEnabled;
391: } else {
392: return m_bIsEnabled;
393: }
394: }
395:
396: /**
397: * Changes the action's icon and name based on a given path.
398: *
399: * @param sPath Full path
400: */
401: private void renameButton(String sPath) {
402: if (!sPath.equals(this .m_sCurrentPath)) {
403: Iterator itor = this .m_nameIconMapping.keySet().iterator();
404: boolean bFound = false;
405: while (itor.hasNext()) {
406: String sTestPath = (String) itor.next();
407: if (sPath.startsWith(sTestPath)) {
408: try {
409: this .getButton().setIcon(
410: IconManager.getInstance().getIcon(
411: (String) this .m_nameIconMapping
412: .get(sTestPath)));
413: this .getMenuItem().setIcon(
414: IconManager.getInstance().getIcon(
415: (String) this .m_nameIconMapping
416: .get(sTestPath)));
417: } catch (Exception e) {
418:
419: }
420: bFound = true;
421: }
422: }
423:
424: if (!bFound) {
425: this .getButton().setText("New collection");
426: this .getButton().setIcon(
427: IconManager.getInstance().getIcon(
428: "16-section.gif"));
429: this .getMenuItem().setText("New collection");
430: this .getMenuItem().setIcon(
431: IconManager.getInstance().getIcon(
432: "16-section.gif"));
433: this .m_sCurrentPath = "";
434: } else {
435: this.m_sCurrentPath = sPath;
436: }
437: }
438: }
439:
440: }
|