KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > HighlightImpl


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 package org.netbeans.modules.java.editor.semantic;
20
21 import java.awt.Color JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import javax.swing.text.Position JavaDoc;
33 import javax.swing.text.Position.Bias;
34 import javax.swing.text.StyledDocument JavaDoc;
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 /**
42  *
43  * @author Jan Lahoda
44  */

45 final class HighlightImpl implements Mark, Highlight {
46     
47     private Document JavaDoc doc;
48     private Position JavaDoc start;
49     private Position JavaDoc start2;
50     private Position JavaDoc end;
51     private Position JavaDoc end2;
52     private Collection JavaDoc<ColoringAttributes> colorings;
53     private Color JavaDoc esColor;
54     
55     public HighlightImpl(Document JavaDoc doc, Position JavaDoc start, Position JavaDoc end, Collection JavaDoc<ColoringAttributes> colorings) {
56         this(doc, start, end, colorings, null);
57     }
58     
59     /** Creates a new instance of Highlight */
60     public HighlightImpl(Document JavaDoc doc, Position JavaDoc start, Position JavaDoc end, Collection JavaDoc<ColoringAttributes> colorings, Color JavaDoc 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 JavaDoc doc, int start, int end, Collection JavaDoc<ColoringAttributes> colorings, Color JavaDoc esColor) throws BadLocationException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getEnhancedColor() {
163         return esColor;
164     }
165     
166     public int[] getAssignedLines() {
167         int line = NbDocument.findLineNumber((StyledDocument JavaDoc) doc, start.getOffset());
168         
169         return new int[] {line, line};
170     }
171     
172     public String JavaDoc getShortDescription() {
173         return "...";
174     }
175     
176     public String JavaDoc getHighlightTestData() {
177         int lineStart = NbDocument.findLineNumber((StyledDocument JavaDoc) doc, start.getOffset());
178         int columnStart = NbDocument.findLineColumn((StyledDocument JavaDoc) doc, start.getOffset());
179         int lineEnd = NbDocument.findLineNumber((StyledDocument JavaDoc) doc, end.getOffset());
180         int columnEnd = NbDocument.findLineColumn((StyledDocument JavaDoc) doc, end.getOffset());
181         
182         return coloringsToString() + ", " + lineStart + ":" + columnStart + "-" + lineEnd + ":" + columnEnd;
183     }
184     
185     private String JavaDoc coloringsToString() {
186         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc<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 JavaDoc doc, String JavaDoc line) throws ParseException JavaDoc, BadLocationException JavaDoc {
236         MessageFormat JavaDoc f = new MessageFormat JavaDoc("[{0}], {1,number,integer}:{2,number,integer}-{3,number,integer}:{4,number,integer}");
237         Object JavaDoc[] args = f.parse(line);
238         
239         String JavaDoc attributesString = (String JavaDoc) args[0];
240         int lineStart = ((Long JavaDoc) args[1]).intValue();
241         int columnStart = ((Long JavaDoc) args[2]).intValue();
242         int lineEnd = ((Long JavaDoc) args[3]).intValue();
243         int columnEnd = ((Long JavaDoc) args[4]).intValue();
244         
245         String JavaDoc[] attrElements = attributesString.split(",");
246         List JavaDoc<ColoringAttributes> attributes = new ArrayList JavaDoc<ColoringAttributes>();
247         
248         for (String JavaDoc a : attrElements) {
249             a = a.trim();
250             
251             attributes.add(ColoringAttributes.valueOf(a));
252         }
253         
254         if (attributes.contains(null))
255             throw new NullPointerException JavaDoc();
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