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