KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > deprecated > ELDrawLayerFactory


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.modules.web.core.syntax.deprecated;
21
22 import org.netbeans.modules.web.core.syntax.deprecated.ELLayerTokenContext;
23 import org.netbeans.modules.web.core.syntax.*;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.event.DocumentListener JavaDoc;
26 import javax.swing.event.DocumentEvent JavaDoc;
27 import org.netbeans.editor.BaseDocument;
28 import org.netbeans.editor.BaseDocumentEvent;
29 import org.netbeans.editor.Coloring;
30 import org.netbeans.editor.DrawContext;
31 import org.netbeans.editor.DrawLayer;
32 import org.netbeans.editor.MarkFactory;
33 import org.netbeans.editor.FinderFactory;
34 import org.netbeans.editor.Analyzer;
35
36 /**
37 * Various EL-layers
38 *
39 * @author Petr Pisl
40 * @deprecated Will be replaced by Semantic Coloring
41 */

42
43 public class ELDrawLayerFactory {
44
45     public static final String JavaDoc EL_LAYER_NAME = "jsp-el-layer"; // NOI18N
46

47     public static final int EL_LAYER_VISIBILITY = 1010;
48
49     /** Layer that colors extra EL information like the methods or special
50      * characters in the character and string literals.
51      */

52     public static class ELLayer extends DrawLayer.AbstractLayer {
53
54         /** End of the area that is resolved right now. It saves
55          * repetitive searches for '(' for multiple fragments
56          * inside one identifier token.
57          */

58         private int resolvedEndOffset;
59
60         private boolean resolvedValue;
61
62         private NonWhitespaceFwdFinder nwFinder = new NonWhitespaceFwdFinder();
63
64         public ELLayer() {
65             super(EL_LAYER_NAME);
66         }
67
68         public void init(DrawContext ctx) {
69             resolvedEndOffset = 0; // nothing resolved
70
}
71
72         public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) {
73             int nextOffset = ctx.getTokenOffset() + ctx.getTokenLength();
74
75             setNextActivityChangeOffset(nextOffset);
76             return true;
77         }
78
79         protected Coloring getMethodColoring(DrawContext ctx) {
80         
81         /*TokenContextPath path = ctx.getTokenContextPath().replaceStart(
82             ELLayerTokenContext.contextPath);
83         return ctx.getEditorUI().getColoring(
84             path.getFullTokenName(ELLayerTokenContext.METHOD));
85         */

86        
87        return ctx.getEditorUI().getColoring(ELLayerTokenContext.contextPath.getFullTokenName(ELLayerTokenContext.METHOD));
88         }
89
90         private boolean isMethod(DrawContext ctx) {
91         
92             int idEndOffset = ctx.getTokenOffset() + ctx.getTokenLength();
93             if (idEndOffset > resolvedEndOffset) { // beyond the resolved area
94
resolvedEndOffset = idEndOffset; // will resolve now
95
int endOffset = ctx.getEndOffset();
96                 int bufferStartOffset = ctx.getBufferStartOffset();
97                 char[] buffer = ctx.getBuffer();
98                 int nwOffset = Analyzer.findFirstNonWhite(buffer,
99                         idEndOffset - bufferStartOffset,
100                         endOffset - idEndOffset);
101                 if (nwOffset >= 0) { // found non-white
102
resolvedValue = (buffer[nwOffset] == '(');
103                 } else { // must resolve after buffer end
104
try {
105                         resolvedValue = (ctx.getEditorUI().getDocument().find(
106                             nwFinder, endOffset, -1) >= 0)
107                                 && (nwFinder.getFoundChar() == '(');
108                     } catch (BadLocationException JavaDoc e) {
109                         resolvedValue = false;
110                     }
111                 }
112             }
113             return resolvedValue;
114         }
115     /** Update draw context by setting colors, fonts and possibly other draw
116     * properties.
117     * The method can use information from the context to find where the painting
118     * process is currently located. It is called only if the layer is active.
119     */

120         public void updateContext(DrawContext ctx) {
121             if (ctx.getTokenID() == ELTokenContext.IDENTIFIER && isMethod(ctx)) {
122                 Coloring mc = getMethodColoring(ctx);
123                 if (mc != null) {
124                     mc.apply(ctx);
125                 }
126             }
127         }
128
129     }
130
131     /** Find first non-white character forward */
132     static class NonWhitespaceFwdFinder extends FinderFactory.GenericFwdFinder {
133
134         private char foundChar;
135
136         public char getFoundChar() {
137             return foundChar;
138         }
139
140         protected int scan(char ch, boolean lastChar) {
141             if (!Character.isWhitespace(ch)) {
142                 found = true;
143                 foundChar = ch;
144                 return 0;
145             }
146             return 1;
147         }
148     }
149
150     /** Find first non-white character backward */
151     public static class NonWhitespaceBwdFinder extends FinderFactory.GenericBwdFinder {
152
153         private char foundChar;
154
155         public char getFoundChar() {
156             return foundChar;
157         }
158
159         protected int scan(char ch, boolean lastChar) {
160             if (!Character.isWhitespace(ch)) {
161                 found = true;
162                 foundChar = ch;
163                 return 0;
164             }
165             return -1;
166         }
167     }
168
169     /** This class watches whether the '(' character was inserted/removed.
170      * It ensures the appropriate part of the document till
171      * the previous non-whitespace will be repainted.
172      */

173     public static class LParenWatcher implements DocumentListener JavaDoc {
174
175         NonWhitespaceBwdFinder nwFinder = new NonWhitespaceBwdFinder();
176
177         private void check(DocumentEvent JavaDoc evt) {
178             if (evt.getDocument() instanceof BaseDocument) {
179                 BaseDocument doc = (BaseDocument)evt.getDocument();
180                 BaseDocumentEvent bevt = (BaseDocumentEvent)evt;
181                 char[] chars = bevt.getChars();
182                 if (chars != null) {
183                     boolean found = false;
184                     for (int i = chars.length - 1; i >= 0; i--) {
185                         if (chars[i] == '(') {
186                             found = true;
187                             break;
188                         }
189                     }
190             
191                     if (found) {
192                         int offset = evt.getOffset();
193                         // Need to repaint
194
int redrawOffset = 0;
195                         if (offset > 0) {
196                             try {
197                                 redrawOffset = doc.find(nwFinder, offset - 1, 0);
198                             } catch (BadLocationException JavaDoc e) {
199                 e.printStackTrace(System.out);
200                             }
201
202                             if (redrawOffset < 0) { // not found non-whitespace
203
redrawOffset = 0;
204                             }
205                         }
206                         doc.repaintBlock(redrawOffset, offset);
207                     }
208                 }
209             }
210         }
211
212         public void insertUpdate(DocumentEvent JavaDoc evt) {
213             check(evt);
214         }
215
216         public void removeUpdate(DocumentEvent JavaDoc evt) {
217             check(evt);
218         }
219
220         public void changedUpdate(DocumentEvent JavaDoc evt) {
221         }
222
223     }
224
225 }
226
227
Popular Tags