001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * SyntaxDocument.java
028: *
029: */
030:
031: package org.syntax.jedit;
032:
033: import org.syntax.jedit.tokenmarker.*;
034: import javax.swing.event.*;
035: import javax.swing.text.*;
036: import javax.swing.undo.UndoableEdit;
037: import javax.swing.undo.UndoManager;
038:
039: /**
040: * A document implementation that can be tokenized by the syntax highlighting
041: * system.
042: *
043: * @author Slava Pestov
044: * @version $Id: SyntaxDocument.java 1167 2008-01-15 18:49:05Z gtoffoli $
045: */
046: public class SyntaxDocument extends PlainDocument implements
047: UndoableEditListener {
048: UndoManager UM;
049:
050: public SyntaxDocument() {
051: UM = new UndoManager();
052: this .addUndoableEditListener((UndoableEditListener) this );
053: }
054:
055: public void undoableEditHappened(UndoableEditEvent e) {
056: //Remember the edit and update the menus
057: UM.addEdit(e.getEdit());
058: }
059:
060: /**
061: * Returns the token marker that is to be used to split lines
062: * of this document up into tokens. May return null if this
063: * document is not to be colorized.
064: */
065: public TokenMarker getTokenMarker() {
066: return tokenMarker;
067: }
068:
069: /**
070: * Sets the token marker that is to be used to split lines of
071: * this document up into tokens. May throw an exception if
072: * this is not supported for this type of document.
073: * @param tm The new token marker
074: */
075: public void setTokenMarker(TokenMarker tm) {
076: tokenMarker = tm;
077: if (tm == null)
078: return;
079: tokenMarker.insertLines(0, getDefaultRootElement()
080: .getElementCount());
081: tokenizeLines();
082: }
083:
084: /**
085: * Reparses the document, by passing all lines to the token
086: * marker. This should be called after the document is first
087: * loaded.
088: */
089: public void tokenizeLines() {
090: tokenizeLines(0, getDefaultRootElement().getElementCount());
091: }
092:
093: /**
094: * Reparses the document, by passing the specified lines to the
095: * token marker. This should be called after a large quantity of
096: * text is first inserted.
097: * @param start The first line to parse
098: * @param len The number of lines, after the first one to parse
099: */
100: public void tokenizeLines(int start, int len) {
101: if (tokenMarker == null
102: || !tokenMarker.supportsMultilineTokens())
103: return;
104:
105: Segment lineSegment = new Segment();
106: Element map = getDefaultRootElement();
107:
108: len += start;
109:
110: try {
111: for (int i = start; i < len; i++) {
112: Element lineElement = map.getElement(i);
113: int lineStart = lineElement.getStartOffset();
114: getText(lineStart, lineElement.getEndOffset()
115: - lineStart - 1, lineSegment);
116: tokenMarker.markTokens(lineSegment, i);
117: }
118: } catch (BadLocationException bl) {
119: bl.printStackTrace();
120: }
121: }
122:
123: /**
124: * Starts a compound edit that can be undone in one operation.
125: * Subclasses that implement undo should override this method;
126: * this class has no undo functionality so this method is
127: * empty.
128: */
129: public void beginCompoundEdit() {
130: }
131:
132: /**
133: * Ends a compound edit that can be undone in one operation.
134: * Subclasses that implement undo should override this method;
135: * this class has no undo functionality so this method is
136: * empty.
137: */
138: public void endCompoundEdit() {
139: }
140:
141: /**
142: * Adds an undoable edit to this document's undo list. The edit
143: * should be ignored if something is currently being undone.
144: * @param edit The undoable edit
145: *
146: * @since jEdit 2.2pre1
147: */
148: public void addUndoableEdit(UndoableEdit edit) {
149: //if (edit != null) UM.addEdit(edit);
150: }
151:
152: // protected members
153: protected TokenMarker tokenMarker;
154:
155: /**
156: * We overwrite this method to update the token marker
157: * state immediately so that any event listeners get a
158: * consistent token marker.
159: */
160: protected void fireInsertUpdate(DocumentEvent evt) {
161: if (tokenMarker != null) {
162: DocumentEvent.ElementChange ch = evt
163: .getChange(getDefaultRootElement());
164: if (ch != null) {
165: tokenMarker.insertLines(ch.getIndex() + 1, ch
166: .getChildrenAdded().length
167: - ch.getChildrenRemoved().length);
168: }
169: }
170:
171: super .fireInsertUpdate(evt);
172: }
173:
174: /**
175: * We overwrite this method to update the token marker
176: * state immediately so that any event listeners get a
177: * consistent token marker.
178: */
179: protected void fireRemoveUpdate(DocumentEvent evt) {
180: if (tokenMarker != null) {
181: DocumentEvent.ElementChange ch = evt
182: .getChange(getDefaultRootElement());
183: if (ch != null) {
184: tokenMarker.deleteLines(ch.getIndex() + 1, ch
185: .getChildrenRemoved().length
186: - ch.getChildrenAdded().length);
187: }
188: }
189:
190: super .fireRemoveUpdate(evt);
191: }
192:
193: /** Getter for property UM.
194: * @return Value of property UM.
195: *
196: */
197: public javax.swing.undo.UndoManager getUM() {
198: if (UM == null) {
199: UM = new UndoManager();
200: }
201: //System.out.println(k);
202: return UM;
203: }
204:
205: /** Setter for property UM.
206: * @param UM New value of property UM.
207: *
208: */
209: public void setUM(javax.swing.undo.UndoManager UM) {
210: this.UM = UM;
211: }
212:
213: }
|