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 CommentAction extends SquirrelAction implements
13: ISQLPanelAction {
14: private ISQLPanelAPI _panel;
15: private ISQLEntryPanel _isqlEntryPanel;
16:
17: public CommentAction(IApplication app, SyntaxPluginResources rsrc)
18: throws IllegalArgumentException {
19: super (app, rsrc);
20: }
21:
22: public CommentAction(IApplication app, SyntaxPluginResources rsrc,
23: ISQLEntryPanel isqlEntryPanel) {
24: this (app, rsrc);
25: _isqlEntryPanel = isqlEntryPanel;
26: }
27:
28: public void actionPerformed(ActionEvent evt) {
29: if (null != _isqlEntryPanel) {
30: comment(_isqlEntryPanel);
31: } else if (null != _panel) {
32: comment(_panel.getSQLEntryPanel());
33: }
34: }
35:
36: private void comment(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 commentedLines = 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] + commentedLines.length() <= caretPosition) {
57: caretPosition += startOfLineComment.length();
58: }
59:
60: commentedLines.append(startOfLineComment).append(lines[i]);
61: if (i < lines.length - 1 || textToComment.endsWith("\n")) {
62: commentedLines.append("\n");
63: }
64:
65: }
66:
67: sqlEntryPanel.setSelectionStart(bounds[0]);
68: sqlEntryPanel.setSelectionEnd(bounds[1]);
69:
70: sqlEntryPanel.replaceSelection(commentedLines.toString());
71:
72: sqlEntryPanel.setCaretPosition(caretPosition);
73:
74: }
75:
76: public void setSQLPanel(ISQLPanelAPI panel) {
77: _panel = panel;
78: }
79:
80: }
|