001: /*
002: * THis is based on DefaultEditorKit and should actually be an
003: * EditorKit. However, sinec we havent used the same therefore I just
004: * have a bunch of TextActions here.
005: * An EditorKit would be the next exercise/extenstion of this.
006: *
007: */
008: package isql;
009:
010: import java.io.*;
011: import java.awt.*;
012: import java.awt.event.ActionEvent;
013: import java.text.*;
014: import javax.swing.*;
015: import javax.swing.KeyStroke;
016: import javax.swing.SwingConstants;
017: import javax.swing.text.*;
018: import util.*;
019:
020: /**
021: * @author Rahul Kumar $Author: rahul_kumar $
022: * @version $Id: Actions.java,v 1.3 2004/01/03 10:12:58 rahul_kumar Exp $
023: */
024: public class Actions {
025:
026: SQLForm _form = null;
027:
028: /** constructor passing whatever is needed. Do we need to pass
029: * SQLString or give a method or what.
030: */
031: public Actions(SQLForm form) {
032: _form = form;
033: }
034:
035: /**
036: * Gets the MIME type of the data that this
037: * kit represents support for. The default
038: * is <code>text/plain</code>.
039: *
040: * @return the type
041: */
042: public String getContentType() {
043: return "text/plain";
044: }
045:
046: /**
047: * Creates a copy of the editor kit. This
048: * allows an implementation to serve as a prototype
049: * for others, so that they can be quickly created.
050: *
051: * @return the copy
052: */
053: //public Object clone() {
054: //return new DefaultEditorKit();
055: //}
056: /**
057: * Fetches a factory that is suitable for producing
058: * views of any models that are produced by this
059: * kit. The default is to have the UI produce the
060: * factory, so this method has no implementation.
061: *
062: * @return the view factory
063: */
064: public ViewFactory getViewFactory() {
065: return null;
066: }
067:
068: /**
069: * Fetches the set of commands that can be used
070: * on a text component that is using a model and
071: * view produced by this kit.
072: *
073: * @return the command list
074: */
075: public Action[] getActions() {
076: return new Action[] { new RunAction(_form),
077: new RunSelectedAction(_form),
078: new SubmitSelectedAction(_form), new GotoInput(_form),
079: new GotoOutput(_form), new GotoHistory(_form),
080: new PopupAction(_form) };
081: }
082:
083: /**
084: * Fetches a caret that can navigate through views
085: * produced by the associated ViewFactory.
086: *
087: * @return the caret
088: */
089: public Caret createCaret() {
090: return null;
091: }
092:
093: /**
094: * Creates an uninitialized text storage model (PlainDocument)
095: * that is appropriate for this type of editor.
096: *
097: * @return the model
098: */
099: //public Document createDefaultDocument() {
100: // return new PlainDocument();
101: // }
102: // --- names of well-known actions ---------------------------
103: /**
104: * @see InsertContentAction
105: */
106: public static final String runSelectAction = "run-select";
107: public static final String runAction = "run";
108: public static final String popupAction = "show-popup";
109: //public static final String deleteAction = "delete-row";
110: //public static final String updateAction = "update-row";
111: //public static final String insertAction = "insert-row";
112:
113: /**
114: * Name of the action to place a line/paragraph break into
115: * the document. If there is a selection, it is removed before
116: * the break is added.
117: * @see InsertBreakAction
118: */
119: public static final String runBatchFileAction = "run-batch-file";
120:
121: /**
122: * Name of the action to place a tab character into
123: * the document. If there is a selection, it is removed before
124: * the tab is added.
125: * @see InsertTabAction
126: */
127: public static final String saveAction = "save";
128:
129: /**
130: * Name of the action to delete the character of content that
131: * precedes the current caret position.
132: * @see DeletePrevCharAction
133: */
134: public static final String readAction = "read";
135:
136: /**
137: * Name of the action to delete the character of content that
138: * follows the current caret position.
139: * @see DeleteNextCharAction
140: */
141: public static final String replaceMatchAction = "replace-match";
142:
143: /**
144: * Name of the action to set the editor into read-only
145: * mode.
146: * @see ReadOnlyAction
147: */
148: public static final String replaceColumnNameAction = "replace-column-name";
149:
150: /**
151: * Name of the action to set the editor into writeable
152: * mode.
153: * @see WritableAction
154: */
155: public static final String replaceTableNameAction = "replace-table-name";
156:
157: /**
158: * Name of the action to cut the selected region
159: * and place the contents into the system clipboard.
160: * @see JTextComponent#cut
161: */
162: public static final String submitSelectedAction = "submit-selected";
163: public static final String submitSingleAction = "submit-single";
164:
165: /**
166: * Name of the action to copy the selected region
167: * and place the contents into the system clipboard.
168: * @see JTextComponent#copy
169: */
170: public static final String processSetAction = "process-set";
171: public static final String commandAction = "commandAction";
172:
173: /**
174: * Name of the action to paste the contents of the
175: * system clipboard into the selected region, or before the
176: * caret if nothing is selected.
177: * @see JTextComponent#paste
178: */
179: public static final String refreshTablesAction = "refresh-tables";
180:
181: /**
182: * Name of the action to create a beep.
183: * @see BeepAction
184: */
185: public static final String exitAction = "exit";
186:
187: /**
188: * Name of the action to page up vertically.
189: * @see PageUpAction
190: */
191: public static final String constructAction = "construct";
192:
193: /**
194: * Name of the action to page down vertically.
195: * @see PageDownAction
196: */
197:
198: /**
199: * Name of the action to page up vertically, and move the
200: * selection.
201: * @see PageUpAction
202: */
203: /*public*/static final String runSelectedAction = "run-selected";
204:
205: /**
206: * Name of the action to page down vertically, and move the
207: * selection.
208: * @see PageDownAction
209: */
210: /*public*/static final String nextTabAction = "next-tab";
211:
212: /**
213: * Name of the Action for moving the caret
214: * logically forward one position.
215: * @see ForwardAction
216: */
217: public static final String previousTabAction = "previous-tab";
218:
219: /**
220: * Name of the Action for moving the caret
221: * logically backward one position.
222: * @see BackwardAction
223: */
224: public static final String reexecuteLastQueryAction = "reexecute-last";
225:
226: /**
227: * Name of the Action for extending the selection
228: * by moving the caret logically forward one position.
229: * @see SelectionForwardAction
230: */
231: // ??
232: public static final String saveOutputAction = "save-output";
233:
234: /**
235: * Name of the Action for extending the selection
236: * by moving the caret logically backward one position.
237: * @see SelectionBackwardAction
238: * @see #getActions
239: */
240: // ??
241: public static final String showAbbreviationsAction = "show-abbrev";
242: public static final String gotoInput = "goto-input";
243: public static final String gotoOutput = "goto-output";
244: public static final String gotoHistory = "goto-history";
245:
246: /**
247: * Name of the Action for moving the caret
248: * logically upward one position.
249: * @see UpAction
250: * @see #getActions
251: */
252:
253: // --- Action implementations ---------------------------------
254:
255: /** Calls the Run command of form object with text objects text.
256: */
257: public static class RunAction extends TextAction {
258:
259: /**
260: * Creates this object with the appropriate identifier.
261: */
262: public RunAction(SQLForm form) {
263: super (runAction);
264: this ._form = form;
265: }
266:
267: final SQLForm _form;
268:
269: /**
270: * The operation to perform when this action is triggered.
271: *
272: * @param e the action event
273: */
274: public void actionPerformed(ActionEvent e) {
275: JTextComponent target = getTextComponent(e);
276: if ((target != null) && (e != null)) {
277: /*
278: if ((! target.isEditable()) || (! target.isEnabled())) {
279: target.getToolkit().beep();
280: return;
281: }
282: */
283: String content = target.getText();
284: if ((content != null) && (content.length() > 0)) {
285: //&& ((mod & ActionEvent.ALT_MASK) == 0))
286: // these commands should get moved outa form
287: _form.Run(content);
288: } else
289: target.getToolkit().beep();
290: }
291: }
292: }
293:
294: public static class GotoInput extends TextAction {
295:
296: public GotoInput(SQLForm form) {
297: super (gotoInput);
298: this ._form = form;
299: }
300:
301: final SQLForm _form;
302:
303: public void actionPerformed(ActionEvent e) {
304: //JComponent target = getComponent(e);
305: //if ((target != null) && (e != null)) {
306: _form.tp.makeInputAreaVisible();
307: //}
308: //else
309: // target.getToolkit().beep();
310: }
311: }
312:
313: public static class GotoOutput extends TextAction {
314:
315: public GotoOutput(SQLForm form) {
316: super (gotoOutput);
317: this ._form = form;
318: }
319:
320: final SQLForm _form;
321:
322: /** tis throws an NPE if connection not made, ht is null */
323: public void actionPerformed(ActionEvent e) {
324: //JComponent target = getComponent(e);
325: //if ((target != null) && (e != null)) {
326: if (_form.htParams == null) {
327: _form.tp.makeOutputAreaVisible();
328: return;
329: }
330: if ("multiple".equalsIgnoreCase((String) _form
331: .getAttribute("tableview", "multiple")))
332: _form.tp.makeFramesVisible();
333: else if ("JTABLE".equalsIgnoreCase((String) _form
334: .getAttribute("outputformat", "JTABLE")))
335: _form.tp.makeTableAreaVisible();
336: else
337: _form.tp.makeOutputAreaVisible();
338: //}
339: //else
340: // target.getToolkit().beep();
341: }
342: }
343:
344: public static class GotoHistory extends TextAction {
345:
346: public GotoHistory(SQLForm form) {
347: super (gotoHistory);
348: this ._form = form;
349: }
350:
351: final SQLForm _form;
352:
353: public void actionPerformed(ActionEvent e) {
354: //JComponent target = getComponent(e);
355: //if ((target != null) && (e != null)) {
356: _form.tp.makeHistoryAreaVisible();
357: //}
358: //else
359: // target.getToolkit().beep();
360: }
361: }
362:
363: /** attempt to map a command to an action generically, so we wont
364: * have to create a new action for each command we want to put on
365: * the menu bar.
366: */
367: public static class CommandAction extends AbstractAction {
368:
369: public CommandAction(SQLForm form, String command) {
370: super (commandAction);
371: this ._form = form;
372: this ._command = command;
373: }
374:
375: final SQLForm _form;
376: final String _command;
377:
378: public void actionPerformed(ActionEvent e) {
379: _form.Run(_command);
380: }
381: }
382:
383: /** extracts table name from sql statement. checks for word after
384: * from and returns. If not found returns the default passed.
385: */
386: public static String getTableFromSQL(String sql, String defaultvalue) {
387: String ret[] = PerlWrapper.perlMatch(
388: "[fF][rR][Oo][Mm]\\s+(\\w+)", sql);
389: if (ret != null)
390: return ret[0];
391: return defaultvalue;
392: }
393:
394: /** calls forms Run() with selected text.
395: */
396: public static class RunSelectedAction extends TextAction {
397:
398: public RunSelectedAction(SQLForm form) {
399: super (runSelectedAction);
400: _form = form;
401: }
402:
403: final SQLForm _form;
404:
405: /**
406: * The operation to perform when this action is triggered.
407: *
408: * @param e the action event
409: */
410: public void actionPerformed(ActionEvent e) {
411: JTextComponent target = getTextComponent(e);
412: if ((target != null) && (e != null)) {
413: /*
414: if ((! target.isEditable()) || (! target.isEnabled())) {
415: target.getToolkit().beep();
416: return;
417: }
418: */
419: String content = target.getSelectedText();
420: if ((content != null) && (content.length() > 0)) {
421: //&& ((mod & ActionEvent.ALT_MASK) == 0))
422: // these commands should get moved outa form
423: _form.Run(content);
424: } else
425: target.getToolkit().beep();
426:
427: }
428: }
429: }
430:
431: public static class SubmitSelectedAction extends TextAction {
432:
433: /**
434: * Creates this object with the appropriate identifier.
435: */
436: public SubmitSelectedAction(SQLForm form) {
437: super (submitSelectedAction);
438: _form = form;
439: }
440:
441: SQLForm _form;
442:
443: /**
444: * The operation to perform when this action is triggered.
445: *
446: * @param e the action event
447: */
448: public void actionPerformed(ActionEvent e) {
449: //System.err.println( "inside control Z");
450: JTextComponent target = getTextComponent(e);
451: if ((target != null) && (e != null)) {
452: /*
453: if ((! target.isEditable()) || (! target.isEnabled())) {
454: target.getToolkit().beep();
455: return;
456: }
457: */
458: String content = target.getSelectedText();
459: if ((content != null) && (content.length() > 0)) {
460: //&& ((mod & ActionEvent.ALT_MASK) == 0))
461: // these commands should get moved outa form
462: _form.Run(content);
463: } else {
464: // no selection try executing this line
465: try {
466: int offs = target.getCaretPosition();
467: int end = Utilities.getRowEnd(target, offs);
468: int start = Utilities.getRowStart(target, offs);
469: //System.err.println( "***** offs:"+offs);
470: String line = target
471: .getText(start, end - start);
472: //System.out.println("*** line:"+ line +":**" );
473: //System.err.println( Utilities.getWordStart(target,offs));
474: //System.err.println( Utilities.getWordEnd(target,offs));
475: //int prev = Utilities.getPreviousWord(target,offs);
476: //System.err.println( "prev:"+prev );
477: _form.Run(line);
478: //System.err.println( "word:"+ target.getText( Utilities.getWordStart(target,offs), Utilities.getWordEnd(target,offs)));
479:
480: int ws1 = Utilities.getWordStart(target, offs);
481: int ws2 = Utilities.getWordEnd(target, offs);
482: //System.out.println("*** curr word:"+
483: // target.getText(
484: // ws1, ws2-ws1));
485:
486: ws1 = Utilities.getPreviousWord(target, offs);
487: ws2 = Utilities.getWordEnd(target, ws1);
488: //System.out.println("*** prev word:"+
489: // target.getText( ws1, ws2-ws1));
490:
491: } catch (BadLocationException bl) {
492: System.err.println("bad location!"
493: + bl.toString());
494: target.getToolkit().beep();
495: }
496:
497: }
498: } // target ! null
499: else
500: System.err.println("null target control Z");
501: } //actionPer
502: } // class
503:
504: public static class PopupAction extends TextAction {
505:
506: public PopupAction(SQLForm form) {
507: super (popupAction);
508: this ._form = form;
509: }
510:
511: final SQLForm _form;
512:
513: public void actionPerformed(ActionEvent e) {
514: JTextComponent target = getTextComponent(e);
515: if ((target != null) && (e != null)) {
516: if (target instanceof JTextArea) {
517: // unfortunately at this point i keep doing this
518: // each time
519: SqlPopupMenu popup = new SqlPopupMenu(target);
520: popup.set(_form.sd.getTabTable());
521: try {
522: int pos1 = Utilities.getWordStart(target,
523: target.getCaretPosition());
524: String text = target.getText(pos1, target
525: .getCaretPosition()
526: - pos1);
527: popup.setMatching(text);
528: // determining x and y
529: int lo = ((JTextArea) target)
530: .getLineOfOffset(target
531: .getCaretPosition());
532: FontMetrics fm = target.getFontMetrics(target
533: .getFont());
534: int rh = fm.getHeight();
535: int lso = ((JTextArea) target)
536: .getLineStartOffset(lo);
537: int x = lo * rh;
538: String line = target.getText(lso, target
539: .getCaretPosition()
540: - lso);
541: int y = fm.stringWidth(line);
542: popup.show(y, x);
543: } catch (Exception exc) {
544: System.err.println("682EXC:" + exc.toString());
545: }
546: //_form._popup.setMatching("AAA");
547: //_form._popup.show();
548: }
549: } // if
550: } // actionPerformed
551: } // class popupAction
552:
553: } // class Actions
|