KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > menu > MenuPanel


1 package com.genimen.djeneric.tools.menu;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Rectangle JavaDoc;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.sql.Connection JavaDoc;
12 import java.sql.DriverManager JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Properties JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import javax.swing.ImageIcon JavaDoc;
21 import javax.swing.JButton JavaDoc;
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JTabbedPane JavaDoc;
25
26 import com.genimen.djeneric.language.Messages;
27 import com.genimen.djeneric.tools.administrator.Administrator;
28 import com.genimen.djeneric.tools.console.Console;
29 import com.genimen.djeneric.tools.logbrowser.LogBrowser;
30 import com.genimen.djeneric.tools.modeler.ModelEditor;
31 import com.genimen.djeneric.tools.specifier.Specifier;
32 import com.genimen.djeneric.tools.strongtyper.StrongTyper;
33 import com.genimen.djeneric.ui.DjSwingWorker;
34 import com.genimen.djeneric.ui.Util;
35 import com.genimen.djeneric.util.DjFileFilter;
36 import com.genimen.djeneric.util.DjLogger;
37 import com.genimen.djeneric.util.DjStringComparator;
38
39 public class MenuPanel extends JPanel JavaDoc
40 {
41   private static final long serialVersionUID = 1L;
42   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
43   JTabbedPane JavaDoc jTabbedPane1 = new JTabbedPane JavaDoc();
44   JPanel JavaDoc mainPanel = new JPanel JavaDoc();
45   JPanel JavaDoc adminPanel = new JPanel JavaDoc();
46   JPanel JavaDoc hsqlPanel = new JPanel JavaDoc();
47   JButton JavaDoc _butModeler = new JButton JavaDoc();
48   JButton JavaDoc _butSpecifier = new JButton JavaDoc();
49   JButton JavaDoc _butStrongTyper = new JButton JavaDoc();
50   JButton JavaDoc _butAdministrator = new JButton JavaDoc();
51   JButton JavaDoc _butLogbrowser = new JButton JavaDoc();
52   JLabel JavaDoc lblModelerDescr = new JLabel JavaDoc();
53   JLabel JavaDoc lblModeler = new JLabel JavaDoc();
54   JLabel JavaDoc lblSpecifierDescr = new JLabel JavaDoc();
55   JLabel JavaDoc lblSpecifier = new JLabel JavaDoc();
56   JLabel JavaDoc lblStrongTyper = new JLabel JavaDoc();
57   JLabel JavaDoc lblStrongTyperDescr = new JLabel JavaDoc();
58   JLabel JavaDoc lblAdminDescr = new JLabel JavaDoc();
59   JLabel JavaDoc lblAdmin = new JLabel JavaDoc();
60   JLabel JavaDoc lblLogBrowser = new JLabel JavaDoc();
61   JLabel JavaDoc lblLogBrowserDescr = new JLabel JavaDoc();
62   JLabel JavaDoc lblStartDb = new JLabel JavaDoc();
63   JButton JavaDoc _butStartdb = new JButton JavaDoc();
64   JButton JavaDoc _butStopdb = new JButton JavaDoc();
65   JLabel JavaDoc lblStartDbDescr = new JLabel JavaDoc();
66   JLabel JavaDoc lblStopDb = new JLabel JavaDoc();
67   JLabel JavaDoc lblStopDbDescr = new JLabel JavaDoc();
68
69   String JavaDoc _repositoriesFile;
70   JLabel JavaDoc lblConsoleDescr = new JLabel JavaDoc();
71   JButton JavaDoc _butConsole = new JButton JavaDoc();
72   JLabel JavaDoc lblConsole = new JLabel JavaDoc();
73   JPanel JavaDoc codingPanel = new JPanel JavaDoc();
74   JLabel JavaDoc lblOptions = new JLabel JavaDoc();
75   JButton JavaDoc _butOptions = new JButton JavaDoc();
76   JLabel JavaDoc lblOptionsDescr = new JLabel JavaDoc();
77
78   Properties JavaDoc _props;
79   JLabel JavaDoc _lblDbOnOff = new JLabel JavaDoc();
80   JLabel JavaDoc _lblDbStatus = new JLabel JavaDoc();
81
82   public MenuPanel()
83   {
84     try
85     {
86       jbInit();
87       updateDbStatus();
88     }
89     catch (Exception JavaDoc ex)
90     {
91       DjLogger.log(ex);
92     }
93   }
94
95   public void updateDbStatus()
96   {
97
98     boolean dbUp = isDbUp();
99     if (dbUp)
100     {
101       _lblDbStatus.setText(Messages.getString("MenuPanel.DatabaseIsUp"));
102       _lblDbOnOff.setIcon(getImageIcon("com/genimen/djeneric/tools/menu/images/dbup.gif"));
103     }
104     else
105     {
106       _lblDbStatus.setText(Messages.getString("MenuPanel.DatabaseIsDown"));
107       _lblDbOnOff.setIcon(getImageIcon("com/genimen/djeneric/tools/menu/images/dbdown.gif"));
108     }
109     _butStartdb.setEnabled(!dbUp);
110     _butStopdb.setEnabled(dbUp);
111   }
112
113   private boolean isDbUp()
114   {
115     try
116     {
117       Class.forName("org.hsqldb.jdbcDriver");
118       Connection JavaDoc conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost", "sa", "");
119       conn.close();
120       return true;
121     }
122     catch (SQLException JavaDoc e)
123     {
124       if (e.getErrorCode() == -80) return false;
125     }
126     catch (ClassNotFoundException JavaDoc e)
127     {
128       // Deliberate fall through
129
}
130     // Database is in unknown state; assume down
131
return false;
132   }
133
134   void jbInit() throws Exception JavaDoc
135   {
136     _butModeler.setIcon(getImageIcon("com/genimen/djeneric/tools/modeler/images/modeler.gif")); //$NON-NLS-1$
137
_butSpecifier.setIcon(getImageIcon("com/genimen/djeneric/tools/specifier/images/specifier.gif")); //$NON-NLS-1$
138
_butStrongTyper.setIcon(getImageIcon("com/genimen/djeneric/tools/strongtyper/images/strongtyper.gif")); //$NON-NLS-1$
139
_butAdministrator.setIcon(getImageIcon("com/genimen/djeneric/tools/administrator/images/admin.gif")); //$NON-NLS-1$
140
_butLogbrowser.setIcon(getImageIcon("com/genimen/djeneric/tools/logbrowser/images/logbrowser.gif")); //$NON-NLS-1$
141
_butStartdb.setIcon(getImageIcon("com/genimen/djeneric/tools/administrator/images/schema.gif")); //$NON-NLS-1$
142
_butStopdb.setIcon(getImageIcon("com/genimen/djeneric/tools/administrator/images/down.gif")); //$NON-NLS-1$
143
_butConsole.setIcon(getImageIcon("com/genimen/djeneric/tools/console/nodes/images/console.gif")); //$NON-NLS-1$
144
_butConsole.addActionListener(new MenuPanel__butConsole_actionAdapter(this)); //$NON-NLS-1$
145
_butOptions.setIcon(getImageIcon("com/genimen/djeneric/tools/menu/images/options.gif"));
146
147     _butStrongTyper.addActionListener(new MenuPanel__butStrongTyper_actionAdapter(this));
148     _butSpecifier.addActionListener(new MenuPanel__butSpecifier_actionAdapter(this));
149     _butModeler.addActionListener(new MenuPanel__butModeler_actionAdapter(this));
150     _butLogbrowser.addActionListener(new MenuPanel__butLogbrowser_actionAdapter(this));
151     _butAdministrator.addActionListener(new MenuPanel__butAdministrator_actionAdapter(this));
152
153     this.setLayout(borderLayout1);
154     mainPanel.setLayout(null);
155     adminPanel.setLayout(null);
156     hsqlPanel.setLayout(null);
157
158     _butAdministrator.setBounds(new Rectangle JavaDoc(9, 7, 24, 24));
159     _butLogbrowser.setBounds(new Rectangle JavaDoc(9, 40, 24, 24));
160     _butModeler.setBounds(new Rectangle JavaDoc(9, 7, 24, 24));
161     _butSpecifier.setBounds(new Rectangle JavaDoc(9, 40, 24, 24));
162     _butStrongTyper.setBounds(new Rectangle JavaDoc(9, 7, 24, 24));
163
164     lblModeler.setText(Messages.getString("MenuPanel.Modeler")); //$NON-NLS-1$
165
lblModeler.setBounds(new Rectangle JavaDoc(46, 4, 234, 16));
166     lblModelerDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
167
lblModelerDescr.setText(Messages.getString("MenuPanel.ModelerDescr")); //$NON-NLS-1$
168
lblModelerDescr.setBounds(new Rectangle JavaDoc(46, 15, 313, 22));
169
170     lblSpecifierDescr.setBounds(new Rectangle JavaDoc(46, 48, 313, 22));
171     lblSpecifierDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
172
lblSpecifierDescr.setText(Messages.getString("MenuPanel.SpecifierDescr")); //$NON-NLS-1$
173
lblSpecifier.setBounds(new Rectangle JavaDoc(46, 37, 234, 16));
174     lblSpecifier.setText(Messages.getString("MenuPanel.Specfier")); //$NON-NLS-1$
175

176     lblStrongTyper.setText(Messages.getString("MenuPanel.StrongTyper")); //$NON-NLS-1$
177
lblStrongTyper.setBounds(new Rectangle JavaDoc(46, 4, 234, 16));
178     lblStrongTyperDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
179
lblStrongTyperDescr.setText(Messages.getString("MenuPanel.StrongTyperDescr")); //$NON-NLS-1$
180
lblStrongTyperDescr.setBounds(new Rectangle JavaDoc(46, 15, 313, 22));
181
182     lblAdminDescr.setBounds(new Rectangle JavaDoc(46, 15, 313, 22));
183     lblAdminDescr.setText(Messages.getString("MenuPanel.AdminDescr")); //$NON-NLS-1$
184
lblAdminDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
185
lblAdmin.setBounds(new Rectangle JavaDoc(46, 4, 234, 16));
186     lblAdmin.setText(Messages.getString("MenuPanel.Admin")); //$NON-NLS-1$
187
lblLogBrowser.setText(Messages.getString("MenuPanel.LogBrowser")); //$NON-NLS-1$
188
lblLogBrowser.setBounds(new Rectangle JavaDoc(46, 37, 234, 16));
189     lblLogBrowserDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
190
lblLogBrowserDescr.setText(Messages.getString("MenuPanel.LogBrowserDescr")); //$NON-NLS-1$
191
lblLogBrowserDescr.setBounds(new Rectangle JavaDoc(46, 48, 313, 22));
192     lblStartDb.setText(Messages.getString("MenuPanel.StartDb")); //$NON-NLS-1$
193
lblStartDb.setBounds(new Rectangle JavaDoc(46, 4, 234, 16));
194     _butStartdb.setBounds(new Rectangle JavaDoc(9, 7, 24, 24));
195     _butStartdb.addActionListener(new MenuPanel__butStartdb_actionAdapter(this));
196     _butStopdb.setBounds(new Rectangle JavaDoc(9, 40, 24, 24));
197     _butStopdb.addActionListener(new MenuPanel__butStopdb_actionAdapter(this));
198     lblStartDbDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
199
lblStartDbDescr.setText(Messages.getString("MenuPanel.StartDbDescr")); //$NON-NLS-1$
200
lblStartDbDescr.setBounds(new Rectangle JavaDoc(46, 15, 313, 22));
201     lblStopDb.setBounds(new Rectangle JavaDoc(46, 37, 234, 16));
202     lblStopDb.setText(Messages.getString("MenuPanel.StopDb")); //$NON-NLS-1$
203
lblStopDbDescr.setBounds(new Rectangle JavaDoc(46, 48, 313, 22));
204     lblStopDbDescr.setText(Messages.getString("MenuPanel.StopDbDescr")); //$NON-NLS-1$
205
lblStopDbDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
206
lblConsoleDescr.setBounds(new Rectangle JavaDoc(46, 81, 313, 22));
207     lblConsoleDescr.setText(Messages.getString("MenuPanel.ConsoleDescr")); //$NON-NLS-1$
208
lblConsoleDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
209
_butConsole.setBounds(new Rectangle JavaDoc(9, 73, 24, 24));
210
211     lblConsole.setBounds(new Rectangle JavaDoc(46, 70, 234, 16));
212     lblConsole.setText(Messages.getString("MenuPanel.Console")); //$NON-NLS-1$
213
codingPanel.setLayout(null);
214     lblOptions.setText(Messages.getString("MenuPanel.Options"));
215     lblOptions.setBounds(new Rectangle JavaDoc(46, 70, 234, 16));
216     _butOptions.setBounds(new Rectangle JavaDoc(9, 73, 24, 24));
217     _butOptions.addActionListener(new MenuPanel__butOptions_actionAdapter(this));
218     lblOptionsDescr.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
219     lblOptionsDescr.setText(Messages.getString("MenuPanel.OptionsDescr"));
220     lblOptionsDescr.setBounds(new Rectangle JavaDoc(46, 81, 313, 22));
221     _lblDbOnOff.setPreferredSize(new Dimension JavaDoc(16, 16));
222     _lblDbOnOff.setBounds(new Rectangle JavaDoc(13, 88, 16, 16));
223     _lblDbStatus.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12)); //$NON-NLS-1$
224
_lblDbStatus.setBounds(new Rectangle JavaDoc(46, 85, 313, 22));
225     this.add(jTabbedPane1, BorderLayout.CENTER);
226     jTabbedPane1.add(mainPanel, Messages.getString("MenuPanel.Main")); //$NON-NLS-1$
227
jTabbedPane1.add(codingPanel, Messages.getString("MenuPanel.Coding")); //$NON-NLS-1$
228
jTabbedPane1.add(adminPanel, Messages.getString("MenuPanel.Administration")); //$NON-NLS-1$
229
jTabbedPane1.add(hsqlPanel, Messages.getString("MenuPanel.Hypersonic")); //$NON-NLS-1$
230
hsqlPanel.add(lblStartDbDescr, null);
231     hsqlPanel.add(_butStopdb, null);
232     hsqlPanel.add(lblStopDb, null);
233     hsqlPanel.add(lblStopDbDescr, null);
234     hsqlPanel.add(_butStartdb, null);
235     hsqlPanel.add(lblStartDb, null);
236     hsqlPanel.add(_lblDbOnOff);
237     hsqlPanel.add(_lblDbStatus);
238     mainPanel.add(_butModeler, null);
239     mainPanel.add(lblModelerDescr, null);
240     mainPanel.add(lblModeler, null);
241     mainPanel.add(lblSpecifier, null);
242     mainPanel.add(lblSpecifierDescr, null);
243     mainPanel.add(_butSpecifier, null);
244     codingPanel.add(lblStrongTyperDescr, null);
245     codingPanel.add(_butStrongTyper, null);
246     codingPanel.add(lblStrongTyper, null);
247     mainPanel.add(lblConsole, null);
248     mainPanel.add(lblConsoleDescr, null);
249     mainPanel.add(_butConsole, null);
250     adminPanel.add(lblAdminDescr, null);
251     adminPanel.add(lblAdmin, null);
252     adminPanel.add(_butAdministrator, null);
253     adminPanel.add(_butLogbrowser, null);
254     adminPanel.add(lblLogBrowserDescr, null);
255     adminPanel.add(lblLogBrowser, null);
256     adminPanel.add(lblOptionsDescr, null);
257     adminPanel.add(_butOptions, null);
258     adminPanel.add(lblOptions, null);
259   }
260
261   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
262   {
263     URL JavaDoc url = MenuPanel.class.getClassLoader().getResource(fileName);
264     if (url != null)
265     {
266       return new ImageIcon JavaDoc(url);
267     }
268     else
269     {
270       return new ImageIcon JavaDoc(""); //$NON-NLS-1$
271
}
272     // Default to empty image
273
}
274
275   void _butModeler_actionPerformed(ActionEvent JavaDoc e)
276   {
277     try
278     {
279       startTool(ModelEditor.class);
280     }
281     catch (IOException JavaDoc e1)
282     {
283       DjLogger.log(e1);
284     }
285   }
286
287   void _butSpecifier_actionPerformed(ActionEvent JavaDoc e)
288   {
289     try
290     {
291       startTool(Specifier.class);
292     }
293     catch (IOException JavaDoc e1)
294     {
295       DjLogger.log(e1);
296     }
297   }
298
299   void _butStrongTyper_actionPerformed(ActionEvent JavaDoc e)
300   {
301     try
302     {
303       startTool(StrongTyper.class);
304     }
305     catch (IOException JavaDoc e1)
306     {
307       DjLogger.log(e1);
308     }
309
310   }
311
312   void _butAdministrator_actionPerformed(ActionEvent JavaDoc e)
313   {
314     try
315     {
316       startTool(Administrator.class);
317     }
318     catch (IOException JavaDoc e1)
319     {
320       DjLogger.log(e1);
321     }
322
323   }
324
325   void _butLogbrowser_actionPerformed(ActionEvent JavaDoc e)
326   {
327     try
328     {
329       startTool(LogBrowser.class);
330     }
331     catch (IOException JavaDoc e1)
332     {
333       DjLogger.log(e1);
334     }
335
336   }
337
338   void _butStartdb_actionPerformed(ActionEvent JavaDoc e)
339   {
340     try
341     {
342       String JavaDoc workingDir = System.getProperty("user.dir"); //$NON-NLS-1$
343
File JavaDoc dbDir = new File JavaDoc(workingDir + "/db"); //$NON-NLS-1$
344

345       if (!dbDir.isDirectory())
346       {
347         dbDir = new File JavaDoc(workingDir + "/../db"); //$NON-NLS-1$
348
if (!dbDir.isDirectory()) dbDir = new File JavaDoc("db"); //$NON-NLS-1$
349
}
350
351       ArrayList JavaDoc lst = fetchDatabaseNames(dbDir);
352       ArrayList JavaDoc ext = fetchExternalDbs();
353       Iterator JavaDoc it = ext.iterator();
354       while (it.hasNext())
355       {
356         String JavaDoc nm = it.next().toString();
357         if (!lst.contains(nm)) lst.add(nm);
358       }
359       Collections.sort(lst, new DjStringComparator(false));
360
361       if (lst.isEmpty())
362       {
363         lst.add(dbDir.getCanonicalPath() + File.separator + Messages.getString("MenuPanel.myNewDb")); //$NON-NLS-1$
364
}
365       DatabaseSelector dbsel = new DatabaseSelector(Util.findFrame(this), lst);
366
367       if (!dbsel.wasCancelled())
368       {
369         startDb(dbsel.getSelectedName());
370         updateRecentDbList(dbsel.getSelectedName());
371       }
372
373     }
374     catch (IOException JavaDoc e1)
375     {
376       DjLogger.log(e1);
377     }
378   }
379
380   private void updateRecentDbList(String JavaDoc selectedName)
381   {
382     ArrayList JavaDoc current = fetchExternalDbs();
383     if (!current.contains(selectedName)) current.add(selectedName);
384     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
385     Iterator JavaDoc it = current.iterator();
386     while (it.hasNext())
387     {
388       if (sb.length() != 0) sb.append(",");
389       sb.append(it.next().toString());
390     }
391     _props.setProperty(Menu.MENU_RECENTDBS, sb.toString());
392   }
393
394   private ArrayList JavaDoc fetchExternalDbs()
395   {
396     ArrayList JavaDoc result = new ArrayList JavaDoc();
397
398     String JavaDoc externalDbs = _props.getProperty(Menu.MENU_RECENTDBS);
399     if (externalDbs != null)
400     {
401       StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(externalDbs, ",");
402       while (stok.hasMoreElements())
403       {
404         String JavaDoc dbName = stok.nextToken().trim();
405         if (!result.contains(dbName))
406         {
407           File JavaDoc testDb = new File JavaDoc(dbName + ".data");
408           if (testDb.exists()) result.add(dbName);
409           else
410           {
411             testDb = new File JavaDoc(dbName + ".properties");
412             if (testDb.exists()) result.add(dbName);
413           }
414         }
415       }
416     }
417     return result;
418   }
419
420   private ArrayList JavaDoc fetchDatabaseNames(File JavaDoc dbDir) throws IOException JavaDoc
421   {
422     ArrayList JavaDoc lst = new ArrayList JavaDoc();
423     if (dbDir.isDirectory())
424     {
425       File JavaDoc[] dbs = dbDir.listFiles(new DjFileFilter("data")); //$NON-NLS-1$
426
for (int i = 0; i < dbs.length; i++)
427       {
428         String JavaDoc nm = dbs[i].getCanonicalPath();
429         if (!dbs[i].isDirectory() && nm.toLowerCase().endsWith(".data")) //$NON-NLS-1$
430
{
431           nm = nm.substring(0, nm.length() - 5);
432           lst.add(nm);
433         }
434       }
435       Collections.sort(lst, new DjStringComparator(false));
436     }
437     return lst;
438   }
439
440   protected void startDb(String JavaDoc dbName)
441   {
442     ArrayList JavaDoc args = new ArrayList JavaDoc();
443     args.add("java"); //$NON-NLS-1$
444

445     addClassPath(args);
446
447     args.add("org.hsqldb.Server"); //$NON-NLS-1$
448
args.add("-database"); //$NON-NLS-1$
449
args.add(dbName);
450
451     new ToolRunner((String JavaDoc[]) args.toArray(new String JavaDoc[0]));
452     try
453     {
454       for (int i = 0; i < 5; i++)
455       {
456         Thread.sleep(200);
457         if (isDbUp())
458         {
459           updateDbStatus();
460           break;
461         }
462       }
463     }
464     catch (InterruptedException JavaDoc e)
465     {
466       DjLogger.log(e);
467     }
468   }
469
470   void _butStopdb_actionPerformed(ActionEvent JavaDoc e)
471   {
472     try
473     {
474       stopDB();
475     }
476     catch (IOException JavaDoc e1)
477     {
478       DjLogger.log(e1);
479     }
480
481   }
482
483   public void setRepositoriesFile(String JavaDoc repositoriesFile)
484   {
485     _repositoriesFile = repositoriesFile;
486   }
487
488   private void startTool(Class JavaDoc clazz) throws IOException JavaDoc
489   {
490     ArrayList JavaDoc args = new ArrayList JavaDoc();
491     args.add("java"); //$NON-NLS-1$
492

493     String JavaDoc vmArg = _props.getProperty(Menu.TOOL_VM_OPTIONS);
494     if (vmArg != null && vmArg.trim().length() > 0)
495     {
496       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(vmArg, " \t\n\r");
497       while (st.hasMoreElements())
498         args.add(st.nextToken());
499     }
500     addPrototypingOption(args);
501     addStripSpacesOption(args);
502     addLanguageOption(args);
503     addUserHomeOption(args);
504     addClassPath(args);
505     args.add(clazz.getName());
506     args.add(_repositoriesFile);
507
508     new ToolRunner((String JavaDoc[]) args.toArray(new String JavaDoc[0]));
509   }
510
511   private void stopDB() throws IOException JavaDoc
512   {
513     ArrayList JavaDoc args = new ArrayList JavaDoc();
514     args.add("java"); //$NON-NLS-1$
515

516     addClassPath(args);
517     args.add("org.hsqldb.util.ShutdownServer"); //$NON-NLS-1$
518
args.add("-user"); //$NON-NLS-1$
519
args.add("sa"); //$NON-NLS-1$
520
args.add("-shutdownarg"); //$NON-NLS-1$
521
args.add("COMPACT"); //$NON-NLS-1$
522

523     new ToolRunner((String JavaDoc[]) args.toArray(new String JavaDoc[0]), this);
524   }
525
526   protected void addClassPath(ArrayList JavaDoc args)
527   {
528     args.add("-cp"); //$NON-NLS-1$
529
args.add(System.getProperty("java.class.path")); //$NON-NLS-1$
530
}
531
532   protected void addPrototypingOption(ArrayList JavaDoc args)
533   {
534     String JavaDoc proto = _props.getProperty(Menu.TOOL_PROTOTYPING, "true"); //$NON-NLS-1$
535
if ("true".equalsIgnoreCase(proto)) args.add("-Ddjeneric.prototyping=" + proto); //$NON-NLS-1$
536
}
537
538   protected void addStripSpacesOption(ArrayList JavaDoc args)
539   {
540     String JavaDoc strip = _props.getProperty(Menu.TOOL_STRIPSPACES, "true"); //$NON-NLS-1$
541
if (strip != null && strip.trim().length() > 0) args.add("-Ddjeneric.stripspaces=" + strip); //$NON-NLS-1$
542
}
543
544   protected void addLanguageOption(ArrayList JavaDoc args)
545   {
546     String JavaDoc lang = _props.getProperty(Menu.TOOL_LANGUAGE); //$NON-NLS-1$
547
if (lang != null && lang.trim().length() > 0) args.add("-Ddjeneric.language=" + lang); //$NON-NLS-1$
548
}
549
550   protected void addUserHomeOption(ArrayList JavaDoc args)
551   {
552     String JavaDoc home = System.getProperty("user.home"); //$NON-NLS-1$
553
if (home == null) return;
554
555     args.add("-Duser.home=" + home); //$NON-NLS-1$
556
}
557
558   void _butConsole_actionPerformed(ActionEvent JavaDoc e)
559   {
560     try
561     {
562       startTool(Console.class);
563     }
564     catch (IOException JavaDoc e1)
565     {
566       DjLogger.log(e1);
567     }
568   }
569
570   void _butOptions_actionPerformed(ActionEvent JavaDoc e)
571   {
572     new OptionsDialog(Util.findFrame(this), _props);
573   }
574
575   public void setProps(Properties JavaDoc props)
576   {
577     _props = props;
578   }
579
580   public void toolExited()
581   {
582     updateDbStatus();
583   }
584
585 }
586
587 class MenuPanel__butModeler_actionAdapter implements java.awt.event.ActionListener JavaDoc
588 {
589   MenuPanel adaptee;
590
591   MenuPanel__butModeler_actionAdapter(MenuPanel adaptee)
592   {
593     this.adaptee = adaptee;
594   }
595
596   public void actionPerformed(ActionEvent JavaDoc e)
597   {
598     adaptee._butModeler_actionPerformed(e);
599   }
600 }
601
602 class MenuPanel__butSpecifier_actionAdapter implements java.awt.event.ActionListener JavaDoc
603 {
604   MenuPanel adaptee;
605
606   MenuPanel__butSpecifier_actionAdapter(MenuPanel adaptee)
607   {
608     this.adaptee = adaptee;
609   }
610
611   public void actionPerformed(ActionEvent JavaDoc e)
612   {
613     adaptee._butSpecifier_actionPerformed(e);
614   }
615 }
616
617 class MenuPanel__butStrongTyper_actionAdapter implements java.awt.event.ActionListener JavaDoc
618 {
619   MenuPanel adaptee;
620
621   MenuPanel__butStrongTyper_actionAdapter(MenuPanel adaptee)
622   {
623     this.adaptee = adaptee;
624   }
625
626   public void actionPerformed(ActionEvent JavaDoc e)
627   {
628     adaptee._butStrongTyper_actionPerformed(e);
629   }
630 }
631
632 class MenuPanel__butAdministrator_actionAdapter implements java.awt.event.ActionListener JavaDoc
633 {
634   MenuPanel adaptee;
635
636   MenuPanel__butAdministrator_actionAdapter(MenuPanel adaptee)
637   {
638     this.adaptee = adaptee;
639   }
640
641   public void actionPerformed(ActionEvent JavaDoc e)
642   {
643     adaptee._butAdministrator_actionPerformed(e);
644   }
645 }
646
647 class MenuPanel__butLogbrowser_actionAdapter implements java.awt.event.ActionListener JavaDoc
648 {
649   MenuPanel adaptee;
650
651   MenuPanel__butLogbrowser_actionAdapter(MenuPanel adaptee)
652   {
653     this.adaptee = adaptee;
654   }
655
656   public void actionPerformed(ActionEvent JavaDoc e)
657   {
658     adaptee._butLogbrowser_actionPerformed(e);
659   }
660 }
661
662 class MenuPanel__butStartdb_actionAdapter implements java.awt.event.ActionListener JavaDoc
663 {
664   MenuPanel adaptee;
665
666   MenuPanel__butStartdb_actionAdapter(MenuPanel adaptee)
667   {
668     this.adaptee = adaptee;
669   }
670
671   public void actionPerformed(ActionEvent JavaDoc e)
672   {
673     adaptee._butStartdb_actionPerformed(e);
674   }
675 }
676
677 class MenuPanel__butStopdb_actionAdapter implements java.awt.event.ActionListener JavaDoc
678 {
679   MenuPanel adaptee;
680
681   MenuPanel__butStopdb_actionAdapter(MenuPanel adaptee)
682   {
683     this.adaptee = adaptee;
684   }
685
686   public void actionPerformed(ActionEvent JavaDoc e)
687   {
688     adaptee._butStopdb_actionPerformed(e);
689   }
690 }
691
692 class ToolRunner extends Thread JavaDoc
693 {
694   String JavaDoc[] _args;
695   MenuPanel _menu;
696
697   public void run()
698   {
699     try
700     {
701       for (int i = 0; i < _args.length; i++)
702         System.out.print(_args[i] + " ");
703       System.out.println();
704
705       Process JavaDoc child = Runtime.getRuntime().exec(_args);
706
707       InputStreamReader JavaDoc inReader = new InputStreamReader JavaDoc(child.getInputStream());
708       InputStreamReader JavaDoc errReader = new InputStreamReader JavaDoc(child.getErrorStream());
709
710       final int EXIT_CHECK_COUNT = 50;
711
712       int someByte = 0;
713       int exitedCheck = EXIT_CHECK_COUNT;
714       while (someByte != -1)
715       {
716         while (inReader.ready())
717         {
718           someByte = inReader.read();
719           System.out.write(someByte);
720         }
721
722         while (errReader.ready())
723         {
724           someByte = errReader.read();
725           System.err.write(someByte);
726         }
727         try
728         {
729           Thread.sleep(100);
730         }
731         catch (InterruptedException JavaDoc e1)
732         {
733           DjLogger.log(e1);
734         }
735
736         if (exitedCheck-- == 0)
737         {
738           exitedCheck = EXIT_CHECK_COUNT;
739           try
740           {
741             child.exitValue();
742             someByte = -1;
743           }
744           catch (IllegalThreadStateException JavaDoc is)
745           {
746             // Ignore this one: child not yet exited
747
}
748         }
749       }
750       System.err.flush();
751       System.out.flush();
752
753       inReader.close();
754       errReader.close();
755
756       callBack();
757       DjLogger.log(Messages.getString("MenuPanel.ToolExited")); //$NON-NLS-1$
758
}
759     catch (IOException JavaDoc e)
760     {
761       DjLogger.log(Messages.getString("MenuPanel.ExecFailure") + e.getMessage()); //$NON-NLS-1$
762
}
763   }
764
765   private void callBack()
766   {
767     if (_menu != null)
768     {
769       DjSwingWorker worker = new DjSwingWorker()
770       {
771         public Object JavaDoc construct()
772         {
773           _menu.toolExited();
774           return null;
775         }
776
777         public void finished()
778         {
779         }
780       };
781       worker.start();
782     }
783   }
784
785   public ToolRunner(String JavaDoc[] args)
786   {
787     _args = args;
788     start();
789   }
790
791   public ToolRunner(String JavaDoc[] args, MenuPanel menu)
792   {
793     _args = args;
794     _menu = menu;
795     start();
796   }
797 }
798
799 class MenuPanel__butConsole_actionAdapter implements java.awt.event.ActionListener JavaDoc
800 {
801   MenuPanel adaptee;
802
803   MenuPanel__butConsole_actionAdapter(MenuPanel adaptee)
804   {
805     this.adaptee = adaptee;
806   }
807
808   public void actionPerformed(ActionEvent JavaDoc e)
809   {
810     adaptee._butConsole_actionPerformed(e);
811   }
812 }
813
814 class MenuPanel__butOptions_actionAdapter implements java.awt.event.ActionListener JavaDoc
815 {
816   MenuPanel adaptee;
817
818   MenuPanel__butOptions_actionAdapter(MenuPanel adaptee)
819   {
820     this.adaptee = adaptee;
821   }
822
823   public void actionPerformed(ActionEvent JavaDoc e)
824   {
825     adaptee._butOptions_actionPerformed(e);
826   }
827 }
828
Popular Tags