1 19 package org.netbeans.modules.java.editor.semantic; 20 21 import java.awt.Color ; 22 import java.text.MessageFormat ; 23 import java.text.ParseException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.List ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Document ; 32 import javax.swing.text.Position ; 33 import javax.swing.text.Position.Bias; 34 import javax.swing.text.StyledDocument ; 35 import org.netbeans.editor.Coloring; 36 import org.netbeans.modules.editor.errorstripe.privatespi.Mark; 37 import org.netbeans.modules.editor.errorstripe.privatespi.Status; 38 import org.netbeans.modules.editor.highlights.spi.Highlight; 39 import org.openide.text.NbDocument; 40 41 45 final class HighlightImpl implements Mark, Highlight { 46 47 private Document doc; 48 private Position start; 49 private Position start2; 50 private Position end; 51 private Position end2; 52 private Collection <ColoringAttributes> colorings; 53 private Color esColor; 54 55 public HighlightImpl(Document doc, Position start, Position end, Collection <ColoringAttributes> colorings) { 56 this(doc, start, end, colorings, null); 57 } 58 59 60 public HighlightImpl(Document doc, Position start, Position end, Collection <ColoringAttributes> colorings, Color esColor) { 61 this.doc = doc; 62 this.start = start; 63 this.end = end; 64 this.colorings = colorings; 65 this.esColor = esColor; 66 } 67 68 public HighlightImpl(Document doc, int start, int end, Collection <ColoringAttributes> colorings, Color esColor) throws BadLocationException { 69 this.doc = doc; 70 this.start = NbDocument.createPosition(doc, start, Bias.Forward); 71 this.start = NbDocument.createPosition(doc, start, Bias.Backward); 72 this.end = NbDocument.createPosition(doc, end, Bias.Backward); 73 this.end2 = NbDocument.createPosition(doc, end, Bias.Forward); 74 this.colorings = colorings; 75 this.esColor = esColor; 76 } 77 78 public int getEnd() { 79 int endPos = end.getOffset(); 80 81 if (end2 == null) 82 return endPos; 83 84 int endPos2 = end2.getOffset(); 85 86 if (endPos == endPos2) 87 return endPos; 88 89 try { 90 String added = doc.getText(endPos, endPos2 - endPos); 91 int newEndPos = endPos; 92 93 for (char c : added.toCharArray()) { 94 if (Character.isJavaIdentifierPart(c)) 95 newEndPos++; 96 } 97 98 if (newEndPos != endPos) { 99 end = NbDocument.createPosition(doc, newEndPos, Bias.Backward); 100 end2 = NbDocument.createPosition(doc, newEndPos, Bias.Forward); 101 } 102 103 return newEndPos; 104 } catch (BadLocationException e) { 105 Logger.getLogger("global").log(Level.FINE, e.getMessage(), e); 106 return endPos; 107 } 108 } 109 110 public int getStart() { 111 int startPos = start.getOffset(); 112 113 if (start2 == null) 114 return startPos; 115 116 int startPos2 = start2.getOffset(); 117 118 if (startPos == startPos2) 119 return startPos; 120 121 try { 122 String added = doc.getText(startPos, startPos2 - startPos); 123 int newStartPos = startPos; 124 125 for (char c : added.toCharArray()) { 126 if (Character.isJavaIdentifierPart(c)) 127 newStartPos++; 128 } 129 130 if (newStartPos != startPos) { 131 start = NbDocument.createPosition(doc, newStartPos, Bias.Forward); 132 start2 = NbDocument.createPosition(doc, newStartPos, Bias.Backward); 133 } 134 135 return newStartPos; 136 } catch (BadLocationException e) { 137 Logger.getLogger("global").log(Level.FINE, e.getMessage(), e); 138 return startPos; 139 } 140 } 141 142 public Coloring getColoring() { 143 return ColoringManager.getColoring(colorings); 144 } 145 146 public String toString() { 147 return "Highlight: [" + colorings + ", " + start.getOffset() + "-" + end.getOffset() + "]"; 148 } 149 150 public int getType() { 151 return TYPE_ERROR_LIKE; 152 } 153 154 public Status getStatus() { 155 return Status.STATUS_OK; 156 } 157 158 public int getPriority() { 159 return PRIORITY_DEFAULT; 160 } 161 162 public Color getEnhancedColor() { 163 return esColor; 164 } 165 166 public int[] getAssignedLines() { 167 int line = NbDocument.findLineNumber((StyledDocument ) doc, start.getOffset()); 168 169 return new int[] {line, line}; 170 } 171 172 public String getShortDescription() { 173 return "..."; 174 } 175 176 public String getHighlightTestData() { 177 int lineStart = NbDocument.findLineNumber((StyledDocument ) doc, start.getOffset()); 178 int columnStart = NbDocument.findLineColumn((StyledDocument ) doc, start.getOffset()); 179 int lineEnd = NbDocument.findLineNumber((StyledDocument ) doc, end.getOffset()); 180 int columnEnd = NbDocument.findLineColumn((StyledDocument ) doc, end.getOffset()); 181 182 return coloringsToString() + ", " + lineStart + ":" + columnStart + "-" + lineEnd + ":" + columnEnd; 183 } 184 185 private String coloringsToString() { 186 StringBuffer result = new StringBuffer (); 187 boolean first = true; 188 189 result.append("["); 190 191 for (ColoringAttributes attribute : coloringsAttributesOrder) { 192 if (colorings.contains(attribute)) { 193 if (!first) { 194 result.append(", "); 195 } 196 197 first = false; 198 result.append(attribute.name()); 199 } 200 } 201 202 result.append("]"); 203 204 return result.toString(); 205 } 206 207 Collection <ColoringAttributes> coloringsAttributesOrder = Arrays.asList(new ColoringAttributes[] { 208 ColoringAttributes.STATIC, 209 ColoringAttributes.ABSTRACT, 210 211 ColoringAttributes.PUBLIC, 212 ColoringAttributes.PROTECTED, 213 ColoringAttributes.PACKAGE_PRIVATE, 214 ColoringAttributes.PRIVATE, 215 216 ColoringAttributes.DEPRECATED, 217 218 ColoringAttributes.FIELD, 219 ColoringAttributes.LOCAL_VARIABLE, 220 ColoringAttributes.PARAMETER, 221 ColoringAttributes.METHOD, 222 ColoringAttributes.CONSTRUCTOR, 223 ColoringAttributes.CLASS, 224 225 ColoringAttributes.UNUSED, 226 227 ColoringAttributes.TYPE_PARAMETER_DECLARATION, 228 ColoringAttributes.TYPE_PARAMETER_USE, 229 230 ColoringAttributes.UNDEFINED, 231 232 ColoringAttributes.MARK_OCCURRENCES, 233 }); 234 235 public static HighlightImpl parse(StyledDocument doc, String line) throws ParseException , BadLocationException { 236 MessageFormat f = new MessageFormat ("[{0}], {1,number,integer}:{2,number,integer}-{3,number,integer}:{4,number,integer}"); 237 Object [] args = f.parse(line); 238 239 String attributesString = (String ) args[0]; 240 int lineStart = ((Long ) args[1]).intValue(); 241 int columnStart = ((Long ) args[2]).intValue(); 242 int lineEnd = ((Long ) args[3]).intValue(); 243 int columnEnd = ((Long ) args[4]).intValue(); 244 245 String [] attrElements = attributesString.split(","); 246 List <ColoringAttributes> attributes = new ArrayList <ColoringAttributes>(); 247 248 for (String a : attrElements) { 249 a = a.trim(); 250 251 attributes.add(ColoringAttributes.valueOf(a)); 252 } 253 254 if (attributes.contains(null)) 255 throw new NullPointerException (); 256 257 int offsetStart = NbDocument.findLineOffset(doc, lineStart) + columnStart; 258 int offsetEnd = NbDocument.findLineOffset(doc, lineEnd) + columnEnd; 259 260 return new HighlightImpl(doc, doc.createPosition(offsetStart), doc.createPosition(offsetEnd), attributes, null); 261 } 262 263 } 264 | Popular Tags |