svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / jedit / SyntaxDocument.java @ 40561
History | View | Annotate | Download (5.17 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* 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, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils.console.jedit; |
25 |
/*
|
26 |
* SyntaxDocument.java - Document that can be tokenized
|
27 |
* Copyright (C) 1999 Slava Pestov
|
28 |
*
|
29 |
* You may use and modify this package for any purpose. Redistribution is
|
30 |
* permitted, in both source and binary form, provided that this notice
|
31 |
* remains intact in all source distributions of this package.
|
32 |
*/
|
33 |
|
34 |
import javax.swing.event.DocumentEvent; |
35 |
import javax.swing.text.BadLocationException; |
36 |
import javax.swing.text.Element; |
37 |
import javax.swing.text.PlainDocument; |
38 |
import javax.swing.text.Segment; |
39 |
import javax.swing.undo.UndoableEdit; |
40 |
|
41 |
/**
|
42 |
* A document implementation that can be tokenized by the syntax highlighting
|
43 |
* system.
|
44 |
*
|
45 |
* @author Slava Pestov
|
46 |
* @version $Id$
|
47 |
*/
|
48 |
public class SyntaxDocument extends PlainDocument |
49 |
{ |
50 |
/**
|
51 |
* Returns the token marker that is to be used to split lines
|
52 |
* of this document up into tokens. May return null if this
|
53 |
* document is not to be colorized.
|
54 |
*/
|
55 |
public TokenMarker getTokenMarker()
|
56 |
{ |
57 |
return tokenMarker;
|
58 |
} |
59 |
|
60 |
/**
|
61 |
* Sets the token marker that is to be used to split lines of
|
62 |
* this document up into tokens. May throw an exception if
|
63 |
* this is not supported for this type of document.
|
64 |
* @param tm The new token marker
|
65 |
*/
|
66 |
public void setTokenMarker(TokenMarker tm) |
67 |
{ |
68 |
tokenMarker = tm; |
69 |
if(tm == null) |
70 |
return;
|
71 |
tokenMarker.insertLines(0,getDefaultRootElement()
|
72 |
.getElementCount()); |
73 |
tokenizeLines(); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Reparses the document, by passing all lines to the token
|
78 |
* marker. This should be called after the document is first
|
79 |
* loaded.
|
80 |
*/
|
81 |
public void tokenizeLines() |
82 |
{ |
83 |
tokenizeLines(0,getDefaultRootElement().getElementCount());
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* Reparses the document, by passing the specified lines to the
|
88 |
* token marker. This should be called after a large quantity of
|
89 |
* text is first inserted.
|
90 |
* @param start The first line to parse
|
91 |
* @param len The number of lines, after the first one to parse
|
92 |
*/
|
93 |
public void tokenizeLines(int start, int len) |
94 |
{ |
95 |
if(tokenMarker == null || !tokenMarker.supportsMultilineTokens()) |
96 |
return;
|
97 |
|
98 |
Segment lineSegment = new Segment(); |
99 |
Element map = getDefaultRootElement();
|
100 |
|
101 |
len += start; |
102 |
|
103 |
try
|
104 |
{ |
105 |
for(int i = start; i < len; i++) |
106 |
{ |
107 |
Element lineElement = map.getElement(i);
|
108 |
int lineStart = lineElement.getStartOffset();
|
109 |
getText(lineStart,lineElement.getEndOffset() |
110 |
- lineStart - 1,lineSegment);
|
111 |
tokenMarker.markTokens(lineSegment,i); |
112 |
} |
113 |
} |
114 |
catch(BadLocationException bl) |
115 |
{ |
116 |
bl.printStackTrace(); |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Starts a compound edit that can be undone in one operation.
|
122 |
* Subclasses that implement undo should override this method;
|
123 |
* this class has no undo functionality so this method is
|
124 |
* empty.
|
125 |
*/
|
126 |
public void beginCompoundEdit() {} |
127 |
|
128 |
/**
|
129 |
* Ends a compound edit that can be undone in one operation.
|
130 |
* Subclasses that implement undo should override this method;
|
131 |
* this class has no undo functionality so this method is
|
132 |
* empty.
|
133 |
*/
|
134 |
public void endCompoundEdit() {} |
135 |
|
136 |
/**
|
137 |
* Adds an undoable edit to this document's undo list. The edit
|
138 |
* should be ignored if something is currently being undone.
|
139 |
* @param edit The undoable edit
|
140 |
*
|
141 |
* @since jEdit 2.2pre1
|
142 |
*/
|
143 |
public void addUndoableEdit(UndoableEdit edit) {} |
144 |
|
145 |
// protected members
|
146 |
protected TokenMarker tokenMarker;
|
147 |
|
148 |
/**
|
149 |
* We overwrite this method to update the token marker
|
150 |
* state immediately so that any event listeners get a
|
151 |
* consistent token marker.
|
152 |
*/
|
153 |
protected void fireInsertUpdate(DocumentEvent evt) |
154 |
{ |
155 |
if(tokenMarker != null) |
156 |
{ |
157 |
DocumentEvent.ElementChange ch = evt.getChange(
|
158 |
getDefaultRootElement()); |
159 |
if(ch != null) |
160 |
{ |
161 |
tokenMarker.insertLines(ch.getIndex() + 1,
|
162 |
ch.getChildrenAdded().length - |
163 |
ch.getChildrenRemoved().length); |
164 |
} |
165 |
} |
166 |
|
167 |
super.fireInsertUpdate(evt);
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* We overwrite this method to update the token marker
|
172 |
* state immediately so that any event listeners get a
|
173 |
* consistent token marker.
|
174 |
*/
|
175 |
protected void fireRemoveUpdate(DocumentEvent evt) |
176 |
{ |
177 |
if(tokenMarker != null) |
178 |
{ |
179 |
DocumentEvent.ElementChange ch = evt.getChange(
|
180 |
getDefaultRootElement()); |
181 |
if(ch != null) |
182 |
{ |
183 |
tokenMarker.deleteLines(ch.getIndex() + 1,
|
184 |
ch.getChildrenRemoved().length - |
185 |
ch.getChildrenAdded().length); |
186 |
} |
187 |
} |
188 |
|
189 |
super.fireRemoveUpdate(evt);
|
190 |
} |
191 |
} |