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.editors.*;
033: import org.openharmonise.him.harmonise.*;
034: import org.openharmonise.him.window.messages.builders.*;
035: import org.openharmonise.him.window.session.*;
036: import org.openharmonise.swing.*;
037: import org.openharmonise.vfs.*;
038: import org.openharmonise.vfs.context.*;
039: import org.openharmonise.vfs.gui.*;
040: import org.openharmonise.vfs.status.*;
041:
042: /**
043: * Action to create a new virtual file in the current collection.
044: *
045: * @author Matthew Large
046: * @version $Revision: 1.1 $
047: *
048: */
049: public class ActionNewFile extends AbstractHIMAction implements
050: HIMAction {
051:
052: public static String ACTION_NAME = "NEW_FILE";
053:
054: /**
055: * true if the action is enabled.
056: */
057: boolean m_bIsEnabled = false;
058:
059: /**
060: * Map of path to name.
061: */
062: private HashMap m_namePathMapping = new HashMap();
063:
064: /**
065: * Map of path to icon.
066: */
067: private HashMap m_nameIconMapping = new HashMap();
068:
069: /**
070: * Current path.
071: */
072: private String m_sCurrentPath = "";
073:
074: /**
075: *
076: */
077: public ActionNewFile() {
078: super ();
079: this .setup();
080: }
081:
082: /**
083: * @param vfFile
084: */
085: public ActionNewFile(VirtualFile vfFile) {
086: super (vfFile);
087: this .setup();
088: }
089:
090: /**
091: * Configures this action.
092: *
093: */
094: private void setup() {
095: RuleGroup andGroup = new RuleGroup();
096: andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
097: super .addEnableRule(andGroup);
098:
099: RuleGroup orGroup = new RuleGroup();
100: orGroup.setGroupType(RuleGroup.GROUPTYPE_OR);
101:
102: orGroup
103: .addEnableRule(new PathRule("/webdav/Content/Documents"));
104: orGroup.addEnableRule(new PathRule(
105: "/webdav/Content/Assets/flash"));
106: orGroup.addEnableRule(new PathRule(
107: "/webdav/Content/Assets/audio"));
108: orGroup.addEnableRule(new PathRule(
109: "/webdav/Content/Assets/movies"));
110: orGroup.addEnableRule(new PathRule(
111: "/webdav/Content/Assets/office"));
112: orGroup.addEnableRule(new PathRule(
113: "/webdav/Content/Assets/image"));
114: orGroup.addEnableRule(new PathRule(
115: "/webdav/Content/Assets/Web Resources"));
116: orGroup.addEnableRule(new PathRule(
117: "/webdav/Content/Assets/e-mail"));
118: orGroup.addEnableRule(new PathRule(
119: "/webdav/Content/Assets/links"));
120: orGroup
121: .addEnableRule(new PathRule(
122: "/webdav/Content/Assets/pdf"));
123: orGroup.addEnableRule(new PathRule(
124: "/webdav/Metadata/Properties"));
125: orGroup.addEnableRule(new PathRule("/webdav/Metadata/Values"));
126: orGroup.addEnableRule(new PathRule("/webdav/Newsletter/draft"));
127: orGroup.addEnableRule(new PathRule("/webdav/Reports/Queries"));
128: orGroup.addEnableRule(new PathRule("/webdav/Users"));
129: orGroup.addEnableRule(new PathRule("/webdav/Users/admin"));
130: orGroup.addEnableRule(new PathRule("/webdav/Users/public"));
131: orGroup.addEnableRule(new PathRule(
132: "/webdav/Website/Page Definition"));
133: orGroup.addEnableRule(new PathRule(
134: "/webdav/Website/Composition/Page Templates"));
135: orGroup.addEnableRule(new PathRule(
136: "/webdav/Website/Composition/Object Templates"));
137: orGroup.addEnableRule(new PathRule(
138: "/webdav/Website/Composition/Includes"));
139: orGroup.addEnableRule(new PathRule(
140: "/webdav/Website/Display/XSLT"));
141: orGroup.addEnableRule(new PathRule(
142: "/webdav/Website/Site Assets/FlashNav"));
143: //orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WEBSITE ) );
144: orGroup.addEnableRule(new PathRule(
145: HarmonisePaths.PATH_WORKFLOW_DEFINITIONS));
146: orGroup.addEnableRule(new PathRule(
147: HarmonisePaths.PATH_WORKFLOW_PROPS));
148: orGroup.addEnableRule(new PathRule(
149: HarmonisePaths.PATH_WORKFLOW_STAGES));
150: orGroup.addEnableRule(new PathRule(
151: HarmonisePaths.PATH_REPORTS_QUERIES));
152:
153: andGroup.addEnableRule(orGroup);
154:
155: EnableRule rule = new IsDirectoryRule();
156: andGroup.addEnableRule(rule);
157:
158: this .m_namePathMapping.put("/webdav/Content/Assets/flash",
159: "New Flash Movie");
160: this .m_namePathMapping.put("/webdav/Content/Assets/movies",
161: "New Movie");
162: this .m_namePathMapping.put("/webdav/Content/Assets/office",
163: "New Office Document");
164: this .m_namePathMapping.put("/webdav/Content/Assets/image",
165: "New Image");
166: this .m_namePathMapping.put(
167: "/webdav/Content/Assets/Web Resources",
168: "New Web Resource");
169: this .m_namePathMapping.put("/webdav/Content/Assets/e-mail",
170: "New E-mail Address");
171: this .m_namePathMapping.put("/webdav/Content/Assets/links",
172: "New Web Link");
173: this .m_namePathMapping.put("/webdav/Content/Assets/pdf",
174: "New PDF");
175: this .m_namePathMapping.put("/webdav/Content/Documents",
176: "New Document");
177: this .m_namePathMapping.put("/webdav/Metadata/Properties",
178: "New Property");
179: this .m_namePathMapping.put("/webdav/Metadata/Values",
180: "New Value");
181: this .m_namePathMapping.put("/webdav/Newsletter/draft",
182: "New Newsletter");
183: this .m_namePathMapping.put("/webdav/Reports/Queries",
184: "New Report");
185: this .m_namePathMapping.put("/webdav/Users", "New User");
186: this .m_namePathMapping.put("/webdav/Website/Page Definition",
187: "New Page Definition");
188: this .m_namePathMapping.put(
189: "/webdav/Website/Composition/Page Templates",
190: "New Page Template");
191: this .m_namePathMapping.put(
192: "/webdav/Website/Composition/Object Templates",
193: "New Object Template");
194: this .m_namePathMapping.put(
195: "/webdav/Website/Composition/Includes", "New Include");
196: this .m_namePathMapping.put("/webdav/Website/Display/XSLT",
197: "New XSLT");
198: this .m_namePathMapping.put(
199: "/webdav/Website/Site Assets/FlashNav",
200: "New Flash Navigation");
201: this .m_namePathMapping.put(HarmonisePaths.PATH_REPORTS_QUERIES,
202: "New Report Query");
203: this .m_namePathMapping.put(HarmonisePaths.PATH_WORKFLOW_PROPS,
204: "New Workflow");
205: this .m_namePathMapping.put(HarmonisePaths.PATH_WORKFLOW_STAGES,
206: "New WorkflowStage");
207:
208: this .m_nameIconMapping.put("/webdav/Content/Assets/flash",
209: "16-flash.gif");
210: this .m_nameIconMapping.put("/webdav/Content/Assets/movies",
211: "16-movie.gif");
212: this .m_nameIconMapping.put("/webdav/Content/Assets/office",
213: "16-document.gif");
214: this .m_nameIconMapping.put("/webdav/Content/Assets/image",
215: "16-image.gif");
216: this .m_nameIconMapping.put(
217: "/webdav/Content/Assets/Web Resources", "16-link.gif");
218: this .m_nameIconMapping.put("/webdav/Content/Assets/e-mail",
219: "16-email.gif");
220: this .m_nameIconMapping.put("/webdav/Content/Assets/links",
221: "16-link.gif");
222: this .m_nameIconMapping.put("/webdav/Content/Assets/pdf",
223: "16-pdf.gif");
224: this .m_nameIconMapping.put("/webdav/Content/Documents",
225: "16-document.gif");
226: this .m_nameIconMapping.put("/webdav/Metadata/Properties",
227: "16-property.gif");
228: this .m_nameIconMapping.put("/webdav/Metadata/Values",
229: "16-value.gif");
230: this .m_nameIconMapping.put("/webdav/Newsletter/draft",
231: "16-newsletter.gif");
232: this .m_nameIconMapping.put("/webdav/Reports/Queries",
233: "16-report.gif");
234: this .m_nameIconMapping.put("/webdav/Users", "16-user.gif");
235: this .m_nameIconMapping.put("/webdav/Website/Page Definition",
236: "16-page-definition.gif");
237: this .m_nameIconMapping.put(
238: "/webdav/Website/Composition/Page Templates",
239: "16-template.gif");
240: this .m_nameIconMapping.put(
241: "/webdav/Website/Composition/Object Templates",
242: "16-object-template.gif");
243: this .m_nameIconMapping.put(
244: "/webdav/Website/Composition/Includes",
245: "16-includes.gif");
246: this .m_nameIconMapping.put("/webdav/Website/Display/XSLT",
247: "16-xslt.gif");
248: this .m_nameIconMapping.put(
249: "/webdav/Website/Site Assets/FlashNav", "16-flash.gif");
250: this .m_nameIconMapping.put(HarmonisePaths.PATH_REPORTS_QUERIES,
251: "16-query.gif");
252: this .m_nameIconMapping.put(HarmonisePaths.PATH_WORKFLOW_PROPS,
253: "16-command-change-status.gif");
254: this .m_nameIconMapping.put(HarmonisePaths.PATH_WORKFLOW_STAGES,
255: "16-workflow-container.gif");
256:
257: }
258:
259: /* (non-Javadoc)
260: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
261: */
262: public void actionPerformed(ActionEvent arg0) {
263: boolean bChange = (ConfigStore.getInstance().getPropertyValue(
264: "FILENAME_DISPLAY") != null && ConfigStore
265: .getInstance().getPropertyValue("FILENAME_DISPLAY")
266: .equals("DISPLAYNAME"));
267:
268: StatusData statusOverall = new VFSStatus();
269:
270: if (bChange) {
271: ConfigStore.getInstance().setProperty("FILENAME_DISPLAY",
272: "FILENAME");
273: ContextEvent ce = new ContextEvent(
274: ContextType.CONTEXT_FILENAME_DISPLAY);
275: ContextHandler.getInstance().fireContextEvent(ce);
276: }
277: String sName = "";
278: if (this .getLastContextDirectory().getFullPath().startsWith(
279: HarmonisePaths.PATH_LINKS)
280: || this .getLastContextDirectory().getFullPath()
281: .startsWith(HarmonisePaths.PATH_EMAIL)) {
282:
283: String sLink = "";
284: String sTitle = "New Web Address";
285: String sPath = null;
286:
287: if (this .getLastContextDirectory().getFullPath()
288: .startsWith(HarmonisePaths.PATH_EMAIL)) {
289: sTitle = "New E-mail Address";
290: }
291:
292: JFrame frame = new JFrame();
293: frame.setIconImage(((ImageIcon) IconManager.getInstance()
294: .getIcon("32-sim-logo.gif")).getImage());
295:
296: DualTextEntryDialog dialog = new DualTextEntryDialog(frame,
297: sTitle);
298: dialog.setLabelText("Only use a-z, A-Z and 0-9 in name.");
299: dialog.setCaption1("Resource name");
300: dialog.setCaption2("Address");
301: dialog.show();
302: sName = dialog.getText1Value();
303: sLink = dialog.getText2Value();
304:
305: while (!sName.equals("") && !this .checkName(sName)) {
306: dialog = new DualTextEntryDialog(frame, "Resource name");
307: dialog
308: .setLabelText("Only use a-z, A-Z and 0-9 in name.");
309: dialog.setLabelColor(Color.RED);
310: dialog.setCaption1("Resource name");
311: dialog.setCaption2("Address");
312: dialog.show();
313: sName = dialog.getText1Value();
314: sLink = dialog.getText2Value();
315: }
316:
317: StateHandler.getInstance().addWait("NEW-FILE-ACTION");
318: try {
319: if (!sName.equals("") && !sLink.equals("")) {
320: sPath = this .getLastContextDirectory()
321: .getFullPath()
322: + "/" + sName;
323:
324: StatusData status = EditorController.getInstance()
325: .createNew(
326: sPath,
327: sLink.getBytes(),
328: this .getLastContextDirectory()
329: .getVFS());
330: statusOverall.addStatusData(status);
331: if (status.isOK()) {
332: VirtualFile vfNewFile = this
333: .getLastContextDirectory().getVFS()
334: .getVirtualFile(sPath).getResource();
335: super .fireSessionEvent("Created", this
336: .getLastContextDirectory().getVFS(),
337: sPath,
338: SessionEventData.RESOURCE_CREATED);
339: } else {
340: }
341: }
342: } catch (Exception e) {
343: e.printStackTrace(System.err);
344: statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
345: } finally {
346: if (bChange) {
347: ConfigStore.getInstance().setProperty(
348: "FILENAME_DISPLAY", "DISPLAYNAME");
349: ContextEvent ce = new ContextEvent(
350: ContextType.CONTEXT_FILENAME_DISPLAY);
351: ContextHandler.getInstance().fireContextEvent(ce);
352: }
353: StateHandler.getInstance()
354: .removeWait("NEW-FILE-ACTION");
355: if (!sName.equals("") && !sLink.equals("")) {
356: StatusMessage statusMessage = new StatusMessage(
357: ActionNewFile.ACTION_NAME, statusOverall);
358: statusMessage.setResourceTitle(sName);
359: statusMessage.setPath(sPath);
360: VFSMessageBuilder.getInstance().fireMessage(
361: statusMessage);
362: }
363: }
364: } else {
365: sName = this .getNewName();
366: String sPath = null;
367:
368: StateHandler.getInstance().addWait("NEW-FILE-ACTION");
369: try {
370: if (!sName.equals("")) {
371: sPath = this .getLastContextDirectory()
372: .getFullPath()
373: + "/" + sName;
374:
375: StatusData status = EditorController.getInstance()
376: .createNew(
377: sPath,
378: this .getLastContextDirectory()
379: .getVFS());
380: statusOverall.addStatusData(status);
381: if (status.isOK()) {
382: VirtualFile vfNewFile = this
383: .getLastContextDirectory().getVFS()
384: .getVirtualFile(sPath).getResource();
385: super .fireSessionEvent("Created", this
386: .getLastContextDirectory().getVFS(),
387: sPath,
388: SessionEventData.RESOURCE_CREATED);
389:
390: if (sPath.startsWith(HarmonisePaths.PATH_USERS)) {
391: VirtualFile vfFile = this
392: .getLastContextDirectory().getVFS()
393: .getVirtualFile(sPath)
394: .getResource();
395: if (vfFile != null) {
396: vfFile.lock();
397: ActionChangeUserPassword action = new ActionChangeUserPassword();
398: action.changePassword(
399: "Set the user's password.",
400: this .getLastContextDirectory()
401: .getVFS()
402: .getVirtualFile(sPath)
403: .getResource());
404: }
405: }
406: } else {
407: }
408: }
409: } catch (Exception e) {
410: e.printStackTrace(System.err);
411: statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
412: } finally {
413: if (bChange) {
414: ConfigStore.getInstance().setProperty(
415: "FILENAME_DISPLAY", "DISPLAYNAME");
416: ContextEvent ce = new ContextEvent(
417: ContextType.CONTEXT_FILENAME_DISPLAY);
418: ContextHandler.getInstance().fireContextEvent(ce);
419: }
420: StateHandler.getInstance()
421: .removeWait("NEW-FILE-ACTION");
422: if (!sName.equals("")) {
423: StatusMessage statusMessage = new StatusMessage(
424: ActionNewFile.ACTION_NAME, statusOverall);
425: statusMessage.setResourceTitle(sName);
426: statusMessage.setPath(sPath);
427: VFSMessageBuilder.getInstance().fireMessage(
428: statusMessage);
429: }
430: }
431: }
432: }
433:
434: /**
435: * Opens a dialog to get a name.
436: *
437: * @return Name or null if cancel was pressed
438: */
439: private String getNewName() {
440: JFrame frame = new JFrame();
441: frame.setIconImage(((ImageIcon) IconManager.getInstance()
442: .getIcon("32-sim-logo.gif")).getImage());
443:
444: SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame,
445: "Resource name");
446: dialog
447: .setLabelText("Only use a-z,A-Z,_ and 0-9 in name, start with a letter");
448: dialog.show();
449: String sName = dialog.getTextValue();
450:
451: while (!sName.equals("") && !this .checkName(sName)) {
452: dialog = new SingleTextEntryDialog(frame, "Resource name");
453: dialog
454: .setLabelText("Only use a-z,A-Z,_ and 0-9 in name, start with a letter");
455: dialog.setLabelColor(Color.RED);
456: dialog.show();
457: sName = dialog.getTextValue();
458: }
459:
460: return sName;
461:
462: }
463:
464: /**
465: * Checks if a name if valid.
466: *
467: * @param sName Name to check
468: * @return true if the name is valid
469: */
470: private boolean checkName(String sName) {
471: boolean bRetn = true;
472: String[] validChars = new String[] { "a", "b", "c", "d", "e",
473: "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
474: "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A",
475: "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
476: "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
477: "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7",
478: "8", "9", "_" };
479: String[] numbers = new String[] { "0", "1", "2", "3", "4", "5",
480: "6", "7", "8", "9" };
481: List aNumbers = Arrays.asList(numbers);
482: List aValidChars = Arrays.asList(validChars);
483:
484: char[] chars = sName.toCharArray();
485: for (int i = 0; i < chars.length; i++) {
486: char c = chars[i];
487: if (i == 0
488: && aNumbers.contains(new String(new char[] { c }))) {
489: bRetn = false;
490: break;
491: }
492: if (!aValidChars.contains(new String(new char[] { c }))) {
493: bRetn = false;
494: break;
495: }
496: }
497:
498: return bRetn;
499: }
500:
501: /* (non-Javadoc)
502: * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
503: */
504: public JMenuItem getMenuItem() {
505: JMenuItem menuItem = super .getMenuItem();
506: menuItem.setAccelerator(KeyStroke.getKeyStroke(this
507: .getAcceleratorKeycode(), this .getAcceleratorMask()));
508:
509: return menuItem;
510: }
511:
512: /* (non-Javadoc)
513: * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
514: */
515: public String getDescription() {
516: return "Creates a new resource under the currently selected collection";
517: }
518:
519: /* (non-Javadoc)
520: * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
521: */
522: public String getText() {
523: return "New ...";
524: }
525:
526: /* (non-Javadoc)
527: * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
528: */
529: public String getToolTip() {
530: return this .getDescription();
531: }
532:
533: /* (non-Javadoc)
534: * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
535: */
536: public Icon getIcon() {
537: return IconManager.getInstance().getIcon("16-document.gif");
538: }
539:
540: /* (non-Javadoc)
541: * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
542: */
543: public int getAcceleratorKeycode() {
544: return KeyEvent.VK_N;
545: }
546:
547: /* (non-Javadoc)
548: * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
549: */
550: public String getMnemonic() {
551: return "N";
552: }
553:
554: /* (non-Javadoc)
555: * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
556: */
557: public int getAcceleratorMask() {
558: return InputEvent.CTRL_MASK;
559: }
560:
561: /* (non-Javadoc)
562: * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.vfs.VirtualFile)
563: */
564: public boolean isEnabled(ContextEvent ce) {
565: //System.out.println("NEW FILE - got ContextEvent[" + ce.CONTEXT_TYPE + "] for path[" + ce.getPath());
566: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_DIRS) {
567:
568: renameButton(ce.getPath());
569:
570: m_bIsEnabled = super .isEnabled(ce);
571: return m_bIsEnabled;
572: } else if (ce.CONTEXT_TYPE == ContextType.CONTEXT_TABS) {
573: m_bIsEnabled = false;
574: this .setEnabled(false);
575: return m_bIsEnabled;
576: } else {
577: this .setEnabled(m_bIsEnabled);
578: return m_bIsEnabled;
579: }
580: }
581:
582: /**
583: * Changes the name and icon of the action based on a given path.
584: *
585: * @param sPath Full path
586: */
587: private void renameButton(String sPath) {
588: if (!sPath.equals(this .m_sCurrentPath)) {
589: Iterator itor = this .m_namePathMapping.keySet().iterator();
590: boolean bFound = false;
591: while (itor.hasNext()) {
592: String sTestPath = (String) itor.next();
593: if (sPath.startsWith(sTestPath)) {
594: try {
595: this .getButton().setText(
596: (String) this .m_namePathMapping
597: .get(sTestPath));
598: this .getButton().setIcon(
599: IconManager.getInstance().getIcon(
600: (String) this .m_nameIconMapping
601: .get(sTestPath)));
602: this .getMenuItem().setText(
603: (String) this .m_namePathMapping
604: .get(sTestPath));
605: this .getMenuItem().setIcon(
606: IconManager.getInstance().getIcon(
607: (String) this .m_nameIconMapping
608: .get(sTestPath)));
609: } catch (Exception e) {
610:
611: }
612: bFound = true;
613: }
614: }
615:
616: if (!bFound) {
617: this .getButton().setText("New ...");
618: this .getButton().setIcon(
619: IconManager.getInstance().getIcon(
620: "16-document.gif"));
621: this .getMenuItem().setText("New ...");
622: this .getMenuItem().setIcon(
623: IconManager.getInstance().getIcon(
624: "16-document.gif"));
625: this .m_sCurrentPath = "";
626: } else {
627: this.m_sCurrentPath = sPath;
628: }
629: }
630: }
631:
632: }
|