1 | /*
|
---|
2 | * 08/11/2009
|
---|
3 | *
|
---|
4 | * DefaultParserNotice.java - Base implementation of a parser notice.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea.parser;
|
---|
10 |
|
---|
11 | import java.awt.Color;
|
---|
12 |
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Base implementation of a parser notice. Most <code>Parser</code>
|
---|
16 | * implementations can return instances of this in their parse result.
|
---|
17 | *
|
---|
18 | * @author Robert Futrell
|
---|
19 | * @version 1.0
|
---|
20 | * @see Parser
|
---|
21 | * @see ParseResult
|
---|
22 | */
|
---|
23 | public class DefaultParserNotice implements ParserNotice {
|
---|
24 |
|
---|
25 | private Parser parser;
|
---|
26 | private int level;
|
---|
27 | private int line;
|
---|
28 | private int offset;
|
---|
29 | private int length;
|
---|
30 | private boolean showInEditor;
|
---|
31 | private Color color;
|
---|
32 | private String message;
|
---|
33 | private String toolTipText;
|
---|
34 |
|
---|
35 | private static final Color[] DEFAULT_COLORS = {
|
---|
36 | new Color(255, 0, 128), // Error
|
---|
37 | new Color(244, 200, 45), // Warning
|
---|
38 | Color.gray, // Info
|
---|
39 | };
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructor.
|
---|
44 | *
|
---|
45 | * @param parser The parser that created this notice.
|
---|
46 | * @param msg The text of the message.
|
---|
47 | * @param line The line number for the message.
|
---|
48 | */
|
---|
49 | public DefaultParserNotice(Parser parser, String msg, int line) {
|
---|
50 | this(parser, msg, line, -1, -1);
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Constructor.
|
---|
56 | *
|
---|
57 | * @param parser The parser that created this notice.
|
---|
58 | * @param message The message.
|
---|
59 | * @param line The line number corresponding to the message.
|
---|
60 | * @param offset The offset in the input stream of the code the
|
---|
61 | * message is concerned with, or <code>-1</code> if unknown.
|
---|
62 | * @param length The length of the code the message is concerned with,
|
---|
63 | * or <code>-1</code> if unknown.
|
---|
64 | */
|
---|
65 | public DefaultParserNotice(Parser parser, String message, int line,
|
---|
66 | int offset, int length) {
|
---|
67 | this.parser = parser;
|
---|
68 | this.message = message;
|
---|
69 | this.line = line;
|
---|
70 | this.offset = offset;
|
---|
71 | this.length = length;
|
---|
72 | setLevel(ERROR);
|
---|
73 | setShowInEditor(true);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Compares this parser notice to another.
|
---|
79 | *
|
---|
80 | * @param obj Another parser notice.
|
---|
81 | * @return How the two parser notices should be sorted relative to one
|
---|
82 | * another.
|
---|
83 | */
|
---|
84 | public int compareTo(Object obj) {
|
---|
85 | int diff = -1;
|
---|
86 | if (obj instanceof ParserNotice) {
|
---|
87 | ParserNotice p2 = (ParserNotice)obj;
|
---|
88 | diff = level - p2.getLevel();
|
---|
89 | if (diff==0) {
|
---|
90 | diff = line - p2.getLine();
|
---|
91 | if (diff==0) {
|
---|
92 | diff = message.compareTo(p2.getMessage());
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | return diff;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * {@inheritDoc}
|
---|
102 | */
|
---|
103 | public boolean containsPosition(int pos) {
|
---|
104 | return offset<=pos && pos<(offset+length);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns whether this parser notice is equal to another one.
|
---|
110 | *
|
---|
111 | * @param obj Another parser notice.
|
---|
112 | * @return Whether the two notices are equal.
|
---|
113 | */
|
---|
114 | public boolean equals(Object obj) {
|
---|
115 | return compareTo(obj)==0;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * {@inheritDoc}
|
---|
121 | */
|
---|
122 | public Color getColor() {
|
---|
123 | Color c = color; // User-defined
|
---|
124 | if (c==null) {
|
---|
125 | c = DEFAULT_COLORS[getLevel()];
|
---|
126 | }
|
---|
127 | return c;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * {@inheritDoc}
|
---|
133 | */
|
---|
134 | public int getLength() {
|
---|
135 | return length;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * {@inheritDoc}
|
---|
141 | */
|
---|
142 | public int getLevel() {
|
---|
143 | return level;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * {@inheritDoc}
|
---|
149 | */
|
---|
150 | public int getLine() {
|
---|
151 | return line;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * {@inheritDoc}
|
---|
157 | */
|
---|
158 | public String getMessage() {
|
---|
159 | return message;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * {@inheritDoc}
|
---|
165 | */
|
---|
166 | public int getOffset() {
|
---|
167 | return offset;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * {@inheritDoc}
|
---|
173 | */
|
---|
174 | public Parser getParser() {
|
---|
175 | return parser;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * {@inheritDoc}
|
---|
181 | */
|
---|
182 | public boolean getShowInEditor() {
|
---|
183 | return showInEditor;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * {@inheritDoc}
|
---|
189 | */
|
---|
190 | public String getToolTipText() {
|
---|
191 | return toolTipText!=null ? toolTipText : getMessage();
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Returns the hash code for this notice.
|
---|
197 | *
|
---|
198 | * @return The hash code.
|
---|
199 | */
|
---|
200 | public int hashCode() {
|
---|
201 | return (line<<16) | offset;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Sets the color to use when painting this notice.
|
---|
207 | *
|
---|
208 | * @param color The color to use.
|
---|
209 | * @see #getColor()
|
---|
210 | */
|
---|
211 | public void setColor(Color color) {
|
---|
212 | this.color = color;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Sets the level of this notice.
|
---|
218 | *
|
---|
219 | * @param level The new level.
|
---|
220 | * @see #getLevel()
|
---|
221 | */
|
---|
222 | public void setLevel(int level) {
|
---|
223 | if (level>INFO) {
|
---|
224 | level = INFO;
|
---|
225 | }
|
---|
226 | else if (level<ERROR) {
|
---|
227 | level = ERROR;
|
---|
228 | }
|
---|
229 | this.level = level;
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Sets whether a squiggle underline should be drawn in the editor for
|
---|
235 | * this notice.
|
---|
236 | *
|
---|
237 | * @param show Whether to draw a squiggle underline.
|
---|
238 | * @see #getShowInEditor()
|
---|
239 | */
|
---|
240 | public void setShowInEditor(boolean show) {
|
---|
241 | showInEditor = show;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Sets the tooltip text to display for this notice.
|
---|
247 | *
|
---|
248 | * @param text The new tooltip text. This can be HTML. If this is
|
---|
249 | * <code>null</code>, then tooltips will return the same text as
|
---|
250 | * {@link #getMessage()}.
|
---|
251 | * @see #getToolTipText()
|
---|
252 | */
|
---|
253 | public void setToolTipText(String text) {
|
---|
254 | this.toolTipText = text;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Returns a string representation of this parser notice.
|
---|
260 | *
|
---|
261 | * @return This parser notice as a string.
|
---|
262 | */
|
---|
263 | public String toString() {
|
---|
264 | return "Line " + getLine() + ": " + getMessage();
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | } |
---|