KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > builder > VAIProductController


1 /*
2  * $RCSfile: VAIProductController.java,v $
3  * @modification $Date: 2001/09/28 19:27:49 $
4  * @version $Id: VAIProductController.java,v 1.1 2001/09/28 19:27:49 hfalk Exp $
5  *
6  */

7
8 package com.memoire.vainstall.builder;
9
10 import com.memoire.vainstall.VAGlobals;
11
12 import com.memoire.vainstall.builder.action.*;
13 import com.memoire.vainstall.builder.event.*;
14 import com.memoire.vainstall.builder.gui.*;
15 import com.memoire.vainstall.builder.util.*;
16
17 import java.awt.event.*;
18 import java.io.File JavaDoc;
19 import java.util.*;
20
21 import javax.swing.*;
22 import javax.swing.event.*;
23 import javax.swing.tree.*;
24
25 /**
26  * This is the product controller class
27  *
28  * @see com.memoire.vainstall.builder.event.VAIProductListener
29  * @see java.awt.event.ActionListener
30  * @see javax.swing.event.InternalFrameListener
31  *
32  * @author Henrik Falk
33  * @version $Id: VAIProductController.java,v 1.1 2001/09/28 19:27:49 hfalk Exp $
34  */

35 public class VAIProductController implements VAIProductListener, InternalFrameListener, ActionListener, MouseListener {
36
37     /**
38      * The product 'internal' frame
39      */

40     VAIProductInternalFrame frame = new VAIProductInternalFrame();
41
42     /**
43      * The title text on the product frame
44      */

45     String JavaDoc title = "";
46
47     /**
48      * The product datamodel
49      */

50     VAIProductModel model = new VAIProductModel();
51
52     /**
53      * Nodes in the tree view on the inernal frame
54      */

55     AbstractVAIProductNode root;
56
57     /**
58      * List of actions that are used for popup menus in the tree view
59      */

60     private Hashtable actionList = new Hashtable();
61
62     /**
63      * we need to remember this a the moment
64      */

65     ProductStepsNode productStepsNode;
66
67     /**
68      * Default constructor
69      */

70     public VAIProductController() {
71         super();
72
73         // listen to changes in the model
74
model.addVAIProductListener(this);
75
76         frame.addInternalFrameListener(this);
77
78         // initialize actionlist
79
getActionList();
80
81         frame.getTree().addMouseListener(this);
82
83     }
84
85     public Hashtable getActionList() {
86
87         if(actionList.size() == 0) {
88
89             // ProductAddRequiredStepsAction
90
ProductAddRequiredStepsAction productAddRequiredStepsAction = new ProductAddRequiredStepsAction();
91             productAddRequiredStepsAction.initialize(this);
92             actionList.put("ProductAddRequiredStepsAction",productAddRequiredStepsAction);
93         }
94         return actionList;
95     }
96
97     /**
98      * Returns whether the data model needs to be saved or not.
99      * @return true if the datamodel needs to be saved
100      */

101     public boolean isDirty() {
102         return model.isDirty();
103     }
104
105     /**
106      * Implements the VAIProductListener interface
107      * @param evt The event that 'explains' what has changed in the model
108      */

109     public void productChanged(VAIProductEvent evt) {
110
111         switch (evt.getType()) {
112             case VAIProductEvent.PROJECT_LOADED:
113                 // Build the nodes for the treemodel and set it
114
frame.setNode(getRootNode());
115
116                 TreePath stepsPath = new TreePath(((DefaultTreeModel)frame.getTree().getModel()).getPathToRoot(productStepsNode));
117                 frame.getTree().expandPath(stepsPath);
118
119                 // set title on frame
120
title = getModel().getProductName()
121                           + VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_Version")
122                           + getModel().getProductVersion();
123                 frame.setTitle(title);
124                 break;
125             case VAIProductEvent.PROJECT_SAVED:
126                 frame.setTitle(title);
127                 break;
128             case VAIProductEvent.PROJECT_DIRTY:
129                 frame.setTitle(VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_Dirty") + title);
130                 break;
131         }
132     }
133
134     /**
135      * Returns a reference to the product frame
136      * @return The product frame
137      */

138     public VAIProductInternalFrame getFrame() {
139         return frame;
140     }
141
142     /**
143      * Returns the datamodel of a product
144      * @return a VAIProductModel
145      */

146     public VAIProductModel getModel() {
147         return model;
148     }
149
150     /**
151      * Implements the ActionListener interface
152      * @param evt The action event
153      */

154     public void actionPerformed(ActionEvent evt) {
155
156         // extract the control that sends the event
157
Object JavaDoc source = evt.getSource();
158
159         // hardcode this for the moment
160
try {
161             getFrame().setVisible(true);
162             getFrame().setSelected(true);
163             getFrame().setMaximum(true);
164         } catch(Exception JavaDoc exc) {
165             // handle this in the future
166
// exc.printStackTrace();
167
}
168
169     }
170
171     /**
172      * internalFrameOpened
173      * @param evt The internal frame event
174      */

175     public void internalFrameOpened(InternalFrameEvent evt) {
176     }
177
178     /**
179      * internalFrameClosing
180      * @param evt The internal frame event
181      */

182     public void internalFrameClosing(InternalFrameEvent evt) {
183     }
184
185     /**
186      * internalFrameClosed
187      * @param evt The internal frame event
188      */

189     public void internalFrameClosed(InternalFrameEvent evt) {
190     }
191
192     /**
193      * internalFrameIconified
194      * @param evt The internal frame event
195      */

196     public void internalFrameIconified(InternalFrameEvent evt) {
197     }
198
199     /**
200      * internalFrameDeiconfied
201      * @param evt The internal frame event
202      */

203     public void internalFrameDeiconified(InternalFrameEvent evt) {
204     }
205
206     /**
207      * internalFrameDeactivated
208      * @param evt The internal frame event
209      */

210     public void internalFrameDeactivated(InternalFrameEvent evt) {
211         getModel().fireVAIProductEvent(new VAIProductEvent(this,VAIProductEvent.PROJECT_DEACTIVATED));
212     }
213
214     /**
215      * internalFrameActivated
216      * @param evt The internal frame event
217      */

218     public void internalFrameActivated(InternalFrameEvent evt) {
219
220         if (getModel().getRequiredList().size() == 0) {
221             if (getModel().isDirty() == false) {
222                 getModel().fireVAIProductEvent(new VAIProductEvent(this,VAIProductEvent.PROJECT_REQUIREMENTS_MET));
223             }
224         } else {
225             getModel().fireVAIProductEvent(new VAIProductEvent(this,VAIProductEvent.PROJECT_REQUIREMENTS_NOTMET));
226         }
227
228
229         getModel().fireVAIProductEvent(new VAIProductEvent(this,VAIProductEvent.PROJECT_ACTIVATED));
230     }
231
232     /**
233      * Create a new project
234      * @param projectName The name of the project which is the product name
235      * @param projectVersion The version of the which is the product version
236      * @param projectDirectory The directory in where the project should be
237      * @param productType The type of product 'install, update, module'
238      * @throws com.memoire.vainstall.builder.util.VAIBuilderException
239      */

240     public void createProject(String JavaDoc projectName, String JavaDoc projectVersion, String JavaDoc projectDirectory, String JavaDoc productType) throws VAIBuilderException {
241
242         // try to create project
243
if (new File JavaDoc(projectDirectory).exists() == true) {
244             throw new VAIBuilderException(VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_CreateProject"),
245                 VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_AlreadyExists"),
246                 VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_AlreadyExistsHelp"));
247         }
248
249         try {
250             boolean ok = new File JavaDoc(projectDirectory).mkdir();
251             if (ok == false) {
252                 throw new VAIBuilderException(VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_CreateProject"),
253                     VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_NotCreated"),
254                     null);
255             }
256         } catch (SecurityException JavaDoc exc) {
257             throw new VAIBuilderException(VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_CreateProject"),
258                 VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_NotCreatedSecurity"),
259                 null);
260         }
261
262         model.setProductName(projectName);
263         model.setProductVersion(projectVersion);
264         model.setProductDirectory(projectDirectory);
265         model.setProductType(productType);
266
267         try {
268             model.save();
269         } catch (Exception JavaDoc exc) {
270             try {
271                 new File JavaDoc(projectDirectory).delete();
272             } catch (Exception JavaDoc innerexc) {
273                 throw new VAIBuilderException(VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_CreateProject"),
274                     VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_NotSaved"),
275                     null);
276             }
277             throw new VAIBuilderException(VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_CreateProject"),
278                 VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_NotSaved"),
279                 null);
280         }
281
282         // Build the nodes for the treemodel and set it
283
frame.setNode(getRootNode());
284
285         TreePath stepsPath = new TreePath(((DefaultTreeModel)frame.getTree().getModel()).getPathToRoot(productStepsNode));
286         frame.getTree().expandPath(stepsPath);
287
288         // set title on frame
289
title = getModel().getProductName()
290                  + VAGlobals.getResource("com.memoire.vainstall.builder.Language","VAIProductController_Version")
291                  + getModel().getProductVersion();
292         frame.setTitle(title);
293     }
294
295     private AbstractVAIProductNode buildUINodes() {
296
297         root = new ProductBaseNode();
298         root.initialize(this);
299
300         // the target node
301
ProductTargetNode targetNode = new ProductTargetNode();
302         targetNode.initialize(this);
303         root.add(targetNode);
304
305         // the visual node
306
ProductVisualNode visualNode = new ProductVisualNode();
307         visualNode.initialize(this);
308         root.add(visualNode);
309
310         // the language node
311
ProductLanguageNode languageNode = new ProductLanguageNode();
312         languageNode.initialize(this);
313         root.add(languageNode);
314
315         AbstractVAIProductNode packageType = null;
316         if (getModel().getProductType().equals("install") == true) {
317             packageType = new ProductInstallNode();
318         }
319         if (getModel().getProductType().equals("update") == true) {
320             packageType = new ProductUpdateNode();
321         }
322         if (getModel().getProductType().equals("module") == true) {
323             packageType = new ProductModuleNode();
324         }
325
326         if (packageType == null) {
327             // something went wrong...
328
return root;
329         }
330
331         packageType.initialize(this);
332         root.add(packageType);
333
334         // the uninstall node
335
ProductUninstallNode uninstallNode = new ProductUninstallNode();
336         uninstallNode.initialize(this);
337         root.add(uninstallNode);
338
339         // the files node
340
ProductFilesNode filesNode = new ProductFilesNode();
341         filesNode.initialize(this);
342         packageType.add(filesNode);
343
344         // At the moment we need to remember this globally
345
productStepsNode = new ProductStepsNode();
346         productStepsNode.initialize(this);
347         packageType.add(productStepsNode);
348
349         if(getModel().getStepsList().size() > 0) {
350             ListIterator iterator = getModel().getStepsList().listIterator();
351             while (iterator.hasNext() == true) {
352                 String JavaDoc type = (String JavaDoc)iterator.next();
353
354                 // properly a stupid hardcoded way to do this
355
if (type.equals("language") == true) {
356                     ProductStepLanguageNode language = new ProductStepLanguageNode();
357                     language.initialize(this);
358                     productStepsNode.add(language);
359                 }
360                 if (type.equals("welcome") == true) {
361                     ProductStepWelcomeNode welcome = new ProductStepWelcomeNode();
362                     welcome.initialize(this);
363                     productStepsNode.add(welcome);
364                 }
365                 if (type.equals("license") == true) {
366                     ProductStepLicenseNode license = new ProductStepLicenseNode();
367                     license.initialize(this);
368                     productStepsNode.add(license);
369                 }
370                 if (type.equals("readme") == true) {
371                     ProductStepReadmeNode readme = new ProductStepReadmeNode();
372                     readme.initialize(this);
373                     productStepsNode.add(readme);
374                 }
375                 if (type.equals("upgrade") == true) {
376                     ProductStepUpgradeNode upgrade = new ProductStepUpgradeNode();
377                     upgrade.initialize(this);
378                     productStepsNode.add(upgrade);
379                 }
380                 if (type.equals("directory") == true) {
381                     ProductStepDirectoryNode directory = new ProductStepDirectoryNode();
382                     directory.initialize(this);
383                     productStepsNode.add(directory);
384                 }
385                 if (type.equals("install") == true) {
386                     ProductStepInstallNode install = new ProductStepInstallNode();
387                     install.initialize(this);
388                     productStepsNode.add(install);
389                 }
390                 if (type.equals("shortcut") == true) {
391                     ProductStepShortcutsNode shortcut = new ProductStepShortcutsNode();
392                     shortcut.initialize(this);
393                     productStepsNode.add(shortcut);
394                 }
395                 if (type.equals("end") == true) {
396                     ProductStepEndNode end = new ProductStepEndNode();
397                     end.initialize(this);
398                     productStepsNode.add(end);
399                 }
400
401             }
402         }
403         return root;
404     }
405
406     /**
407      * Adds a node before the named on the same level
408      * Check for root
409      */

410     public void addUINodeBefore(String JavaDoc uiName, AbstractVAIProductNode node) {
411
412         Enumeration nodes = root.postorderEnumeration();
413         while(nodes.hasMoreElements() == true) {
414             AbstractVAIProductNode tmpNode = (AbstractVAIProductNode)nodes.nextElement();
415             if(tmpNode.getName().equals(uiName) == true) {
416                 int index = tmpNode.getParent().getIndex(tmpNode);
417                 if (index == 0) {
418                     tmpNode.add(node);
419                 } else {
420                     ((MutableTreeNode)tmpNode.getParent()).insert(node,index-1);
421                 }
422                 break;
423             }
424         }
425         ((DefaultTreeModel)frame.getTree().getModel()).nodeStructureChanged(node);
426     }
427
428     /**
429      * Adds a node after the named on the same level
430      */

431     public void addUINodeAfter(String JavaDoc uiName, AbstractVAIProductNode node) {
432
433         Enumeration nodes = root.postorderEnumeration();
434         while(nodes.hasMoreElements() == true) {
435             AbstractVAIProductNode tmpNode = (AbstractVAIProductNode)nodes.nextElement();
436             if(tmpNode.getName().equals(uiName) == true) {
437                 ((MutableTreeNode)tmpNode.getParent()).insert(node,tmpNode.getParent().getIndex(tmpNode));
438                 break;
439             }
440         }
441         ((DefaultTreeModel)frame.getTree().getModel()).nodeStructureChanged(node);
442     }
443
444     /**
445      * remove a node
446      */

447     public void removeUINode(String JavaDoc uiName) {
448
449         AbstractVAIProductNode parent = null;
450
451         Enumeration nodes = root.postorderEnumeration();
452         while(nodes.hasMoreElements() == true) {
453             AbstractVAIProductNode tmpNode = (AbstractVAIProductNode)nodes.nextElement();
454             if(tmpNode.getName().equals(uiName) == true) {
455                 parent = (AbstractVAIProductNode)tmpNode.getParent();
456                 tmpNode.removeFromParent();
457                 tmpNode = null;
458                 break;
459             }
460         }
461         ((DefaultTreeModel)frame.getTree().getModel()).nodeStructureChanged(parent);
462     }
463
464     /**
465      * Adds a node before the named on the same level
466      * Check for root
467      */

468     public void addUIChildNodeBefore(String JavaDoc uiName, AbstractVAIProductNode node) {
469
470         Enumeration nodes = root.postorderEnumeration();
471         while(nodes.hasMoreElements() == true) {
472             AbstractVAIProductNode tmpNode = (AbstractVAIProductNode)nodes.nextElement();
473             if(tmpNode.getName().equals(uiName) == true) {
474                 int index = tmpNode.getIndex(tmpNode);
475                 if (index == 0) {
476                     tmpNode.add(node);
477                 } else {
478                     tmpNode.insert((MutableTreeNode)node,index-1);
479                 }
480                 break;
481             }
482         }
483         ((DefaultTreeModel)frame.getTree().getModel()).nodeStructureChanged(node);
484     }
485
486     /**
487      * Adds a node after the named on the same level
488      */

489     public void addUIChildNodeAfter(String JavaDoc uiName, AbstractVAIProductNode node) {
490
491         Enumeration nodes = root.postorderEnumeration();
492         while(nodes.hasMoreElements() == true) {
493             AbstractVAIProductNode tmpNode = (AbstractVAIProductNode)nodes.nextElement();
494             if(tmpNode.getName().equals(uiName) == true) {
495                 tmpNode.insert((MutableTreeNode)node,tmpNode.getParent().getIndex(tmpNode));
496                 break;
497             }
498         }
499
500         ((DefaultTreeModel)frame.getTree().getModel()).nodeStructureChanged(node);
501     }
502
503
504     public AbstractVAIProductNode getRootNode() {
505         if (root == null) {
506             buildUINodes();
507         }
508         return root;
509     }
510
511 public void mouseClicked(MouseEvent e){}
512 public void mouseEntered(MouseEvent e){}
513 public void mouseExited(MouseEvent e){}
514
515     public void mousePressed(MouseEvent evt) {
516
517         if (evt.isPopupTrigger()) {
518
519         TreePath path = frame.getTree().getSelectionPath();
520         if (path == null) {
521             return;
522         }
523         Object JavaDoc obj = path.getLastPathComponent();
524
525         if (obj instanceof DefaultMutableTreeNode) {
526             Object JavaDoc userObject = ((DefaultMutableTreeNode)obj).getUserObject();
527
528             if (userObject instanceof AbstractVAIProductNode) {
529                 AbstractVAIProductNode node = (AbstractVAIProductNode)userObject;
530                 if (node.getPopupMenu() != null) {
531                     node.getPopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
532                 }
533             }
534         }
535
536         }
537     }
538
539     public void mouseReleased(MouseEvent evt) {
540         if (evt.isPopupTrigger()) {
541
542         TreePath path = frame.getTree().getSelectionPath();
543         if (path == null) {
544             return;
545         }
546         Object JavaDoc obj = path.getLastPathComponent();
547
548         if (obj instanceof DefaultMutableTreeNode) {
549             Object JavaDoc userObject = ((DefaultMutableTreeNode)obj).getUserObject();
550
551             if (userObject instanceof AbstractVAIProductNode) {
552                 AbstractVAIProductNode node = (AbstractVAIProductNode)userObject;
553                 if (node.getPopupMenu() != null) {
554                     node.getPopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
555                 }
556             }
557         }
558
559         }
560     }
561
562 }
563
Popular Tags