01: package net.sourceforge.squirrel_sql.plugins.syntax;
02:
03: import java.awt.event.ActionEvent;
04:
05: import javax.swing.text.BadLocationException;
06: import javax.swing.text.JTextComponent;
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.ISQLPanelAPI;
11: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
12: import net.sourceforge.squirrel_sql.client.session.action.ISQLPanelAction;
13:
14: public class DuplicateLineAction extends SquirrelAction implements
15: ISQLPanelAction {
16: private static final long serialVersionUID = 1L;
17:
18: transient private ISQLPanelAPI _panel;
19: private ISQLEntryPanel _isqlEntryPanel;
20:
21: public DuplicateLineAction(IApplication app,
22: SyntaxPluginResources rsrc) throws IllegalArgumentException {
23: super (app, rsrc);
24: }
25:
26: public DuplicateLineAction(IApplication app,
27: SyntaxPluginResources rsrc, ISQLEntryPanel isqlEntryPanel) {
28: this (app, rsrc);
29: _isqlEntryPanel = isqlEntryPanel;
30: }
31:
32: public void actionPerformed(ActionEvent evt) {
33:
34: if (null != _isqlEntryPanel) {
35: duplicateLineAction(_isqlEntryPanel);
36: } else if (null != _panel) {
37: duplicateLineAction(_panel.getSQLEntryPanel());
38: }
39: }
40:
41: private void duplicateLineAction(ISQLEntryPanel sqlEntryPanel) {
42: try {
43: JTextComponent txtComp = sqlEntryPanel.getTextComponent();
44:
45: int docLen = txtComp.getDocument().getLength();
46: String text = txtComp.getDocument().getText(0,
47: txtComp.getDocument().getLength());
48:
49: int caretPosition = txtComp.getCaretPosition();
50:
51: int lineBeg = 0;
52: for (int i = caretPosition - 1; i > 0; --i) {
53: if (text.charAt(i) == '\n') {
54: lineBeg = i;
55: break;
56: }
57: }
58:
59: int lineEnd = txtComp.getDocument().getLength();
60: for (int i = caretPosition; i < docLen; ++i) {
61: if (text.charAt(i) == '\n') {
62: lineEnd = i;
63: break;
64: }
65: }
66:
67: String line = text.substring(lineBeg, lineEnd);
68:
69: if (0 == lineBeg) {
70: line += "\n";
71: }
72:
73: txtComp.getDocument().insertString(lineBeg, line, null);
74:
75: } catch (BadLocationException e) {
76: throw new RuntimeException(e);
77: }
78: }
79:
80: public void setSQLPanel(ISQLPanelAPI panel) {
81: _panel = panel;
82: }
83:
84: }
|