001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.util;
032:
033: import java.io.IOException;
034: import java.sql.Connection;
035: import java.sql.DriverManager;
036: import java.sql.SQLException;
037: import java.awt.BorderLayout;
038: import java.awt.Button;
039: import java.awt.CardLayout;
040: import java.awt.Dimension;
041: import java.awt.FileDialog;
042: import java.awt.Font;
043: import java.awt.Frame;
044: import java.awt.GridLayout;
045: import java.awt.Insets;
046: import java.awt.Menu;
047: import java.awt.MenuBar;
048: import java.awt.MenuItem;
049: import java.awt.Panel;
050: import java.awt.TextArea;
051: import java.awt.Toolkit;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.awt.event.KeyEvent;
055: import java.awt.event.KeyListener;
056: import java.awt.event.WindowListener;
057: import java.awt.image.MemoryImageSource;
058:
059: import org.hsqldb.lib.java.JavaSystem;
060:
061: /**
062: * Class declaration
063: *
064: *
065: * @version 1.0.0.1
066: * @author ulrivo@users
067: *
068: */
069: public class ZaurusDatabaseManager extends DatabaseManager implements
070: ActionListener, WindowListener, KeyListener {
071:
072: // (ulrivo): new buttons to switch the cards
073: Button butTree;
074: Button butCommand;
075: Button butResult;
076: Button butEditor;
077:
078: // (ulrivo): Panel pCard with CardLayout inside Frame fMain
079: Panel pCard;
080: CardLayout layoutCard;
081:
082: // the editor/input form
083: ZaurusEditor eEditor;
084:
085: // (ulrivo): variables set by arguments from the commandline
086: static String defDriver;
087: static String defURL;
088: static String defUser;
089: static String defPassword;
090: static String defQuery;
091: static String defDirectory;
092: static String defDatabase;
093: static int defWidth = 237;
094: static int defHeight = 259;
095: static int defLocX = 0;
096: static int defLocY = 0;
097:
098: /**
099: * Method declaration
100: *
101: *
102: * @param c
103: */
104: public void connect(Connection c) {
105:
106: if (c == null) {
107: return;
108: }
109:
110: if (cConn != null) {
111: try {
112: cConn.close();
113: } catch (SQLException e) {
114: }
115: }
116:
117: cConn = c;
118:
119: try {
120: dMeta = cConn.getMetaData();
121: sStatement = cConn.createStatement();
122: } catch (SQLException e) {
123: e.printStackTrace();
124: }
125:
126: refreshTree();
127: }
128:
129: /**
130: * Method declaration
131: *
132: *
133: * @param arg
134: */
135: public static void main(String[] arg) {
136:
137: bMustExit = true;
138:
139: // (ulrivo): read all arguments from the command line
140: int i = 0;
141:
142: while (i < arg.length) {
143: if (arg[i].equalsIgnoreCase("-driver")
144: && (i + 1 < arg.length)) {
145: i++;
146:
147: defDriver = arg[i];
148: } else if (arg[i].equalsIgnoreCase("-url")
149: && (i + 1 < arg.length)) {
150: i++;
151:
152: defURL = arg[i];
153: } else if (arg[i].equalsIgnoreCase("-width")
154: && (i + 1 < arg.length)) {
155: i++;
156:
157: try {
158: defWidth = Integer.parseInt(arg[i]);
159: } catch (Exception e) {
160: }
161: } else if (arg[i].equalsIgnoreCase("-height")
162: && (i + 1 < arg.length)) {
163: i++;
164:
165: try {
166: defHeight = Integer.parseInt(arg[i]);
167: } catch (Exception e) {
168: }
169: } else if (arg[i].equalsIgnoreCase("-locx")
170: && (i + 1 < arg.length)) {
171: i++;
172:
173: try {
174: defLocX = Integer.parseInt(arg[i]);
175: } catch (Exception e) {
176: }
177: } else if (arg[i].equalsIgnoreCase("-locy")
178: && (i + 1 < arg.length)) {
179: i++;
180:
181: try {
182: defLocY = Integer.parseInt(arg[i]);
183: } catch (Exception e) {
184: }
185: } else if (arg[i].equalsIgnoreCase("-user")
186: && (i + 1 < arg.length)) {
187: i++;
188:
189: defUser = arg[i];
190: } else if (arg[i].equalsIgnoreCase("-password")
191: && (i + 1 < arg.length)) {
192: i++;
193:
194: defPassword = arg[i];
195: } else if (arg[i].equalsIgnoreCase("-query")
196: && (i + 1 < arg.length)) {
197: i++;
198:
199: defQuery = arg[i];
200: } else if (arg[i].equalsIgnoreCase("-defDirectory")
201: && (i + 1 < arg.length)) {
202: i++;
203:
204: defDirectory = arg[i];
205: } else if (arg[i].equalsIgnoreCase("-database")
206: && (i + 1 < arg.length)) {
207: i++;
208:
209: defDatabase = arg[i];
210: } else {
211: showUsage();
212:
213: return;
214: }
215:
216: i++;
217: }
218:
219: ZaurusDatabaseManager m = new ZaurusDatabaseManager();
220:
221: m.main();
222:
223: // (ulrivo): make default connection if arguments set via the command line
224: Connection c = null;
225:
226: if ((defDriver != null && defURL != null)
227: || (defDatabase != null)) {
228: if (defDatabase != null) {
229: defDriver = "org.hsqldb.jdbcDriver";
230: defURL = "jdbc:hsqldb:" + defDatabase;
231: defUser = "sa";
232: defPassword = "";
233: }
234:
235: try {
236: Class.forName(defDriver).newInstance();
237:
238: c = DriverManager.getConnection(defURL, defUser,
239: defPassword);
240: } catch (Exception e) {
241: System.out.println("No connection for " + defDriver
242: + " at " + defURL);
243: e.printStackTrace();
244: }
245: } else {
246: c = ZaurusConnectionDialog.createConnection(m.fMain,
247: "Connect", new Insets(defWidth, defHeight, defLocX,
248: defLocY));
249: }
250:
251: if (c == null) {
252: return;
253: }
254:
255: m.connect(c);
256: }
257:
258: private static void showUsage() {
259:
260: System.out
261: .println("Usage: java org.hsqldb.util.ZaurusDatabaseManager [options]");
262: System.out.println("where options could be:");
263: System.out
264: .println("If the next two options are set, the specified connection will be used:");
265: System.out.println(" -driver dr");
266: System.out.println(" -url address");
267: System.out.println("-user name");
268: System.out.println("-password passw");
269: System.out
270: .println("Alternative the database argument is used,");
271: System.out
272: .println("and the hsqldb Driver Standalone is choosen for user 'sa'.");
273: System.out.println("-database db");
274: System.out
275: .println("-query qu the query qu will be read during initialization");
276: System.out
277: .println("-defaultDirectory defdir default dir for the file open dialog");
278: System.out
279: .println("If the next two options are set, the frame will be set to the specified values:");
280: System.out.println(" -width width");
281: System.out.println(" -height height");
282: System.out.println("-locX positon left ");
283: System.out.println("-locY positon top ");
284: System.out.println("");
285: System.out
286: .println("1. Example: java org.hsqldb.util.ZaurusDatabaseManager +");
287: System.out.println(" -driver 'org.hsqldb.jdbcDriver' +");
288: System.out.println(" -url 'jdbc:hsqldb:test'");
289: System.out
290: .println("2. Example: java org.hsqldb.util.ZaurusDatabaseManager +");
291: System.out.println(" -database 'test'");
292: }
293:
294: /**
295: * Method declaration
296: *
297: */
298: public void main() {
299:
300: fMain = new Frame("HSQLDB Database Manager for Zaurus");
301: imgEmpty = createImage(new MemoryImageSource(2, 2,
302: new int[4 * 4], 2, 2));
303:
304: fMain.setIconImage(imgEmpty);
305: fMain.addWindowListener(this );
306:
307: MenuBar bar = new MenuBar();
308:
309: // no shortcuts used
310: String[] fitems = { "-Connect...", "--", "-Open Script...",
311: "-Save Script...", "-Save Result...", "--", "-Exit" };
312:
313: addMenu(bar, "File", fitems);
314:
315: String[] vitems = { "-Refresh Tree", "--", "-View Tree",
316: "-View Command", "-View Result", "-View Editor", "--",
317: "-Results in Grid", "-Results in Text" };
318:
319: addMenu(bar, "View", vitems);
320:
321: String[] sitems = { "-SELECT", "-INSERT", "-UPDATE", "-DELETE",
322: "--", "-CREATE TABLE", "-DROP TABLE", "-CREATE INDEX",
323: "-DROP INDEX", "--", "-SCRIPT", "-SHUTDOWN", "--",
324: "-Test Script" };
325:
326: addMenu(bar, "SQL", sitems);
327:
328: Menu recent = new Menu("Recent");
329:
330: mRecent = new Menu("Recent");
331:
332: bar.add(mRecent);
333:
334: String[] soptions = { "-AutoCommit on", "-AutoCommit off",
335: "-Commit", "-Rollback", "--", "-Disable MaxRows",
336: "-Set MaxRows to 100", "--", "-Logging on",
337: "-Logging off", "--", "-Insert test data" // , "-Transfer"
338: };
339:
340: addMenu(bar, "Options", soptions);
341:
342: String[] shelp = { "-Show HTML-Help in browser" };
343:
344: addMenu(bar, "?", shelp);
345: fMain.setMenuBar(bar);
346: fMain.setSize(defWidth, defHeight);
347: fMain.add("Center", this );
348: initGUI();
349:
350: sRecent = new String[iMaxRecent];
351:
352: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
353: Dimension size = fMain.getSize();
354:
355: // (ulrivo): arguments from command line or
356: // full size on screen with less than 640 width
357: if (d.width > 640) {
358: fMain.setLocation((d.width - size.width) / 2,
359: (d.height - size.height) / 2);
360: } else if (defWidth > 0 && defHeight > 0) {
361: fMain.setLocation(defLocX, defLocY);
362: fMain.setSize(defWidth, defHeight);
363: } else {
364: fMain.setLocation(0, 0);
365: fMain.setSize(d);
366: }
367:
368: fMain.show();
369:
370: // (ulrivo): load query from command line
371: if (defQuery != null) {
372: txtCommand
373: .setText(DatabaseManagerCommon.readFile(defQuery));
374: }
375:
376: txtCommand.requestFocus();
377: }
378:
379: /**
380: * Method declaration
381: *
382: *
383: * @param k
384: */
385: public void keyTyped(KeyEvent k) {
386:
387: // Strg+Enter or Shift+Enter executes the actual SQL statement in command panel
388: if (k.getKeyChar() == '\n'
389: && (k.isControlDown() || k.isShiftDown())) {
390: k.consume();
391: execute();
392: layoutCard.show(pCard, "result");
393: }
394: }
395:
396: public void keyPressed(KeyEvent k) {
397:
398: // System.out.println("Key pressed: " + k.getKeyCode());
399: }
400:
401: /**
402: * Method declaration
403: *
404: *
405: * @param ev
406: */
407: public void actionPerformed(ActionEvent ev) {
408:
409: String s = ev.getActionCommand();
410:
411: if (s == null) {
412: if (ev.getSource() instanceof MenuItem) {
413: MenuItem i;
414:
415: s = ((MenuItem) ev.getSource()).getLabel();
416: }
417: }
418:
419: if (s.equals("Execute")) {
420: execute();
421: layoutCard.show(pCard, "result");
422: } else if (s.equals("Tree")) {
423: layoutCard.show(pCard, "tree");
424: } else if (s.equals("Command")) {
425: layoutCard.show(pCard, "command");
426: } else if (s.equals("Result")) {
427: layoutCard.show(pCard, "result");
428: } else if (s.equals("Editor")) {
429: layoutCard.show(pCard, "editor");
430: } else if (s.equals("Exit")) {
431: windowClosing(null);
432: } else if (s.equals("Logging on")) {
433: JavaSystem.setLogToSystem(true);
434: } else if (s.equals("Logging off")) {
435: JavaSystem.setLogToSystem(false);
436: } else if (s.equals("Refresh Tree")) {
437: refreshTree();
438: layoutCard.show(pCard, "tree");
439: } else if (s.startsWith("#")) {
440: int i = Integer.parseInt(s.substring(1));
441:
442: txtCommand.setText(sRecent[i]);
443: } else if (s.equals("Connect...")) {
444: connect(ZaurusConnectionDialog.createConnection(fMain,
445: "Connect", new Insets(defWidth, defHeight, defLocX,
446: defLocY)));
447: refreshTree();
448: layoutCard.show(pCard, "tree");
449: } else if (s.equals("View Tree")) {
450: layoutCard.show(pCard, "tree");
451: } else if (s.equals("View Command")) {
452: layoutCard.show(pCard, "command");
453: } else if (s.equals("View Result")) {
454: layoutCard.show(pCard, "result");
455: } else if (s.equals("View Editor")) {
456: layoutCard.show(pCard, "editor");
457: } else if (s.equals("Results in Grid")) {
458: iResult = 0;
459:
460: pResult.removeAll();
461: pResult.add("Center", gResult);
462: pResult.doLayout();
463: layoutCard.show(pCard, "result");
464: } else if (s.equals("Open Script...")) {
465: FileDialog f = new FileDialog(fMain, "Open Script",
466: FileDialog.LOAD);
467:
468: // (ulrivo): set default directory if set from command line
469: if (defDirectory != null) {
470: f.setDirectory(defDirectory);
471: }
472:
473: f.show();
474:
475: String file = f.getFile();
476:
477: if (file != null) {
478: txtCommand.setText(DatabaseManagerCommon.readFile(f
479: .getDirectory()
480: + file));
481: }
482:
483: layoutCard.show(pCard, "command");
484: } else if (s.equals("Save Script...")) {
485: FileDialog f = new FileDialog(fMain, "Save Script",
486: FileDialog.SAVE);
487:
488: // (ulrivo): set default directory if set from command line
489: if (defDirectory != null) {
490: f.setDirectory(defDirectory);
491: }
492:
493: f.show();
494:
495: String file = f.getFile();
496:
497: if (file != null) {
498: DatabaseManagerCommon.writeFile(
499: f.getDirectory() + file, txtCommand.getText());
500: }
501: } else if (s.equals("Save Result...")) {
502: FileDialog f = new FileDialog(fMain, "Save Result",
503: FileDialog.SAVE);
504:
505: // (ulrivo): set default directory if set from command line
506: if (defDirectory != null) {
507: f.setDirectory(defDirectory);
508: }
509:
510: f.show();
511:
512: String file = f.getFile();
513:
514: if (file != null) {
515: showResultInText();
516: DatabaseManagerCommon.writeFile(
517: f.getDirectory() + file, txtResult.getText());
518: }
519: } else if (s.equals("Results in Text")) {
520: iResult = 1;
521:
522: pResult.removeAll();
523: pResult.add("Center", txtResult);
524: pResult.doLayout();
525: showResultInText();
526: layoutCard.show(pCard, "result");
527: } else if (s.equals("AutoCommit on")) {
528: try {
529: cConn.setAutoCommit(true);
530: } catch (SQLException e) {
531: }
532: } else if (s.equals("AutoCommit off")) {
533: try {
534: cConn.setAutoCommit(false);
535: } catch (SQLException e) {
536: }
537: } else if (s.equals("Commit")) {
538: try {
539: cConn.commit();
540: } catch (SQLException e) {
541: }
542: } else if (s.equals("Insert test data")) {
543: insertTestData();
544: layoutCard.show(pCard, "result");
545: } else if (s.equals("Rollback")) {
546: try {
547: cConn.rollback();
548: } catch (SQLException e) {
549: }
550: } else if (s.equals("Disable MaxRows")) {
551: try {
552: sStatement.setMaxRows(0);
553: } catch (SQLException e) {
554: }
555: } else if (s.equals("Set MaxRows to 100")) {
556: try {
557: sStatement.setMaxRows(100);
558: } catch (SQLException e) {
559: }
560: } else if (s.equals("SELECT")) {
561: showHelp(DatabaseManagerCommon.selectHelp);
562: } else if (s.equals("INSERT")) {
563: showHelp(DatabaseManagerCommon.insertHelp);
564: } else if (s.equals("UPDATE")) {
565: showHelp(DatabaseManagerCommon.updateHelp);
566: } else if (s.equals("DELETE")) {
567: showHelp(DatabaseManagerCommon.deleteHelp);
568: } else if (s.equals("CREATE TABLE")) {
569: showHelp(DatabaseManagerCommon.createTableHelp);
570: } else if (s.equals("DROP TABLE")) {
571: showHelp(DatabaseManagerCommon.dropTableHelp);
572: } else if (s.equals("CREATE INDEX")) {
573: showHelp(DatabaseManagerCommon.createIndexHelp);
574: } else if (s.equals("DROP INDEX")) {
575: showHelp(DatabaseManagerCommon.dropIndexHelp);
576: } else if (s.equals("CHECKPOINT")) {
577: showHelp(DatabaseManagerCommon.checkpointHelp);
578: } else if (s.equals("SCRIPT")) {
579: showHelp(DatabaseManagerCommon.scriptHelp);
580: } else if (s.equals("SHUTDOWN")) {
581: showHelp(DatabaseManagerCommon.shutdownHelp);
582: } else if (s.equals("SET")) {
583: showHelp(DatabaseManagerCommon.setHelp);
584: } else if (s.equals("Test Script")) {
585: showHelp(DatabaseManagerCommon.testHelp);
586: } else if (s.equals("Show HTML-Help in browser")) {
587: try {
588: System.out.println("Starting Opera on index.html");
589: Runtime
590: .getRuntime()
591: .exec(
592: new String[] { "opera",
593: "/home/QtPalmtop/help/html/hsqldb/index.html" });
594: } catch (IOException e) {
595: System.out.println("A problem with Opera occured.");
596: }
597: }
598: }
599:
600: /**
601: * Method declaration
602: *
603: */
604: private void initGUI() {
605:
606: Panel pQuery = new Panel();
607: Panel pCommand = new Panel();
608:
609: // define a Panel pCard which takes four different cards/views:
610: // tree of tables, command SQL text area, result window and an editor/input form
611: pCard = new Panel();
612: layoutCard = new CardLayout(2, 2);
613:
614: pCard.setLayout(layoutCard);
615:
616: // four buttons at the top to quickly switch between the four views
617: butTree = new Button("Tree");
618: butCommand = new Button("Command");
619: butResult = new Button("Result");
620: butEditor = new Button("Editor");
621:
622: butTree.addActionListener(this );
623: butCommand.addActionListener(this );
624: butResult.addActionListener(this );
625: butEditor.addActionListener(this );
626:
627: Panel pButtons = new Panel();
628:
629: pButtons.setLayout(new GridLayout(1, 4, 8, 8));
630: pButtons.add(butTree);
631: pButtons.add(butCommand);
632: pButtons.add(butResult);
633: pButtons.add(butEditor);
634:
635: pResult = new Panel();
636:
637: pQuery.setLayout(new BorderLayout());
638: pCommand.setLayout(new BorderLayout());
639: pResult.setLayout(new BorderLayout());
640:
641: Font fFont = new Font("Dialog", Font.PLAIN, 12);
642:
643: txtCommand = new TextArea(5, 40);
644:
645: txtCommand.addKeyListener(this );
646:
647: txtResult = new TextArea(20, 40);
648:
649: txtCommand.setFont(fFont);
650: txtResult.setFont(new Font("Courier", Font.PLAIN, 12));
651:
652: butExecute = new Button("Execute");
653:
654: butExecute.addActionListener(this );
655: pCommand.add("South", butExecute);
656: pCommand.add("Center", txtCommand);
657:
658: gResult = new Grid();
659:
660: setLayout(new BorderLayout());
661: pResult.add("Center", gResult);
662:
663: tTree = new Tree();
664:
665: tTree.setMinimumSize(new Dimension(200, 100));
666: gResult.setMinimumSize(new Dimension(200, 300));
667:
668: eEditor = new ZaurusEditor();
669:
670: pCard.add("tree", tTree);
671: pCard.add("command", pCommand);
672: pCard.add("result", pResult);
673: pCard.add("editor", eEditor);
674: fMain.add("Center", pCard);
675: fMain.add("North", pButtons);
676: doLayout();
677: fMain.pack();
678: }
679:
680: protected void refreshTree() {
681: super.refreshTree();
682: eEditor.refresh(cConn);
683: }
684: }
|