KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > MarkFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import javax.swing.text.Position JavaDoc;
24
25 /**
26 * Various marks are located here
27 *
28 * @author Miloslav Metelka
29 * @version 1.00
30 */

31
32 public class MarkFactory {
33
34     private MarkFactory() {
35         // no instantiation
36
}
37
38     /** Syntax mark holds info about scan state of syntax scanner.
39      * This helps in redraws because reparsing after insert/delete is done
40      * only from nearest left syntax mark. Moreover rescaning is done only
41      * until there are marks with different scan state. As soon as mark
42      * is found with same parsing info as rescanning scanner has, parsing
43      * ends.
44      * @deprecated syntax marks are no longer used to hold lexer states.
45      */

46     public static class SyntaxMark extends Mark {
47
48         /** Syntax mark state info */
49         private Syntax.StateInfo stateInfo;
50
51         private TokenItem tokenItem;
52
53         /** Get state info of this mark */
54         public Syntax.StateInfo getStateInfo() {
55             return stateInfo;
56         }
57
58         public void updateStateInfo(Syntax syntax) {
59             if (stateInfo == null) {
60                 stateInfo = syntax.createStateInfo();
61             }
62             syntax.storeState(stateInfo);
63         }
64
65         void setStateInfo(Syntax.StateInfo stateInfo) {
66           this.stateInfo = stateInfo;
67         }
68
69         public TokenItem getTokenItem() {
70             return tokenItem;
71         }
72
73         void setTokenItem(TokenItem tokenItem) {
74             this.tokenItem = tokenItem;
75         }
76
77         /** When removal occurs */
78         protected void removeUpdateAction(int pos, int len) {
79             try {
80                 remove();
81             } catch (InvalidMarkException e) {
82                 // shouldn't happen
83
}
84         }
85
86     }
87
88     /** Mark that can have its position updated by where it's located */
89     public static class ContextMark extends Mark {
90
91         public ContextMark(boolean stayBOL) {
92             this(false, stayBOL);
93         }
94
95         public ContextMark(boolean insertAfter, boolean stayBOL) {
96             this(insertAfter ? Position.Bias.Backward : Position.Bias.Forward, stayBOL);
97         }
98
99         public ContextMark(Position.Bias JavaDoc bias, boolean stayBOL) {
100             super(bias);
101         }
102
103     }
104
105     /** Activation mark for particular layer. When layer is not active
106     * its updateContext() method is not called.
107     */

108     public static class DrawMark extends ContextMark {
109
110         /** Activation flag means either activate layer or deactivate it */
111         protected boolean activateLayer;
112
113         /** Reference to draw layer this mark belogns to */
114         String JavaDoc layerName;
115
116         /** Reference to extended UI if this draw mark is info-specific or
117         * null if it's document-wide.
118         */

119         WeakReference JavaDoc editorUIRef;
120
121         public DrawMark(String JavaDoc layerName, EditorUI editorUI) {
122             this(layerName, editorUI, Position.Bias.Forward);
123         }
124         
125         public DrawMark(String JavaDoc layerName, EditorUI editorUI, Position.Bias JavaDoc bias) {
126             super(bias, false);
127             this.layerName = layerName;
128             setEditorUI(editorUI);
129         }
130
131         public boolean isDocumentMark() {
132             return (editorUIRef == null);
133         }
134
135         public EditorUI getEditorUI() {
136             if (editorUIRef != null) {
137                 return (EditorUI)editorUIRef.get();
138             }
139             return null;
140         }
141
142         public void setEditorUI(EditorUI editorUI) {
143             this.editorUIRef = (editorUI != null) ? new WeakReference JavaDoc(editorUI) : null;
144         }
145
146         public boolean isValidUI() {
147             return !(editorUIRef != null && editorUIRef.get() == null);
148         }
149
150         public void setActivateLayer(boolean activateLayer) {
151             this.activateLayer = activateLayer;
152         }
153
154         public boolean getActivateLayer() {
155             return activateLayer;
156         }
157
158         public boolean removeInvalid() {
159             if (!isValidUI() && isValid()) {
160                 try {
161                     this.remove();
162                 } catch (InvalidMarkException e) {
163                     throw new IllegalStateException JavaDoc(e.toString());
164                 }
165                 return true; // invalid and removed
166
}
167             return false; // valid
168
}
169
170         public String JavaDoc toString() {
171             try {
172                 return "pos=" + getOffset() + ", line=" + getLine(); // NOI18N
173
} catch (InvalidMarkException e) {
174                 return "mark not valid"; // NOI18N
175
}
176         }
177
178     }
179
180     /** Support for draw marks chained in double linked list */
181     public static class ChainDrawMark extends DrawMark {
182
183         /** Next mark in chain */
184         protected ChainDrawMark next;
185
186         /** Previous mark in chain */
187         protected ChainDrawMark prev;
188
189         public ChainDrawMark(String JavaDoc layerName, EditorUI editorUI) {
190             this(layerName, editorUI, Position.Bias.Forward);
191         }
192         
193         public ChainDrawMark(String JavaDoc layerName, EditorUI editorUI, Position.Bias JavaDoc bias) {
194             super(layerName, editorUI, bias);
195         }
196
197         public final ChainDrawMark getNext() {
198             return next;
199         }
200
201         public final void setNext(ChainDrawMark mark) {
202             next = mark;
203         }
204
205         /** Set next mark in chain */
206         public void setNextChain(ChainDrawMark mark) {
207             this.next = mark;
208             if (mark != null) {
209                 mark.prev = this;
210             }
211         }
212
213         public final ChainDrawMark getPrev() {
214             return prev;
215         }
216
217         public final void setPrev(ChainDrawMark mark) {
218             prev = mark;
219         }
220
221         /** Set previous mark in chain */
222         public void setPrevChain(ChainDrawMark mark) {
223             this.prev = mark;
224             if (mark != null) {
225                 mark.next = this;
226             }
227         }
228
229         /** Insert mark before this one in chain
230         * @return inserted mark
231         */

232         public ChainDrawMark insertChain(ChainDrawMark mark) {
233             ChainDrawMark thisPrev = this.prev;
234             mark.prev = thisPrev;
235             mark.next = this;
236             if (thisPrev != null) {
237                 thisPrev.next = mark;
238             }
239             this.prev = mark;
240             return mark;
241         }
242
243         /** Remove this mark from the chain
244         * @return next chain member or null for end of chain
245         */

246         public ChainDrawMark removeChain() {
247             ChainDrawMark thisNext = this.next;
248             ChainDrawMark thisPrev = this.prev;
249             if (thisPrev != null) { // not the first
250
thisPrev.next = thisNext;
251                 this.prev = null;
252             }
253             if (thisNext != null) { // not the last
254
thisNext.prev = thisPrev;
255                 this.next = null;
256             }
257             try {
258                 this.remove(); // remove the mark from DocMarks
259
} catch (InvalidMarkException e) {
260                 // already removed
261
}
262             return thisNext;
263         }
264
265         public String JavaDoc toStringChain() {
266             return toString() + (next != null ? "\n" + next.toStringChain() : ""); // NOI18N
267
}
268
269         public String JavaDoc toString() {
270             return super.toString() + ", " // NOI18N
271
+ ((prev != null) ? ((next != null) ? "chain member" // NOI18N
272
: "last member") : ((next != null) ? "first member" // NOI18N
273
: "standalone member")); // NOI18N
274
}
275
276     }
277 }
278
Popular Tags