001: /*
002: * $Author$
003: * $Id$
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package isql.commands;
013:
014: import isql.Command;
015: import util.PerlWrapper;
016: import isql.*;
017: import util.*;
018: import javax.swing.JTextArea;
019: import javax.swing.table.TableModel;
020: import java.awt.Font;
021: import java.util.*;
022:
023: /**
024: * The command object associated with the abbr command, used to
025: * store abbreviations.
026: * @author rahul kumar <rahul_kumar@yahoo.com>
027: * @see XXX
028: */
029:
030: public class BookmarkCommand implements Command {
031:
032: /** ctor/constructor.
033: */
034: public BookmarkCommand() {
035: }
036:
037: public String[] getCommandList() {
038: return commands;
039: }
040:
041: public void execute(SQLForm form, String fword, String SQLString) {
042: _form = form;
043: SQLTabbedPane tp = _form.tp;
044: //SQLJDBC myjdbc = _form.myjdbc;
045: Map htBookmarks = _form.getBookmarkMap();
046:
047: if (SQLString.equals(fword)) {
048: // print bookmarks if only fword printed
049: StringBuffer sb = new StringBuffer();
050: sb.append('\n');
051: for (Iterator e = htBookmarks.keySet().iterator(); e
052: .hasNext();) {
053: sb.append(e.next()).append(' ');
054: }
055: tp.appendInputArea(sb.toString());
056: return;
057: }
058: String[] tmp = PerlWrapper.perlMatch(
059: "bookmark\\s+(\\w+)\\s+(.+)", SQLString);
060: if (tmp != null) { // at least 2 words were found
061: String name = tmp[0]; // bookmark name or key in Map
062: String sql = (String) htBookmarks.get(name);
063: if ("run".equals(tmp[1].trim())) {
064: if (sql == null) {
065: _form.popup("No bookmark by name:" + name);
066: return;
067: }
068: _form.Run(sql);
069: return;
070: } else if ("get".equals(tmp[1].trim())) {
071: if (sql == null) {
072: _form.popup("No bookmark by name:" + name);
073: return;
074: }
075: tp.appendInputArea('\n' + sql);
076: return;
077: } else {
078: String[] tmp1 = PerlWrapper.perlMatch(
079: "(file|sql|system)\\s+(.+)", tmp[1]);
080: if (tmp1 != null) {
081: if ("file".equals(tmp1[0])) {
082: try {
083: htBookmarks.put(name, IsqlUtil
084: .getFileContents(tmp1[1]));
085: _form.setErrorArea("\nAdded " + name
086: + " to bookmarks");
087: return;
088: } catch (Exception exc) {
089: _form.setErrorArea('\n' + exc.toString());
090: }
091: } else if ("sql".equals(tmp1[0])) {
092: System.out.println("sqlstring:" + SQLString);
093: System.out.println("putting:" + tmp1[1]);
094: htBookmarks.put(name, tmp1[1]);
095: _form.setErrorArea("\nAdded " + name
096: + " to bookmarks");
097: return;
098: } else if ("system".equals(tmp1[0])) {
099: try {
100: htBookmarks.put(name, IsqlUtil
101: .getExecOutput(ArrayUtil.split(
102: tmp1[1], ' ')));
103: _form.setErrorArea("\nAdded " + name
104: + " to bookmarks");
105: return;
106: } catch (Exception exc) {
107: _form.setErrorArea('\n' + exc.toString());
108: }
109: } // system
110: else {
111: _form.popup(usage);
112: return;
113: }
114: } // tmp1
115: }
116: } // tmp not null
117: else { // less than 2 words
118: if (SQLString.indexOf(" help") > -1) {
119: tp.appendInputArea('\n' + usage);
120: return;
121: } else if (SQLString.indexOf(" list") > -1) {
122: // print bookmarks
123: StringBuffer sb = new StringBuffer();
124: sb.append('\n');
125: for (Iterator e = htBookmarks.keySet().iterator(); e
126: .hasNext();) {
127: String key = (String) e.next();
128: String value = (String) htBookmarks.get(key);
129: sb.append(key).append(':').append(value).append(
130: '\n');
131: }
132: tp.appendInputArea(sb.toString());
133: return;
134: } else if (SQLString.indexOf(" repaint") > -1) {
135: _form.makeBookmarkMenu();
136: return;
137: }
138: } // if less than 2 words
139: _form.popup(usage);
140: }
141:
142: public String getUsage(String s) {
143: return usage;
144: }
145:
146: String commands[] = { "bookmark" };
147: SQLForm _form;
148:
149: final String usage = "Usage:bookmark name (sql|file|system) text"
150: + " or bookmark name (get|run) or bookmark (list|help|repaint) or bookmark";
151:
152: ////// START INSTANCE VARIABLES //////
153:
154: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
155: static final String P = "BookmarkCommand"; // used in exception strings
156:
157: } // end of class
|