001: package net.sourceforge.squirrel_sql.client.session;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; 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.JTextArea;
026: import javax.swing.event.CaretListener;
027: import javax.swing.event.UndoableEditListener;
028: import javax.swing.text.BadLocationException;
029: import javax.swing.text.JTextComponent;
030: import javax.swing.undo.UndoManager;
031:
032: import net.sourceforge.squirrel_sql.client.gui.dnd.FileEditorDropTargetListener;
033: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
034: import net.sourceforge.squirrel_sql.fw.gui.FontInfo;
035: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
036: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
037:
038: public class DefaultSQLEntryPanel extends BaseSQLEntryPanel {
039: /** Logger for this class. */
040: private static ILogger s_log = LoggerController
041: .createLogger(DefaultSQLEntryPanel.class);
042:
043: /** Current session. */
044: private ISession _session;
045:
046: /** Text area control. */
047: private MyTextArea _comp;
048:
049: @SuppressWarnings("unused")
050: private DropTarget dt;
051:
052: public DefaultSQLEntryPanel(ISession session) {
053: super (session.getApplication());
054: if (session == null) {
055: throw new IllegalArgumentException("ISession == null");
056: }
057:
058: _session = session;
059: _comp = new MyTextArea(session, this );
060:
061: dt = new DropTarget(_comp, new FileEditorDropTargetListener(
062: session));
063: }
064:
065: /**
066: * Retrieve the text area component. Normally this would be a subclass
067: * of <TT>javax.swing.text.JTextComponent</TT> but a plugin may use a
068: * class otehr than a Swing text control.
069: *
070: * @return The text area component.
071: */
072: public JTextComponent getTextComponent() {
073: return _comp;
074: }
075:
076: /**
077: * If the component returned by <TT>getTextComponent</TT> contains
078: * its own scroll bars return <TT>true</TT> other wise this component
079: * will be wrapped in the scroll pane when added to the SQL panel.
080: *
081: * @return <TT>true</TT> if text component already handles scrolling.
082: */
083: public boolean getDoesTextComponentHaveScroller() {
084: return false;
085: }
086:
087: public void addUndoableEditListener(UndoableEditListener lis) {
088: _comp.getDocument().addUndoableEditListener(lis);
089: }
090:
091: public void removeUndoableEditListener(UndoableEditListener lis) {
092: _comp.getDocument().removeUndoableEditListener(lis);
093: }
094:
095: /*
096: * @see ISQLEntryPanel#hasOwnUndoableManager()
097: */
098: public boolean hasOwnUndoableManager() {
099: return false;
100: }
101:
102: /**
103: * @see ISQLEntryPanel#getText()
104: */
105: public String getText() {
106: return _comp.getText();
107: }
108:
109: /**
110: * @see ISQLEntryPanel#getSelectedText()
111: */
112: public String getSelectedText() {
113: return _comp.getSelectedText();
114: }
115:
116: /**
117: * Replace the contents of the SQL entry area with the passed
118: * SQL script without selecting it.
119: *
120: * @param sqlScript The script to be placed in the SQL entry area..
121: */
122: public void setText(String sqlScript) {
123: setText(sqlScript, true);
124: }
125:
126: /**
127: * Replace the contents of the SQL entry area with the passed
128: * SQL script and specify whether to select it.
129: *
130: * @param sqlScript The script to be placed in the SQL entry area..
131: * @param select If <TT>true</TT> then select the passed script
132: * in the sql entry area.
133: */
134: public void setText(String sqlScript, boolean select) {
135: _comp.setText(sqlScript);
136: if (select) {
137: setSelectionEnd(getText().length());
138: setSelectionStart(0);
139: }
140: _comp.setCaretPosition(0);
141: }
142:
143: /**
144: * Append the passed SQL script to the SQL entry area but don't select
145: * it.
146: *
147: * @param sqlScript The script to be appended.
148: */
149: public void appendText(String sqlScript) {
150: appendText(sqlScript, false);
151: }
152:
153: /**
154: * Append the passed SQL script to the SQL entry area and specify
155: * whether it should be selected.
156: *
157: * @param sqlScript The script to be appended.
158: * @param select If <TT>true</TT> then select the passed script
159: * in the sql entry area.
160: */
161: public void appendText(String sqlScript, boolean select) {
162: final int start = select ? getText().length() : 0;
163: _comp.append(sqlScript);
164: if (select) {
165: setSelectionEnd(getText().length());
166: setSelectionStart(start);
167: }
168: }
169:
170: /**
171: * Replace the currently selected text in the SQL entry area
172: * with the passed text.
173: *
174: * @param sqlScript The script to be placed in the SQL entry area.
175: */
176: public void replaceSelection(String sqlScript) {
177: _comp.replaceSelection(sqlScript);
178: }
179:
180: /**
181: * @see ISQLEntryPanel#getCaretPosition()
182: */
183: public int getCaretPosition() {
184: return _comp.getCaretPosition();
185: }
186:
187: /**
188: * @see ISQLEntryPanel#setTabSize(int)
189: */
190: public void setTabSize(int tabSize) {
191: _comp.setTabSize(tabSize);
192: }
193:
194: public void setFont(Font font) {
195: _comp.setFont(font);
196: }
197:
198: /**
199: * @see ISQLEntryPanel#addMouseListener(MouseListener)
200: */
201: public void addMouseListener(MouseListener lis) {
202: _comp.addMouseListener(lis);
203: }
204:
205: /**
206: * @see ISQLEntryPanel#removeListener(MouseListener)
207: */
208: public void removeMouseListener(MouseListener lis) {
209: _comp.removeMouseListener(lis);
210: }
211:
212: /**
213: * @see ISQLEntryPanel#setCaretPosition(int)
214: */
215: public void setCaretPosition(int pos) {
216: _comp.setCaretPosition(pos);
217: }
218:
219: /*
220: * @see ISQLEntryPanel#getCaretLineNumber()
221: */
222: public int getCaretLineNumber() {
223: try {
224: return _comp.getLineOfOffset(_comp.getCaretPosition());
225: } catch (BadLocationException ex) {
226: return 0;
227: }
228: }
229:
230: public int getCaretLinePosition() {
231: int caretPos = _comp.getCaretPosition();
232: int caretLineOffset = caretPos;
233: try {
234: caretLineOffset = _comp
235: .getLineStartOffset(getCaretLineNumber());
236: } catch (BadLocationException ex) {
237: s_log.error("BadLocationException in getCaretLinePosition",
238: ex);
239: }
240: return caretPos - caretLineOffset;
241: }
242:
243: /**
244: * @see ISQLEntryPanel#getSelectionStart()
245: */
246: public int getSelectionStart() {
247: return _comp.getSelectionStart();
248: }
249:
250: /**
251: * @see ISQLEntryPanel#setSelectionStart(int)
252: */
253: public void setSelectionStart(int pos) {
254: _comp.setSelectionStart(pos);
255: }
256:
257: /**
258: * @see ISQLEntryPanel#getSelectionEnd()
259: */
260: public int getSelectionEnd() {
261: return _comp.getSelectionEnd();
262: }
263:
264: /**
265: * @see ISQLEntryPanel#setSelectionEnd(int)
266: */
267: public void setSelectionEnd(int pos) {
268: _comp.setSelectionEnd(pos);
269: }
270:
271: /**
272: * @see ISQLEntryPanel#hasFocus()
273: */
274: public boolean hasFocus() {
275: return _comp.hasFocus();
276: }
277:
278: /**
279: * @see ISQLEntryPanel#requestFocus()
280: */
281: public void requestFocus() {
282: _comp.requestFocus();
283: }
284:
285: /*
286: * @see ISQLEntryPanel#addCaretListener(CaretListener)
287: */
288: public void addCaretListener(CaretListener lis) {
289: _comp.addCaretListener(lis);
290: }
291:
292: /*
293: * @see ISQLEntryPanel#removeCaretListener(CaretListener)
294: */
295: public void removeCaretListener(CaretListener lis) {
296: _comp.removeCaretListener(lis);
297: }
298:
299: public void addSQLTokenListener(SQLTokenListener tl) {
300: // Not implemented
301: }
302:
303: public void removeSQLTokenListener(SQLTokenListener tl) {
304: // Not implemented
305: }
306:
307: public ISession getSession() {
308: return _session;
309: }
310:
311: private static class MyTextArea extends JTextArea {
312: private DefaultSQLEntryPanel _pnl;
313:
314: private MyTextArea(ISession session, DefaultSQLEntryPanel pnl) {
315: super ();
316: _pnl = pnl;
317: SessionProperties props = session.getProperties();
318: final FontInfo fi = props.getFontInfo();
319: if (fi != null) {
320: this .setFont(props.getFontInfo().createFont());
321: }
322: }
323:
324: // public void addNotify()
325: // {
326: // super.addNotify();
327: // _pnl.addMouseListener(_pnl._sqlEntryMouseListener);
328: // }
329: //
330: // public void removeNotify()
331: // {
332: // super.removeNotify();
333: // _pnl.removeMouseListener(_pnl._sqlEntryMouseListener);
334: // }
335: }
336:
337: /* (non-Javadoc)
338: * @see net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel#setUndoManager(javax.swing.undo.UndoManager)
339: */
340: public void setUndoManager(UndoManager manager) {
341: // no support for undo
342: }
343: }
|