001: package net.sourceforge.squirrel_sql.plugins.syntax.netbeans;
002:
003: /*
004: * Copyright (C) 2004 Gerd Wagner
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.ActionEvent;
024: import java.awt.event.MouseListener;
025: import java.util.HashMap;
026:
027: import javax.swing.SwingUtilities;
028: import javax.swing.event.CaretListener;
029: import javax.swing.event.UndoableEditListener;
030: import javax.swing.text.Document;
031: import javax.swing.text.Element;
032: import javax.swing.text.JTextComponent;
033: import javax.swing.text.PlainDocument;
034: import javax.swing.undo.UndoManager;
035:
036: import net.sourceforge.squirrel_sql.client.IApplication;
037: import net.sourceforge.squirrel_sql.client.gui.dnd.FileEditorDropTargetListener;
038: import net.sourceforge.squirrel_sql.client.session.BaseSQLEntryPanel;
039: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
040: import net.sourceforge.squirrel_sql.client.session.ISession;
041: import net.sourceforge.squirrel_sql.client.session.SQLTokenListener;
042: import net.sourceforge.squirrel_sql.client.session.parser.IParserEventsProcessor;
043: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
044: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
045: import net.sourceforge.squirrel_sql.plugins.syntax.SyntaxPreferences;
046: import net.sourceforge.squirrel_sql.plugins.syntax.SyntaxPugin;
047:
048: import org.netbeans.editor.ext.ExtKit;
049:
050: public class NetbeansSQLEntryPanel extends BaseSQLEntryPanel {
051: /** Logger for this class. */
052: private static final ILogger s_log = LoggerController
053: .createLogger(NetbeansSQLEntryPanel.class);
054:
055: /** Application API. */
056: @SuppressWarnings("unused")
057: private IApplication _app;
058:
059: /** Text component. */
060: private NetbeansSQLEditorPane _textArea;
061:
062: private SyntaxFactory _syntaxFactory;
063:
064: private ISession _session;
065:
066: private SyntaxPugin _plugin;
067:
068: private NetbeansPropertiesWrapper _propertiesWrapper;
069:
070: @SuppressWarnings("unused")
071: private DropTarget dt;
072:
073: NetbeansSQLEntryPanel(ISession session, SyntaxPreferences prefs,
074: SyntaxFactory syntaxFactory, SyntaxPugin plugin,
075: HashMap<String, Object> props) {
076: super (session.getApplication());
077: if (session == null) {
078: throw new IllegalArgumentException("Null ISession passed");
079: }
080:
081: _propertiesWrapper = new NetbeansPropertiesWrapper(props);
082:
083: _plugin = plugin;
084:
085: _syntaxFactory = syntaxFactory;
086: _session = session;
087: _plugin = plugin;
088:
089: _app = session.getApplication();
090:
091: _textArea = new NetbeansSQLEditorPane(session, prefs,
092: syntaxFactory, _plugin, getIdentifier(),
093: _propertiesWrapper);
094:
095: dt = new DropTarget(_textArea,
096: new FileEditorDropTargetListener(session));
097: }
098:
099: public int getCaretLineNumber() {
100: final int pos = getCaretPosition();
101: final Document doc = _textArea.getDocument();
102: final Element docElem = doc.getDefaultRootElement();
103: return docElem.getElementIndex(pos);
104: }
105:
106: /**
107: * @see ISQLEntryPanel#gettextComponent()
108: */
109: public JTextComponent getTextComponent() {
110: return _textArea;
111: }
112:
113: /**
114: * If the component returned by <TT>getTextComponent</TT> contains its own
115: * scroll bars return <TT>true</TT> other wise this component will be
116: * wrapped in the scroll pane when added to the SQL panel.
117: *
118: * @return <TT>true</TT> if text component already handles scrolling.
119: */
120: public boolean getDoesTextComponentHaveScroller() {
121: return false;
122: }
123:
124: /**
125: * @see ISQLEntryPanel#getText()
126: */
127: public String getText() {
128: return _textArea.getText();
129: }
130:
131: public void setFont(Font font) {
132: // See SQLSettingsInitializer to find out how fonts are
133: // handled in the Netbeans editor.
134: // _textArea.setFont(font);
135: }
136:
137: /**
138: * @see ISQLEntryPanel#getSelectedText()
139: */
140: public String getSelectedText() {
141: return _textArea.getSelectedText();
142: }
143:
144: /**
145: * Replace the contents of the SQL entry area with the passed SQL script
146: * without selecting it.
147: *
148: * @param sqlScript
149: * The script to be placed in the SQL entry area..
150: */
151: public void setText(String text) {
152: setText(text, true);
153: triggerParser();
154: }
155:
156: /**
157: * Replace the contents of the SQL entry area with the passed SQL script and
158: * specify whether to select it.
159: *
160: * @param sqlScript
161: * The script to be placed in the SQL entry area..
162: * @param select
163: * If <TT>true</TT> then select the passed script in the sql
164: * entry area.
165: */
166: public void setText(String text, boolean select) {
167: _textArea.setText(text);
168: if (select) {
169: setSelectionEnd(_textArea.getDocument().getLength());
170: setSelectionStart(0);
171: }
172: triggerParser();
173: }
174:
175: /**
176: * Append the passed SQL script to the SQL entry area but don't select it.
177: *
178: * @param sqlScript
179: * The script to be appended.
180: */
181: public void appendText(String sqlScript) {
182: appendText(sqlScript, false);
183: }
184:
185: /**
186: * Append the passed SQL script to the SQL entry area and specify whether it
187: * should be selected.
188: *
189: * @param sqlScript
190: * The script to be appended.
191: * @param select
192: * If <TT>true</TT> then select the passed script in the sql
193: * entry area.
194: */
195: public void appendText(String sqlScript, boolean select) {
196: Document doc = _textArea.getDocument();
197:
198: try {
199: int start = 0;
200: if (select) {
201: start = doc.getLength();
202: }
203:
204: doc.insertString(doc.getLength(), sqlScript, null);
205:
206: if (select) {
207: setSelectionEnd(doc.getLength());
208: setSelectionStart(start);
209: }
210:
211: triggerParser();
212:
213: } catch (Exception ex) {
214: s_log.error("Error appending text to text area", ex);
215: }
216: }
217:
218: /**
219: * @see ISQLEntryPanel#getCaretPosition()
220: */
221: public int getCaretPosition() {
222: return _textArea.getCaretPosition();
223: }
224:
225: public void setCaretPosition(int value) {
226: _textArea.setCaretPosition(value);
227: }
228:
229: /**
230: * @see ISQLEntryPanel#setTabSize(int)
231: */
232: public void setTabSize(int tabSize) {
233: _textArea.getDocument().putProperty(
234: PlainDocument.tabSizeAttribute,
235: Integer.valueOf(tabSize));
236: }
237:
238: /**
239: * @see ISQLEntryPanel#getSelectionStart()
240: */
241: public int getSelectionStart() {
242: return _textArea.getSelectionStart();
243: }
244:
245: /**
246: * @see ISQLEntryPanel#setSelectionStart(int)
247: */
248: public void setSelectionStart(int pos) {
249: _textArea.setSelectionStart(pos);
250: }
251:
252: /**
253: * @see ISQLEntryPanel#getSelectionEnd()
254: */
255: public int getSelectionEnd() {
256: return _textArea.getSelectionEnd();
257: }
258:
259: /**
260: * @see ISQLEntryPanel#setSelectionEnd(int)
261: */
262: public void setSelectionEnd(int pos) {
263: _textArea.setSelectionEnd(pos);
264: }
265:
266: /**
267: * Replace the currently selected text in the SQL entry area with the passed
268: * text.
269: *
270: * @param sqlScript
271: * The script to be placed in the SQL entry area.
272: */
273: public void replaceSelection(String sqlScript) {
274: _textArea.replaceSelection(sqlScript);
275:
276: triggerParser();
277:
278: }
279:
280: private void triggerParser() {
281: IParserEventsProcessor parserEventsProcessor = _propertiesWrapper
282: .getParserEventsProcessor(getIdentifier(), _session);
283:
284: if (null != parserEventsProcessor) {
285: parserEventsProcessor.triggerParser();
286: }
287: }
288:
289: /**
290: * @see ISQLEntryPanel#hasFocus()
291: */
292: public boolean hasFocus() {
293: return _textArea.hasFocus();
294: }
295:
296: /**
297: * @see ISQLEntryPanel#requestFocus()
298: */
299: public void requestFocus() {
300: SwingUtilities.invokeLater(new Runnable() {
301: public void run() {
302: _textArea.requestFocus();
303: }
304: });
305: }
306:
307: /**
308: * @see ISQLEntryPanel#addMouseListener(java.awt.event.MouseListener)
309: */
310: public void addMouseListener(MouseListener lis) {
311: _textArea.addMouseListener(lis);
312: }
313:
314: /**
315: * @see ISQLEntryPanel#removeMouseListener(java.awt.event.MouseListener)
316: */
317: public void removeMouseListener(MouseListener lis) {
318: _textArea.removeMouseListener(lis);
319: }
320:
321: public void updateFromPreferences() {
322: _textArea.updateFromPreferences();
323: }
324:
325: /**
326: * @see ISQLEntryPanel#hasOwnUndoableManager()
327: */
328: public boolean hasOwnUndoableManager() {
329: return false;
330: }
331:
332: /**
333: * @see ISQLEntryPanel#addUndoableEditListener(javax.swing.event.UndoableEditListener)
334: */
335: public void addUndoableEditListener(UndoableEditListener listener) {
336: _textArea.addUndoableEditListener(listener);
337: }
338:
339: /**
340: * @see ISQLEntryPanel#removeUndoableEditListener(javax.swing.event.UndoableEditListener)
341: */
342: public void removeUndoableEditListener(UndoableEditListener listener) {
343: _textArea.getDocument().removeUndoableEditListener(listener);
344: }
345:
346: /**
347: * @see ISQLEntryPanel#getCaretLinePosition()
348: */
349: public int getCaretLinePosition() {
350: String textTillCarret = getText().substring(0,
351: getCaretPosition());
352:
353: int lineFeedIndex = textTillCarret.lastIndexOf('\n');
354: if (-1 == lineFeedIndex) {
355: return getCaretPosition();
356: } else {
357: return getCaretPosition() - lineFeedIndex - 1;
358: }
359:
360: // this didn't work
361: // final int pos = getCaretPosition();
362: // final Document doc = _textArea.getStyledDocument();
363: // final Element docElem = doc.getDefaultRootElement();
364: // final Element lineElem = docElem.getElement(getCaretLineNumber());
365: // return lineElem.getElementIndex(pos);
366: }
367:
368: /**
369: * @see ISQLEntryPanel#addCaretListener(javax.swing.event.CaretListener)
370: */
371: public void addCaretListener(CaretListener lis) {
372: _textArea.addCaretListener(lis);
373: }
374:
375: /**
376: * @see ISQLEntryPanel#removeCaretListener(javax.swing.event.CaretListener)
377: */
378: public void removeCaretListener(CaretListener lis) {
379: _textArea.removeCaretListener(lis);
380: }
381:
382: public void addSQLTokenListener(SQLTokenListener tl) {
383: _syntaxFactory.addSQLTokenListeners(_session, tl);
384: }
385:
386: public void removeSQLTokenListener(SQLTokenListener tl) {
387: _syntaxFactory.addSQLTokenListeners(_session, tl);
388: }
389:
390: public ISession getSession() {
391: return _session;
392: }
393:
394: public void showFindDialog(ActionEvent evt) {
395: SQLKit kit = (SQLKit) _textArea.getEditorKit();
396: kit.getActionByName(ExtKit.findAction).actionPerformed(evt);
397: }
398:
399: public void showReplaceDialog(ActionEvent evt) {
400: SQLKit kit = (SQLKit) _textArea.getEditorKit();
401: kit.getActionByName(ExtKit.replaceAction).actionPerformed(evt);
402: }
403:
404: /*
405: * (non-Javadoc)
406: *
407: * @see net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel#setUndoManager(javax.swing.undo.UndoManager)
408: */
409: public void setUndoManager(UndoManager manager) {
410: _textArea.setUndoManager(manager);
411: }
412:
413: /**
414: * Sets the session referenced by this class to null so that it can be
415: * garbage-collected.
416: */
417: public void sessionEnding() {
418: _session = null;
419: }
420: }
|