01: package net.sourceforge.squirrel_sql.client.session.action;
02:
03: import java.awt.event.ActionEvent;
04: import java.util.ArrayList;
05:
06: import javax.swing.JOptionPane;
07:
08: import net.sourceforge.squirrel_sql.client.IApplication;
09: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
10: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
11: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
12: import net.sourceforge.squirrel_sql.client.session.ISession;
13: import net.sourceforge.squirrel_sql.client.session.ObjectTreeSearch;
14: import net.sourceforge.squirrel_sql.fw.util.StringManager;
15: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
16:
17: public class ViewObjectAtCursorInObjectTreeAction extends
18: SquirrelAction implements ISQLPanelAction {
19:
20: /**
21: * Current panel.
22: */
23: private ISQLPanelAPI _panel;
24:
25: /** Internationalized strings for this class */
26: private static final StringManager s_stringMgr = StringManagerFactory
27: .getStringManager(ViewObjectAtCursorInObjectTreeAction.class);
28:
29: /**
30: * Ctor specifying Application API.
31: *
32: * @param app Application API.
33: */
34: public ViewObjectAtCursorInObjectTreeAction(IApplication app) {
35: super (app);
36: }
37:
38: public void setSQLPanel(ISQLPanelAPI panel) {
39: _panel = panel;
40: setEnabled(null != _panel && _panel.isInMainSessionWindow());
41: }
42:
43: /**
44: * View the Object at cursor in the Object Tree
45: *
46: * @param evt Event being executed.
47: */
48: public synchronized void actionPerformed(ActionEvent evt) {
49: if (_panel == null) {
50: return;
51: }
52:
53: String stringAtCursor = getStringAtCursor();
54:
55: new ObjectTreeSearch().viewObjectInObjectTree(stringAtCursor,
56: _panel.getSession());
57: }
58:
59: private String getStringAtCursor() {
60: String text = _panel.getSQLEntryPanel().getText();
61: int caretPos = _panel.getSQLEntryPanel().getCaretPosition();
62:
63: int lastIndexOfText = Math.max(0, text.length() - 1);
64: int beginPos = Math.min(caretPos, lastIndexOfText); // The Math.min is for the Caret at the end of the text
65: while (0 < beginPos
66: && false == isParseStop(text.charAt(beginPos), false)) {
67: --beginPos;
68: }
69:
70: int endPos = caretPos;
71: while (endPos < text.length()
72: && false == isParseStop(text.charAt(endPos), true)) {
73: ++endPos;
74: }
75:
76: return text.substring(beginPos, endPos).trim();
77:
78: }
79:
80: private boolean isParseStop(char c, boolean treatDotAsStop) {
81: return '(' == c || ')' == c || '\'' == c
82: || Character.isWhitespace(c)
83: || (treatDotAsStop && '.' == c);
84:
85: }
86: }
|