001: package net.sourceforge.squirrel_sql.plugins.syntax.oster;
002:
003: /*
004: * Copyright (C) 2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: import java.awt.Font;
022: import java.awt.dnd.DropTarget;
023: import java.awt.event.MouseListener;
024:
025: import javax.swing.event.CaretListener;
026: import javax.swing.event.UndoableEditListener;
027: import javax.swing.text.Document;
028: import javax.swing.text.Element;
029: import javax.swing.text.JTextComponent;
030: import javax.swing.text.PlainDocument;
031: import javax.swing.undo.UndoManager;
032:
033: import net.sourceforge.squirrel_sql.client.IApplication;
034: import net.sourceforge.squirrel_sql.client.gui.dnd.FileEditorDropTargetListener;
035: import net.sourceforge.squirrel_sql.client.session.BaseSQLEntryPanel;
036: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
037: import net.sourceforge.squirrel_sql.client.session.ISession;
038: import net.sourceforge.squirrel_sql.client.session.SQLTokenListener;
039: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
040: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
041: import net.sourceforge.squirrel_sql.plugins.syntax.SyntaxPreferences;
042:
043: public class OsterSQLEntryPanel extends BaseSQLEntryPanel {
044: /** Logger for this class. */
045: private static final ILogger s_log = LoggerController
046: .createLogger(OsterSQLEntryPanel.class);
047:
048: /** Application API. */
049: private IApplication _app;
050:
051: /** Text component. */
052: private OsterTextControl _textArea;
053:
054: @SuppressWarnings("unused")
055: private DropTarget dt;
056:
057: private ISession _session;
058:
059: OsterSQLEntryPanel(ISession session, SyntaxPreferences prefs) {
060: super (session.getApplication());
061: _session = session;
062:
063: if (session == null) {
064: throw new IllegalArgumentException("Null ISession passed");
065: }
066:
067: if (prefs == null) {
068: throw new IllegalArgumentException(
069: "Null Preferences passed");
070: }
071:
072: _app = session.getApplication();
073:
074: _textArea = new OsterTextControl(session, prefs,
075: getIdentifier());
076:
077: dt = new DropTarget(_textArea,
078: new FileEditorDropTargetListener(session));
079: }
080:
081: public void endColorerThread() {
082: _textArea.endColorerThread();
083: }
084:
085: /**
086: * @see ISQLEntryPanel#gettextComponent()
087: */
088: public JTextComponent getTextComponent() {
089: return _textArea;
090: }
091:
092: /**
093: * If the component returned by <TT>getTextComponent</TT> contains
094: * its own scroll bars return <TT>true</TT> other wise this component
095: * will be wrapped in the scroll pane when added to the SQL panel.
096: *
097: * @return <TT>true</TT> if text component already handles scrolling.
098: */
099: public boolean getDoesTextComponentHaveScroller() {
100: return false;
101: }
102:
103: /**
104: * @see ISQLEntryPanel#getText()
105: */
106: public String getText() {
107: return _textArea.getText();
108: }
109:
110: public void setFont(Font font) {
111: _textArea.setFont(font);
112: }
113:
114: /**
115: * @see ISQLEntryPanel#getSelectedText()
116: */
117: public String getSelectedText() {
118: return _textArea.getSelectedText();
119: }
120:
121: /**
122: * Replace the contents of the SQL entry area with the passed
123: * SQL script without selecting it.
124: *
125: * @param sqlScript The script to be placed in the SQL entry area..
126: */
127: public void setText(String text) {
128: setText(text, true);
129: }
130:
131: /**
132: * Replace the contents of the SQL entry area with the passed
133: * SQL script and specify whether to select it.
134: *
135: * @param sqlScript The script to be placed in the SQL entry area..
136: * @param select If <TT>true</TT> then select the passed script
137: * in the sql entry area.
138: */
139: public void setText(String text, boolean select) {
140: _textArea.setText(text);
141:
142: if (select) {
143: setSelectionEnd(_textArea.getDocument().getLength());
144: setSelectionStart(0);
145: }
146: }
147:
148: /**
149: * Append the passed SQL script to the SQL entry area but don't select
150: * it.
151: *
152: * @param sqlScript The script to be appended.
153: */
154: public void appendText(String sqlScript) {
155: appendText(sqlScript, false);
156: }
157:
158: /**
159: * Append the passed SQL script to the SQL entry area and specify
160: * whether it should be selected.
161: *
162: * @param sqlScript The script to be appended.
163: * @param select If <TT>true</TT> then select the passed script
164: * in the sql entry area.
165: */
166: public void appendText(String sqlScript, boolean select) {
167: Document doc = _textArea.getDocument();
168:
169: try {
170: int start = 0;
171: if (select) {
172: start = doc.getLength();
173: }
174:
175: doc.insertString(doc.getLength(), sqlScript, null);
176:
177: if (select) {
178: setSelectionEnd(doc.getLength());
179: setSelectionStart(start);
180: }
181: } catch (Exception ex) {
182: s_log.error("Error appending text to text area", ex);
183: }
184: }
185:
186: /**
187: * @see ISQLEntryPanel#getCaretPosition()
188: */
189: public int getCaretPosition() {
190: return _textArea.getCaretPosition();
191: }
192:
193: public void setCaretPosition(int value) {
194: _textArea.setCaretPosition(value);
195: }
196:
197: /**
198: * @see ISQLEntryPanel#setTabSize(int)
199: */
200: public void setTabSize(int tabSize) {
201: _textArea.getDocument().putProperty(
202: PlainDocument.tabSizeAttribute,
203: Integer.valueOf(tabSize));
204: }
205:
206: /**
207: * @see ISQLEntryPanel#getSelectionStart()
208: */
209: public int getSelectionStart() {
210: return _textArea.getSelectionStart();
211: }
212:
213: /**
214: * @see ISQLEntryPanel#setSelectionStart(int)
215: */
216: public void setSelectionStart(int pos) {
217: _textArea.setSelectionStart(pos);
218: }
219:
220: /**
221: * @see ISQLEntryPanel#getSelectionEnd()
222: */
223: public int getSelectionEnd() {
224: return _textArea.getSelectionEnd();
225: }
226:
227: /**
228: * @see ISQLEntryPanel#setSelectionEnd(int)
229: */
230: public void setSelectionEnd(int pos) {
231: _textArea.setSelectionEnd(pos);
232: }
233:
234: /**
235: * Replace the currently selected text in the SQL entry area
236: * with the passed text.
237: *
238: * @param sqlScript The script to be placed in the SQL entry area.
239: */
240: public void replaceSelection(String sqlScript) {
241: _textArea.replaceSelection(sqlScript);
242: }
243:
244: /**
245: * @see ISQLEntryPanel#hasFocus()
246: */
247: public boolean hasFocus() {
248: return _textArea.hasFocus();
249: }
250:
251: /**
252: * @see ISQLEntryPanel#requestFocus()
253: */
254: public void requestFocus() {
255: _textArea.requestFocus();
256: }
257:
258: /**
259: * @see ISQLEntryPanel#addMouseListener(MouseListener)
260: */
261: public void addMouseListener(MouseListener lis) {
262: _textArea.addMouseListener(lis);
263: }
264:
265: /**
266: * @see ISQLEntryPanel#removeMouseListener(MouseListener)
267: */
268: public void removeMouseListener(MouseListener lis) {
269: _textArea.removeMouseListener(lis);
270: }
271:
272: public void updateFromPreferences() {
273: _textArea.updateFromPreferences();
274: }
275:
276: /**
277: * @see ISQLEntryPanel#hasOwnUndoableManager()
278: */
279: public boolean hasOwnUndoableManager() {
280: return false;
281: }
282:
283: /**
284: * @see ISQLEntryPanel#addUndoableEditListener(UndoableEditListener)
285: */
286: public void addUndoableEditListener(UndoableEditListener listener) {
287: _textArea.getDocument().addUndoableEditListener(listener);
288: }
289:
290: /**
291: * @see ISQLEntryPanel#removeUndoableEditListener(UndoableEditListener)
292: */
293: public void removeUndoableEditListener(UndoableEditListener listener) {
294: _textArea.getDocument().removeUndoableEditListener(listener);
295: }
296:
297: /* (non-Javadoc)
298: * @see net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel#setUndoManager(javax.swing.undo.UndoManager)
299: */
300: public void setUndoManager(UndoManager manager) {
301: // no support for undo
302: }
303:
304: /**
305: * @see ISQLEntryPanel#getCaretLineNumber()
306: */
307: public int getCaretLineNumber() {
308: final int pos = getCaretPosition();
309: final Document doc = _textArea.getStyledDocument();
310: final Element docElem = doc.getDefaultRootElement();
311: return docElem.getElementIndex(pos);
312: }
313:
314: /**
315: * @see ISQLEntryPanel#getCaretLinePosition()
316: */
317: public int getCaretLinePosition() {
318: String textTillCarret = getText().substring(0,
319: getCaretPosition());
320:
321: int lineFeedIndex = textTillCarret.lastIndexOf('\n');
322: if (-1 == lineFeedIndex) {
323: return getCaretPosition();
324: } else {
325: return getCaretPosition() - lineFeedIndex - 1;
326: }
327:
328: // this didn't work
329: // final int pos = getCaretPosition();
330: // final Document doc = _textArea.getStyledDocument();
331: // final Element docElem = doc.getDefaultRootElement();
332: // final Element lineElem = docElem.getElement(getCaretLineNumber());
333: // return lineElem.getElementIndex(pos);
334: }
335:
336: /**
337: * @see ISQLEntryPanel#addCaretListener(CaretListener)
338: */
339: public void addCaretListener(CaretListener lis) {
340: _textArea.addCaretListener(lis);
341: }
342:
343: /**
344: * @see ISQLEntryPanel#removeCaretListener(CaretListener)
345: */
346: public void removeCaretListener(CaretListener lis) {
347: _textArea.removeCaretListener(lis);
348: }
349:
350: public void addSQLTokenListener(SQLTokenListener tl) {
351: _textArea.addSQLTokenListener(tl);
352: }
353:
354: public void removeSQLTokenListener(SQLTokenListener tl) {
355: _textArea.removeSQLTokenListener(tl);
356: }
357:
358: public ISession getSession() {
359: return _session;
360: }
361:
362: }
|