001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.ui;
024:
025: import java.awt.Cursor;
026: import java.awt.Point;
027: import java.awt.event.MouseEvent;
028: import java.io.IOException;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.net.URLConnection;
032: import java.net.URLStreamHandler;
033:
034: import javax.swing.JEditorPane;
035: import javax.swing.SwingUtilities;
036: import javax.swing.event.HyperlinkEvent;
037: import javax.swing.event.MouseInputAdapter;
038: import javax.swing.text.AttributeSet;
039: import javax.swing.text.DefaultStyledDocument;
040: import javax.swing.text.Position;
041:
042: /**
043: * Class for mouse input support on the SQL command editor.
044: * <p>
045: *
046: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
047: * @version 1.0
048: */
049: public class SqlCommandMouseAdapter extends MouseInputAdapter {
050:
051: /**
052: * This is used by viewToModel to avoid allocing a new array each time.
053: */
054: private Position.Bias[] bias = new Position.Bias[1];
055:
056: public void mouseClicked(MouseEvent e) {
057:
058: JEditorPane editor = (JEditorPane) e.getSource();
059: if (SwingUtilities.isLeftMouseButton(e)) {
060: Point pt = new Point(e.getX(), e.getY());
061: int pos = editor.viewToModel(pt);
062: if (pos >= 0) {
063: DefaultStyledDocument document = (DefaultStyledDocument) editor
064: .getDocument();
065: AttributeSet as = document.getCharacterElement(pos)
066: .getAttributes();
067: if (Boolean.TRUE == as.getAttribute("hyperlinked")) {
068: activateLink(pos, editor, e.getX(), e.getY());
069: }
070: }
071: }
072: }
073:
074: public void mouseMoved(MouseEvent e) {
075:
076: JEditorPane editor = (JEditorPane) e.getSource();
077:
078: Point pt = new Point(e.getX(), e.getY());
079: int pos = editor.getUI().viewToModel(editor, pt, bias);
080: if (bias[0] == Position.Bias.Backward && pos > 0) {
081: pos--;
082: }
083: boolean setCursor = false;
084: if (pos >= 0) {
085: DefaultStyledDocument document = (DefaultStyledDocument) editor
086: .getDocument();
087: AttributeSet as = document.getCharacterElement(pos)
088: .getAttributes();
089: if ((e.getModifiers() & org.isqlviewer.swing.SwingUtilities.MENU_SHORTCUT_MASK) == org.isqlviewer.swing.SwingUtilities.MENU_SHORTCUT_MASK) {
090: if (Boolean.TRUE == as.getAttribute("hyperlinked")) {
091: setCursor = true;
092: }
093: }
094: }
095: if (setCursor) {
096: editor.setCursor(Cursor
097: .getPredefinedCursor(Cursor.HAND_CURSOR));
098: } else {
099: editor.setCursor(Cursor
100: .getPredefinedCursor(Cursor.TEXT_CURSOR));
101: }
102: }
103:
104: private void activateLink(int pos, JEditorPane editor, int x, int y) {
105:
106: String text = editor.getText();
107: int currentLocation = Math.min(text.length() - 1, pos);
108: currentLocation = Math.max(0, currentLocation);
109:
110: int fromIndex = 0;
111: int toIndex = text.length() - 1;
112: for (int i = currentLocation; i >= 0; i--) {
113: char c = text.charAt(i);
114: if (Character.isWhitespace(c)) {
115: fromIndex = i;
116: break;
117: }
118: }
119: boolean found = false;
120: for (int i = currentLocation; i < toIndex; i++) {
121: char c = text.charAt(i);
122: if (Character.isWhitespace(c)) {
123: toIndex = i;
124: found = true;
125: break;
126: }
127: }
128: if (!found) {
129: toIndex = text.length();
130: }
131:
132: String currentWord = text.substring(fromIndex, toIndex).trim();
133: if (currentWord.length() == 0) {
134: return;
135: }
136: URL url;
137: try {
138: url = new URL("table", null, -1, currentWord,
139: new InternalURLStreamHandler());
140: HyperlinkEvent hle = new HyperlinkEvent(editor,
141: HyperlinkEvent.EventType.ACTIVATED, url);
142: editor.fireHyperlinkUpdate(hle);
143: } catch (MalformedURLException e) {
144: }
145: }
146:
147: private static class InternalURLStreamHandler extends
148: URLStreamHandler {
149:
150: @Override
151: protected URLConnection openConnection(URL u)
152: throws IOException {
153:
154: return new NullURLConnection(u);
155: }
156: }
157:
158: private static class NullURLConnection extends URLConnection {
159:
160: protected NullURLConnection(URL url) {
161:
162: super (url);
163: }
164:
165: public void connect() {
166:
167: }
168: }
169: }
|