KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > AnnotationType


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
20 package org.netbeans.editor;
21
22 import java.awt.Color JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import java.awt.Image JavaDoc;
28 import java.awt.Toolkit JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import org.netbeans.editor.AnnotationTypes;
32 import java.util.ResourceBundle JavaDoc;
33 import org.openide.ErrorManager;
34
35 /** Definition of the annotation type. Annotation type is defined by attributes like
36  * highlight color, foreground color, glyph icon, etc. Each annotation added to document
37  * has reference to the name of the annotation type which defines how the annotation
38  * will be drawn.
39  *
40  * @author David Konecny
41  * @since 07/2001
42  */

43
44 public class AnnotationType {
45
46     /** Property name for Name (String) */
47     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
48

49     /** Property name for Description (String) */
50     public static final String JavaDoc PROP_DESCRIPTION = "description"; // NOI18N
51

52     /** Property name for Visible (boolean) */
53     public static final String JavaDoc PROP_VISIBLE = "visible"; // NOI18N
54

55     /** Property name for Glyph (URL) */
56     public static final String JavaDoc PROP_GLYPH_URL = "glyph"; // NOI18N
57

58     /** Property name for Highlight (Color) */
59     public static final String JavaDoc PROP_HIGHLIGHT_COLOR = "highlight"; // NOI18N
60

61     /** Property name for Foreground (Color) */
62     public static final String JavaDoc PROP_FOREGROUND_COLOR = "foreground"; // NOI18N
63

64     /** Property name for WaveUnderline (Color) */
65     public static final String JavaDoc PROP_WAVEUNDERLINE_COLOR = "waveunderline"; // NOI18N
66

67     /** Property name for WholeLine (boolean) */
68     public static final String JavaDoc PROP_WHOLE_LINE = "wholeline"; // NOI18N
69

70     /** Property name for ContentType (String) */
71     public static final String JavaDoc PROP_CONTENT_TYPE = "contenttype"; // NOI18N
72

73     /** Property name for Actions (Action[]) */
74     public static final String JavaDoc PROP_ACTIONS = "actions"; // NOI18N
75

76     /** Property name for TooltipText (String) */
77     public static final String JavaDoc PROP_TOOLTIP_TEXT = "tooltipText"; // NOI18N
78

79     /** Property name for InheritForegroundColor (Boolean) */
80     public static final String JavaDoc PROP_INHERIT_FOREGROUND_COLOR = "inheritForegroundColor"; // NOI18N
81

82     /** Property name for UseHighlightColor (Boolean) */
83     public static final String JavaDoc PROP_USE_HIGHLIGHT_COLOR = "useHighlightColor"; // NOI18N
84

85     /** Property name for UseWaveUnderlineColor (Boolean) */
86     public static final String JavaDoc PROP_USE_WAVEUNDERLINE_COLOR = "useWaveUnderlineColor"; // NOI18N
87

88     public static final String JavaDoc PROP_USE_CUSTOM_SIDEBAR_COLOR = "useCustomSidebarColor"; // NOI18N
89

90     public static final String JavaDoc PROP_CUSTOM_SIDEBAR_COLOR = "customSidebarColor"; // NOI18N
91

92     public static final String JavaDoc PROP_SEVERITY = "severity"; // NOI18N
93

94     public static final String JavaDoc PROP_BROWSEABLE = "browseable"; // NOI18N
95

96     public static final String JavaDoc PROP_PRIORITY = "priority"; // NOI18N
97

98     /** Property name for Combinations (AnnotationType.CombinationMember[]).
99      * If some annotation type has set this property, it means that editor
100      * must check if line contains all types which are defined in this array.
101      * If it contains, then all this annotation types become hidden and this
102      * type is shown instead of them. */

103     public static final String JavaDoc PROP_COMBINATIONS = "combinations"; // NOI18N
104

105     public static final String JavaDoc PROP_COMBINATION_ORDER = "combinationOrder"; // NOI18N
106

107     public static final String JavaDoc PROP_COMBINATION_MINIMUM_OPTIONALS = "combinationMinimumOptionals"; // NOI18N
108

109     /** Property holding the object which represent the source of this annotation type.
110      * This property is used during the saving of the changes in annotation type. */

111     public static final String JavaDoc PROP_FILE = "file"; // NOI18N
112

113     public static final String JavaDoc PROP_LOCALIZING_BUNDLE = "bundle"; // NOI18N
114

115     public static final String JavaDoc PROP_DESCRIPTION_KEY = "desciptionKey"; // NOI18N
116

117     public static final String JavaDoc PROP_ACTIONS_FOLDER = "actionsFolder"; // NOI18N
118

119     public static final String JavaDoc PROP_COMBINATION_TOOLTIP_TEXT_KEY = "tooltipTextKey"; // NOI18N
120

121     /** Storage of all annotation type properties. */
122     private Map JavaDoc properties;
123
124     /** Support for property change listeners*/
125     private PropertyChangeSupport JavaDoc support;
126     
127     /** Glyph icon loaded from URL into Image */
128     private Image JavaDoc img = null;
129     
130     /** Coloring composed from foreground and highlight color*/
131     private Coloring col;
132     
133     public AnnotationType() {
134         properties = new HashMap JavaDoc(15*4/3);
135         support = new PropertyChangeSupport JavaDoc(this);
136     }
137
138     /** Getter for Glyph property
139      * @return URL of the glyph icon */

140     public java.net.URL JavaDoc getGlyph() {
141         URL JavaDoc u = (java.net.URL JavaDoc)getProp(PROP_GLYPH_URL);
142         if (u == null)
143             u = AnnotationTypes.getDefaultGlyphURL();
144         return u;
145     }
146
147     /** Setter for the Glyph property
148      * @param glyph URL to gpylh icon */

149     public void setGlyph(java.net.URL JavaDoc glyph) {
150         putProp(PROP_GLYPH_URL, glyph);
151     }
152
153     /** Gets Image which represent the glyph. This method is called
154      * only from AWT thead and so it is not necessary to synchronize it.
155      */

156     public Image JavaDoc getGlyphImage() {
157         if (img == null) {
158             img = Toolkit.getDefaultToolkit().getImage(getGlyph());
159         }
160         return img;
161     }
162     
163     /** Whether the annotation type has its own glyph icon or not */
164     public boolean isDefaultGlyph() {
165         if (getProp(PROP_GLYPH_URL) == null)
166             return true;
167         else
168             return false;
169     }
170     
171     /** Getter for Highlight property
172      * @return highlight color */

173     public java.awt.Color JavaDoc getHighlight() {
174         return (java.awt.Color JavaDoc)getProp(PROP_HIGHLIGHT_COLOR);
175     }
176
177     /** Setter for the Highlight property
178      * @param highlight highlight color */

179     public void setHighlight(java.awt.Color JavaDoc highlight) {
180         col = null; // force the create new coloring
181
putProp(PROP_HIGHLIGHT_COLOR, highlight);
182         firePropertyChange(PROP_HIGHLIGHT_COLOR, null, null);
183         processChange();
184     }
185
186     /** Getter for UseHighlightColor property
187      * @return whether the highlight color should be used or not */

188     public boolean isUseHighlightColor() {
189         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_USE_HIGHLIGHT_COLOR);
190         if (b == null)
191             return true;
192         return b.booleanValue();
193     }
194
195     /** Setter for the UseHighlightColor property
196      * @param use use highlight color */

197     public void setUseHighlightColor(boolean use) {
198         if (isUseHighlightColor() != use) {
199             col = null; // force the create new coloring
200
putProp(PROP_USE_HIGHLIGHT_COLOR, use ? Boolean.TRUE : Boolean.FALSE);
201             firePropertyChange(PROP_USE_HIGHLIGHT_COLOR, null, null);
202             processChange();
203         }
204     }
205     
206     /** Getter for Foreground property
207      * @return foreground color */

208     public java.awt.Color JavaDoc getForegroundColor() {
209         return (java.awt.Color JavaDoc)getProp(PROP_FOREGROUND_COLOR);
210     }
211
212     /** Setter for the Foreground property
213      * @param foregroundColor foreground color */

214     public void setForegroundColor(java.awt.Color JavaDoc foregroundColor) {
215         col = null; // force the create new coloring
216
putProp(PROP_FOREGROUND_COLOR, foregroundColor);
217         firePropertyChange(PROP_FOREGROUND_COLOR, null, null);
218         processChange();
219     }
220
221     /** Getter for InheritForegroundColor property
222      * @return whether the foreground color should be inherit or not */

223     public boolean isInheritForegroundColor() {
224         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_INHERIT_FOREGROUND_COLOR);
225         if (b == null)
226             return true;
227         return b.booleanValue();
228     }
229
230     /** Setter for the InheritfForegroundColor property
231      * @param inherit inherit foreground color */

232     public void setInheritForegroundColor(boolean inherit) {
233         if (isInheritForegroundColor() != inherit) {
234             col = null; // force the create new coloring
235
putProp(PROP_INHERIT_FOREGROUND_COLOR, inherit ? Boolean.TRUE : Boolean.FALSE);
236             firePropertyChange(PROP_INHERIT_FOREGROUND_COLOR, null, null);
237             processChange();
238         }
239     }
240     
241     /** Getter for WaveUnderline property
242      * @return waveunderline color */

243     public java.awt.Color JavaDoc getWaveUnderlineColor() {
244         return (java.awt.Color JavaDoc)getProp(PROP_WAVEUNDERLINE_COLOR);
245     }
246
247     /** Setter for the WaveUnderline property
248      * @param waveunderline wave underline color */

249     public void setWaveUnderlineColor(java.awt.Color JavaDoc waveunderline) {
250         col = null; // force the create new coloring
251
putProp(PROP_WAVEUNDERLINE_COLOR, waveunderline);
252         firePropertyChange(PROP_WAVEUNDERLINE_COLOR, null, null);
253         processChange();
254     }
255
256     /** Getter for UseWaveUnderlineColor property
257      * @return whether the waveunderline color should be used or not */

258     public boolean isUseWaveUnderlineColor() {
259         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_USE_WAVEUNDERLINE_COLOR);
260         if (b == null)
261             return true;
262         return b.booleanValue();
263     }
264
265     /** Setter for the UseWaveUnderlineColor property
266      * @param use use wave underline color */

267     public void setUseWaveUnderlineColor(boolean use) {
268         if (isUseWaveUnderlineColor() != use) {
269             col = null; // force the create new coloring
270
putProp(PROP_USE_WAVEUNDERLINE_COLOR, use ? Boolean.TRUE : Boolean.FALSE);
271             firePropertyChange(PROP_USE_WAVEUNDERLINE_COLOR, null, null);
272             processChange();
273         }
274     }
275     
276     /** Process change of some setting. It means that
277      * listeners are notified and change is saved. */

278     private void processChange() {
279         // if type does not have this property it is just being loaded
280
if (getProp(AnnotationType.PROP_FILE) == null)
281             return;
282         // force repaint of all documents
283
Settings.touchValue(null, null);
284         AnnotationTypes.getTypes().saveType(this);
285     }
286
287     /** Gets all the colors composed as Coloring
288      * @return coloring containing all colors */

289     public Coloring getColoring() {
290         if (col == null)
291             col = new Coloring(null, Coloring.FONT_MODE_DEFAULT, isInheritForegroundColor() ? null : getForegroundColor(), isUseHighlightColor() ? getHighlight() : null, null, null, isUseWaveUnderlineColor() ? getWaveUnderlineColor() : null);
292         return col;
293     }
294     
295     /** Getter for Actions property
296      * @return array of actions */

297     public javax.swing.Action JavaDoc[] getActions() {
298         return (javax.swing.Action JavaDoc[])getProp(PROP_ACTIONS);
299     }
300
301     /** Setter for Actions property
302      * @return array of actions */

303     public void setActions(javax.swing.Action JavaDoc[] actions) {
304         putProp(PROP_ACTIONS, actions);
305     }
306
307     /** Getter for Combinations property
308      * @return array of combinations */

309     public CombinationMember[] getCombinations() {
310         return (CombinationMember[])getProp(PROP_COMBINATIONS);
311     }
312
313     /** Setter for Combinations property */
314     public void setCombinations(CombinationMember[] combs) {
315         putProp(PROP_COMBINATIONS, combs);
316     }
317
318     /** Getter for Name property
319      * @return annotation type name */

320     public String JavaDoc getName() {
321         return (String JavaDoc)getProp(PROP_NAME);
322     }
323
324     /** Setter for the Name property
325      * @param name name of the annotation type */

326     public void setName(String JavaDoc name) {
327         putProp(PROP_NAME, name);
328     }
329     
330     /** Getter for Description property
331      * @return localized description of the annotation type */

332     public String JavaDoc getDescription() {
333         String JavaDoc desc = (String JavaDoc)getProp(PROP_DESCRIPTION);
334         if (desc == null) {
335             String JavaDoc localizer = (String JavaDoc)getProp(PROP_LOCALIZING_BUNDLE);
336             String JavaDoc key = (String JavaDoc)getProp(PROP_DESCRIPTION_KEY);
337             try {
338                 ResourceBundle JavaDoc bundle = ImplementationProvider.getDefault().getResourceBundle(localizer);
339                 desc = bundle.getString(key);
340             }catch(java.util.MissingResourceException JavaDoc mre){
341                 desc = key;
342             }
343             setDescription(desc); // cache it
344
}
345         return desc;
346     }
347
348     /** Setter for the Description property
349      * @param name localized description of the annotation type */

350     public void setDescription(String JavaDoc name) {
351         putProp(PROP_DESCRIPTION, name);
352     }
353     
354     /** Getter for TooltipText property
355      * @return localized TooltipText of the annotation type */

356     public String JavaDoc getTooltipText() {
357         String JavaDoc text = (String JavaDoc)getProp(PROP_TOOLTIP_TEXT);
358         if (text == null) {
359             String JavaDoc localizer = (String JavaDoc)getProp(PROP_LOCALIZING_BUNDLE);
360             String JavaDoc key = (String JavaDoc)getProp(PROP_COMBINATION_TOOLTIP_TEXT_KEY);
361             ResourceBundle JavaDoc bundle = ImplementationProvider.getDefault().getResourceBundle(localizer);
362             text = bundle.getString(key);
363             setTooltipText(text); // cache it
364
}
365         return text;
366     }
367
368     /** Setter for the TooltipText property
369      * @param name localized TooltipText of the annotation type */

370     public void setTooltipText(String JavaDoc text) {
371         putProp(PROP_TOOLTIP_TEXT, text);
372     }
373     
374     /** Getter for CombinationOrder property
375      * @return order of the annotation type */

376     public int getCombinationOrder() {
377         if (getProp(PROP_COMBINATION_ORDER) == null)
378             return 0;
379         return ((Integer JavaDoc)getProp(PROP_COMBINATION_ORDER)).intValue();
380     }
381
382     /** Setter for the CombinationOrder property
383      * @param order order of the annotation type combination */

384     public void setCombinationOrder(int order) {
385         putProp(PROP_COMBINATION_ORDER, new Integer JavaDoc(order));
386     }
387     
388     /** Setter for the CombinationOrder property
389      * @param ord order of the annotation type combination */

390     public void setCombinationOrder(String JavaDoc ord) {
391         int order;
392         try {
393             order = Integer.parseInt(ord);
394         } catch (NumberFormatException JavaDoc ex) {
395             Utilities.annotateLoggable(ex);
396             return;
397         }
398         putProp(PROP_COMBINATION_ORDER, new Integer JavaDoc(order));
399     }
400
401     /** Getter for MinimumOptionals property
402      * @return minimum number of the optional annotation types which
403      * must be matched */

404     public int getMinimumOptionals() {
405         if (getProp(PROP_COMBINATION_MINIMUM_OPTIONALS) == null)
406             return 0;
407         return ((Integer JavaDoc)getProp(PROP_COMBINATION_MINIMUM_OPTIONALS)).intValue();
408     }
409
410     public void setMinimumOptionals(int min) {
411         putProp(PROP_COMBINATION_MINIMUM_OPTIONALS, new Integer JavaDoc(min));
412     }
413     
414     public void setMinimumOptionals(String JavaDoc m) {
415         int min;
416         try {
417             min = Integer.parseInt(m);
418         } catch (NumberFormatException JavaDoc ex) {
419             Utilities.annotateLoggable(ex);
420             return;
421         }
422         putProp(PROP_COMBINATION_MINIMUM_OPTIONALS, new Integer JavaDoc(min));
423     }
424     
425     /** Getter for Visible property
426      * @return whether the annoation type is visible or not */

427     public boolean isVisible() {
428         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_VISIBLE);
429         if (b == null)
430             return false;
431         return b.booleanValue();
432     }
433
434     /** Setter for the Visible property
435      * @param vis visibility of the annotation type */

436     public void setVisible(boolean vis) {
437         putProp(PROP_VISIBLE, vis ? Boolean.TRUE : Boolean.FALSE);
438     }
439
440     /** Setter for the Visible property
441      * @param vis visibility of the annotation type */

442     public void setVisible(String JavaDoc vis) {
443         putProp(PROP_VISIBLE, Boolean.valueOf(vis));
444     }
445
446     /** Getter for WholeLine property
447      * @return whether this annotation type is whole line or not */

448     public boolean isWholeLine() {
449         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_WHOLE_LINE);
450         if (b == null)
451             return true;
452         return b.booleanValue();
453     }
454
455     /** Setter for the WholeLine property
456      * @param wl whether the annotation type is whole line or not */

457     public void setWholeLine(boolean wl) {
458         putProp(PROP_WHOLE_LINE, wl ? Boolean.TRUE : Boolean.FALSE);
459     }
460
461     /** Setter for the WholeLine property
462      * @param wl whether the annotation type is whole line or not */

463     public void setWholeLine(String JavaDoc wl) {
464         putProp(PROP_WHOLE_LINE, Boolean.valueOf(wl));
465     }
466
467     /** Getter for ContentType property
468      * @return list of content types separated by commas */

469     public String JavaDoc getContentType() {
470         return (String JavaDoc)getProp(PROP_CONTENT_TYPE);
471     }
472
473     /** Setter for the ContentType property
474      * @param ct list of content type separeted by commas */

475     public void setContentType(String JavaDoc ct) {
476         putProp(PROP_CONTENT_TYPE, ct);
477     }
478
479     public boolean isUseCustomSidebarColor() {
480         return ((Boolean JavaDoc)getProp(PROP_USE_CUSTOM_SIDEBAR_COLOR)).booleanValue();
481     }
482
483     public void setUseCustomSidebarColor(boolean value) {
484         putProp(PROP_USE_CUSTOM_SIDEBAR_COLOR, Boolean.valueOf(value));
485     }
486
487     public Color JavaDoc getCustomSidebarColor() {
488         return (Color JavaDoc) getProp(PROP_CUSTOM_SIDEBAR_COLOR);
489     }
490
491     public void setCustomSidebarColor(Color JavaDoc customSidebarColor) {
492         putProp(PROP_CUSTOM_SIDEBAR_COLOR, customSidebarColor);
493     }
494     
495     public Severity getSeverity() {
496         return (Severity) getProp(PROP_SEVERITY);
497     }
498
499     public void setSeverity(Severity severity) {
500         putProp(PROP_SEVERITY, severity);
501     }
502     
503     public int getPriority() {
504         return ((Integer JavaDoc) getProp(PROP_PRIORITY)).intValue();
505     }
506
507     public void setPriority(int priority) {
508         putProp(PROP_PRIORITY, new Integer JavaDoc(priority));
509     }
510
511     public boolean isBrowseable() {
512         return ((Boolean JavaDoc)getProp(PROP_BROWSEABLE)).booleanValue();
513     }
514
515     public void setBrowseable(boolean browseable) {
516         putProp(PROP_BROWSEABLE, Boolean.valueOf(browseable));
517     }
518
519     /** Gets property for appropriate string value */
520     public Object JavaDoc getProp(String JavaDoc prop){
521         return properties.get(prop);
522     }
523     
524     /** Puts property to Map */
525     public void putProp(Object JavaDoc key, Object JavaDoc value){
526         if (value == null) {
527             properties.remove(key);
528             return;
529         }
530         properties.put(key,value);
531     }
532     
533     public String JavaDoc toString() {
534         return "AnnotationType: name='" + getName() + "', description='" + getDescription() + // NOI18N
535
"', visible=" + isVisible() + ", wholeline=" + isWholeLine() + // NOI18N
536
", glyph=" + getGlyph() + ", highlight=" + getHighlight() + // NOI18N
537
", foreground=" + getForegroundColor() + // NOI18N
538
"', inheritForeground=" + isInheritForegroundColor() + //NOI18N
539
", contenttype="+getContentType(); //NOI18N
540

541     }
542
543     /** Add listeners on changes of annotation type properties
544      * @param l change listener*/

545     final public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
546         support.addPropertyChangeListener (l);
547     }
548     
549     /** Remove listeners on changes of annotation type properties
550      * @param l change listener*/

551     final public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
552         support.removePropertyChangeListener (l);
553     }
554
555     /** Fire property change to registered listeners. */
556     final protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
557         support.firePropertyChange(propertyName, oldValue, newValue);
558     }
559
560
561     /** Hepler class describing annotation type and whether all
562      * occurences of this type should be absorbed by combination or not.
563      * The annonation type which want to combine some other types, must
564      * define array of instances of this helper class. See
565      * AnnotationType.PROP_COMBINATIONS property.
566      */

567     public static final class CombinationMember {
568
569         /** Name of the annotation type */
570         private String JavaDoc type;
571         
572         /** Whether all occurences of this type should be absorbed or not */
573         private boolean absorbAll;
574
575         /** Whether this combination member is options or not */
576         private boolean optional;
577         
578         /** Minimum count of this type which must be found on the line to make
579          * valid combination */

580         private int minimumCount;
581
582         public CombinationMember(String JavaDoc type, boolean absorbAll, boolean optional, int minimumCount) {
583             this.type = type;
584             this.absorbAll = absorbAll;
585             this.optional = optional;
586             this.minimumCount = minimumCount;
587         }
588
589         public CombinationMember(String JavaDoc type, boolean absorbAll, boolean optional, String JavaDoc minimumCount) {
590             this.type = type;
591             this.absorbAll = absorbAll;
592             this.optional = optional;
593             if (minimumCount != null && minimumCount.length() > 0) {
594                 try {
595                     this.minimumCount = Integer.parseInt(minimumCount);
596                 } catch (NumberFormatException JavaDoc ex) {
597                     Utilities.annotateLoggable(ex);
598                     this.minimumCount = 0;
599                 }
600             } else
601                 this.minimumCount = 0;
602         }
603
604         /** Gets name of the annotation type */
605         public String JavaDoc getName() {
606             return type;
607         }
608
609         /** Getter for AbsorbAll property */
610         public boolean isAbsorbAll() {
611             return absorbAll;
612         }
613         
614         /** Getter for Optional property */
615         public boolean isOptional() {
616             return optional;
617         }
618         
619         /** Getter for MinimumCount property */
620         public int getMinimumCount() {
621             return minimumCount;
622         }
623     }
624     
625     public static final class Severity implements Comparable JavaDoc {
626         
627         /**Status OK.
628          */

629         private static final int STATUS_NONE_NUMBER = 0;
630         
631         /**Status OK.
632          */

633         private static final int STATUS_OK_NUMBER = 1;
634         
635         /**Status warning.
636          */

637         private static final int STATUS_WARNING_NUMBER = 2;
638         
639         /**Status error.
640          */

641         private static final int STATUS_ERROR_NUMBER = 3;
642         
643         /**Status OK.
644          */

645         public static final Severity STATUS_NONE = new Severity(STATUS_NONE_NUMBER);
646         
647         /**Status OK.
648          */

649         public static final Severity STATUS_OK = new Severity(STATUS_OK_NUMBER);
650         
651         /**Status warning.
652          */

653         public static final Severity STATUS_WARNING = new Severity(STATUS_WARNING_NUMBER);
654         
655         /**Status error.
656          */

657         public static final Severity STATUS_ERROR = new Severity(STATUS_ERROR_NUMBER);
658         
659         private static final Severity[] VALUES = new Severity[] {STATUS_NONE, STATUS_OK, STATUS_WARNING, STATUS_ERROR};
660         
661         private static final Color JavaDoc[] DEFAULT_STATUS_COLORS = new Color JavaDoc[] {Color.WHITE, Color.GREEN, Color.YELLOW, Color.RED};
662         
663         private int status;
664         
665         /**Creates a Status with a given status value.
666          *
667          * @param status status value to use
668          * @see #STATUS_ERROR
669          * @see #STATUS_WARNING
670          * @see #STATUS_OK
671          * @throws IllegalArgumentException if one of the provided statuses is something
672          * else then {@link #STATUS_ERROR},
673          * {@link #STATUS_WARNING} and
674          * {@link #STATUS_OK}
675          */

676         private Severity(int status) throws IllegalArgumentException JavaDoc {
677             if (status != STATUS_NONE_NUMBER && status != STATUS_ERROR_NUMBER && status != STATUS_WARNING_NUMBER && status != STATUS_OK_NUMBER)
678                 throw new IllegalArgumentException JavaDoc("Invalid status provided: " + status); // NOI18N
679
this.status = status;
680         }
681         
682         /**Returns the numerical status assigned to this {@link Status}.
683          *
684          * @return numerical status
685          */

686         private int getStatus() {
687             return status;
688         }
689         
690         /**{@inheritDoc}*/
691         public int compareTo(Object JavaDoc o) {
692             Severity remote = (Severity) o;
693             
694             if (status > remote.status) {
695                 return 1;
696             }
697             
698             if (status < remote.status) {
699                 return -1;
700             }
701             
702             return 0;
703         }
704         
705         /**{@inheritDoc}*/
706         public boolean equals(Object JavaDoc o) {
707             if (!(o instanceof Severity)) {
708                 return false;
709             }
710             
711             Severity remote = (Severity) o;
712             
713             return status == remote.status;
714         }
715         
716         /**{@inheritDoc}*/
717         public int hashCode() {
718             return 43 ^ status;
719         }
720         
721         private static String JavaDoc[] STATUS_NAMES = new String JavaDoc[] {
722             "none", "ok", "warning", "error" // NOI18N
723
};
724         
725         /**Returns a {@link String} representation of the {@link Status}.
726          * The format of the {@link String} is not specified.
727          * This method should only be used for debugging purposes.
728          *
729          * @return {@link String} representation of this object
730          */

731         public String JavaDoc toString() {
732             return "[Status: " + STATUS_NAMES[getStatus()] + "]"; // NOI18N
733
}
734         
735         /**Return the more important status out of the two given statuses.
736          * The statuses are ordered as follows:
737          * {@link #STATUS_ERROR}&gt;{@link #STATUS_WARNING}&gt;{@link #STATUS_OK}.
738          *
739          * @param first one provided status
740          * @param second another provided status
741          * @return the more important status out of the two provided statuses
742          * @throws IllegalArgumentException if one of the provided statuses is something
743          * else then {@link #STATUS_ERROR},
744          * {@link #STATUS_WARNING} and
745          * {@link #STATUS_OK}
746          */

747         public static Severity getCompoundStatus(Severity first, Severity second) throws IllegalArgumentException JavaDoc {
748             if (first != STATUS_ERROR && first != STATUS_WARNING && first != STATUS_OK)
749                 throw new IllegalArgumentException JavaDoc("Invalid status provided: " + first); // NOI18N
750

751             if (second != STATUS_ERROR && second != STATUS_WARNING && second != STATUS_OK)
752                 throw new IllegalArgumentException JavaDoc("Invalid status provided: " + second); // NOI18N
753

754             return VALUES[Math.max(first.getStatus(), second.getStatus())];
755         }
756         
757         /**Returns default {@link Color} for a given {@link Status}.
758          *
759          * @param s {@link Status} for which default color should be found
760          * @return default {@link Color} for a given {@link Status}
761          */

762         public static Color JavaDoc getDefaultColor(Severity s) {
763             return DEFAULT_STATUS_COLORS[s.getStatus()];
764         }
765         
766         public static Severity valueOf(String JavaDoc severity) {
767             Severity severityValue = Severity.STATUS_NONE;
768             
769             if (severity != null) {
770                 if ("ok".equals(severity)) { // NOI18N
771
severityValue = AnnotationType.Severity.STATUS_OK;
772                 } else {
773                     if ("warning".equals(severity)) { // NOI18N
774
severityValue = AnnotationType.Severity.STATUS_WARNING;
775                     } else {
776                         if ("error".equals(severity)) { // NOI18N
777
severityValue = AnnotationType.Severity.STATUS_ERROR;
778                         }
779                     }
780                 }
781             }
782             
783             return severityValue;
784         }
785         
786         public String JavaDoc getName() {
787             return STATUS_NAMES[status];
788         }
789     }
790 }
791
Popular Tags