1 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 ; 25 import javax.swing.event.DocumentListener ; 26 import javax.swing.event.DocumentEvent ; 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 42 43 public class ELDrawLayerFactory { 44 45 public static final String EL_LAYER_NAME = "jsp-el-layer"; 47 public static final int EL_LAYER_VISIBILITY = 1010; 48 49 52 public static class ELLayer extends DrawLayer.AbstractLayer { 53 54 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; } 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 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) { resolvedEndOffset = idEndOffset; 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) { resolvedValue = (buffer[nwOffset] == '('); 103 } else { try { 105 resolvedValue = (ctx.getEditorUI().getDocument().find( 106 nwFinder, endOffset, -1) >= 0) 107 && (nwFinder.getFoundChar() == '('); 108 } catch (BadLocationException e) { 109 resolvedValue = false; 110 } 111 } 112 } 113 return resolvedValue; 114 } 115 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 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 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 173 public static class LParenWatcher implements DocumentListener { 174 175 NonWhitespaceBwdFinder nwFinder = new NonWhitespaceBwdFinder(); 176 177 private void check(DocumentEvent 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 int redrawOffset = 0; 195 if (offset > 0) { 196 try { 197 redrawOffset = doc.find(nwFinder, offset - 1, 0); 198 } catch (BadLocationException e) { 199 e.printStackTrace(System.out); 200 } 201 202 if (redrawOffset < 0) { redrawOffset = 0; 204 } 205 } 206 doc.repaintBlock(redrawOffset, offset); 207 } 208 } 209 } 210 } 211 212 public void insertUpdate(DocumentEvent evt) { 213 check(evt); 214 } 215 216 public void removeUpdate(DocumentEvent evt) { 217 check(evt); 218 } 219 220 public void changedUpdate(DocumentEvent evt) { 221 } 222 223 } 224 225 } 226 227 | Popular Tags |