KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > Generator


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.genimen.djeneric.tools.generator;
32
33 import java.awt.AWTEvent JavaDoc;
34 import java.awt.BorderLayout JavaDoc;
35 import java.awt.Color JavaDoc;
36 import java.awt.Component JavaDoc;
37 import java.awt.Dimension JavaDoc;
38 import java.awt.Image JavaDoc;
39 import java.awt.SystemColor JavaDoc;
40 import java.awt.Toolkit JavaDoc;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.InputEvent JavaDoc;
43 import java.awt.event.KeyEvent JavaDoc;
44 import java.awt.event.KeyListener JavaDoc;
45 import java.awt.event.WindowEvent JavaDoc;
46 import java.io.File JavaDoc;
47 import java.io.FileNotFoundException JavaDoc;
48 import java.net.URL JavaDoc;
49
50 import javax.swing.ImageIcon JavaDoc;
51 import javax.swing.JFileChooser JavaDoc;
52 import javax.swing.JLabel JavaDoc;
53 import javax.swing.JMenu JavaDoc;
54 import javax.swing.JMenuBar JavaDoc;
55 import javax.swing.JMenuItem JavaDoc;
56 import javax.swing.JOptionPane JavaDoc;
57 import javax.swing.JPanel JavaDoc;
58 import javax.swing.JScrollPane JavaDoc;
59 import javax.swing.JSplitPane JavaDoc;
60 import javax.swing.JTextArea JavaDoc;
61
62 import com.genimen.djeneric.repository.DjPersistenceManager;
63 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
64 import com.genimen.djeneric.repository.DjSession;
65 import com.genimen.djeneric.repository.DjUserContextAssociation;
66 import com.genimen.djeneric.repository.exceptions.DjenericException;
67 import com.genimen.djeneric.tools.common.AboutDialog;
68 import com.genimen.djeneric.tools.common.DjenericTool;
69 import com.genimen.djeneric.tools.generator.dialogs.OptionsDialog;
70 import com.genimen.djeneric.tools.generator.dialogs.ProjectPropertiesDialog;
71 import com.genimen.djeneric.tools.generator.ui.CloseableScrollpane;
72 import com.genimen.djeneric.tools.generator.ui.ProjectPanel;
73 import com.genimen.djeneric.tools.generator.util.ProjectProperties;
74 import com.genimen.djeneric.tools.generator.util.TemplateSetManager;
75 import com.genimen.djeneric.tools.specifier.tree.DjenericFolder;
76 import com.genimen.djeneric.ui.DjTabbedPane;
77 import com.genimen.djeneric.util.DjEnvironment;
78 import com.genimen.djeneric.util.DjFileFilter;
79 import com.genimen.djeneric.util.DjLogPrintStream;
80 import com.genimen.djeneric.util.DjLogger;
81 import com.genimen.djeneric.util.DjStatusDisplayer;
82 import com.genimen.djeneric.util.DjVersion;
83
84 public class Generator extends DjenericTool implements KeyListener JavaDoc, DjStatusDisplayer
85 {
86   private static final long serialVersionUID = 1L;
87   protected final String JavaDoc PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("generator.properties");
88   protected final static String JavaDoc LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("generator.log");
89
90   public final static String JavaDoc GENERATOR_CONSOLE_TITLE = "Generator log";
91
92   public final static String JavaDoc OK_MSG = "Ok";
93   public final static String JavaDoc KEY_TEMPLATEPATH = "path.template";
94   public final static String JavaDoc KEY_INCLUDEPATH = "path.include";
95
96   public final static String JavaDoc PROJECT_EXTENSION = "dgp";
97
98   JSplitPane JavaDoc _splitpane = new JSplitPane JavaDoc();
99   JMenuBar JavaDoc _menu = new JMenuBar JavaDoc();
100   JMenu JavaDoc mnuFile = new JMenu JavaDoc();
101   JMenuItem JavaDoc mnuExit = new JMenuItem JavaDoc();
102   JMenu JavaDoc mnuHelp = new JMenu JavaDoc();
103   JMenuItem JavaDoc mnuAbout = new JMenuItem JavaDoc();
104   DjPersistenceManager _mgr;
105   DjSession _session;
106   DjenericFolder _root = new DjenericFolder();
107
108   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
109   Color JavaDoc _normalColor = new Color JavaDoc(102, 102, 153);
110
111   JPanel JavaDoc _desktopPanel = new JPanel JavaDoc();
112   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
113   DjTabbedPane _tabs = new DjTabbedPane();
114   JMenuItem JavaDoc mnuNewProject = new JMenuItem JavaDoc();
115   JMenuItem JavaDoc mnuOpenProject = new JMenuItem JavaDoc();
116   JMenuItem JavaDoc mnuProjectProperties = new JMenuItem JavaDoc();
117   JMenuItem JavaDoc mnuOptions = new JMenuItem JavaDoc();
118   JMenu JavaDoc mnuTools = new JMenu JavaDoc();
119   TemplateSetManager _templateSetManager = new TemplateSetManager();
120   JMenuItem JavaDoc mnuSaveProject = new JMenuItem JavaDoc();
121   JMenuItem JavaDoc mnuSaveAs = new JMenuItem JavaDoc();
122   ProjectProperties _projectProperties = new ProjectProperties();
123   String JavaDoc _projectFileName = null;
124   ProjectPanel _pnlProject;
125   JMenu JavaDoc mnuTabs = new JMenu JavaDoc();
126   JMenuItem JavaDoc mnuNextTab = new JMenuItem JavaDoc();
127   JMenuItem JavaDoc mnuPrevTab = new JMenuItem JavaDoc();
128   JMenuItem JavaDoc mnuCloseTab = new JMenuItem JavaDoc();
129   JMenuItem JavaDoc mnuCloseAll = new JMenuItem JavaDoc();
130
131   /**
132    * Construct the frame
133    *
134    * @param repositories
135    * Description of the Parameter
136    */

137   public Generator(String JavaDoc repositories)
138   {
139     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
140     setIconImage(getImage("generator.gif"));
141
142     try
143     {
144       setStatusLabel(_lblStatus);
145       _splitpane.setVisible(false);
146       getContentPane().setBackground(SystemColor.desktop);
147       invalidate();
148       loadProps(PROPERTIES_FILE_NAME);
149       setupWindow();
150
151       setupPersistenceManager(repositories);
152       setupSession();
153       setupContext();
154       setupProjectPane();
155
156       jbInit();
157
158       updateTitle();
159       setupTemplateSetManager();
160       validate();
161     }
162     catch (Exception JavaDoc e)
163     {
164       handleException(e);
165       System.exit(-1);
166     }
167   }
168
169   public void setupTemplateSetManager()
170   {
171     _templateSetManager.rescanTemplateSets(getTemplatePath());
172   }
173
174   public void setupProjectPane() throws Exception JavaDoc
175   {
176     _pnlProject = new ProjectPanel(this, _projectProperties);
177     _splitpane.add(_pnlProject, JSplitPane.TOP);
178   }
179
180   public TemplateSetManager getTemplateSetManager()
181   {
182     return _templateSetManager;
183   }
184
185   protected void setupPersistenceManager(String JavaDoc repositories) throws DjenericException, FileNotFoundException JavaDoc
186   {
187     DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(this, repositories);
188     _mgr = fact.createInstance();
189   }
190
191   protected void setupSession() throws DjenericException
192   {
193     _session = _mgr.createSession();
194     _session.setValidateObjectsAfterLoading(false);
195   }
196
197   protected void setupContext() throws DjenericException
198   {
199     DjUserContextAssociation[] ctxt = _mgr.getCurrentUser().getContextAssociations();
200     if (ctxt.length == 1) _mgr.getContextManager().setCurrentContext(ctxt[0].getContext());
201
202     else
203     // a length of 0 will throw an exception; that's what we want here
204
_mgr.determineContext();
205   }
206
207   protected void setupWindow()
208   {
209     int divloc = Integer.parseInt(getProperty("window.dividerloc", "225"));
210     _splitpane.setDividerLocation(divloc);
211
212     int width = Integer.parseInt(getProperty("window.width", "400"));
213     int height = Integer.parseInt(getProperty("window.height", "400"));
214     setSize(new Dimension JavaDoc(width, height));
215     addKeyListener(this);
216   }
217
218   protected void updateTitle()
219   {
220     String JavaDoc title = "Untitled";
221     if (_projectFileName != null) title = _projectFileName;
222
223     try
224     {
225       setTitle("Djeneric Generator v" + DjVersion.getVersion() + " - " + title + " - "
226                + getManager().getConnectionTitle());
227     }
228     catch (Exception JavaDoc x)
229     {
230       DjLogger.log(x);
231     }
232   }
233
234   /**
235    * Component initialization
236    *
237    * @exception Exception
238    * Description of the Exception
239    */

240   protected void jbInit() throws Exception JavaDoc
241   {
242     mnuFile.setMnemonic('F');
243     mnuFile.setText("File");
244     mnuExit.setText("Exit");
245     mnuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.ALT_MASK, false));
246     mnuExit.addActionListener(new java.awt.event.ActionListener JavaDoc()
247     {
248       public void actionPerformed(ActionEvent JavaDoc e)
249       {
250         mnuExit(e);
251       }
252     });
253     mnuHelp.setMnemonic('H');
254     mnuHelp.setText("Help");
255     mnuAbout.setText("About");
256     mnuAbout.addActionListener(new java.awt.event.ActionListener JavaDoc()
257     {
258       public void actionPerformed(ActionEvent JavaDoc e)
259       {
260         mnuAbout(e);
261       }
262     });
263     this.setJMenuBar(_menu);
264
265     _lblStatus.setText("OK");
266     _lblStatus.setOpaque(true);
267     _desktopPanel.setLayout(borderLayout2);
268     _desktopPanel.setBackground(SystemColor.desktop);
269     mnuNewProject.setMnemonic('P');
270     mnuNewProject.setText("New Project...");
271     mnuNewProject.setAccelerator(javax.swing.KeyStroke.getKeyStroke(78, java.awt.event.KeyEvent.CTRL_MASK, false));
272     mnuNewProject.addActionListener(new java.awt.event.ActionListener JavaDoc()
273     {
274       public void actionPerformed(ActionEvent JavaDoc e)
275       {
276         mnuNewProject(e);
277       }
278     });
279     mnuOpenProject.setMnemonic('O');
280     mnuOpenProject.setText("Open Project...");
281     mnuOpenProject.setAccelerator(javax.swing.KeyStroke.getKeyStroke(79, java.awt.event.KeyEvent.CTRL_MASK, false));
282     mnuOpenProject.addActionListener(new java.awt.event.ActionListener JavaDoc()
283     {
284       public void actionPerformed(ActionEvent JavaDoc e)
285       {
286         mnuOpenProject(e);
287       }
288     });
289     mnuProjectProperties.setText("Project Properties...");
290     mnuProjectProperties.addActionListener(new java.awt.event.ActionListener JavaDoc()
291     {
292       public void actionPerformed(ActionEvent JavaDoc e)
293       {
294         mnuProjectProperties(e);
295       }
296     });
297     mnuOptions.setText("Options...");
298     mnuOptions.addActionListener(new java.awt.event.ActionListener JavaDoc()
299     {
300       public void actionPerformed(ActionEvent JavaDoc e)
301       {
302         mnuOptions(e);
303       }
304     });
305     mnuTools.setMnemonic('T');
306     mnuTools.setText("Tools");
307     mnuSaveProject.setMnemonic('S');
308     mnuSaveProject.setText("Save Project");
309     mnuSaveProject.setAccelerator(javax.swing.KeyStroke.getKeyStroke(83, java.awt.event.KeyEvent.CTRL_MASK, false));
310     mnuSaveProject.addActionListener(new java.awt.event.ActionListener JavaDoc()
311     {
312       public void actionPerformed(ActionEvent JavaDoc e)
313       {
314         mnuSaveProject(e);
315       }
316     });
317     mnuSaveAs.setMnemonic('A');
318     mnuSaveAs.setText("Save Project As...");
319     mnuSaveAs.addActionListener(new java.awt.event.ActionListener JavaDoc()
320     {
321       public void actionPerformed(ActionEvent JavaDoc e)
322       {
323         mnuSaveAs(e);
324       }
325     });
326     mnuTabs.setMnemonic('A');
327     mnuTabs.setText("Tabs");
328     mnuNextTab.setText("Next");
329     mnuNextTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(117, java.awt.event.KeyEvent.CTRL_MASK, false));
330     mnuNextTab.addActionListener(new java.awt.event.ActionListener JavaDoc()
331     {
332       public void actionPerformed(ActionEvent JavaDoc e)
333       {
334         mnuNextTab(e);
335       }
336     });
337     mnuPrevTab.setText("Previous");
338     mnuPrevTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(117, java.awt.event.KeyEvent.CTRL_MASK, false));
339     mnuPrevTab.addActionListener(new java.awt.event.ActionListener JavaDoc()
340     {
341       public void actionPerformed(ActionEvent JavaDoc e)
342       {
343         mnuPrevTab(e);
344       }
345     });
346     mnuCloseTab.setText("Close");
347     mnuCloseTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.CTRL_MASK, false));
348     mnuCloseTab.addActionListener(new java.awt.event.ActionListener JavaDoc()
349     {
350       public void actionPerformed(ActionEvent JavaDoc e)
351       {
352         mnuCloseTab(e);
353       }
354     });
355     mnuCloseAll.setText("Close All");
356     mnuCloseAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.CTRL_MASK
357                                                                        | java.awt.event.KeyEvent.SHIFT_MASK, false));
358     mnuCloseAll.addActionListener(new java.awt.event.ActionListener JavaDoc()
359     {
360       public void actionPerformed(ActionEvent JavaDoc e)
361       {
362         mnuCloseAll(e);
363       }
364     });
365     _splitpane.setOneTouchExpandable(true);
366     this.getContentPane().add(_splitpane, BorderLayout.CENTER);
367     _splitpane.add(_desktopPanel, JSplitPane.BOTTOM);
368     _desktopPanel.add(_tabs, BorderLayout.CENTER);
369     this.getContentPane().add(_lblStatus, BorderLayout.SOUTH);
370     _menu.add(mnuFile);
371     _menu.add(mnuTools);
372     _menu.add(mnuTabs);
373     _menu.add(mnuHelp);
374     mnuFile.add(mnuNewProject);
375     mnuFile.add(mnuOpenProject);
376     mnuFile.add(mnuSaveProject);
377     mnuFile.add(mnuSaveAs);
378     mnuFile.addSeparator();
379     mnuFile.add(mnuProjectProperties);
380     mnuFile.addSeparator();
381     mnuFile.add(mnuExit);
382     mnuHelp.add(mnuAbout);
383     mnuTools.add(mnuOptions);
384     mnuTabs.add(mnuNextTab);
385     mnuTabs.add(mnuPrevTab);
386     mnuTabs.add(mnuCloseTab);
387     mnuTabs.add(mnuCloseAll);
388     _splitpane.setDividerLocation(270);
389
390     setTabsVisible(false);
391
392   }
393
394   /**
395    * Overridden so we can exit when window is closed
396    *
397    * @param e
398    * Description of the Parameter
399    */

400   protected void processWindowEvent(WindowEvent JavaDoc e)
401   {
402     if (e.getID() == WindowEvent.WINDOW_CLOSING)
403     {
404       try
405       {
406         if (canExit()) exitProgram();
407       }
408       catch (Exception JavaDoc iox)
409       {
410         setStatusMessage(iox);
411       }
412     }
413     else super.processWindowEvent(e);
414
415   }
416
417   protected void exitProgram()
418   {
419     _pnlProject.close();
420     super.exitProgram();
421   }
422
423   protected boolean canExit() throws Exception JavaDoc
424   {
425     return isOkToAbandon();
426   }
427
428   protected void updateProperties()
429   {
430     setProperty("window.width", "" + getWidth());
431     setProperty("window.height", "" + getHeight());
432     setProperty("window.dividerloc", "" + _splitpane.getDividerLocation());
433     if (_projectFileName != null) setProperty("lastopened", _projectFileName);
434
435   }
436
437   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
438   {
439     URL JavaDoc url = Generator.class.getResource("images/" + fileName);
440     if (url != null) return new ImageIcon JavaDoc(url);
441     else return new ImageIcon JavaDoc("");
442     // Default to empty image
443
}
444
445   public static Image JavaDoc getImage(String JavaDoc fileName)
446   {
447     URL JavaDoc url = Generator.class.getResource("images/" + fileName);
448     if (url != null) return Toolkit.getDefaultToolkit().getImage(url);
449
450     else return Toolkit.getDefaultToolkit().getImage("");
451   }
452
453   public DjPersistenceManager getManager()
454   {
455     return _mgr;
456   }
457
458   public DjSession getSession()
459   {
460     return _session;
461   }
462
463   void mnuExit(ActionEvent JavaDoc e)
464   {
465     try
466     {
467       if (canExit()) exitProgram();
468     }
469     catch (Exception JavaDoc x)
470     {
471       setStatusMessage(x);
472     }
473   }
474
475   public void closeTab(Component JavaDoc comp)
476   {
477     _tabs.remove(comp);
478     if (_tabs.getComponentCount() == 0) setTabsVisible(false);
479   }
480
481   public void keyTyped(KeyEvent JavaDoc e)
482   {
483   }
484
485   public void keyPressed(KeyEvent JavaDoc e)
486   {
487     if (_tabs.getComponentCount() == 0) return;
488
489     if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0)
490         && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
491     {
492       prevTab();
493     }
494     else if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
495     {
496       nextTab();
497     }
498     else if ((e.getKeyCode() == KeyEvent.VK_F4) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
499     {
500       closeTab(_tabs.getSelectedComponent());
501     }
502   }
503
504   protected void prevTab()
505   {
506     int idx = _tabs.getSelectedIndex();
507     if (idx == -1) return;
508     if (idx > 0)
509     {
510       idx--;
511     }
512     else
513     {
514       idx = _tabs.getComponentCount() - 1;
515     }
516     _tabs.setSelectedIndex(idx);
517   }
518
519   protected void nextTab()
520   {
521     int idx = _tabs.getSelectedIndex();
522     if (idx == -1) return;
523     if (idx < _tabs.getComponentCount() - 1)
524     {
525       idx++;
526     }
527     else
528     {
529       idx = 0;
530     }
531     _tabs.setSelectedIndex(idx);
532   }
533
534   public void keyReleased(KeyEvent JavaDoc e)
535   {
536   }
537
538   public void setTabsVisible(boolean visible)
539   {
540     _tabs.setVisible(visible);
541     if (visible)
542     {
543       _desktopPanel.setBackground(new Color JavaDoc(212, 208, 200));
544     }
545     else
546     {
547       _desktopPanel.setBackground(SystemColor.desktop);
548     }
549
550   }
551
552   void mnuAbout(ActionEvent JavaDoc e)
553   {
554     new AboutDialog(this);
555   }
556
557   void mnuNewProject(ActionEvent JavaDoc e)
558   {
559     try
560     {
561       if (!isOkToAbandon()) return;
562
563       ProjectProperties newProj = new ProjectProperties();
564
565       ProjectPropertiesDialog ppd = new ProjectPropertiesDialog(this, "New Project", newProj, _templateSetManager);
566       if (!ppd.wasCancelled())
567       {
568         newProj.copyInto(_projectProperties);
569         _projectProperties.setModified(true);
570         _projectFileName = null;
571         notifyProjectSwitched();
572         setProjectVisible(true);
573       }
574     }
575     catch (Exception JavaDoc x)
576     {
577       setStatusMessage(x);
578     }
579   }
580
581   public void addTab(Component JavaDoc comp, String JavaDoc title, boolean setSelected)
582   {
583     _tabs.add(comp, title);
584     if (setSelected) _tabs.setSelectedComponent(comp);
585     setTabsVisible(true);
586   }
587
588   void mnuOptions(ActionEvent JavaDoc e)
589   {
590     OptionsDialog od = new OptionsDialog(this, getProperties());
591     if (!od.wasCancelled()) rescanTemplateSets();
592   }
593
594   void mnuProjectProperties(ActionEvent JavaDoc e)
595   {
596     ProjectPropertiesDialog ppd = new ProjectPropertiesDialog(this, "Project Properties", _projectProperties,
597         _templateSetManager);
598     if (!ppd.wasCancelled()) notifyProjectSwitched();
599   }
600
601   public boolean isOkToOverwrite(File JavaDoc file)
602   {
603     int result = JOptionPane.showOptionDialog(this, "Do you want to overwrite " + file.getAbsolutePath() + "?",
604                                               "File exists", JOptionPane.DEFAULT_OPTION,
605                                               JOptionPane.INFORMATION_MESSAGE, null
606                                               // 0 1
607
, new String JavaDoc[]{"Overwrite", "Cancel"}, null);
608     return result == 0;
609   }
610
611   boolean isOkToAbandon() throws Exception JavaDoc
612   {
613     if (isModified())
614     {
615       int result = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Changes made",
616                                                 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null
617                                                 // 0 1 2
618
, new String JavaDoc[]{"Save", "Discard", "Cancel"}, null);
619       if ((result != 0) && (result != 1))
620       {
621         return false;
622       }
623       // cancel or closed
624
if (result == 0)
625       {
626         return saveProject();
627       }
628     }
629     return true;
630   }
631
632   protected boolean saveProject() throws Exception JavaDoc
633   {
634     if (_projectFileName == null) return saveProjectAs();
635     else return doSaveProject();
636   }
637
638   protected boolean doSaveProject() throws Exception JavaDoc
639   {
640     _projectProperties.save(_projectFileName);
641     setStatusMessage("Project saved to " + _projectFileName);
642     return true;
643   }
644
645   protected void initFileChooser(JFileChooser JavaDoc fc)
646   {
647     fc.setCurrentDirectory(new File JavaDoc(getProperty("filelocation", System.getProperties().getProperty("user.dir"))));
648   }
649
650   protected void recordFileChooserSettings(JFileChooser JavaDoc fc)
651   {
652     setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath());
653   }
654
655   protected boolean saveProjectAs() throws Exception JavaDoc
656   {
657     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
658     initFileChooser(fc);
659     fc.setDialogTitle("Save Project As");
660     DjFileFilter filter = new DjFileFilter(new String JavaDoc[]{PROJECT_EXTENSION}, "Djeneric Generator Project");
661     fc.setFileFilter(filter);
662     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
663     if (_projectFileName != null)
664     {
665       fc.setSelectedFile(new File JavaDoc(_projectFileName));
666     }
667
668     if (fc.showSaveDialog(this) != JFileChooser.CANCEL_OPTION)
669     {
670       recordFileChooserSettings(fc);
671       String JavaDoc fileName = fc.getSelectedFile().getAbsolutePath();
672       if (!fileName.toLowerCase().endsWith("." + PROJECT_EXTENSION))
673       {
674         fileName += "." + PROJECT_EXTENSION;
675       }
676       File JavaDoc chk = new File JavaDoc(fileName);
677       if (chk.exists() && !isOkToOverwrite(chk)) return false;
678       _projectFileName = fileName;
679       updateTitle();
680       return doSaveProject();
681     }
682     return false;
683   }
684
685   void mnuSaveProject(ActionEvent JavaDoc e)
686   {
687     try
688     {
689       saveProject();
690     }
691     catch (Exception JavaDoc x)
692     {
693       setStatusMessage(x);
694     }
695   }
696
697   void mnuSaveAs(ActionEvent JavaDoc e)
698   {
699     try
700     {
701       saveProjectAs();
702     }
703     catch (Exception JavaDoc x)
704     {
705       setStatusMessage(x);
706     }
707
708   }
709
710   void mnuOpenProject(ActionEvent JavaDoc e)
711   {
712     openProject();
713   }
714
715   public void openProject()
716   {
717     try
718     {
719       if (isOkToAbandon())
720       {
721         JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
722         fc.setDialogTitle("Open Project");
723         DjFileFilter filter = new DjFileFilter(new String JavaDoc[]{PROJECT_EXTENSION}, "Djeneric Generator Project");
724         fc.setFileFilter(filter);
725         initFileChooser(fc);
726
727         String JavaDoc path = "";
728         if (_projectFileName == null) path = System.getProperties().getProperty("user.dir");
729         fc.setCurrentDirectory(new File JavaDoc(path));
730         fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
731
732         fc.setSelectedFile(new File JavaDoc(getProperty("lastopened", "")));
733
734         if (fc.showDialog(this, "Open Project") == JFileChooser.APPROVE_OPTION)
735         {
736           ProjectProperties newProj = new ProjectProperties();
737           newProj.load(fc.getSelectedFile().getAbsolutePath());
738           newProj.copyInto(_projectProperties);
739           _projectProperties.setModified(false);
740           _projectFileName = fc.getSelectedFile().getAbsolutePath();
741           updateTitle();
742           rescanTemplateSets();
743           setStatusMessage("Project definitions loaded from " + _projectFileName);
744           updateProperties();
745           setProjectVisible(true);
746         }
747       }
748     }
749     catch (Exception JavaDoc x)
750     {
751       setStatusMessage(x);
752     }
753   }
754
755   protected boolean isModified()
756   {
757     return _projectProperties.isModified();
758   }
759
760   protected void setProjectVisible(boolean b)
761   {
762     _splitpane.setVisible(b);
763     invalidate();
764     validate();
765   }
766
767   protected void rescanTemplateSets()
768   {
769     _templateSetManager.rescanTemplateSets(getProperty(KEY_TEMPLATEPATH, "."));
770     _pnlProject.refreshTemplates();
771
772   }
773
774   protected void notifyProjectSwitched()
775   {
776     updateTitle();
777     rescanTemplateSets();
778   }
779
780   public JTextArea JavaDoc getTextTab(String JavaDoc title, boolean setSelected)
781   {
782     for (int i = 0; i < _tabs.getComponentCount(); i++)
783     {
784       if (_tabs.getTitleAt(i).equals(title))
785       {
786         Component JavaDoc c = _tabs.getComponent(i);
787         if (c instanceof JScrollPane JavaDoc)
788         {
789           JScrollPane JavaDoc scr = (JScrollPane JavaDoc) c;
790           JTextArea JavaDoc ta = (JTextArea JavaDoc) scr.getViewport().getComponent(0);
791           if (setSelected) _tabs.setSelectedIndex(i);
792           return ta;
793         }
794       }
795     }
796     return createTextTab(title, setSelected);
797   }
798
799   private JTextArea JavaDoc createTextTab(String JavaDoc title, boolean setSelected)
800   {
801     CloseableScrollpane scr = new CloseableScrollpane(this);
802     JTextArea JavaDoc ta = new JTextArea JavaDoc();
803     ta.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 11));
804     ta.setEditable(false);
805
806     scr.getViewport().add(ta);
807     addTab(scr, title, setSelected);
808     return ta;
809   }
810
811   void mnuNextTab(ActionEvent JavaDoc e)
812   {
813     nextTab();
814   }
815
816   void mnuCloseTab(ActionEvent JavaDoc e)
817   {
818     closeTab(_tabs.getSelectedComponent());
819   }
820
821   void mnuPrevTab(ActionEvent JavaDoc e)
822   {
823     prevTab();
824   }
825
826   void mnuCloseAll(ActionEvent JavaDoc e)
827   {
828     while (_tabs.getSelectedComponent() != null)
829       closeTab(_tabs.getSelectedComponent());
830   }
831
832   public String JavaDoc getTemplatePath()
833   {
834     return getProperties().getProperty(Generator.KEY_TEMPLATEPATH, "");
835   }
836
837   public String JavaDoc getIncludePath()
838   {
839     return getProperties().getProperty(Generator.KEY_INCLUDEPATH, "");
840   }
841
842   public static void main(String JavaDoc[] args)
843   {
844     try
845     {
846       DjenericTool.setLookAndFeel();
847
848       if (args.length < 1)
849       {
850         System.out.println("Usage: java " + Generator.class.getName() + " <repositories.xml>");
851         return;
852       }
853       String JavaDoc banner = Generator.class.getName() + " version " + DjVersion.getVersion();
854       DjLogPrintStream.logAll(banner, LOG_FILE_NAME);
855
856       System.out.println("Reading " + args[0]);
857       new Generator(args[0]).startApp();
858     }
859     catch (Exception JavaDoc e)
860     {
861       DjLogger.log(e);
862     }
863   }
864
865 }
Popular Tags