KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > strongtyper > StrongTyper


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.strongtyper;
32
33 import java.awt.AWTEvent JavaDoc;
34 import java.awt.BorderLayout JavaDoc;
35 import java.awt.Color JavaDoc;
36 import java.awt.Dimension JavaDoc;
37 import java.awt.FlowLayout JavaDoc;
38 import java.awt.Image JavaDoc;
39 import java.awt.Toolkit JavaDoc;
40 import java.awt.event.ActionEvent JavaDoc;
41 import java.awt.event.KeyEvent JavaDoc;
42 import java.awt.event.WindowEvent JavaDoc;
43 import java.io.BufferedWriter JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.FileWriter JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.Arrays JavaDoc;
49 import java.util.Collections JavaDoc;
50 import java.util.StringTokenizer JavaDoc;
51
52 import javax.swing.DefaultComboBoxModel JavaDoc;
53 import javax.swing.ImageIcon JavaDoc;
54 import javax.swing.JButton JavaDoc;
55 import javax.swing.JCheckBox JavaDoc;
56 import javax.swing.JComboBox JavaDoc;
57 import javax.swing.JFileChooser JavaDoc;
58 import javax.swing.JLabel JavaDoc;
59 import javax.swing.JList JavaDoc;
60 import javax.swing.JOptionPane JavaDoc;
61 import javax.swing.JPanel JavaDoc;
62 import javax.swing.JScrollPane JavaDoc;
63 import javax.swing.JTabbedPane JavaDoc;
64 import javax.swing.JTextArea JavaDoc;
65 import javax.swing.JTextField JavaDoc;
66
67 import com.genimen.djeneric.language.Messages;
68 import com.genimen.djeneric.repository.DjExtent;
69 import com.genimen.djeneric.repository.DjPackage;
70 import com.genimen.djeneric.repository.DjPersistenceManager;
71 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
72 import com.genimen.djeneric.repository.DjSession;
73 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
74 import com.genimen.djeneric.tools.common.DjenericTool;
75 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
76 import com.genimen.djeneric.ui.Util;
77 import com.genimen.djeneric.util.DjEnvironment;
78 import com.genimen.djeneric.util.DjFileFilter;
79 import com.genimen.djeneric.util.DjFileUtil;
80 import com.genimen.djeneric.util.DjLogPrintStream;
81 import com.genimen.djeneric.util.DjLogger;
82 import com.genimen.djeneric.util.DjStringComparator;
83 import com.genimen.djeneric.util.DjStringReplacer;
84 import com.genimen.djeneric.util.DjVersion;
85
86 public class StrongTyper extends DjenericTool
87 {
88   private static final long serialVersionUID = 1L;
89   private final String JavaDoc PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("strongtyper.properties");
90   private final static String JavaDoc LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("strongtyper.log");
91
92   private final String JavaDoc SST_EXTENSION = "sts";
93
94   public final static String JavaDoc OK_MSG = Messages.getString("global.Ok");
95
96   private final static String JavaDoc CODE_START_TAG = "//<DjenericManualCode%>";
97   private final static String JavaDoc CODE_END_TAG = "//</DjenericManualCode>";
98   private final static String JavaDoc GENERATE_SUPER = "//<DjenericGenerateSuper/>";
99   private final static String JavaDoc REGENERATION_TAGS = "\n" + "// Place your custom code BETWEEN the following tags\n"
100                                                      + StrongTyper.CODE_START_TAG + "\n" + StrongTyper.CODE_END_TAG
101                                                      + "\n";
102
103   DjPersistenceManager _mgr;
104   DjSession _session;
105   Color JavaDoc _normalColor = new Color JavaDoc(102, 102, 153);
106   JTabbedPane JavaDoc _tabs = new JTabbedPane JavaDoc();
107   JPanel JavaDoc _pnlSelection = new JPanel JavaDoc();
108   JScrollPane JavaDoc _scrSelection = new JScrollPane JavaDoc();
109   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
110   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
111   JPanel JavaDoc _pnlOptions = new JPanel JavaDoc();
112   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
113   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
114   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
115   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
116   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
117   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
118   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
119   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
120   JLabel JavaDoc jLabel4 = new JLabel JavaDoc();
121   JTextField JavaDoc _edtPackage = new JTextField JavaDoc();
122   JTextField JavaDoc _edtPrefix = new JTextField JavaDoc();
123   JTextField JavaDoc _edtSuffix = new JTextField JavaDoc();
124   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
125   JTextField JavaDoc _edtDirectory = new JTextField JavaDoc();
126   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
127   JButton JavaDoc _butBrowse = new JButton JavaDoc();
128   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
129   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
130   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
131   JButton JavaDoc _butSave = new JButton JavaDoc();
132   JButton JavaDoc _butLoad = new JButton JavaDoc();
133   JPanel JavaDoc jPanel6 = new JPanel JavaDoc();
134   BorderLayout JavaDoc borderLayout5 = new BorderLayout JavaDoc();
135   JPanel JavaDoc jPanel7 = new JPanel JavaDoc();
136   JButton JavaDoc _butGenerate = new JButton JavaDoc();
137   DefaultComboBoxModel JavaDoc _model;
138   JList JavaDoc _lstExtents = new JList JavaDoc();
139   JLabel JavaDoc jLabel5 = new JLabel JavaDoc();
140   JTextField JavaDoc _edtSystemPrefix = new JTextField JavaDoc();
141   JPanel JavaDoc jPanel8 = new JPanel JavaDoc();
142   JCheckBox JavaDoc _chkGenerateAll = new JCheckBox JavaDoc();
143   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
144   JLabel JavaDoc jLabel6 = new JLabel JavaDoc();
145   JTextField JavaDoc _edtImplPackage = new JTextField JavaDoc();
146   JPanel JavaDoc _pnlLog = new JPanel JavaDoc();
147   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
148   BorderLayout JavaDoc borderLayout6 = new BorderLayout JavaDoc();
149   JTextArea JavaDoc _txtLog = new JTextArea JavaDoc();
150   JPanel JavaDoc jPanel9 = new JPanel JavaDoc();
151   BorderLayout JavaDoc borderLayout7 = new BorderLayout JavaDoc();
152   JLabel JavaDoc jLabel7 = new JLabel JavaDoc();
153   DjPackage _defaultPackage = new DjPackage(Messages
154                                                        .getString("ExtentSelectorDialog.DefaultPackage"));
155   DjPackage _allPackages = new DjPackage(Messages
156                                                        .getString("ExtentSelectorDialog.AllPackages"));
157   JPanel JavaDoc jPanel10 = new JPanel JavaDoc();
158   DjVerticalFlowLayout verticalFlowLayout3 = new DjVerticalFlowLayout();
159   JPanel JavaDoc jPanel11 = new JPanel JavaDoc();
160   DjVerticalFlowLayout verticalFlowLayout4 = new DjVerticalFlowLayout();
161   JLabel JavaDoc jLabel8 = new JLabel JavaDoc();
162   JTextField JavaDoc _edtSelection = new JTextField JavaDoc();
163   JPanel JavaDoc jPanel12 = new JPanel JavaDoc();
164   BorderLayout JavaDoc borderLayout8 = new BorderLayout JavaDoc();
165   JComboBox JavaDoc _cbbPackage = new JComboBox JavaDoc();
166   JButton JavaDoc _butAddPackage = new JButton JavaDoc();
167
168   /**
169    * Construct the frame
170    *
171    * @param repositories
172    * Description of the Parameter
173    */

174   public StrongTyper(String JavaDoc repositories)
175   {
176     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
177     setIconImage(getImage("strongtyper.gif"));
178
179     try
180     {
181       setStatusLabel(_lblStatus);
182       DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(this, repositories);
183       _mgr = fact.createInstance();
184
185       _session = _mgr.createSession();
186
187       DjExtent[] extents = _mgr.getExtents();
188       Arrays.sort(extents, new DjStringComparator(false));
189       _model = new DefaultComboBoxModel JavaDoc(extents);
190
191       ArrayList JavaDoc packs = new ArrayList JavaDoc();
192       packs.add(_allPackages);
193       packs.add(_defaultPackage);
194       packs.addAll(Arrays.asList(_mgr.getPackages()));
195       _cbbPackage.setModel(new DefaultComboBoxModel JavaDoc(packs.toArray()));
196
197       jbInit();
198
199       _butAddPackage.setIcon(getImageIcon("add.gif"));
200
201       Util.sizeLabels(jPanel9);
202       Util.sizeButtons(jPanel9);
203
204       loadProps(PROPERTIES_FILE_NAME);
205
206       copyPropertiesToFields();
207
208       updateSelection();
209
210       int width = Integer.parseInt(getProperty("window.width", "400"));
211       int height = Integer.parseInt(getProperty("window.height", "400"));
212       setSize(new Dimension JavaDoc(width, height));
213
214       setTitle("Djeneric Strong Typer v" + DjVersion.getVersion() + " logged on to " + _mgr.getCurrentRepository());
215     }
216     catch (Exception JavaDoc e)
217     {
218       handleException(e);
219       System.exit(-1);
220     }
221   }
222
223   /**
224    * Component initialization
225    *
226    * @exception Exception
227    * Description of the Exception
228    */

229   private void jbInit() throws Exception JavaDoc
230   {
231     _scrSelection.setPreferredSize(new Dimension JavaDoc(260, 131));
232     _pnlSelection.setLayout(borderLayout1);
233     _lblStatus.setText(Messages.getString("global.Ok"));
234     _pnlOptions.setLayout(borderLayout2);
235     jPanel1.setLayout(verticalFlowLayout1);
236     jPanel2.setLayout(verticalFlowLayout2);
237     jLabel1.setText(Messages.getString("StrongTyper.ImplPackage"));
238     jLabel2.setText(Messages.getString("StrongTyper.ClassNamePrefix"));
239     jLabel3.setText(Messages.getString("StrongTyper.ClassNameSuffix"));
240     jLabel4.setText(Messages.getString("StrongTyper.OutputRoot"));
241     jPanel3.setLayout(borderLayout3);
242     _butBrowse.setPreferredSize(new Dimension JavaDoc(21, 21));
243     _butBrowse.setText("...");
244     _butBrowse.addActionListener(new java.awt.event.ActionListener JavaDoc()
245     {
246       public void actionPerformed(ActionEvent JavaDoc e)
247       {
248         _butBrowse_actionPerformed(e);
249       }
250     });
251     jPanel4.setLayout(borderLayout4);
252     _butSave.setText(Messages.getString("global.Save"));
253     _butSave.addActionListener(new java.awt.event.ActionListener JavaDoc()
254     {
255       public void actionPerformed(ActionEvent JavaDoc e)
256       {
257         _butSave_actionPerformed(e);
258       }
259     });
260     _butLoad.setText(Messages.getString("StrongTyper.Load"));
261     _butLoad.addActionListener(new java.awt.event.ActionListener JavaDoc()
262     {
263       public void actionPerformed(ActionEvent JavaDoc e)
264       {
265         _butLoad_actionPerformed(e);
266       }
267     });
268     jPanel6.setLayout(borderLayout5);
269     _butGenerate.setText(Messages.getString("global.Generate"));
270     _butGenerate.addActionListener(new java.awt.event.ActionListener JavaDoc()
271     {
272       public void actionPerformed(ActionEvent JavaDoc e)
273       {
274         _butGenerate_actionPerformed(e);
275       }
276     });
277     _lstExtents.setModel(_model);
278     jLabel5.setText(Messages.getString("StrongTyper.Systemprefix"));
279     _edtPackage.setPreferredSize(new Dimension JavaDoc(4, 21));
280     _edtPackage.setToolTipText(Messages.getString("StrongTyper.Package1"));
281     _edtPrefix.setPreferredSize(new Dimension JavaDoc(4, 21));
282     _edtPrefix.setToolTipText(Messages.getString("StrongTyper.ClassNamePrefixDescr"));
283     _edtSuffix.setPreferredSize(new Dimension JavaDoc(4, 21));
284     _edtSuffix.setToolTipText(Messages.getString("StrongTyper.ClassNameSuffixDescr"));
285     _edtSystemPrefix.setPreferredSize(new Dimension JavaDoc(4, 21));
286     _edtSystemPrefix.setToolTipText(Messages.getString("StrongTyper.PmSessionPrefix"));
287     _edtDirectory.setPreferredSize(new Dimension JavaDoc(4, 21));
288     _edtDirectory.setToolTipText(Messages.getString("StrongTyper.DestDir"));
289     _chkGenerateAll.setText(Messages.getString("StrongTyper.GenerateManagerSession"));
290     jPanel8.setLayout(flowLayout1);
291     flowLayout1.setAlignment(FlowLayout.LEFT);
292     jLabel6.setText(Messages.getString("StrongTyper.Package"));
293     _edtImplPackage.setPreferredSize(new Dimension JavaDoc(4, 21));
294     _edtImplPackage.setToolTipText(Messages.getString("StrongTyper.Package1"));
295     _pnlLog.setLayout(borderLayout6);
296     _txtLog.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 11));
297     _txtLog.setEditable(false);
298     jPanel9.setLayout(borderLayout7);
299     jLabel7.setText("Package");
300     jPanel10.setLayout(verticalFlowLayout3);
301     jPanel11.setLayout(verticalFlowLayout4);
302     jLabel8.setText(Messages.getString("StrongTyper.Selection"));
303     _edtSelection.setToolTipText(Messages.getString("StrongTyper.SelectionHowTo"));
304     _edtSelection.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
305     {
306       public void keyReleased(KeyEvent JavaDoc e)
307       {
308         _edtSelection_keyReleased(e);
309       }
310     });
311     jPanel12.setLayout(borderLayout8);
312     _cbbPackage.setMinimumSize(new Dimension JavaDoc(26, 21));
313     _cbbPackage.setPreferredSize(new Dimension JavaDoc(21, 21));
314     _butAddPackage.setPreferredSize(new Dimension JavaDoc(21, 21));
315     _butAddPackage.setSelected(false);
316     _butAddPackage.addActionListener(new java.awt.event.ActionListener JavaDoc()
317     {
318       public void actionPerformed(ActionEvent JavaDoc e)
319       {
320         butAdd_actionPerformed(e);
321       }
322     });
323     jPanel5.add(_butLoad, null);
324     this.getContentPane().add(_tabs, BorderLayout.CENTER);
325     this.getContentPane().add(_lblStatus, BorderLayout.SOUTH);
326     _tabs.add(_pnlSelection, Messages.getString("StrongTyper.Selection"));
327     _pnlSelection.add(_scrSelection, BorderLayout.CENTER);
328     _scrSelection.getViewport().add(_lstExtents, null);
329     _tabs.add(_pnlOptions, Messages.getString("global.Options"));
330     _pnlOptions.add(jPanel1, BorderLayout.WEST);
331     jPanel1.add(jLabel6, null);
332     jPanel1.add(jLabel1, null);
333     jPanel1.add(jLabel5, null);
334     jPanel1.add(jLabel2, null);
335     jPanel1.add(jLabel3, null);
336     jPanel1.add(jLabel4, null);
337     _pnlOptions.add(jPanel2, BorderLayout.CENTER);
338     jPanel2.add(_edtPackage, null);
339     jPanel2.add(_edtImplPackage, null);
340     jPanel2.add(_edtSystemPrefix, null);
341     jPanel2.add(_edtPrefix, null);
342     jPanel2.add(_edtSuffix, null);
343     jPanel2.add(jPanel3, null);
344     jPanel3.add(_edtDirectory, BorderLayout.CENTER);
345     jPanel3.add(_butBrowse, BorderLayout.EAST);
346     _pnlOptions.add(jPanel4, BorderLayout.SOUTH);
347     jPanel4.add(jPanel5, BorderLayout.EAST);
348     jPanel5.add(_butSave, null);
349     _tabs.add(_pnlLog, Messages.getString("StrongTyper.Log"));
350     _pnlSelection.add(jPanel6, BorderLayout.SOUTH);
351     jPanel6.add(jPanel7, BorderLayout.EAST);
352     jPanel7.add(_butGenerate, null);
353     jPanel6.add(jPanel8, BorderLayout.CENTER);
354     jPanel8.add(_chkGenerateAll, null);
355     _pnlSelection.add(jPanel9, BorderLayout.NORTH);
356     jPanel9.add(jPanel10, BorderLayout.WEST);
357     jPanel10.add(jLabel7, null);
358     jPanel10.add(jLabel8, null);
359     jPanel9.add(jPanel11, BorderLayout.CENTER);
360     jPanel11.add(jPanel12, null);
361     jPanel12.add(_cbbPackage, BorderLayout.CENTER);
362     jPanel12.add(_butAddPackage, BorderLayout.EAST);
363     jPanel11.add(_edtSelection, null);
364     _pnlLog.add(jScrollPane1, BorderLayout.CENTER);
365     jScrollPane1.getViewport().add(_txtLog, null);
366     Util.sizeLabels(jPanel1);
367     Util.sizeButtons(jPanel4);
368   }
369
370   /**
371    * Overridden so we can exit when window is closed
372    *
373    * @param e
374    * Description of the Parameter
375    */

376   protected void processWindowEvent(WindowEvent JavaDoc e)
377   {
378     if (e.getID() == WindowEvent.WINDOW_CLOSING)
379     {
380       exitProgram();
381     }
382     super.processWindowEvent(e);
383
384   }
385
386   protected void updateProperties()
387   {
388     setProperty("window.width", "" + getWidth());
389     setProperty("window.height", "" + getHeight());
390
391     setProperty("options.directory", _edtDirectory.getText());
392     setProperty("options.package", _edtPackage.getText());
393     setProperty("options.implpackage", _edtImplPackage.getText());
394     setProperty("options.prefix", _edtPrefix.getText());
395     setProperty("options.systemprefix", _edtSystemPrefix.getText());
396     setProperty("options.suffix", _edtSuffix.getText());
397     setProperty("options.generatetoplevel", "" + _chkGenerateAll.isSelected());
398     setProperty("options.selection", _edtSelection.getText());
399   }
400
401   protected void copyPropertiesToFields()
402   {
403     _edtDirectory.setText(getProperty("options.directory", ""));
404     _edtPackage.setText(getProperty("options.package", ""));
405     _edtImplPackage.setText(getProperty("options.implpackage", ""));
406     _edtPrefix.setText(getProperty("options.prefix", ""));
407     _edtSystemPrefix.setText(getProperty("options.systemprefix", ""));
408     _edtSuffix.setText(getProperty("options.suffix", ""));
409     _chkGenerateAll.setSelected(getProperty("options.generatetoplevel", "true").equalsIgnoreCase("true"));
410     _edtSelection.setText(getProperty("options.selection", ""));
411   }
412
413   private static String JavaDoc replaceTagId(String JavaDoc src, int id)
414   {
415     DjStringReplacer sr = new DjStringReplacer(src);
416     String JavaDoc to = "";
417     if (id != 0) to = " id=\"" + String.valueOf(id) + "\"";
418     return sr.replace("%", to);
419   }
420
421   public static String JavaDoc getRegenerationTags(int id)
422   {
423     return replaceTagId(REGENERATION_TAGS, id);
424   }
425
426   private String JavaDoc getRegenerationStartTag(int id)
427   {
428     return replaceTagId(CODE_START_TAG, id);
429   }
430
431   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
432   {
433     URL JavaDoc url = StrongTyper.class.getResource("images/" + fileName);
434     if (url != null) return new ImageIcon JavaDoc(url);
435     else return new ImageIcon JavaDoc("");
436     // Default to empty image
437
}
438
439   public static Image JavaDoc getImage(String JavaDoc fileName)
440   {
441     URL JavaDoc url = StrongTyper.class.getResource("images/" + fileName);
442     if (url != null) return Toolkit.getDefaultToolkit().getImage(url);
443
444     else return Toolkit.getDefaultToolkit().getImage("");
445   }
446
447   public DjPersistenceManager getManager()
448   {
449     return _mgr;
450   }
451
452   public DjSession getSession()
453   {
454     return _session;
455   }
456
457   void _butBrowse_actionPerformed(ActionEvent JavaDoc e)
458   {
459     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
460     fc.setDialogTitle(Messages.getString("global.GeneratePath"));
461     String JavaDoc path = _edtDirectory.getText();
462     if (path.trim().length() == 0) path = System.getProperties().getProperty("user.dir");
463     fc.setCurrentDirectory(new File JavaDoc(path));
464     fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
465
466     if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
467     {
468       _edtDirectory.setText(fc.getSelectedFile().getAbsolutePath());
469     }
470   }
471
472   void _butLoad_actionPerformed(ActionEvent JavaDoc e)
473   {
474     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
475     initFileChooser(fc);
476     DjFileFilter filter = new DjFileFilter(new String JavaDoc[]{SST_EXTENSION}, Messages
477         .getString("StrongTyper.StrongTyperSettings"));
478     fc.setFileFilter(filter);
479     fc.setDialogTitle(Messages.getString("StrongTyper.LoadSettings"));
480     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
481
482     if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
483     {
484       recordFileChooserSettings(fc);
485       getPropsFrom(fc.getSelectedFile().getAbsolutePath());
486       copyPropertiesToFields();
487     }
488
489   }
490
491   protected void initFileChooser(JFileChooser JavaDoc fc)
492   {
493     fc.setCurrentDirectory(new File JavaDoc(getProperty("filelocation", System.getProperties().getProperty("user.dir"))));
494   }
495
496   protected void recordFileChooserSettings(JFileChooser JavaDoc fc)
497   {
498     setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath());
499   }
500
501   boolean isOkToOverwrite(File JavaDoc file)
502   {
503     int result = JOptionPane.showOptionDialog(this,
504                                               Messages.getString("global.Sure2Overwrite", file.getAbsolutePath()),
505                                               Messages.getString("global.FileExists"), JOptionPane.DEFAULT_OPTION,
506                                               JOptionPane.INFORMATION_MESSAGE, null
507                                               // 0 1
508
, new String JavaDoc[]{Messages.getString("global.Overwrite"),
509                                                   Messages.getString("global.Cancel")}, null);
510     return result == 0;
511   }
512
513   void _butSave_actionPerformed(ActionEvent JavaDoc e)
514   {
515     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
516     DjFileFilter filter = new DjFileFilter(new String JavaDoc[]{SST_EXTENSION}, Messages
517         .getString("StrongTyper.StrongTyperSettings"));
518     fc.setFileFilter(filter);
519
520     fc.setDialogTitle(Messages.getString("StrongTyper.SaveSettings"));
521     initFileChooser(fc);
522     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
523
524     if (fc.showSaveDialog(this) != JFileChooser.CANCEL_OPTION)
525     {
526       recordFileChooserSettings(fc);
527       String JavaDoc fileName = fc.getSelectedFile().getAbsolutePath();
528       if (!fileName.toLowerCase().endsWith("." + SST_EXTENSION))
529       {
530         fileName += "." + SST_EXTENSION;
531       }
532       File JavaDoc chk = new File JavaDoc(fileName);
533       if (chk.exists())
534       {
535         if (!isOkToOverwrite(chk))
536         {
537           return;
538         }
539       }
540       saveProps(fileName);
541     }
542
543   }
544
545   private void initGenerator(Generator gen) throws ObjectNotDefinedException
546   {
547     gen.setItfPackageName(_edtPackage.getText());
548     gen.setImplPackageName(_edtImplPackage.getText());
549     gen.setPrefix(_edtPrefix.getText());
550     gen.setSystemPrefix(_edtSystemPrefix.getText());
551     gen.setSuffix(_edtSuffix.getText());
552     gen.setAbstract(false);
553     gen.setExtentSet(Arrays.asList(getExtents()));
554   }
555
556   private String JavaDoc getRootDirectory()
557   {
558     String JavaDoc dirName = _edtDirectory.getText();
559     if (!dirName.endsWith("/") && !dirName.endsWith("\\")) dirName += "/";
560
561     return dirName;
562   }
563
564   private void writeFile(String JavaDoc fileName, String JavaDoc code) throws Exception JavaDoc
565   {
566     BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(fileName));
567     w.write(code);
568     w.close();
569   }
570
571   // The following takes a little care: the extents that are present in the
572
// list
573
// ARE NOT PRESENT IN THE MODEL anymore because the model is reloaded
574
// frequently. So look the up and add the ones found in the current model.
575
// This is needed to ensure the latest model is used for generation
576
private DjExtent[] getExtents() throws ObjectNotDefinedException
577   {
578     ArrayList JavaDoc all = new ArrayList JavaDoc();
579     Object JavaDoc[] e = _lstExtents.getSelectedValues();
580     for (int i = 0; i < e.length; i++)
581     {
582       all.add(_mgr.getExtent(((DjExtent) e[i]).getName()));
583     }
584     return (DjExtent[]) all.toArray(new DjExtent[0]);
585   }
586
587   public void createDirectory(String JavaDoc dir)
588   {
589     try
590     {
591       File JavaDoc fd;
592       if (dir.endsWith("/") || dir.endsWith("\\"))
593       {
594         fd = new File JavaDoc(dir.substring(0, dir.length() - 1));
595         fd.mkdirs();
596       }
597       else
598       {
599         fd = new File JavaDoc(dir);
600         fd.mkdirs();
601       }
602     }
603     catch (Exception JavaDoc iox)
604     {
605       DjLogger.log("createDirectory " + dir + ": " + iox.getMessage());
606     }
607   }
608
609   private String JavaDoc getOutputDir(Generator gen)
610   {
611     String JavaDoc pack = gen.getPackageName();
612     DjStringReplacer sr = new DjStringReplacer(pack);
613
614     String JavaDoc result = getRootDirectory() + sr.replace(".", "/");
615     createDirectory(result);
616     if (!result.endsWith("/") && !result.endsWith("\\")) result += File.separator;
617     return result;
618   }
619
620   public void logMessage(String JavaDoc msg)
621   {
622     _txtLog.append(msg);
623   }
624
625   private void process(Generator gen) throws Exception JavaDoc
626   {
627     process(gen, true);
628   }
629
630   public String JavaDoc fetchOrgCode(String JavaDoc code, int tagId) throws Exception JavaDoc
631   {
632     String JavaDoc startTag = getRegenerationStartTag(tagId);
633
634     int sidx = code.indexOf(startTag);
635     if (sidx == -1) return null;
636
637     int eidx = code.indexOf(CODE_END_TAG, sidx);
638
639     if ((sidx != -1 && eidx == -1) || (sidx == -1 && eidx != -1)) throw new Exception JavaDoc(Messages
640         .getString("StrongTyper.UnmatchedTags", String.valueOf(tagId)));
641     String JavaDoc result = code.substring(sidx + startTag.length(), eidx);
642     if (result.trim().length() == 0) return null;
643     return result;
644   }
645
646   private void process(Generator gen, boolean overwrite) throws Exception JavaDoc
647   {
648     process(gen, overwrite, true);
649   }
650
651   private void process(Generator gen, boolean overwrite, boolean callInit) throws Exception JavaDoc
652   {
653     if (callInit) initGenerator(gen);
654
655     String JavaDoc fileName = getOutputDir(gen) + gen.getClassName() + ".java";
656     String JavaDoc genCode = gen.getCode();
657
658     File JavaDoc ex = new File JavaDoc(fileName);
659     if (ex.exists())
660     { // First check to see if we need to generate an abstract super class
661
// in stead of this one:
662

663       String JavaDoc oldCode = DjFileUtil.readFile(fileName);
664
665       if (!gen.isAbstract() && oldCode.toUpperCase().indexOf(GENERATE_SUPER.toUpperCase()) != -1)
666       {
667         gen.setAbstract(true);
668
669         // Use recursive call but make sure init is NOT called.
670
// Afterwards just return without further processing:
671
process(gen, overwrite, false);
672         return;
673       }
674
675       if (!overwrite)
676       {
677         logMessage("\n> "
678                    + Messages.getString("StrongTyper.NOTOverwritten", gen.getPackageName() + "." + gen.getClassName())
679                    + "\n\n");
680         return;
681       }
682
683       for (int i = 0; i < 10; i++)
684       {
685         String JavaDoc snippet = fetchOrgCode(oldCode, i);
686
687         if (snippet != null)
688         {
689           int idx = genCode.indexOf(getRegenerationStartTag(i));
690           if (idx == -1) throw new Exception JavaDoc(Messages.getString("StrongTyper.StartCodeTags", String.valueOf(i),
691                                                                 fileName));
692           int eidx = genCode.indexOf(CODE_END_TAG, idx);
693           if (eidx == -1) throw new Exception JavaDoc(Messages.getString("StrongTyper.EndTagsNotFound", fileName));
694           genCode = genCode.substring(0, idx) + getRegenerationStartTag(i) + snippet + genCode.substring(eidx);
695           logMessage(Messages.getString("StrongTyper.ManualCode", String.valueOf(i)));
696         }
697       }
698     }
699
700     writeFile(fileName, genCode);
701     logMessage(Messages.getString("StrongTyper.Generated", gen.getPackageName() + "." + gen.getClassName()) + "\n");
702   }
703
704   void _butGenerate_actionPerformed(ActionEvent JavaDoc e)
705   {
706     try
707     {
708       // (re)Load the model to accomodate an interactive approach where the
709
// modeler is
710
// open and changes are made followed by a generate iteratively
711
_mgr.loadModel();
712
713       _txtLog.setText("");
714       if (_edtSystemPrefix.getText().trim().length() == 0) throw new Exception JavaDoc(Messages
715           .getString("StrongTyper.SystemPrefixReqd"));
716       logMessage(Messages.getString("StrongTyper.Writing2", _edtDirectory.getText()) + "\n");
717       logMessage(Messages.getString("StrongTyper.UsingItfPackage", _edtPackage.getText()) + "\n");
718       logMessage(Messages.getString("StrongTyper.UsingImplPakage", _edtImplPackage.getText()) + "\n");
719       logMessage("------------------------------------------------------------\n");
720       ObjectGenerator gen = new ObjectGenerator(_mgr);
721       InterfaceGenerator intfGen = new InterfaceGenerator(_mgr);
722       QbeGenerator qgen = new QbeGenerator(_mgr);
723       QbeInterfaceGenerator qintfGen = new QbeInterfaceGenerator(_mgr);
724       CursorGenerator cgen = new CursorGenerator(_mgr);
725       CursorInterfaceGenerator cintfGen = new CursorInterfaceGenerator(_mgr);
726
727       DjExtent[] extents = getExtents();
728
729       for (int i = 0; i < extents.length; i++)
730       {
731         try
732         {
733           gen.setExtent(extents[i]);
734           process(gen);
735
736           intfGen.setExtent(extents[i]);
737           process(intfGen);
738
739           qgen.setExtent(extents[i]);
740           process(qgen);
741
742           qintfGen.setExtent(extents[i]);
743           process(qintfGen);
744
745           cgen.setExtent(extents[i]);
746           process(cgen);
747
748           cintfGen.setExtent(extents[i]);
749           process(cintfGen);
750         }
751         catch (Exception JavaDoc x)
752         {
753           logMessage("EXCEPTION: " + x.getMessage() + "\n");
754           setStatusMessage(x);
755         }
756       }
757
758       if (_chkGenerateAll.isSelected())
759       {
760         process(new OqlGenerator());
761         process(new OqlInterfaceGenerator());
762         process(new ManagerGenerator());
763         process(new ExceptionGenerator());
764         process(new NotFoundExceptionGenerator());
765         process(new MultipleFoundExceptionGenerator());
766         process(new ExceptionGenerator());
767         process(new ParentObjectGenerator());
768         process(new ParentInterfaceGenerator());
769         process(new AbstractSessionGenerator(extents));
770         process(new SessionInterfaceGenerator(extents, getManager().getDomains()));
771         process(new SessionGenerator(extents), false);
772       }
773       logMessage("\n" + Messages.getString("StrongTyper.Done"));
774       setStatusMessage(Messages.getString("StrongTyper.AllFilesGenerated"));
775     }
776     catch (Exception JavaDoc x)
777     {
778       logMessage("EXCEPTION: " + x.getMessage() + "\n");
779       setStatusMessage(x);
780     }
781     _tabs.setSelectedComponent(_pnlLog);
782   }
783
784   public static void main(String JavaDoc[] args)
785   {
786     try
787     {
788       DjenericTool.setLookAndFeel();
789
790       if (args.length < 1)
791       {
792         System.out
793             .println(Messages.getString("StrongTyper.Usage", StrongTyper.class.getName()) + " <repositories.xml>");
794         return;
795       }
796       String JavaDoc banner = Messages.getString("global.Version", StrongTyper.class.getName(), DjVersion.getVersion());
797       DjLogPrintStream.logAll(banner, LOG_FILE_NAME);
798
799       System.out.println(Messages.getString("global.Reading", args[0]));
800       new StrongTyper(args[0]).startApp();
801     }
802     catch (Exception JavaDoc e)
803     {
804       DjLogger.log(e);
805     }
806   }
807
808   void updateSelection()
809   {
810     DjExtent[] extents = _mgr.getExtents();
811
812     ArrayList JavaDoc selection = new ArrayList JavaDoc();
813
814     StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(_edtSelection.getText(), ";, ");
815     while (stok.hasMoreTokens())
816     {
817       String JavaDoc currentPackage = stok.nextToken();
818
819       for (int i = 0; i < extents.length; i++)
820       {
821         DjExtent ext = extents[i];
822         DjPackage curPack = ext.getPackage();
823         if ((curPack == null && currentPackage.equals(_defaultPackage.getName()))
824             || (curPack != null && curPack.getName().equals(currentPackage)) || currentPackage.equals("*")) if (!selection
825             .contains(ext)) selection.add(ext);
826       }
827     }
828
829     Collections.sort(selection, new DjStringComparator(false));
830     _model = new DefaultComboBoxModel JavaDoc(selection.toArray());
831     _lstExtents.setModel(_model);
832
833     _lstExtents.getSelectionModel().setSelectionInterval(0, _model.getSize() - 1);
834
835   }
836
837   void butAdd_actionPerformed(ActionEvent JavaDoc e)
838   {
839     String JavaDoc packName = _cbbPackage.getSelectedItem().toString();
840     String JavaDoc cur = _edtSelection.getText();
841
842     if (packName.equals(Messages.getString("ExtentSelectorDialog.AllPackages"))) packName = "*";
843
844     if (cur.trim().length() != 0) cur += "," + packName;
845     else cur = packName;
846
847     _edtSelection.setText(cur);
848     updateSelection();
849   }
850
851   void _edtSelection_keyReleased(KeyEvent JavaDoc e)
852   {
853     updateSelection();
854   }
855
856 }
Popular Tags