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.editor.hints;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.beans.PropertyChangeListener;
045: import javax.swing.text.BadLocationException;
046: import javax.swing.text.Position;
047: import org.netbeans.spi.editor.hints.LazyFixList;
048: import org.netbeans.spi.editor.hints.Severity;
049: import org.openide.text.Annotation;
050: import org.openide.util.WeakListeners;
051:
052: /**
053: *
054: * @author Jan Lahoda
055: */
056: public class ParseErrorAnnotation extends Annotation implements
057: PropertyChangeListener {
058:
059: private final Severity severity;
060: private final LazyFixList fixes;
061: private final String description;
062: private final Position lineStart;
063: private final AnnotationHolder holder;
064:
065: /** Creates a new instance of ParseErrorAnnotation */
066: public ParseErrorAnnotation(Severity severity, LazyFixList fixes,
067: String description, Position lineStart,
068: AnnotationHolder holder) {
069: this .severity = severity;
070: this .fixes = fixes;
071: this .description = description;
072: this .lineStart = lineStart;
073: this .holder = holder;
074:
075: if (fixes.probablyContainsFixes() && !fixes.isComputed()) {
076: fixes.addPropertyChangeListener(WeakListeners
077: .propertyChange(this , fixes));
078: }
079: }
080:
081: public String getAnnotationType() {
082: boolean hasFixes = fixes.isComputed()
083: && !fixes.getFixes().isEmpty();
084:
085: switch (severity) {
086: case ERROR:
087: if (hasFixes)
088: return "org-netbeans-spi-editor-hints-parser_annotation_err_fixable";
089: else
090: return "org-netbeans-spi-editor-hints-parser_annotation_err";
091:
092: case WARNING:
093: if (hasFixes)
094: return "org-netbeans-spi-editor-hints-parser_annotation_warn_fixable";
095: else
096: return "org-netbeans-spi-editor-hints-parser_annotation_warn";
097: case VERIFIER:
098: if (hasFixes)
099: return "org-netbeans-spi-editor-hints-parser_annotation_verifier_fixable";
100: else
101: return "org-netbeans-spi-editor-hints-parser_annotation_verifier";
102: case HINT:
103: if (hasFixes)
104: return "org-netbeans-spi-editor-hints-parser_annotation_hint_fixable";
105: else
106: return "org-netbeans-spi-editor-hints-parser_annotation_hint";
107: default:
108: throw new IllegalArgumentException(String.valueOf(severity));
109: }
110: }
111:
112: public String getShortDescription() {
113: return description;
114: }
115:
116: public void propertyChange(PropertyChangeEvent evt) {
117: if (fixes.isComputed()) {
118: try {
119: holder.detachAnnotation(this );
120: holder.attachAnnotation(lineStart, this );
121: } catch (BadLocationException ex) {
122: throw new IllegalStateException(ex);
123: }
124: }
125: }
126:
127: public LazyFixList getFixes() {
128: return fixes;
129: }
130:
131: public String getDescription() {
132: return description;
133: }
134:
135: public int getLineNumber() {
136: return holder.lineNumber(lineStart);
137: }
138:
139: Severity getSeverity() {
140: return severity;
141: }
142: }
|