001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.gsfret.editor.semantic;
042:
043: import java.awt.Color;
044: import java.util.Arrays;
045: import java.util.Collection;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.text.BadLocationException;
049: import javax.swing.text.Document;
050: import javax.swing.text.Position;
051: import javax.swing.text.Position.Bias;
052: import javax.swing.text.StyledDocument;
053: import org.netbeans.modules.gsf.api.ColoringAttributes;
054: import org.netbeans.editor.Coloring;
055: import org.netbeans.modules.editor.errorstripe.privatespi.Mark;
056: import org.netbeans.modules.editor.errorstripe.privatespi.Status;
057: import org.netbeans.modules.editor.highlights.spi.Highlight;
058: import org.netbeans.modules.gsf.Language;
059: import org.openide.text.NbDocument;
060:
061: /**
062: * This file is originally from Retouche, the Java Support
063: * infrastructure in NetBeans. I have modified the file as little
064: * as possible to make merging Retouche fixes back as simple as
065: * possible.
066: *
067: *
068: * @author Jan Lahoda
069: */
070: final class HighlightImpl implements Mark, Highlight {
071:
072: private Document doc;
073: private Position start;
074: private Position start2;
075: private Position end;
076: private Position end2;
077: private Collection<ColoringAttributes> colorings;
078: private Color esColor;
079: private Language language;
080:
081: public HighlightImpl(Document doc, Position start, Position end,
082: Collection<ColoringAttributes> colorings) {
083: this (doc, start, end, colorings, null);
084: }
085:
086: /** Creates a new instance of Highlight */
087: public HighlightImpl(Document doc, Position start, Position end,
088: Collection<ColoringAttributes> colorings, Color esColor) {
089: this .doc = doc;
090: this .start = start;
091: this .end = end;
092: this .colorings = colorings;
093: this .esColor = esColor;
094: }
095:
096: public HighlightImpl(Language language, Document doc, int start,
097: int end, Collection<ColoringAttributes> colorings,
098: Color esColor) throws BadLocationException {
099: this .language = language;
100: this .doc = doc;
101: this .start = NbDocument
102: .createPosition(doc, start, Bias.Forward);
103: this .start = NbDocument.createPosition(doc, start,
104: Bias.Backward);
105: this .end = NbDocument.createPosition(doc, end, Bias.Backward);
106: this .end2 = NbDocument.createPosition(doc, end, Bias.Forward);
107: this .colorings = colorings;
108: this .esColor = esColor;
109: }
110:
111: public int getEnd() {
112: int endPos = end.getOffset();
113:
114: if (end2 == null)
115: return endPos;
116:
117: int endPos2 = end2.getOffset();
118:
119: if (endPos == endPos2)
120: return endPos;
121:
122: try {
123: String added = doc.getText(endPos, endPos2 - endPos);
124: int newEndPos = endPos;
125:
126: for (char c : added.toCharArray()) {
127: if (Character.isJavaIdentifierPart(c))
128: newEndPos++;
129: }
130:
131: if (newEndPos != endPos) {
132: end = NbDocument.createPosition(doc, newEndPos,
133: Bias.Backward);
134: end2 = NbDocument.createPosition(doc, newEndPos,
135: Bias.Forward);
136: }
137:
138: return newEndPos;
139: } catch (BadLocationException e) {
140: Logger.getLogger("global").log(Level.FINE, e.getMessage(),
141: e);
142: return endPos;
143: }
144: }
145:
146: public int getStart() {
147: int startPos = start.getOffset();
148:
149: if (start2 == null)
150: return startPos;
151:
152: int startPos2 = start2.getOffset();
153:
154: if (startPos == startPos2)
155: return startPos;
156:
157: try {
158: String added = doc.getText(startPos, startPos2 - startPos);
159: int newStartPos = startPos;
160:
161: for (char c : added.toCharArray()) {
162: if (Character.isJavaIdentifierPart(c))
163: newStartPos++;
164: }
165:
166: if (newStartPos != startPos) {
167: start = NbDocument.createPosition(doc, newStartPos,
168: Bias.Forward);
169: start2 = NbDocument.createPosition(doc, newStartPos,
170: Bias.Backward);
171: }
172:
173: return newStartPos;
174: } catch (BadLocationException e) {
175: Logger.getLogger("global").log(Level.FINE, e.getMessage(),
176: e);
177: return startPos;
178: }
179: }
180:
181: public Coloring getColoring() {
182: return language.getColoringManager().getColoring(colorings);
183: }
184:
185: public String toString() {
186: return "Highlight: [" + colorings + ", " + start.getOffset()
187: + "-" + end.getOffset() + "]";
188: }
189:
190: public int getType() {
191: return TYPE_ERROR_LIKE;
192: }
193:
194: public Status getStatus() {
195: return Status.STATUS_OK;
196: }
197:
198: public int getPriority() {
199: return PRIORITY_DEFAULT;
200: }
201:
202: public Color getEnhancedColor() {
203: return esColor;
204: }
205:
206: public int[] getAssignedLines() {
207: int line = NbDocument.findLineNumber((StyledDocument) doc,
208: start.getOffset());
209:
210: return new int[] { line, line };
211: }
212:
213: public String getShortDescription() {
214: return "...";
215: }
216:
217: public String getHighlightTestData() {
218: int lineStart = NbDocument.findLineNumber((StyledDocument) doc,
219: start.getOffset());
220: int columnStart = NbDocument.findLineColumn(
221: (StyledDocument) doc, start.getOffset());
222: int lineEnd = NbDocument.findLineNumber((StyledDocument) doc,
223: end.getOffset());
224: int columnEnd = NbDocument.findLineColumn((StyledDocument) doc,
225: end.getOffset());
226:
227: return coloringsToString() + ", " + lineStart + ":"
228: + columnStart + "-" + lineEnd + ":" + columnEnd;
229: }
230:
231: private String coloringsToString() {
232: StringBuffer result = new StringBuffer();
233: boolean first = true;
234:
235: result.append("[");
236:
237: for (ColoringAttributes attribute : coloringsAttributesOrder) {
238: if (colorings.contains(attribute)) {
239: if (!first) {
240: result.append(", ");
241: }
242:
243: first = false;
244: result.append(attribute.name());
245: }
246: }
247:
248: result.append("]");
249:
250: return result.toString();
251: }
252:
253: Collection<ColoringAttributes> coloringsAttributesOrder = Arrays
254: .asList(new ColoringAttributes[] {
255: ColoringAttributes.STATIC,
256: ColoringAttributes.ABSTRACT,
257:
258: ColoringAttributes.PUBLIC,
259: ColoringAttributes.PROTECTED,
260: ColoringAttributes.PACKAGE_PRIVATE,
261: ColoringAttributes.PRIVATE,
262:
263: ColoringAttributes.DEPRECATED,
264:
265: ColoringAttributes.FIELD,
266: ColoringAttributes.LOCAL_VARIABLE,
267: ColoringAttributes.PARAMETER,
268: ColoringAttributes.METHOD,
269: ColoringAttributes.CONSTRUCTOR,
270: ColoringAttributes.CLASS,
271:
272: ColoringAttributes.UNUSED,
273:
274: ColoringAttributes.TYPE_PARAMETER_DECLARATION,
275: ColoringAttributes.TYPE_PARAMETER_USE,
276:
277: ColoringAttributes.UNDEFINED,
278:
279: ColoringAttributes.MARK_OCCURRENCES, });
280:
281: // public static HighlightImpl parse(StyledDocument doc, String line) throws ParseException, BadLocationException {
282: // MessageFormat f = new MessageFormat("[{0}], {1,number,integer}:{2,number,integer}-{3,number,integer}:{4,number,integer}");
283: // Object[] args = f.parse(line);
284: //
285: // String attributesString = (String) args[0];
286: // int lineStart = ((Long) args[1]).intValue();
287: // int columnStart = ((Long) args[2]).intValue();
288: // int lineEnd = ((Long) args[3]).intValue();
289: // int columnEnd = ((Long) args[4]).intValue();
290: //
291: // String[] attrElements = attributesString.split(",");
292: // List<ColoringAttributes> attributes = new ArrayList<ColoringAttributes>();
293: //
294: // for (String a : attrElements) {
295: // a = a.trim();
296: //
297: // attributes.add(ColoringAttributes.valueOf(a));
298: // }
299: //
300: // if (attributes.contains(null))
301: // throw new NullPointerException();
302: //
303: // int offsetStart = NbDocument.findLineOffset(doc, lineStart) + columnStart;
304: // int offsetEnd = NbDocument.findLineOffset(doc, lineEnd) + columnEnd;
305: //
306: // return new HighlightImpl(doc, doc.createPosition(offsetStart), doc.createPosition(offsetEnd), attributes, null);
307: // }
308:
309: }
|