01: package net.sourceforge.squirrel_sql.plugins.syntax;
02:
03: import net.sourceforge.squirrel_sql.client.IApplication;
04: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
05: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
06: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
07: import net.sourceforge.squirrel_sql.client.session.action.ISQLPanelAction;
08: import net.sourceforge.squirrel_sql.plugins.syntax.SyntaxPluginResources;
09:
10: import java.awt.event.ActionEvent;
11:
12: public class UncommentAction extends SquirrelAction implements
13: ISQLPanelAction {
14: private ISQLPanelAPI _panel;
15: private ISQLEntryPanel _isqlEntryPanel;
16:
17: public UncommentAction(IApplication app, SyntaxPluginResources rsrc)
18: throws IllegalArgumentException {
19: super (app, rsrc);
20: }
21:
22: public UncommentAction(IApplication app,
23: SyntaxPluginResources rsrc, ISQLEntryPanel isqlEntryPanel) {
24: this (app, rsrc);
25: _isqlEntryPanel = isqlEntryPanel;
26: }
27:
28: public void actionPerformed(ActionEvent evt) {
29: if (null != _isqlEntryPanel) {
30: uncomment(_isqlEntryPanel);
31: } else if (null != _panel) {
32: uncomment(_panel.getSQLEntryPanel());
33: }
34: }
35:
36: private void uncomment(ISQLEntryPanel sqlEntryPanel) {
37: int[] bounds = sqlEntryPanel.getBoundsOfSQLToBeExecuted();
38:
39: if (bounds[0] == bounds[1]) {
40: return;
41: }
42:
43: int caretPosition = sqlEntryPanel.getCaretPosition();
44:
45: String textToComment = sqlEntryPanel.getText().substring(
46: bounds[0], bounds[1]);
47:
48: String[] lines = textToComment.split("\n");
49:
50: StringBuffer uncommentedLines = new StringBuffer();
51:
52: String startOfLineComment = sqlEntryPanel.getSession()
53: .getProperties().getStartOfLineComment();
54:
55: for (int i = 0; i < lines.length; i++) {
56: if (bounds[0] + uncommentedLines.length() < caretPosition) {
57: if (lines[i].startsWith(startOfLineComment)) {
58: caretPosition -= startOfLineComment.length();
59: }
60: }
61:
62: if (lines[i].startsWith(startOfLineComment)) {
63: uncommentedLines.append(lines[i]
64: .substring(startOfLineComment.length()));
65: } else {
66: uncommentedLines.append(lines[i]);
67: }
68:
69: if (i < lines.length - 1 || textToComment.endsWith("\n")) {
70: uncommentedLines.append("\n");
71: }
72:
73: }
74:
75: sqlEntryPanel.setSelectionStart(bounds[0]);
76: sqlEntryPanel.setSelectionEnd(bounds[1]);
77:
78: sqlEntryPanel.replaceSelection(uncommentedLines.toString());
79:
80: sqlEntryPanel.setCaretPosition(caretPosition);
81:
82: }
83:
84: public void setSQLPanel(ISQLPanelAPI panel) {
85: _panel = panel;
86: }
87:
88: }
|