KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > syntax > jedit > SyntaxDocument


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * SyntaxDocument.java
28  *
29  */

30
31 package org.syntax.jedit;
32
33 import org.syntax.jedit.tokenmarker.*;
34 import javax.swing.event.*;
35 import javax.swing.text.*;
36 import javax.swing.undo.UndoableEdit JavaDoc;
37 import javax.swing.undo.UndoManager JavaDoc;
38
39
40 /**
41  * A document implementation that can be tokenized by the syntax highlighting
42  * system.
43  *
44  * @author Slava Pestov
45  * @version $Id: SyntaxDocument.java 932 2006-10-20 09:32:45Z gtoffoli $
46  */

47 public class SyntaxDocument extends PlainDocument implements UndoableEditListener
48 {
49         UndoManager JavaDoc UM;
50
51         public SyntaxDocument()
52         {
53             UM = new UndoManager JavaDoc();
54             this.addUndoableEditListener((UndoableEditListener)this );
55         }
56
57         public void undoableEditHappened(UndoableEditEvent e) {
58             //Remember the edit and update the menus
59
UM.addEdit(e.getEdit());
60         }
61
62         
63     /**
64      * Returns the token marker that is to be used to split lines
65      * of this document up into tokens. May return null if this
66      * document is not to be colorized.
67      */

68     public TokenMarker getTokenMarker()
69     {
70         return tokenMarker;
71     }
72
73     /**
74      * Sets the token marker that is to be used to split lines of
75      * this document up into tokens. May throw an exception if
76      * this is not supported for this type of document.
77      * @param tm The new token marker
78      */

79     public void setTokenMarker(TokenMarker tm)
80     {
81         tokenMarker = tm;
82         if(tm == null)
83             return;
84         tokenMarker.insertLines(0,getDefaultRootElement()
85             .getElementCount());
86         tokenizeLines();
87     }
88
89     /**
90      * Reparses the document, by passing all lines to the token
91      * marker. This should be called after the document is first
92      * loaded.
93      */

94     public void tokenizeLines()
95     {
96         tokenizeLines(0,getDefaultRootElement().getElementCount());
97     }
98
99     /**
100      * Reparses the document, by passing the specified lines to the
101      * token marker. This should be called after a large quantity of
102      * text is first inserted.
103      * @param start The first line to parse
104      * @param len The number of lines, after the first one to parse
105      */

106     public void tokenizeLines(int start, int len)
107     {
108         if(tokenMarker == null || !tokenMarker.supportsMultilineTokens())
109             return;
110
111         Segment lineSegment = new Segment();
112         Element map = getDefaultRootElement();
113
114         len += start;
115
116         try
117         {
118             for(int i = start; i < len; i++)
119             {
120                 Element lineElement = map.getElement(i);
121                 int lineStart = lineElement.getStartOffset();
122                 getText(lineStart,lineElement.getEndOffset()
123                     - lineStart - 1,lineSegment);
124                 tokenMarker.markTokens(lineSegment,i);
125             }
126         }
127         catch(BadLocationException bl)
128         {
129             bl.printStackTrace();
130         }
131     }
132
133     /**
134      * Starts a compound edit that can be undone in one operation.
135      * Subclasses that implement undo should override this method;
136      * this class has no undo functionality so this method is
137      * empty.
138      */

139     public void beginCompoundEdit() {}
140
141     /**
142      * Ends a compound edit that can be undone in one operation.
143      * Subclasses that implement undo should override this method;
144      * this class has no undo functionality so this method is
145      * empty.
146      */

147     public void endCompoundEdit() {}
148
149     /**
150      * Adds an undoable edit to this document's undo list. The edit
151      * should be ignored if something is currently being undone.
152      * @param edit The undoable edit
153      *
154      * @since jEdit 2.2pre1
155      */

156     public void addUndoableEdit(UndoableEdit JavaDoc edit) {
157             //if (edit != null) UM.addEdit(edit);
158
}
159
160     // protected members
161
protected TokenMarker tokenMarker;
162
163     /**
164      * We overwrite this method to update the token marker
165      * state immediately so that any event listeners get a
166      * consistent token marker.
167      */

168     protected void fireInsertUpdate(DocumentEvent evt)
169     {
170         if(tokenMarker != null)
171         {
172             DocumentEvent.ElementChange ch = evt.getChange(
173                 getDefaultRootElement());
174             if(ch != null)
175             {
176                 tokenMarker.insertLines(ch.getIndex() + 1,
177                     ch.getChildrenAdded().length -
178                     ch.getChildrenRemoved().length);
179             }
180         }
181                 
182                 super.fireInsertUpdate(evt);
183     }
184     
185     /**
186      * We overwrite this method to update the token marker
187      * state immediately so that any event listeners get a
188      * consistent token marker.
189      */

190     protected void fireRemoveUpdate(DocumentEvent evt)
191     {
192         if(tokenMarker != null)
193         {
194             DocumentEvent.ElementChange ch = evt.getChange(
195                 getDefaultRootElement());
196             if(ch != null)
197             {
198                 tokenMarker.deleteLines(ch.getIndex() + 1,
199                     ch.getChildrenRemoved().length -
200                     ch.getChildrenAdded().length);
201             }
202         }
203
204         super.fireRemoveUpdate(evt);
205     }
206         
207         /** Getter for property UM.
208          * @return Value of property UM.
209          *
210          */

211         public javax.swing.undo.UndoManager JavaDoc getUM() {
212             if (UM == null) {
213                 UM = new UndoManager JavaDoc();
214             }
215             //System.out.println(k);
216
return UM;
217         }
218         
219         /** Setter for property UM.
220          * @param UM New value of property UM.
221          *
222          */

223         public void setUM(javax.swing.undo.UndoManager JavaDoc UM) {
224             this.UM = UM;
225         }
226         
227 }
228
Popular Tags