001: /*
002: * $RCSfile: VAIProductController.java,v $
003: * @modification $Date: 2001/09/28 19:27:49 $
004: * @version $Id: VAIProductController.java,v 1.1 2001/09/28 19:27:49 hfalk Exp $
005: *
006: */
007:
008: package com.memoire.vainstall.builder;
009:
010: import com.memoire.vainstall.VAGlobals;
011:
012: import com.memoire.vainstall.builder.action.*;
013: import com.memoire.vainstall.builder.event.*;
014: import com.memoire.vainstall.builder.gui.*;
015: import com.memoire.vainstall.builder.util.*;
016:
017: import java.awt.event.*;
018: import java.io.File;
019: import java.util.*;
020:
021: import javax.swing.*;
022: import javax.swing.event.*;
023: import javax.swing.tree.*;
024:
025: /**
026: * This is the product controller class
027: *
028: * @see com.memoire.vainstall.builder.event.VAIProductListener
029: * @see java.awt.event.ActionListener
030: * @see javax.swing.event.InternalFrameListener
031: *
032: * @author Henrik Falk
033: * @version $Id: VAIProductController.java,v 1.1 2001/09/28 19:27:49 hfalk Exp $
034: */
035: public class VAIProductController implements VAIProductListener,
036: InternalFrameListener, ActionListener, MouseListener {
037:
038: /**
039: * The product 'internal' frame
040: */
041: VAIProductInternalFrame frame = new VAIProductInternalFrame();
042:
043: /**
044: * The title text on the product frame
045: */
046: String title = "";
047:
048: /**
049: * The product datamodel
050: */
051: VAIProductModel model = new VAIProductModel();
052:
053: /**
054: * Nodes in the tree view on the inernal frame
055: */
056: AbstractVAIProductNode root;
057:
058: /**
059: * List of actions that are used for popup menus in the tree view
060: */
061: private Hashtable actionList = new Hashtable();
062:
063: /**
064: * we need to remember this a the moment
065: */
066: ProductStepsNode productStepsNode;
067:
068: /**
069: * Default constructor
070: */
071: public VAIProductController() {
072: super ();
073:
074: // listen to changes in the model
075: model.addVAIProductListener(this );
076:
077: frame.addInternalFrameListener(this );
078:
079: // initialize actionlist
080: getActionList();
081:
082: frame.getTree().addMouseListener(this );
083:
084: }
085:
086: public Hashtable getActionList() {
087:
088: if (actionList.size() == 0) {
089:
090: // ProductAddRequiredStepsAction
091: ProductAddRequiredStepsAction productAddRequiredStepsAction = new ProductAddRequiredStepsAction();
092: productAddRequiredStepsAction.initialize(this );
093: actionList.put("ProductAddRequiredStepsAction",
094: productAddRequiredStepsAction);
095: }
096: return actionList;
097: }
098:
099: /**
100: * Returns whether the data model needs to be saved or not.
101: * @return true if the datamodel needs to be saved
102: */
103: public boolean isDirty() {
104: return model.isDirty();
105: }
106:
107: /**
108: * Implements the VAIProductListener interface
109: * @param evt The event that 'explains' what has changed in the model
110: */
111: public void productChanged(VAIProductEvent evt) {
112:
113: switch (evt.getType()) {
114: case VAIProductEvent.PROJECT_LOADED:
115: // Build the nodes for the treemodel and set it
116: frame.setNode(getRootNode());
117:
118: TreePath stepsPath = new TreePath(((DefaultTreeModel) frame
119: .getTree().getModel())
120: .getPathToRoot(productStepsNode));
121: frame.getTree().expandPath(stepsPath);
122:
123: // set title on frame
124: title = getModel().getProductName()
125: + VAGlobals.getResource(
126: "com.memoire.vainstall.builder.Language",
127: "VAIProductController_Version")
128: + getModel().getProductVersion();
129: frame.setTitle(title);
130: break;
131: case VAIProductEvent.PROJECT_SAVED:
132: frame.setTitle(title);
133: break;
134: case VAIProductEvent.PROJECT_DIRTY:
135: frame.setTitle(VAGlobals.getResource(
136: "com.memoire.vainstall.builder.Language",
137: "VAIProductController_Dirty")
138: + title);
139: break;
140: }
141: }
142:
143: /**
144: * Returns a reference to the product frame
145: * @return The product frame
146: */
147: public VAIProductInternalFrame getFrame() {
148: return frame;
149: }
150:
151: /**
152: * Returns the datamodel of a product
153: * @return a VAIProductModel
154: */
155: public VAIProductModel getModel() {
156: return model;
157: }
158:
159: /**
160: * Implements the ActionListener interface
161: * @param evt The action event
162: */
163: public void actionPerformed(ActionEvent evt) {
164:
165: // extract the control that sends the event
166: Object source = evt.getSource();
167:
168: // hardcode this for the moment
169: try {
170: getFrame().setVisible(true);
171: getFrame().setSelected(true);
172: getFrame().setMaximum(true);
173: } catch (Exception exc) {
174: // handle this in the future
175: // exc.printStackTrace();
176: }
177:
178: }
179:
180: /**
181: * internalFrameOpened
182: * @param evt The internal frame event
183: */
184: public void internalFrameOpened(InternalFrameEvent evt) {
185: }
186:
187: /**
188: * internalFrameClosing
189: * @param evt The internal frame event
190: */
191: public void internalFrameClosing(InternalFrameEvent evt) {
192: }
193:
194: /**
195: * internalFrameClosed
196: * @param evt The internal frame event
197: */
198: public void internalFrameClosed(InternalFrameEvent evt) {
199: }
200:
201: /**
202: * internalFrameIconified
203: * @param evt The internal frame event
204: */
205: public void internalFrameIconified(InternalFrameEvent evt) {
206: }
207:
208: /**
209: * internalFrameDeiconfied
210: * @param evt The internal frame event
211: */
212: public void internalFrameDeiconified(InternalFrameEvent evt) {
213: }
214:
215: /**
216: * internalFrameDeactivated
217: * @param evt The internal frame event
218: */
219: public void internalFrameDeactivated(InternalFrameEvent evt) {
220: getModel().fireVAIProductEvent(
221: new VAIProductEvent(this ,
222: VAIProductEvent.PROJECT_DEACTIVATED));
223: }
224:
225: /**
226: * internalFrameActivated
227: * @param evt The internal frame event
228: */
229: public void internalFrameActivated(InternalFrameEvent evt) {
230:
231: if (getModel().getRequiredList().size() == 0) {
232: if (getModel().isDirty() == false) {
233: getModel()
234: .fireVAIProductEvent(
235: new VAIProductEvent(
236: this ,
237: VAIProductEvent.PROJECT_REQUIREMENTS_MET));
238: }
239: } else {
240: getModel()
241: .fireVAIProductEvent(
242: new VAIProductEvent(
243: this ,
244: VAIProductEvent.PROJECT_REQUIREMENTS_NOTMET));
245: }
246:
247: getModel().fireVAIProductEvent(
248: new VAIProductEvent(this ,
249: VAIProductEvent.PROJECT_ACTIVATED));
250: }
251:
252: /**
253: * Create a new project
254: * @param projectName The name of the project which is the product name
255: * @param projectVersion The version of the which is the product version
256: * @param projectDirectory The directory in where the project should be
257: * @param productType The type of product 'install, update, module'
258: * @throws com.memoire.vainstall.builder.util.VAIBuilderException
259: */
260: public void createProject(String projectName,
261: String projectVersion, String projectDirectory,
262: String productType) throws VAIBuilderException {
263:
264: // try to create project
265: if (new File(projectDirectory).exists() == true) {
266: throw new VAIBuilderException(VAGlobals.getResource(
267: "com.memoire.vainstall.builder.Language",
268: "VAIProductController_CreateProject"), VAGlobals
269: .getResource(
270: "com.memoire.vainstall.builder.Language",
271: "VAIProductController_AlreadyExists"),
272: VAGlobals.getResource(
273: "com.memoire.vainstall.builder.Language",
274: "VAIProductController_AlreadyExistsHelp"));
275: }
276:
277: try {
278: boolean ok = new File(projectDirectory).mkdir();
279: if (ok == false) {
280: throw new VAIBuilderException(
281: VAGlobals
282: .getResource(
283: "com.memoire.vainstall.builder.Language",
284: "VAIProductController_CreateProject"),
285: VAGlobals
286: .getResource(
287: "com.memoire.vainstall.builder.Language",
288: "VAIProductController_NotCreated"),
289: null);
290: }
291: } catch (SecurityException exc) {
292: throw new VAIBuilderException(VAGlobals.getResource(
293: "com.memoire.vainstall.builder.Language",
294: "VAIProductController_CreateProject"), VAGlobals
295: .getResource(
296: "com.memoire.vainstall.builder.Language",
297: "VAIProductController_NotCreatedSecurity"),
298: null);
299: }
300:
301: model.setProductName(projectName);
302: model.setProductVersion(projectVersion);
303: model.setProductDirectory(projectDirectory);
304: model.setProductType(productType);
305:
306: try {
307: model.save();
308: } catch (Exception exc) {
309: try {
310: new File(projectDirectory).delete();
311: } catch (Exception innerexc) {
312: throw new VAIBuilderException(
313: VAGlobals
314: .getResource(
315: "com.memoire.vainstall.builder.Language",
316: "VAIProductController_CreateProject"),
317: VAGlobals
318: .getResource(
319: "com.memoire.vainstall.builder.Language",
320: "VAIProductController_NotSaved"),
321: null);
322: }
323: throw new VAIBuilderException(VAGlobals.getResource(
324: "com.memoire.vainstall.builder.Language",
325: "VAIProductController_CreateProject"), VAGlobals
326: .getResource(
327: "com.memoire.vainstall.builder.Language",
328: "VAIProductController_NotSaved"), null);
329: }
330:
331: // Build the nodes for the treemodel and set it
332: frame.setNode(getRootNode());
333:
334: TreePath stepsPath = new TreePath(((DefaultTreeModel) frame
335: .getTree().getModel()).getPathToRoot(productStepsNode));
336: frame.getTree().expandPath(stepsPath);
337:
338: // set title on frame
339: title = getModel().getProductName()
340: + VAGlobals.getResource(
341: "com.memoire.vainstall.builder.Language",
342: "VAIProductController_Version")
343: + getModel().getProductVersion();
344: frame.setTitle(title);
345: }
346:
347: private AbstractVAIProductNode buildUINodes() {
348:
349: root = new ProductBaseNode();
350: root.initialize(this );
351:
352: // the target node
353: ProductTargetNode targetNode = new ProductTargetNode();
354: targetNode.initialize(this );
355: root.add(targetNode);
356:
357: // the visual node
358: ProductVisualNode visualNode = new ProductVisualNode();
359: visualNode.initialize(this );
360: root.add(visualNode);
361:
362: // the language node
363: ProductLanguageNode languageNode = new ProductLanguageNode();
364: languageNode.initialize(this );
365: root.add(languageNode);
366:
367: AbstractVAIProductNode packageType = null;
368: if (getModel().getProductType().equals("install") == true) {
369: packageType = new ProductInstallNode();
370: }
371: if (getModel().getProductType().equals("update") == true) {
372: packageType = new ProductUpdateNode();
373: }
374: if (getModel().getProductType().equals("module") == true) {
375: packageType = new ProductModuleNode();
376: }
377:
378: if (packageType == null) {
379: // something went wrong...
380: return root;
381: }
382:
383: packageType.initialize(this );
384: root.add(packageType);
385:
386: // the uninstall node
387: ProductUninstallNode uninstallNode = new ProductUninstallNode();
388: uninstallNode.initialize(this );
389: root.add(uninstallNode);
390:
391: // the files node
392: ProductFilesNode filesNode = new ProductFilesNode();
393: filesNode.initialize(this );
394: packageType.add(filesNode);
395:
396: // At the moment we need to remember this globally
397: productStepsNode = new ProductStepsNode();
398: productStepsNode.initialize(this );
399: packageType.add(productStepsNode);
400:
401: if (getModel().getStepsList().size() > 0) {
402: ListIterator iterator = getModel().getStepsList()
403: .listIterator();
404: while (iterator.hasNext() == true) {
405: String type = (String) iterator.next();
406:
407: // properly a stupid hardcoded way to do this
408: if (type.equals("language") == true) {
409: ProductStepLanguageNode language = new ProductStepLanguageNode();
410: language.initialize(this );
411: productStepsNode.add(language);
412: }
413: if (type.equals("welcome") == true) {
414: ProductStepWelcomeNode welcome = new ProductStepWelcomeNode();
415: welcome.initialize(this );
416: productStepsNode.add(welcome);
417: }
418: if (type.equals("license") == true) {
419: ProductStepLicenseNode license = new ProductStepLicenseNode();
420: license.initialize(this );
421: productStepsNode.add(license);
422: }
423: if (type.equals("readme") == true) {
424: ProductStepReadmeNode readme = new ProductStepReadmeNode();
425: readme.initialize(this );
426: productStepsNode.add(readme);
427: }
428: if (type.equals("upgrade") == true) {
429: ProductStepUpgradeNode upgrade = new ProductStepUpgradeNode();
430: upgrade.initialize(this );
431: productStepsNode.add(upgrade);
432: }
433: if (type.equals("directory") == true) {
434: ProductStepDirectoryNode directory = new ProductStepDirectoryNode();
435: directory.initialize(this );
436: productStepsNode.add(directory);
437: }
438: if (type.equals("install") == true) {
439: ProductStepInstallNode install = new ProductStepInstallNode();
440: install.initialize(this );
441: productStepsNode.add(install);
442: }
443: if (type.equals("shortcut") == true) {
444: ProductStepShortcutsNode shortcut = new ProductStepShortcutsNode();
445: shortcut.initialize(this );
446: productStepsNode.add(shortcut);
447: }
448: if (type.equals("end") == true) {
449: ProductStepEndNode end = new ProductStepEndNode();
450: end.initialize(this );
451: productStepsNode.add(end);
452: }
453:
454: }
455: }
456: return root;
457: }
458:
459: /**
460: * Adds a node before the named on the same level
461: * Check for root
462: */
463: public void addUINodeBefore(String uiName,
464: AbstractVAIProductNode node) {
465:
466: Enumeration nodes = root.postorderEnumeration();
467: while (nodes.hasMoreElements() == true) {
468: AbstractVAIProductNode tmpNode = (AbstractVAIProductNode) nodes
469: .nextElement();
470: if (tmpNode.getName().equals(uiName) == true) {
471: int index = tmpNode.getParent().getIndex(tmpNode);
472: if (index == 0) {
473: tmpNode.add(node);
474: } else {
475: ((MutableTreeNode) tmpNode.getParent()).insert(
476: node, index - 1);
477: }
478: break;
479: }
480: }
481: ((DefaultTreeModel) frame.getTree().getModel())
482: .nodeStructureChanged(node);
483: }
484:
485: /**
486: * Adds a node after the named on the same level
487: */
488: public void addUINodeAfter(String uiName,
489: AbstractVAIProductNode node) {
490:
491: Enumeration nodes = root.postorderEnumeration();
492: while (nodes.hasMoreElements() == true) {
493: AbstractVAIProductNode tmpNode = (AbstractVAIProductNode) nodes
494: .nextElement();
495: if (tmpNode.getName().equals(uiName) == true) {
496: ((MutableTreeNode) tmpNode.getParent()).insert(node,
497: tmpNode.getParent().getIndex(tmpNode));
498: break;
499: }
500: }
501: ((DefaultTreeModel) frame.getTree().getModel())
502: .nodeStructureChanged(node);
503: }
504:
505: /**
506: * remove a node
507: */
508: public void removeUINode(String uiName) {
509:
510: AbstractVAIProductNode parent = null;
511:
512: Enumeration nodes = root.postorderEnumeration();
513: while (nodes.hasMoreElements() == true) {
514: AbstractVAIProductNode tmpNode = (AbstractVAIProductNode) nodes
515: .nextElement();
516: if (tmpNode.getName().equals(uiName) == true) {
517: parent = (AbstractVAIProductNode) tmpNode.getParent();
518: tmpNode.removeFromParent();
519: tmpNode = null;
520: break;
521: }
522: }
523: ((DefaultTreeModel) frame.getTree().getModel())
524: .nodeStructureChanged(parent);
525: }
526:
527: /**
528: * Adds a node before the named on the same level
529: * Check for root
530: */
531: public void addUIChildNodeBefore(String uiName,
532: AbstractVAIProductNode node) {
533:
534: Enumeration nodes = root.postorderEnumeration();
535: while (nodes.hasMoreElements() == true) {
536: AbstractVAIProductNode tmpNode = (AbstractVAIProductNode) nodes
537: .nextElement();
538: if (tmpNode.getName().equals(uiName) == true) {
539: int index = tmpNode.getIndex(tmpNode);
540: if (index == 0) {
541: tmpNode.add(node);
542: } else {
543: tmpNode.insert((MutableTreeNode) node, index - 1);
544: }
545: break;
546: }
547: }
548: ((DefaultTreeModel) frame.getTree().getModel())
549: .nodeStructureChanged(node);
550: }
551:
552: /**
553: * Adds a node after the named on the same level
554: */
555: public void addUIChildNodeAfter(String uiName,
556: AbstractVAIProductNode node) {
557:
558: Enumeration nodes = root.postorderEnumeration();
559: while (nodes.hasMoreElements() == true) {
560: AbstractVAIProductNode tmpNode = (AbstractVAIProductNode) nodes
561: .nextElement();
562: if (tmpNode.getName().equals(uiName) == true) {
563: tmpNode.insert((MutableTreeNode) node, tmpNode
564: .getParent().getIndex(tmpNode));
565: break;
566: }
567: }
568:
569: ((DefaultTreeModel) frame.getTree().getModel())
570: .nodeStructureChanged(node);
571: }
572:
573: public AbstractVAIProductNode getRootNode() {
574: if (root == null) {
575: buildUINodes();
576: }
577: return root;
578: }
579:
580: public void mouseClicked(MouseEvent e) {
581: }
582:
583: public void mouseEntered(MouseEvent e) {
584: }
585:
586: public void mouseExited(MouseEvent e) {
587: }
588:
589: public void mousePressed(MouseEvent evt) {
590:
591: if (evt.isPopupTrigger()) {
592:
593: TreePath path = frame.getTree().getSelectionPath();
594: if (path == null) {
595: return;
596: }
597: Object obj = path.getLastPathComponent();
598:
599: if (obj instanceof DefaultMutableTreeNode) {
600: Object userObject = ((DefaultMutableTreeNode) obj)
601: .getUserObject();
602:
603: if (userObject instanceof AbstractVAIProductNode) {
604: AbstractVAIProductNode node = (AbstractVAIProductNode) userObject;
605: if (node.getPopupMenu() != null) {
606: node.getPopupMenu().show(evt.getComponent(),
607: evt.getX(), evt.getY());
608: }
609: }
610: }
611:
612: }
613: }
614:
615: public void mouseReleased(MouseEvent evt) {
616: if (evt.isPopupTrigger()) {
617:
618: TreePath path = frame.getTree().getSelectionPath();
619: if (path == null) {
620: return;
621: }
622: Object obj = path.getLastPathComponent();
623:
624: if (obj instanceof DefaultMutableTreeNode) {
625: Object userObject = ((DefaultMutableTreeNode) obj)
626: .getUserObject();
627:
628: if (userObject instanceof AbstractVAIProductNode) {
629: AbstractVAIProductNode node = (AbstractVAIProductNode) userObject;
630: if (node.getPopupMenu() != null) {
631: node.getPopupMenu().show(evt.getComponent(),
632: evt.getX(), evt.getY());
633: }
634: }
635: }
636:
637: }
638: }
639:
640: }
|