1 30 31 package com.genimen.djeneric.tools.console; 32 33 import java.awt.AWTEvent ; 34 import java.awt.BorderLayout ; 35 import java.awt.Color ; 36 import java.awt.Dimension ; 37 import java.awt.Image ; 38 import java.awt.Toolkit ; 39 import java.awt.event.InputEvent ; 40 import java.awt.event.KeyEvent ; 41 import java.awt.event.KeyListener ; 42 import java.awt.event.WindowEvent ; 43 import java.net.URL ; 44 import java.util.ArrayList ; 45 46 import javax.swing.ImageIcon ; 47 import javax.swing.JLabel ; 48 import javax.swing.JOptionPane ; 49 import javax.swing.JPanel ; 50 import javax.swing.JScrollPane ; 51 import javax.swing.JSplitPane ; 52 import javax.swing.JTabbedPane ; 53 import javax.swing.JTextArea ; 54 import javax.swing.JTextField ; 55 import javax.swing.JTree ; 56 import javax.swing.SwingConstants ; 57 import javax.swing.event.ChangeEvent ; 58 import javax.swing.event.TreeExpansionEvent ; 59 import javax.swing.event.TreeSelectionEvent ; 60 import javax.swing.tree.DefaultMutableTreeNode ; 61 import javax.swing.tree.DefaultTreeModel ; 62 import javax.swing.tree.TreePath ; 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 , ConsoleExitListener 83 { 84 private static final long serialVersionUID = 1L; 85 int _maxRows = 100; 86 int _maxColSize = 100; 87 88 private final String PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("console.properties"); 89 private final static String LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("console.log"); 90 91 Color _normalColor = new Color (102, 102, 153); 92 93 ArrayList _allStatements = new ArrayList (); 94 int _currentStatementIdx = 0; 95 96 JPanel contentPane; 97 JLabel _lblStatus = new JLabel (); 98 BorderLayout borderLayout1 = new BorderLayout (); 99 JTabbedPane _tabs = new JTabbedPane (); 100 JPanel _pnlStructures = new JPanel (); 101 ConsolePanel _pnlSqlConsole; 102 JPanel _pnlOptions = new JPanel (); 103 BorderLayout borderLayout2 = new BorderLayout (); 104 JSplitPane _splitStructure = new JSplitPane (); 105 106 RootNode _root = new RootNode(); 107 DefaultTreeModel _treeModel = new DefaultTreeModel (_root); 108 CustomTreeCellRenderer _treeCellRendrer = new CustomTreeCellRenderer(); 109 110 BorderLayout borderLayout5 = new BorderLayout (); 111 JPanel jPanel4 = new JPanel (); 112 JPanel jPanel5 = new JPanel (); 113 JLabel jLabel1 = new JLabel (); 114 JLabel jLabel2 = new JLabel (); 115 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 116 JTextField _edtMaxRows = new JTextField (); 117 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 118 JTextField _edtMaxColSize = new JTextField (); 119 JScrollPane jScrollPane1 = new JScrollPane (); 120 JTextArea _edtDetails = new JTextArea (); 121 JScrollPane jScrollPane2 = new JScrollPane (); 122 JTree _tree = new JTree (); 123 124 130 public Console(String 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 (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 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 _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 (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 e) 182 { 183 handleException(e); 184 System.exit(-1); 185 } 186 } 187 188 192 public DjContext selectContext(DjContext[] ctxts) throws LogonException 193 { 194 ContextDialog cd = new ContextDialog(this, ctxts); 195 return cd.getContext(); 196 } 197 198 199 205 private void jbInit() throws Exception 206 { 207 contentPane = (JPanel ) this.getContentPane(); 209 contentPane.setLayout(borderLayout1); 210 this.setSize(new Dimension (552, 384)); 211 this.addWindowListener(new java.awt.event.WindowAdapter () 212 { 213 public void windowOpened(WindowEvent 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 (70, 21)); 225 _edtMaxRows.setToolTipText("100"); 226 _edtMaxRows.setText("200"); 227 _edtMaxRows.setHorizontalAlignment(SwingConstants.RIGHT); 228 _edtMaxRows.addKeyListener(new java.awt.event.KeyAdapter () 229 { 230 public void keyReleased(KeyEvent e) 231 { 232 _edtMaxRows_keyReleased(e); 233 } 234 }); 235 jPanel5.setLayout(verticalFlowLayout2); 236 verticalFlowLayout2.setHorizontalFill(false); 237 _edtMaxColSize.setPreferredSize(new Dimension (70, 21)); 238 _edtMaxColSize.setText("50"); 239 _edtMaxColSize.setHorizontalAlignment(SwingConstants.RIGHT); 240 _edtMaxColSize.addKeyListener(new java.awt.event.KeyAdapter () 241 { 242 public void keyReleased(KeyEvent e) 243 { 244 _edtMaxColSize_keyReleased(e); 245 } 246 }); 247 _edtDetails.setFont(new java.awt.Font ("Monospaced", 0, 11)); 248 _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener () 249 { 250 public void treeWillExpand(TreeExpansionEvent e) 251 { 252 _tree_treeWillExpand(e); 253 } 254 255 public void treeWillCollapse(TreeExpansionEvent e) 256 { 257 } 258 }); 259 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 260 { 261 public void valueChanged(TreeSelectionEvent e) 262 { 263 _tree_valueChanged(e); 264 } 265 }); 266 _tabs.addChangeListener(new javax.swing.event.ChangeListener () 267 { 268 public void stateChanged(ChangeEvent 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 310 protected void processWindowEvent(WindowEvent e) 311 { 312 super.processWindowEvent(e); 313 if (e.getID() == WindowEvent.WINDOW_CLOSING) 314 { 315 exitProgram(); 316 } 317 } 318 319 void _tree_treeWillExpand(TreeExpansionEvent e) 320 { 321 if (e.getPath() == null) return; 322 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (e.getPath().getLastPathComponent()); 323 324 try 325 { 326 if (node instanceof CustomTreeNode) ((CustomTreeNode) node).expandNode(); 327 } 328 catch (Exception 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 result = new ArrayList (10); 338 339 TreePath [] tp = _tree.getSelectionPaths(); 340 341 for (int qq = 0; qq < tp.length; qq++) 342 { 343 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (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 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 getImageIcon(String fileName) 370 { 371 URL url = Console.class.getResource("nodes/images/" + fileName); 372 if (url != null) return new ImageIcon (url); 373 else return new ImageIcon (""); 374 } 376 377 public static Image getImage(String fileName) 378 { 379 URL 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 e) 386 { 387 _pnlSqlConsole.requestFocus(); 388 } 389 390 void _tree_treeWillCollapse(TreeExpansionEvent e) 391 { 392 } 393 394 void _edtMaxColSize_keyReleased(KeyEvent e) 395 { 396 try 397 { 398 _pnlSqlConsole.setMaxColumnSize(Integer.parseInt(_edtMaxColSize.getText())); 399 } 400 catch (Exception x) 401 { 402 } 404 } 405 406 void _edtMaxRows_keyReleased(KeyEvent e) 407 { 408 try 409 { 410 _pnlSqlConsole.setMaxRowsToFetch(Integer.parseInt(_edtMaxRows.getText())); 411 } 412 catch (Exception x) 413 { 414 } 416 } 417 418 void _tabs_stateChanged(ChangeEvent e) 419 { 420 if (_tabs.getSelectedComponent() == _pnlSqlConsole) 421 { 422 _pnlSqlConsole.requestFocus(); 423 } 424 } 425 426 public void keyTyped(KeyEvent e) 427 { 428 } 429 430 public void keyPressed(KeyEvent 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 e) 451 { 452 } 453 454 public void consoleExit() 455 { 456 exitProgram(); 457 } 458 459 public static void main(String [] 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 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 e) 478 { 479 DjLogger.log(e); 480 } 481 } 482 } | Popular Tags |