KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > ProblemLocation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import org.eclipse.jdt.core.IJavaModelMarker;
14 import org.eclipse.jdt.core.compiler.CategorizedProblem;
15 import org.eclipse.jdt.core.compiler.IProblem;
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.CompilationUnit;
18
19 import org.eclipse.jdt.ui.text.java.*;
20
21 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
22 import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation;
23 import org.eclipse.jdt.internal.ui.javaeditor.JavaMarkerAnnotation;
24
25 /**
26  *
27  */

28 public class ProblemLocation implements IProblemLocation {
29
30     private final int fId;
31     private final String JavaDoc[] fArguments;
32     private final int fOffset;
33     private final int fLength;
34     private final boolean fIsError;
35     private final String JavaDoc fMarkerType;
36
37     public ProblemLocation(int offset, int length, IJavaAnnotation annotation) {
38         fId= annotation.getId();
39         fArguments= annotation.getArguments();
40         fOffset= offset;
41         fLength= length;
42         fIsError= JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotation.getType());
43         
44         String JavaDoc markerType= annotation.getMarkerType();
45         fMarkerType= markerType != null ? markerType : IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER;
46     }
47
48     public ProblemLocation(int offset, int length, int id, String JavaDoc[] arguments, boolean isError, String JavaDoc markerType) {
49         fId= id;
50         fArguments= arguments;
51         fOffset= offset;
52         fLength= length;
53         fIsError= isError;
54         fMarkerType= markerType;
55     }
56     
57     public ProblemLocation(IProblem problem) {
58         fId= problem.getID();
59         fArguments= problem.getArguments();
60         fOffset= problem.getSourceStart();
61         fLength= problem.getSourceEnd() - fOffset + 1;
62         fIsError= problem.isError();
63         fMarkerType= problem instanceof CategorizedProblem ? ((CategorizedProblem) problem).getMarkerType() : IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER;
64     }
65
66
67     /* (non-Javadoc)
68      * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getProblemId()
69      */

70     public int getProblemId() {
71         return fId;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getProblemArguments()
76      */

77     public String JavaDoc[] getProblemArguments() {
78         return fArguments;
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getLength()
83      */

84     public int getLength() {
85         return fLength;
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getOffset()
90      */

91     public int getOffset() {
92         return fOffset;
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.jdt.ui.text.java.IProblemLocation#isError()
97      */

98     public boolean isError() {
99         return fIsError;
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.jdt.ui.text.java.IProblemLocation#getMarkerType()
104      */

105     public String JavaDoc getMarkerType() {
106         return fMarkerType;
107     }
108     
109     /*
110      * (non-Javadoc)
111      * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getCoveringNode(org.eclipse.jdt.core.dom.CompilationUnit)
112      */

113     public ASTNode getCoveringNode(CompilationUnit astRoot) {
114         NodeFinder finder= new NodeFinder(fOffset, fLength);
115         astRoot.accept(finder);
116         return finder.getCoveringNode();
117     }
118
119     /*
120      * (non-Javadoc)
121      * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getCoveredNode(org.eclipse.jdt.core.dom.CompilationUnit)
122      */

123     public ASTNode getCoveredNode(CompilationUnit astRoot) {
124         NodeFinder finder= new NodeFinder(fOffset, fLength);
125         astRoot.accept(finder);
126         return finder.getCoveredNode();
127     }
128
129     public String JavaDoc toString() {
130         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
131         buf.append("Id: ").append(getErrorCode(fId)).append('\n'); //$NON-NLS-1$
132
buf.append('[').append(fOffset).append(", ").append(fLength).append(']').append('\n'); //$NON-NLS-1$
133
String JavaDoc[] arg= fArguments;
134         if (arg != null) {
135             for (int i= 0; i < arg.length; i++) {
136                 buf.append(arg[i]);
137                 buf.append('\n');
138             }
139         }
140         return buf.toString();
141     }
142
143     private String JavaDoc getErrorCode(int code) {
144         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
145
146         if ((code & IProblem.TypeRelated) != 0) {
147             buf.append("TypeRelated + "); //$NON-NLS-1$
148
}
149         if ((code & IProblem.FieldRelated) != 0) {
150             buf.append("FieldRelated + "); //$NON-NLS-1$
151
}
152         if ((code & IProblem.ConstructorRelated) != 0) {
153             buf.append("ConstructorRelated + "); //$NON-NLS-1$
154
}
155         if ((code & IProblem.MethodRelated) != 0) {
156             buf.append("MethodRelated + "); //$NON-NLS-1$
157
}
158         if ((code & IProblem.ImportRelated) != 0) {
159             buf.append("ImportRelated + "); //$NON-NLS-1$
160
}
161         if ((code & IProblem.Internal) != 0) {
162             buf.append("Internal + "); //$NON-NLS-1$
163
}
164         if ((code & IProblem.Syntax) != 0) {
165             buf.append("Syntax + "); //$NON-NLS-1$
166
}
167         if ((code & IProblem.Javadoc) != 0) {
168             buf.append("Javadoc + "); //$NON-NLS-1$
169
}
170         buf.append(code & IProblem.IgnoreCategoriesMask);
171
172         return buf.toString();
173     }
174
175
176 }
177
Popular Tags