KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > ui > ProjectPanel


1 package com.genimen.djeneric.tools.generator.ui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Insets JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.io.BufferedWriter JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileWriter JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Hashtable JavaDoc;
15
16 import javax.swing.BoxLayout JavaDoc;
17 import javax.swing.JButton JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.JOptionPane JavaDoc;
20 import javax.swing.JPanel JavaDoc;
21 import javax.swing.JScrollPane JavaDoc;
22 import javax.swing.JSplitPane JavaDoc;
23 import javax.swing.JTextArea JavaDoc;
24 import javax.swing.RepaintManager JavaDoc;
25 import javax.swing.event.TreeExpansionEvent JavaDoc;
26 import javax.swing.event.TreeSelectionEvent JavaDoc;
27 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
28 import javax.swing.tree.DefaultTreeModel JavaDoc;
29 import javax.swing.tree.ExpandVetoException JavaDoc;
30 import javax.swing.tree.TreePath JavaDoc;
31
32 import com.genimen.djeneric.repository.DjExtent;
33 import com.genimen.djeneric.repository.DjModelView;
34 import com.genimen.djeneric.repository.DjPersistenceManager;
35 import com.genimen.djeneric.repository.DjSession;
36 import com.genimen.djeneric.structure.ExtentUsage;
37 import com.genimen.djeneric.tools.common.ModelViewDialog;
38 import com.genimen.djeneric.tools.generator.Generator;
39 import com.genimen.djeneric.tools.generator.core.ParseException;
40 import com.genimen.djeneric.tools.generator.tree.GeneratorTreeCellRenderer;
41 import com.genimen.djeneric.tools.generator.tree.GlobalTemplateNode;
42 import com.genimen.djeneric.tools.generator.tree.GlobalTemplatesFolder;
43 import com.genimen.djeneric.tools.generator.util.ProjectProperties;
44 import com.genimen.djeneric.tools.generator.util.Template;
45 import com.genimen.djeneric.tools.generator.util.TemplateSetManager;
46 import com.genimen.djeneric.tools.specifier.tree.DjenericFolder;
47 import com.genimen.djeneric.tools.specifier.tree.DjenericNode;
48 import com.genimen.djeneric.tools.specifier.tree.DjenericTree;
49 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeNode;
50 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
51 import com.genimen.djeneric.util.DjLogger;
52 import com.genimen.djeneric.util.DjProperties;
53
54 /**
55  * Description of the Class
56  *
57  *@author wrietude
58  *@created June 14, 2002
59  */

60 public class ProjectPanel extends JPanel JavaDoc
61 {
62   private static final long serialVersionUID = 1L;
63   /**
64    * Description of the Field
65    */

66   public final static String JavaDoc OK_MSG = "Ok";
67
68   Hashtable JavaDoc _allTemplateCheckBoxes = new Hashtable JavaDoc();
69   DjProperties _templateSettings = new DjProperties();
70   Hashtable JavaDoc _allTemplates = new Hashtable JavaDoc();
71   ArrayList JavaDoc _allTemplatesWithErrors = new ArrayList JavaDoc();
72
73   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
74   JPanel JavaDoc _treePanel = new JPanel JavaDoc();
75   JButton JavaDoc _butGenerate = new JButton JavaDoc();
76   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
77   DjenericFolder _root = new DjenericFolder();
78   DefaultTreeModel JavaDoc _treeModel = new DefaultTreeModel JavaDoc(_root);
79   DjenericTree _tree = new DjenericTree();
80   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
81   GeneratorTreeCellRenderer _treeCellRendrer = new GeneratorTreeCellRenderer();
82   JPanel JavaDoc _templateListPanel = new JPanel JavaDoc()
83                                                      {
84                                                        private static final long serialVersionUID = 1L;
85                                                        Insets JavaDoc insets = new Insets JavaDoc(0, 4, 0,
86                                                                                                       0);
87
88                                                        public Insets JavaDoc getInsets()
89                                                        {
90                                                          return insets;
91                                                        }
92                                                      };
93   BoxLayout JavaDoc _boxlayout = new BoxLayout JavaDoc(_templateListPanel, BoxLayout.Y_AXIS);
94   JScrollPane JavaDoc jScrollPane2 = new JScrollPane JavaDoc();
95   JButton JavaDoc _butFilter = new JButton JavaDoc();
96   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
97   JButton JavaDoc _butReload = new JButton JavaDoc();
98   JSplitPane JavaDoc _splitPane = new JSplitPane JavaDoc();
99   JButton JavaDoc _butRescan = new JButton JavaDoc();
100   JButton JavaDoc _butCollapseAll = new JButton JavaDoc();
101   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
102   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
103   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
104   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
105   JLabel JavaDoc _lblTemplateTitle = new JLabel JavaDoc();
106   JButton JavaDoc _butExpandAll = new JButton JavaDoc();
107   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
108   DjPersistenceManager _mgr;
109   DjSession _session;
110   Generator _generator;
111   TemplateSetManager _templateSetManager;
112   ProjectProperties _projectProperties;
113
114   /**
115    * Constructor for the ProjectPanel object
116    */

117   public ProjectPanel()
118   {
119     try
120     {
121       jbInit();
122     }
123     catch (Exception JavaDoc ex)
124     {
125       DjLogger.log(ex);
126     }
127   }
128
129   /**
130    * Constructor for the ProjectPanel object
131    *
132    *@param generator Description of the Parameter
133    *@param projectProperties Description of the Parameter
134    */

135   public ProjectPanel(Generator generator, ProjectProperties projectProperties)
136   {
137     try
138     {
139       _projectProperties = projectProperties;
140       _templateSetManager = generator.getTemplateSetManager();
141       _mgr = generator.getManager();
142       _session = generator.getSession();
143       _generator = generator;
144       jbInit();
145       _splitPane.setDividerLocation(Integer.parseInt(_generator.getProperty("window.treedividerloc", "300")));
146       setupNavigator();
147     }
148     catch (Exception JavaDoc ex)
149     {
150       DjLogger.log(ex);
151     }
152   }
153
154   /**
155    * Description of the Method
156    *
157    *@exception Exception Description of the Exception
158    */

159   void jbInit() throws Exception JavaDoc
160   {
161     _treePanel.setLayout(borderLayout2);
162     this.setLayout(borderLayout1);
163     _butGenerate.setPreferredSize(new Dimension JavaDoc(24, 24));
164     _butGenerate.setToolTipText("Generate");
165     _butGenerate.addActionListener(new java.awt.event.ActionListener JavaDoc()
166     {
167       public void actionPerformed(ActionEvent JavaDoc e)
168       {
169         _butGenerate_actionPerformed(e);
170       }
171     });
172     jPanel2.setLayout(verticalFlowLayout1);
173     _tree.setRootVisible(false);
174     _tree.setCellRenderer(_treeCellRendrer);
175     _tree.setModel(_treeModel);
176     _tree.setShowsRootHandles(true);
177     _tree.setAutoscrolls(true);
178     _tree.setDoubleBuffered(true);
179     _tree.setToggleClickCount(0);
180     _tree.setManager(getManager());
181     _tree.setSession(getSession());
182     _tree.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
183     {
184       public void mouseClicked(MouseEvent JavaDoc e)
185       {
186         _tree_mouseClicked(e);
187       }
188     });
189     _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener JavaDoc()
190     {
191       public void treeWillExpand(TreeExpansionEvent JavaDoc e) throws ExpandVetoException JavaDoc
192       {
193         _tree_treeWillExpand(e);
194       }
195
196       public void treeWillCollapse(TreeExpansionEvent JavaDoc e)
197       {
198       }
199     });
200     _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc()
201     {
202       public void valueChanged(TreeSelectionEvent JavaDoc e)
203       {
204         _tree_valueChanged(e);
205       }
206     });
207     _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc()
208     {
209       public void valueChanged(TreeSelectionEvent JavaDoc e)
210       {
211         _tree_valueChanged(e);
212       }
213     });
214     _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener JavaDoc()
215     {
216       public void treeWillExpand(TreeExpansionEvent JavaDoc e) throws ExpandVetoException JavaDoc
217       {
218         _tree_treeWillExpand(e);
219       }
220
221       public void treeWillCollapse(TreeExpansionEvent JavaDoc e)
222       {
223       }
224     });
225     jPanel1.setLayout(borderLayout3);
226     _templateListPanel.setLayout(_boxlayout);
227     _butFilter.addActionListener(new java.awt.event.ActionListener JavaDoc()
228     {
229       public void actionPerformed(ActionEvent JavaDoc e)
230       {
231         _butFilter_actionPerformed(e);
232       }
233     });
234     _butFilter.setToolTipText("Find");
235     _butFilter.setPreferredSize(new Dimension JavaDoc(24, 24));
236     _butReload.setPreferredSize(new Dimension JavaDoc(24, 24));
237     _butReload.setToolTipText("Reload selected object from repository");
238     _butReload.addActionListener(new java.awt.event.ActionListener JavaDoc()
239     {
240       public void actionPerformed(ActionEvent JavaDoc e)
241       {
242         _butReload_actionPerformed(e);
243       }
244     });
245     _splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
246     _butRescan.addActionListener(new java.awt.event.ActionListener JavaDoc()
247     {
248       public void actionPerformed(ActionEvent JavaDoc e)
249       {
250         _butRescan_actionPerformed(e);
251       }
252     });
253     _butRescan.setToolTipText("Rescan template set");
254     _butRescan.setPreferredSize(new Dimension JavaDoc(24, 24));
255     _butCollapseAll.setPreferredSize(new Dimension JavaDoc(24, 24));
256     _butCollapseAll.setToolTipText("Collapse all");
257     _butCollapseAll.addActionListener(new java.awt.event.ActionListener JavaDoc()
258     {
259       public void actionPerformed(ActionEvent JavaDoc e)
260       {
261         _butCollapseAll_actionPerformed(e);
262       }
263     });
264     jLabel2.setText(" ");
265     jLabel2.setPreferredSize(new Dimension JavaDoc(3, 6));
266     jLabel1.setPreferredSize(new Dimension JavaDoc(3, 6));
267     jLabel1.setText(" ");
268     _lblTemplateTitle.setText("Associated templates");
269     _butExpandAll.setPreferredSize(new Dimension JavaDoc(24, 24));
270     _butExpandAll.setToolTipText("Expand all");
271     _butExpandAll.addActionListener(new java.awt.event.ActionListener JavaDoc()
272     {
273       public void actionPerformed(ActionEvent JavaDoc e)
274       {
275         _butExpandAll_actionPerformed(e);
276       }
277     });
278     _tree.putClientProperty("JTree.lineStyle", "Angled");
279     _treePanel.add(jScrollPane1, BorderLayout.CENTER);
280     _treePanel.add(jPanel2, BorderLayout.WEST);
281     jScrollPane1.getViewport().add(_tree, null);
282     jPanel2.add(_butRescan, null);
283     jPanel2.add(_butReload, null);
284     jPanel2.add(_butFilter, null);
285     jPanel2.add(jLabel1, null);
286     jPanel2.add(_butGenerate, null);
287     jPanel2.add(jLabel2, null);
288     jPanel2.add(_butExpandAll, null);
289     jPanel2.add(_butCollapseAll, null);
290     _splitPane.add(jPanel1, JSplitPane.BOTTOM);
291     _splitPane.add(_treePanel, JSplitPane.TOP);
292     jPanel1.add(jScrollPane2, BorderLayout.CENTER);
293     jPanel1.add(_lblTemplateTitle, BorderLayout.NORTH);
294     jScrollPane2.getViewport().add(_templateListPanel, null);
295     this.add(_splitPane, BorderLayout.CENTER);
296
297     _butCollapseAll.setIcon(Generator.getImageIcon("collapseall.gif"));
298     _butExpandAll.setIcon(Generator.getImageIcon("expandall.gif"));
299     _butReload.setIcon(Generator.getImageIcon("reload.gif"));
300     _butRescan.setIcon(Generator.getImageIcon("refresh.gif"));
301     _butFilter.setIcon(Generator.getImageIcon("filter.gif"));
302     _butGenerate.setIcon(Generator.getImageIcon("generate.gif"));
303
304   }
305
306   /**
307    * Description of the Method
308    *
309    *@param e Description of the Parameter
310    */

311   void _butGenerate_actionPerformed(ActionEvent JavaDoc e)
312   {
313     performGenerate();
314   }
315
316   /**
317    * Description of the Method
318    */

319   void performGenerate()
320   {
321     initGenerateBatch();
322     JTextArea JavaDoc logPane = _generator.getTextTab(Generator.GENERATOR_CONSOLE_TITLE, true);
323     DjenericTreeNode[] sels = getSelectedNodes();
324     try
325     {
326       long currentTime = System.currentTimeMillis();
327       for (int i = 0; i < sels.length; i++)
328       {
329         if (sels[i] instanceof GlobalTemplateNode)
330         {
331           GlobalTemplateNode gtn = (GlobalTemplateNode) sels[i];
332           generateObjectBased(gtn.getTemplate(), logPane, null);
333         }
334
335         if (sels[i] instanceof GlobalTemplatesFolder)
336         {
337           GlobalTemplatesFolder gtf = (GlobalTemplatesFolder) sels[i];
338           GlobalTemplateNode[] gtns = gtf.getChildNodes();
339           for (int g = 0; g < gtns.length; g++)
340           {
341             generateObjectBased(gtns[g].getTemplate(), logPane, null);
342           }
343         }
344
345         if (sels[i] instanceof DjenericNode)
346         {
347           generate((DjenericNode) sels[i], logPane);
348         }
349
350         if (sels[i] instanceof DjenericFolder)
351         {
352           DjenericFolder folder = (DjenericFolder) sels[i];
353           DjenericNode[] subs = folder.getChildNodes();
354           for (int x = 0; x < subs.length; x++)
355           {
356             generate(subs[x], logPane);
357           }
358         }
359       }
360       currentTime = (System.currentTimeMillis() - currentTime) / 1000;
361       printMsg(logPane, "\nDone in " + currentTime + " seconds.\n");
362       setStatusMessage(OK_MSG);
363     }
364     catch (Exception JavaDoc x)
365     {
366       setStatusMessage(x.getMessage(), false);
367       DjLogger.log(x);
368     }
369   }
370
371   /**
372    * Description of the Method
373    *
374    *@param e Description of the Parameter
375    *@exception ExpandVetoException Description of the Exception
376    */

377   void _tree_treeWillExpand(TreeExpansionEvent JavaDoc e) throws ExpandVetoException JavaDoc
378   {
379     try
380     {
381       if (e.getPath().getLastPathComponent() instanceof DjenericTreeNode)
382       {
383         DjenericTreeNode node = (DjenericTreeNode) e.getPath().getLastPathComponent();
384         node.expandNode();
385       }
386       setStatusMessage(OK_MSG);
387     }
388     catch (Exception JavaDoc x)
389     {
390       setStatusMessage(x);
391       throw new ExpandVetoException JavaDoc(new TreeExpansionEvent JavaDoc(this, e.getPath()), x.getMessage());
392     }
393   }
394
395   /**
396    * Description of the Method
397    *
398    *@param e Description of the Parameter
399    *@exception ExpandVetoException Description of the Exception
400    */

401   void _tree_treeWillCollapse(TreeExpansionEvent JavaDoc e)
402   {
403   }
404
405   /**
406    * Description of the Method
407    *
408    *@param e Description of the Parameter
409    */

410   void _tree_valueChanged(TreeSelectionEvent JavaDoc e)
411   {
412     displayAssociatedTemplates();
413   }
414
415   /**
416    * Description of the Method
417    *
418    *@param e Description of the Parameter
419    */

420   void _butFilter_actionPerformed(ActionEvent JavaDoc e)
421   {
422
423     DjenericTreeNode[] sels = getSelectedNodes();
424     try
425     {
426       for (int i = 0; i < sels.length; i++)
427       {
428         if (sels[i].canFilter())
429         {
430           sels[i].filter();
431         }
432       }
433
434     }
435     catch (Exception JavaDoc x)
436     {
437       setStatusMessage(x);
438     }
439
440   }
441
442   /**
443    * Description of the Method
444    *
445    *@param e Description of the Parameter
446    */

447   void _butReload_actionPerformed(ActionEvent JavaDoc e)
448   {
449
450     DjenericTreeNode[] sels = getSelectedNodes();
451     try
452     {
453       for (int i = 0; i < sels.length; i++)
454       {
455         sels[i].reload();
456       }
457
458       setStatusMessage(OK_MSG);
459     }
460     catch (Exception JavaDoc x)
461     {
462       setStatusMessage(x);
463     }
464   }
465
466   /**
467    * Description of the Method
468    *
469    *@param e Description of the Parameter
470    */

471   void _butRescan_actionPerformed(ActionEvent JavaDoc e)
472   {
473     refreshTemplates();
474   }
475
476   /**
477    * Description of the Method
478    *
479    *@param e Description of the Parameter
480    */

481   void _butCollapseAll_actionPerformed(ActionEvent JavaDoc e)
482   {
483     DjenericTreeNode[] sels = getSelectedNodes();
484     try
485     {
486       for (int i = 0; i < sels.length; i++)
487       {
488         sels[i].collapseAll();
489       }
490
491       setStatusMessage(OK_MSG);
492     }
493     catch (Exception JavaDoc x)
494     {
495       setStatusMessage(x);
496     }
497   }
498
499   /**
500    * Description of the Method
501    *
502    *@param e Description of the Parameter
503    */

504   void _butExpandAll_actionPerformed(ActionEvent JavaDoc e)
505   {
506     DjenericTreeNode[] sels = getSelectedNodes();
507     try
508     {
509       for (int i = 0; i < sels.length; i++)
510       {
511         sels[i].expandAll(0);
512       }
513
514       setStatusMessage(OK_MSG);
515     }
516     catch (Exception JavaDoc x)
517     {
518       setStatusMessage(x);
519     }
520   }
521
522   /**
523    * Description of the Method
524    *
525    *@exception Exception Description of the Exception
526    */

527   protected void setupNavigator() throws Exception JavaDoc
528   {
529     _root.setTree(_tree);
530
531     ModelViewDialog vd = new ModelViewDialog(this, _mgr.getCurrentUser());
532     DjModelView selectedView = vd.getView();
533
534     ExtentUsage[] navigatorStructure = selectedView.getExtentUsages();
535
536     _root.insertAsFolder(new GlobalTemplatesFolder(_templateSetManager, _projectProperties));
537
538     for (int i = 0; i < navigatorStructure.length; i++)
539     {
540       DjenericFolder folder = new DjenericFolder();
541       folder.setExtentUsage(navigatorStructure[i]);
542       _root.insertAsFolder(folder);
543     }
544     _treeModel.nodeStructureChanged(_root);
545     _tree_valueChanged(null);
546
547   }
548
549   /**
550    * Gets the manager attribute of the ProjectPanel object
551    *
552    *@return The manager value
553    */

554   public DjPersistenceManager getManager()
555   {
556     return _mgr;
557   }
558
559   /**
560    * Gets the session attribute of the ProjectPanel object
561    *
562    *@return The session value
563    */

564   public DjSession getSession()
565   {
566     return _session;
567   }
568
569   /**
570    * Gets the selectedNodes attribute of the ProjectPanel object
571    *
572    *@return The selectedNodes value
573    */

574   public DjenericTreeNode[] getSelectedNodes()
575   {
576     if (_tree.getSelectionPath() == null)
577     {
578       return new DjenericTreeNode[0];
579     }
580     ArrayList JavaDoc result = new ArrayList JavaDoc(10);
581
582     TreePath JavaDoc[] tp = _tree.getSelectionPaths();
583
584     for (int qq = 0; qq < tp.length; qq++)
585     {
586       DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) (tp[qq].getLastPathComponent());
587       if (node instanceof DjenericTreeNode)
588       {
589         result.add(node);
590       }
591     }
592     return (DjenericTreeNode[]) result.toArray(new DjenericTreeNode[0]);
593   }
594
595   /**
596    * Sets the statusMessage attribute of the ProjectPanel object
597    *
598    *@param msg The new statusMessage value
599    */

600   public void setStatusMessage(String JavaDoc msg)
601   {
602     _generator.setStatusMessage(msg, true);
603   }
604
605   /**
606    * Sets the statusMessage attribute of the ProjectPanel object
607    *
608    *@param msg The new statusMessage value
609    *@param informative The new statusMessage value
610    */

611   public void setStatusMessage(String JavaDoc msg, boolean informative)
612   {
613     _generator.setStatusMessage(msg, informative);
614   }
615
616   public void setStatusMessage(Throwable JavaDoc t)
617   {
618     _generator.setStatusMessage(t);
619   }
620
621   /**
622    * Sets the statusMessageNow attribute of the ProjectPanel object
623    *
624    *@param msg The new statusMessageNow value
625    *@param informative The new statusMessageNow value
626    */

627   public void setStatusMessageNow(String JavaDoc msg, boolean informative)
628   {
629     _generator.setStatusMessage(msg, informative);
630   }
631
632   /**
633    * Description of the Method
634    */

635   void displayAssociatedTemplates()
636   {
637     DjenericTreeNode[] sels = getSelectedNodes();
638
639     boolean canCollapse = false;
640     boolean canExpand = false;
641     boolean canReload = false;
642     boolean canFilter = false;
643     boolean canGenerate = false;
644
645     for (int i = 0; i < sels.length; i++)
646     {
647       canFilter |= sels[i].canFilter();
648       canCollapse = true;
649       canExpand |= (sels[i].getChildCount() > 0);
650       canReload |= sels[i] instanceof DjenericNode;
651       canReload |= sels[i] instanceof DjenericFolder;
652       canGenerate |= sels[i] instanceof GlobalTemplateNode;
653       canGenerate |= sels[i] instanceof GlobalTemplatesFolder;
654     }
655     _butCollapseAll.setEnabled(canCollapse);
656     _butExpandAll.setEnabled(canExpand);
657     _butCollapseAll.setEnabled(canExpand);
658     _butReload.setEnabled(canReload);
659     _butFilter.setEnabled(canFilter);
660
661     if (sels.length > 0)
662     {
663       canGenerate |= updateTemplateList(sels[0]);
664     }
665
666     _butGenerate.setEnabled(canGenerate);
667   }
668
669   private boolean specializationsHaveTemplate(DjExtent ext)
670   {
671     DjExtent[] specs = ext.getSpecializations();
672
673     boolean result = false;
674     for (int i = 0; i < specs.length && !result; i++)
675     {
676       ArrayList JavaDoc templates = (ArrayList JavaDoc) _allTemplateCheckBoxes.get(specs[i].getObjectType());
677       if (templates != null) return true;
678       result |= specializationsHaveTemplate(specs[i]);
679     }
680     return result;
681   }
682
683   /**
684    * Description of the Method
685    *
686    *@param node Description of the Parameter
687    *@return Description of the Return Value
688    */

689   boolean updateTemplateList(DjenericTreeNode node)
690   {
691     boolean result = false;
692
693     _templateListPanel.removeAll();
694
695     DjExtent ext = null;
696     if (node instanceof DjenericNode)
697     {
698       ext = ((DjenericNode) node).getObject().getExtent();
699     }
700     else if (node instanceof DjenericFolder)
701     {
702       ext = node.getExtentUsage().getExtent();
703
704       // This folder might contain objects that are in fact specializations
705
// of this extent. Check to see if they have a template; in that case
706
// the returned result must be true
707

708       result = specializationsHaveTemplate(ext);
709     }
710
711     while (ext != null)
712     {
713       String JavaDoc objectType = ext.getObjectType();
714
715       ArrayList JavaDoc templates = (ArrayList JavaDoc) _allTemplateCheckBoxes.get(objectType);
716       if (templates != null)
717       {
718         for (int i = 0; i < templates.size(); i++)
719         {
720           _templateListPanel.add((Component JavaDoc) templates.get(i));
721         }
722         result = true;
723       }
724       ext = ext.getSuper();
725     }
726
727     if (node instanceof GlobalTemplateNode || node instanceof GlobalTemplatesFolder)
728     {
729       _templateListPanel.add(new JLabel JavaDoc("Global template"));
730     }
731     else if (!result)
732     {
733       _templateListPanel.add(new JLabel JavaDoc("No template available"));
734     }
735
736     _templateListPanel.invalidate();
737     _templateListPanel.validate();
738     _templateListPanel.repaint();
739     return result;
740   }
741
742   /**
743    * Description of the Method
744    *
745    *@param ta Description of the Parameter
746    *@param msg Description of the Parameter
747    */

748   void printMsg(JTextArea JavaDoc ta, String JavaDoc msg)
749   {
750     ta.append(msg);
751     ta.append("\n");
752     RepaintManager JavaDoc rm = RepaintManager.currentManager(ta);
753     if (rm != null)
754     {
755       rm.paintDirtyRegions();
756     }
757   }
758
759   /**
760    * Description of the Method
761    *
762    *@param node Description of the Parameter
763    *@param logPane Description of the Parameter
764    *@exception Exception Description of the Exception
765    */

766   void generate(DjenericNode node, JTextArea JavaDoc logPane) throws Exception JavaDoc
767   {
768     DjExtent ext = node.getObject().getExtent();
769
770     while (ext != null)
771     {
772       String JavaDoc objectType = ext.getObjectType();
773
774       ArrayList JavaDoc templates = (ArrayList JavaDoc) _allTemplateCheckBoxes.get(objectType);
775       if (templates != null)
776       {
777         for (int i = 0; i < templates.size(); i++)
778         {
779           TemplateCheckBox cb = (TemplateCheckBox) templates.get(i);
780           if (cb.isSelected())
781           {
782             generateObjectBased(cb.getTemplate(), logPane, node);
783           }
784         }
785       }
786       ext = ext.getSuper();
787     }
788   }
789
790   /**
791    * Description of the Method
792    *
793    *@param fileName Description of the Parameter
794    *@exception IOException Description of the Exception
795    */

796   public void createDirectories(String JavaDoc fileName)
797   {
798     int idx = fileName.lastIndexOf("/");
799     int idx2 = fileName.lastIndexOf("\\");
800     if (idx == -1)
801     {
802       idx = idx2;
803     }
804     if (idx < idx2)
805     {
806       idx = idx2;
807     }
808     if (idx != -1)
809     {
810       fileName = fileName.substring(0, idx);
811     }
812
813     File JavaDoc fd = new File JavaDoc(fileName);
814     fd.mkdirs();
815   }
816
817   /**
818    * Description of the Method
819    */

820   public void refreshTemplates()
821   {
822     _allTemplateCheckBoxes.clear();
823     _allTemplates.clear();
824     _allTemplatesWithErrors.clear();
825
826     Template[] allTemplates = _templateSetManager.getTemplates(_projectProperties.getTemplateSets());
827     for (int i = 0; i < allTemplates.length; i++)
828     {
829       String JavaDoc objectTypeBasedOn = "<Root based>";
830       if (allTemplates[i].isRootBased())
831       {
832         objectTypeBasedOn = allTemplates[i].getObjectType();
833       }
834
835       ArrayList JavaDoc templateCbs = (ArrayList JavaDoc) _allTemplateCheckBoxes.get(objectTypeBasedOn);
836       if (templateCbs == null)
837       {
838         templateCbs = new ArrayList JavaDoc();
839         _allTemplateCheckBoxes.put(objectTypeBasedOn, templateCbs);
840       }
841       templateCbs.add(new TemplateCheckBox(allTemplates[i], _projectProperties));
842
843       ArrayList JavaDoc template = (ArrayList JavaDoc) _allTemplates.get(objectTypeBasedOn);
844       if (template == null)
845       {
846         template = new ArrayList JavaDoc();
847         _allTemplates.put(objectTypeBasedOn, template);
848       }
849       template.add(allTemplates[i]);
850     }
851     String JavaDoc loadLog = _templateSetManager.getLoadLog();
852
853     if (loadLog.length() != 0)
854     {
855       JTextArea JavaDoc ta = _generator.getTextTab("Template load log", true);
856       ta.setText(loadLog);
857     }
858
859     try
860     {
861       for (int i = 0; i < _root.getChildCount(); i++)
862       {
863         if (_root.getChildAt(i) instanceof DjenericTreeNode)
864         {
865           DjenericTreeNode dtn = (DjenericTreeNode) _root.getChildAt(i);
866           dtn.reload();
867         }
868       }
869     }
870     catch (Exception JavaDoc x)
871     {
872       setStatusMessage("Reloaded ok, but: " + x.getMessage());
873     }
874     displayAssociatedTemplates();
875
876     setStatusMessage("Templates reloaded");
877   }
878
879   /**
880    * Description of the Method
881    */

882   public void close()
883   {
884     _generator.setProperty("window.treedividerloc", "" + _splitPane.getDividerLocation());
885   }
886
887   /**
888    * Description of the Method
889    *
890    *@param tpl Description of the Parameter
891    *@param code Description of the Parameter
892    *@param logPane Description of the Parameter
893    *@exception IOException Description of the Exception
894    *@exception ParseException Description of the Exception
895    */

896   private void writeCode(Template tpl, String JavaDoc code, JTextArea JavaDoc logPane) throws IOException JavaDoc, ParseException
897   {
898     String JavaDoc outputPath = _projectProperties.getOutputPath();
899     String JavaDoc fileName = tpl.getFileName();
900     if (!outputPath.endsWith("/") && !outputPath.endsWith("\\"))
901     {
902       outputPath += File.separator;
903     }
904     outputPath += fileName;
905
906     createDirectories(outputPath);
907     File JavaDoc file = new File JavaDoc(outputPath);
908     if (checkOverwrite(file))
909     {
910       printMsg(logPane, "Writing to " + outputPath);
911       BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
912       w.write(code);
913       w.close();
914       if (_projectProperties.isOpenAfterGenerate())
915       {
916         JTextArea JavaDoc ta = _generator.getTextTab(fileName, false);
917         ta.setText(code);
918       }
919     }
920     else
921     {
922       printMsg(logPane, "Skipped; file existed and not overwritten: " + outputPath);
923     }
924   }
925
926   boolean _dontAskAgain = false;
927
928   /**
929    * Description of the Method
930    */

931   protected void initGenerateBatch()
932   {
933     _dontAskAgain = false;
934   }
935
936   /**
937    * Description of the Method
938    *
939    *@param file Description of the Parameter
940    *@return Description of the Return Value
941    */

942   private boolean checkOverwrite(File JavaDoc file)
943   {
944     if (_dontAskAgain)
945     {
946       return true;
947     }
948     if (!file.exists())
949     {
950       return true;
951     }
952
953     if (_projectProperties.getOverwriteAction() == ProjectProperties.OVERWRITE_ALWAYS)
954     {
955       return true;
956     }
957     if (_projectProperties.getOverwriteAction() == ProjectProperties.OVERWRITE_NEVER)
958     {
959       return false;
960     }
961
962     int result = JOptionPane.showOptionDialog(this, "Do you want to overwrite " + file.getAbsolutePath() + "?",
963                                               "File exists", JOptionPane.DEFAULT_OPTION,
964                                               JOptionPane.INFORMATION_MESSAGE, null
965                                               // 0 1
966
, new String JavaDoc[]{"Overwrite", "Overwrite All", "Skip"}, null);
967     _dontAskAgain = result == 1;
968     return (result == 0 || result == 1);
969   }
970
971   /**
972    * Description of the Method
973    *
974    *@param tpl Description of the Parameter
975    *@param logPane Description of the Parameter
976    *@param node Description of the Parameter
977    */

978   private void generateObjectBased(Template tpl, JTextArea JavaDoc logPane, DjenericNode node)
979   {
980     try
981     {
982       if (node != null)
983       {
984         printMsg(logPane, "Generating " + tpl.getModuleName() + " based on " + node.getObject().toString());
985       }
986       else
987       {
988         printMsg(logPane, "Generating " + tpl.getModuleName() + " (global template)");
989       }
990
991       String JavaDoc code;
992       if (node != null)
993       {
994         code = tpl.generate(getManager(), node.getObject().getObjectId());
995       }
996       else
997       {
998         code = tpl.generate(getManager());
999       }
1000
1001      writeCode(tpl, code, logPane);
1002    }
1003    catch (ParseException x)
1004    {
1005      printMsg(logPane, "Exception occurred in template " + tpl + ", line " + x.getLine() + ", column " + x.getColumn()
1006                        + "\n" + x.getMessage());
1007      DjLogger.log(x);
1008    }
1009    catch (Exception JavaDoc x)
1010    {
1011      printMsg(logPane, "Exception occurred in template " + tpl + "\n" + x.getMessage());
1012      DjLogger.log(x);
1013    }
1014  }
1015
1016  /**
1017   * Description of the Method
1018   *
1019   *@param e Description of the Parameter
1020   */

1021  void _tree_mouseClicked(MouseEvent JavaDoc e)
1022  {
1023    if (e.getClickCount() > 1)
1024    {
1025      _butGenerate_actionPerformed(null);
1026    }
1027  }
1028}
Popular Tags