001: /*
002: * TextCommenter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.editor;
013:
014: import javax.swing.text.BadLocationException;
015: import workbench.log.LogMgr;
016:
017: /**
018: *
019: * @author support@sql-workbench.net
020: */
021: public class TextCommenter {
022: private JEditTextArea editor;
023:
024: public TextCommenter(JEditTextArea client) {
025: this .editor = client;
026: }
027:
028: public void commentSelection() {
029: String commentChar = editor.getCommentChar();
030: boolean isCommented = this .isSelectionCommented(commentChar);
031: // Comment Selection acts as a toggle.
032: // if the complete selection is already commented
033: // the comments will be removed.
034: doComment(commentChar, !isCommented);
035: }
036:
037: public void unCommentSelection() {
038: doComment(editor.getCommentChar(), false);
039: }
040:
041: private void doComment(String commentChar, boolean comment) {
042: int startline = editor.getSelectionStartLine();
043: int realEndline = editor.getSelectionEndLine();
044: int endline = realEndline;
045:
046: if (commentChar == null)
047: commentChar = "--";
048:
049: int cLength = commentChar.length();
050:
051: int pos = editor.getSelectionEnd(endline)
052: - editor.getLineStartOffset(endline);
053: if (pos == 0 && endline > 0)
054: endline--;
055: SyntaxDocument document = editor.getDocument();
056:
057: try {
058: document.beginCompoundEdit();
059: for (int line = startline; line <= endline; line++) {
060: String text = editor.getLineText(line);
061: if (text == null || text.trim().length() == 0)
062: continue;
063: int lineStart = editor.getLineStartOffset(line);
064: if (comment) {
065: document.insertString(lineStart, commentChar, null);
066: } else {
067: pos = text.indexOf(commentChar);
068: if (pos > -1) {
069: document.remove(lineStart, pos + cLength);
070: }
071: }
072: }
073: } catch (BadLocationException e) {
074: LogMgr.logError("TextManipulator.doComment()",
075: "Error when processing comment", e);
076: } finally {
077: document.endCompoundEdit();
078: }
079: }
080:
081: protected boolean isSelectionCommented(String commentChar) {
082: int startline = editor.getSelectionStartLine();
083: int realEndline = editor.getSelectionEndLine();
084: int endline = realEndline;
085: if (commentChar == null)
086: commentChar = "--";
087:
088: int pos = editor.getSelectionEnd(endline)
089: - editor.getLineStartOffset(endline);
090: if (pos == 0 && endline > 0)
091: endline--;
092:
093: for (int line = startline; line <= endline; line++) {
094: String text = editor.getLineText(line);
095: if (text == null || text.trim().length() == 0)
096: continue;
097: if (!text.startsWith(commentChar))
098: return false;
099: }
100: return true;
101: }
102:
103: }
|