KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.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.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 /**
43  * This file is originally from Retouche, the Java Support
44  * infrastructure in NetBeans. I have modified the file as little
45  * as possible to make merging Retouche fixes back as simple as
46  * possible.
47  *
48  *
49  * @author Jan Lahoda
50  */

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