KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > console > Console


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.console;
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.Image JavaDoc;
38 import java.awt.Toolkit JavaDoc;
39 import java.awt.event.InputEvent JavaDoc;
40 import java.awt.event.KeyEvent JavaDoc;
41 import java.awt.event.KeyListener JavaDoc;
42 import java.awt.event.WindowEvent JavaDoc;
43 import java.net.URL JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 import javax.swing.ImageIcon JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JOptionPane JavaDoc;
49 import javax.swing.JPanel JavaDoc;
50 import javax.swing.JScrollPane JavaDoc;
51 import javax.swing.JSplitPane JavaDoc;
52 import javax.swing.JTabbedPane JavaDoc;
53 import javax.swing.JTextArea JavaDoc;
54 import javax.swing.JTextField JavaDoc;
55 import javax.swing.JTree JavaDoc;
56 import javax.swing.SwingConstants JavaDoc;
57 import javax.swing.event.ChangeEvent JavaDoc;
58 import javax.swing.event.TreeExpansionEvent JavaDoc;
59 import javax.swing.event.TreeSelectionEvent JavaDoc;
60 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
61 import javax.swing.tree.DefaultTreeModel JavaDoc;
62 import javax.swing.tree.TreePath JavaDoc;
63
64 import com.genimen.djeneric.language.Messages;
65 import com.genimen.djeneric.repository.DjContext;
66 import com.genimen.djeneric.repository.DjPersistenceManager;
67 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
68 import com.genimen.djeneric.repository.exceptions.LogonException;
69 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager;
70 import com.genimen.djeneric.tools.common.ContextDialog;
71 import com.genimen.djeneric.tools.common.DjenericTool;
72 import com.genimen.djeneric.tools.console.nodes.CustomTreeCellRenderer;
73 import com.genimen.djeneric.tools.console.nodes.CustomTreeNode;
74 import com.genimen.djeneric.tools.console.nodes.RootNode;
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.DjLogPrintStream;
79 import com.genimen.djeneric.util.DjLogger;
80 import com.genimen.djeneric.util.DjVersion;
81
82 public class Console extends DjenericTool implements KeyListener JavaDoc, ConsoleExitListener
83 {
84   private static final long serialVersionUID = 1L;
85   int _maxRows = 100;
86   int _maxColSize = 100;
87
88   private final String JavaDoc PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("console.properties");
89   private final static String JavaDoc LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("console.log");
90
91   Color JavaDoc _normalColor = new Color JavaDoc(102, 102, 153);
92
93   ArrayList JavaDoc _allStatements = new ArrayList JavaDoc();
94   int _currentStatementIdx = 0;
95
96   JPanel JavaDoc contentPane;
97   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
98   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
99   JTabbedPane JavaDoc _tabs = new JTabbedPane JavaDoc();
100   JPanel JavaDoc _pnlStructures = new JPanel JavaDoc();
101   ConsolePanel _pnlSqlConsole;
102   JPanel JavaDoc _pnlOptions = new JPanel JavaDoc();
103   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
104   JSplitPane JavaDoc _splitStructure = new JSplitPane JavaDoc();
105
106   RootNode _root = new RootNode();
107   DefaultTreeModel JavaDoc _treeModel = new DefaultTreeModel JavaDoc(_root);
108   CustomTreeCellRenderer _treeCellRendrer = new CustomTreeCellRenderer();
109
110   BorderLayout JavaDoc borderLayout5 = new BorderLayout JavaDoc();
111   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
112   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
113   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
114   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
115   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
116   JTextField JavaDoc _edtMaxRows = new JTextField JavaDoc();
117   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
118   JTextField JavaDoc _edtMaxColSize = new JTextField JavaDoc();
119   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
120   JTextArea JavaDoc _edtDetails = new JTextArea JavaDoc();
121   JScrollPane JavaDoc jScrollPane2 = new JScrollPane JavaDoc();
122   JTree JavaDoc _tree = new JTree JavaDoc();
123
124   /**
125    * Construct the frame
126    *
127    * @param repositories
128    * Description of the Parameter
129    */

130   public Console(String JavaDoc repositories)
131   {
132     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
133     try
134     {
135       setStatusLabel(_lblStatus);
136       setIconImage(getImage("console.gif"));
137       _pnlSqlConsole = new ConsolePanel();
138
139       DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(this, repositories);
140       DjPersistenceManager mgr = fact.createInstance();
141
142       if (!(mgr instanceof RdbmsPersistenceManager)) throw new Exception JavaDoc(Messages.getString("Console.RdbmsOnly", mgr
143           .getClass().getName()));
144
145       RdbmsPersistenceManager p = (RdbmsPersistenceManager) mgr;
146
147       try
148       {
149         p.determineContext();
150       }
151       catch (Exception JavaDoc x)
152       {
153         JOptionPane.showMessageDialog(this, Messages.getString("Console.GlobalMode"));
154       }
155
156       _pnlSqlConsole.setManager(p);
157       _pnlSqlConsole.addExitListener(this);
158
159       jbInit();
160
161       loadProps(PROPERTIES_FILE_NAME);
162
163       _edtMaxColSize.setText(getProperty("options.maxcolsize", "50"));
164       _edtMaxRows.setText(getProperty("options.maxrows", "100"));
165
166       _edtMaxRows_keyReleased(null);
167       // make current values known to the sqlconsole panel
168
_edtMaxColSize_keyReleased(null);
169
170       _pnlSqlConsole.setShowInternal(getProperty("options.showinternal", "false").equals("true"));
171       _pnlSqlConsole.setVirtualMode(getProperty("options.virtualmode", "true").equals("true"));
172
173       int width = Integer.parseInt(getProperty("window.width", "400"));
174       int height = Integer.parseInt(getProperty("window.height", "400"));
175       setSize(new Dimension JavaDoc(width, height));
176       _pnlSqlConsole.setDividerLocation(Integer.parseInt(getProperty("window.dividerloc", "100")));
177       _splitStructure.setDividerLocation(Integer.parseInt(getProperty("window.structuredividerloc", "100")));
178       this.addKeyListener(this);
179       setTitle("Virtual Database Console v" + DjVersion.getVersion() + " - " + mgr.getConnectionTitle());
180     }
181     catch (Exception JavaDoc e)
182     {
183       handleException(e);
184       System.exit(-1);
185     }
186   }
187
188   /**
189    * We override the method here to be able to cancel the context selection (so we can enter
190    * the global mode)
191    */

192   public DjContext selectContext(DjContext[] ctxts) throws LogonException
193   {
194     ContextDialog cd = new ContextDialog(this, ctxts);
195     return cd.getContext();
196   }
197
198   
199   /**
200    * Component initialization
201    *
202    * @exception Exception
203    * Description of the Exception
204    */

205   private void jbInit() throws Exception JavaDoc
206   {
207     //setIconImage(Toolkit.getDefaultToolkit().createImage(Console.class.getResource("[Your Icon]")));
208
contentPane = (JPanel JavaDoc) this.getContentPane();
209     contentPane.setLayout(borderLayout1);
210     this.setSize(new Dimension JavaDoc(552, 384));
211     this.addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
212     {
213       public void windowOpened(WindowEvent JavaDoc e)
214       {
215         this_windowOpened(e);
216       }
217     });
218     _lblStatus.setText(" ");
219     _pnlStructures.setLayout(borderLayout2);
220     _pnlOptions.setLayout(borderLayout5);
221     jLabel1.setText(Messages.getString("Console.MaxFetch"));
222     jLabel2.setText(Messages.getString("Console.MaxColSize"));
223     jPanel4.setLayout(verticalFlowLayout1);
224     _edtMaxRows.setPreferredSize(new Dimension JavaDoc(70, 21));
225     _edtMaxRows.setToolTipText("100");
226     _edtMaxRows.setText("200");
227     _edtMaxRows.setHorizontalAlignment(SwingConstants.RIGHT);
228     _edtMaxRows.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
229     {
230       public void keyReleased(KeyEvent JavaDoc e)
231       {
232         _edtMaxRows_keyReleased(e);
233       }
234     });
235     jPanel5.setLayout(verticalFlowLayout2);
236     verticalFlowLayout2.setHorizontalFill(false);
237     _edtMaxColSize.setPreferredSize(new Dimension JavaDoc(70, 21));
238     _edtMaxColSize.setText("50");
239     _edtMaxColSize.setHorizontalAlignment(SwingConstants.RIGHT);
240     _edtMaxColSize.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
241     {
242       public void keyReleased(KeyEvent JavaDoc e)
243       {
244         _edtMaxColSize_keyReleased(e);
245       }
246     });
247     _edtDetails.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 11));
248     _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener JavaDoc()
249     {
250       public void treeWillExpand(TreeExpansionEvent JavaDoc e)
251       {
252         _tree_treeWillExpand(e);
253       }
254
255       public void treeWillCollapse(TreeExpansionEvent JavaDoc e)
256       {
257       }
258     });
259     _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc()
260     {
261       public void valueChanged(TreeSelectionEvent JavaDoc e)
262       {
263         _tree_valueChanged(e);
264       }
265     });
266     _tabs.addChangeListener(new javax.swing.event.ChangeListener JavaDoc()
267     {
268       public void stateChanged(ChangeEvent JavaDoc e)
269       {
270         _tabs_stateChanged(e);
271       }
272     });
273     contentPane.add(_lblStatus, BorderLayout.SOUTH);
274     contentPane.add(_tabs, BorderLayout.CENTER);
275     _pnlStructures.add(_splitStructure, BorderLayout.CENTER);
276     jPanel4.add(jLabel1, null);
277     jPanel4.add(jLabel2, null);
278     _pnlOptions.add(jPanel5, BorderLayout.CENTER);
279     _pnlOptions.add(jPanel4, BorderLayout.WEST);
280     jPanel5.add(_edtMaxRows, null);
281     jPanel5.add(_edtMaxColSize, null);
282     _splitStructure.add(jScrollPane2, JSplitPane.LEFT);
283     _splitStructure.add(jScrollPane1, JSplitPane.RIGHT);
284     jScrollPane2.getViewport().add(_tree, null);
285     jScrollPane1.getViewport().add(_edtDetails, null);
286     _tabs.add(_pnlSqlConsole, Messages.getString("Console.SqlConsole"));
287     _tabs.add(_pnlStructures, Messages.getString("Console.Structures"));
288     _tabs.add(_pnlOptions, Messages.getString("global.Options"));
289     _splitStructure.setDividerLocation(150);
290
291     _tree.setModel(_treeModel);
292     _tree.setShowsRootHandles(true);
293     _tree.setAutoscrolls(true);
294     _tree.setDoubleBuffered(true);
295     _tree.setCellRenderer(_treeCellRendrer);
296
297     _root.setTree(_tree);
298     _root.setModel(_treeModel);
299     _root.setSession(_pnlSqlConsole.getSession());
300     _root.insertAsChild(new RootNode());
301     Util.sizeLabels(jPanel4);
302   }
303
304   /**
305    * Overridden so we can exit when window is closed
306    *
307    * @param e
308    * Description of the Parameter
309    */

310   protected void processWindowEvent(WindowEvent JavaDoc e)
311   {
312     super.processWindowEvent(e);
313     if (e.getID() == WindowEvent.WINDOW_CLOSING)
314     {
315       exitProgram();
316     }
317   }
318
319   void _tree_treeWillExpand(TreeExpansionEvent JavaDoc e)
320   {
321     if (e.getPath() == null) return;
322     DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) (e.getPath().getLastPathComponent());
323
324     try
325     {
326       if (node instanceof CustomTreeNode) ((CustomTreeNode) node).expandNode();
327     }
328     catch (Exception JavaDoc x)
329     {
330       DjLogger.log(x);
331     }
332   }
333
334   public CustomTreeNode[] getSelectedNodes()
335   {
336     if (_tree.getSelectionPath() == null) return new CustomTreeNode[0];
337     ArrayList JavaDoc result = new ArrayList JavaDoc(10);
338
339     TreePath JavaDoc[] tp = _tree.getSelectionPaths();
340
341     for (int qq = 0; qq < tp.length; qq++)
342     {
343       DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) (tp[qq].getLastPathComponent());
344       if (node instanceof CustomTreeNode) result.add(node);
345     }
346     return (CustomTreeNode[]) result.toArray(new CustomTreeNode[0]);
347   }
348
349   void _tree_valueChanged(TreeSelectionEvent JavaDoc e)
350   {
351     CustomTreeNode[] selected = getSelectedNodes();
352     if (selected.length > 0) _edtDetails.setText(selected[0].getDetailDescription());
353   }
354
355   protected void updateProperties()
356   {
357     setProperty("options.maxcolsize", _edtMaxColSize.getText());
358     setProperty("options.maxrows", _edtMaxRows.getText());
359
360     setProperty("options.showinternal", "" + _pnlSqlConsole.isShowInternal());
361     setProperty("options.virtualmode", "" + _pnlSqlConsole.isVirtualMode());
362
363     setProperty("window.width", "" + getWidth());
364     setProperty("window.height", "" + getHeight());
365     setProperty("window.dividerloc", "" + _pnlSqlConsole.getDividerLocation());
366     setProperty("window.structuredividerloc", "" + _splitStructure.getDividerLocation());
367   }
368
369   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
370   {
371     URL JavaDoc url = Console.class.getResource("nodes/images/" + fileName);
372     if (url != null) return new ImageIcon JavaDoc(url);
373     else return new ImageIcon JavaDoc("");
374     // Default to empty image
375
}
376
377   public static Image JavaDoc getImage(String JavaDoc fileName)
378   {
379     URL JavaDoc url = Console.class.getResource("nodes/images/" + fileName);
380     if (url != null) return Toolkit.getDefaultToolkit().getImage(url);
381
382     else return Toolkit.getDefaultToolkit().getImage("");
383   }
384
385   void this_windowOpened(WindowEvent JavaDoc e)
386   {
387     _pnlSqlConsole.requestFocus();
388   }
389
390   void _tree_treeWillCollapse(TreeExpansionEvent JavaDoc e)
391   {
392   }
393
394   void _edtMaxColSize_keyReleased(KeyEvent JavaDoc e)
395   {
396     try
397     {
398       _pnlSqlConsole.setMaxColumnSize(Integer.parseInt(_edtMaxColSize.getText()));
399     }
400     catch (Exception JavaDoc x)
401     {
402       // ignore invalid numbers
403
}
404   }
405
406   void _edtMaxRows_keyReleased(KeyEvent JavaDoc e)
407   {
408     try
409     {
410       _pnlSqlConsole.setMaxRowsToFetch(Integer.parseInt(_edtMaxRows.getText()));
411     }
412     catch (Exception JavaDoc x)
413     {
414       // ignore invalid numbers
415
}
416   }
417
418   void _tabs_stateChanged(ChangeEvent JavaDoc e)
419   {
420     if (_tabs.getSelectedComponent() == _pnlSqlConsole)
421     {
422       _pnlSqlConsole.requestFocus();
423     }
424   }
425
426   public void keyTyped(KeyEvent JavaDoc e)
427   {
428   }
429
430   public void keyPressed(KeyEvent JavaDoc e)
431   {
432     if (_tabs.getComponentCount() == 0) return;
433     if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0)
434         && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
435     {
436       int idx = _tabs.getSelectedIndex();
437       if (idx > 0) idx--;
438       else idx = _tabs.getComponentCount() - 1;
439       _tabs.setSelectedIndex(idx);
440     }
441     else if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
442     {
443       int idx = _tabs.getSelectedIndex();
444       if (idx < _tabs.getComponentCount() - 1) idx++;
445       else idx = 0;
446       _tabs.setSelectedIndex(idx);
447     }
448   }
449
450   public void keyReleased(KeyEvent JavaDoc e)
451   {
452   }
453
454   public void consoleExit()
455   {
456     exitProgram();
457   }
458
459   public static void main(String JavaDoc[] args)
460   {
461     try
462     {
463       DjenericTool.setLookAndFeel();
464
465       if (args.length < 1)
466       {
467         System.out.println(Messages.getString("global.Usage", Console.class.getName()));
468         return;
469       }
470
471       String JavaDoc banner = Console.class.getName() + Messages.getString("Console.Version") + " " + DjVersion.getVersion();
472       DjLogPrintStream.logAll(banner, LOG_FILE_NAME);
473
474       System.out.println(Messages.getString("global.Reading", args[0]));
475       new Console(args[0]).startApp();
476     }
477     catch (Exception JavaDoc e)
478     {
479       DjLogger.log(e);
480     }
481   }
482 }
Popular Tags