KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > AnnotationTypeLookup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.texteditor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtensionPoint;
21 import org.eclipse.core.runtime.Platform;
22
23 import org.eclipse.jface.resource.StringConverter;
24
25 import org.eclipse.ui.editors.text.EditorsUI;
26
27
28 /**
29  * Provides the strategy for determining annotation types for given markers.
30  *
31  * @since 3.0
32  */

33 public final class AnnotationTypeLookup {
34
35     /**
36      * Record representing an annotation type mapping.
37      */

38     private static class AnnotationTypeMapping {
39
40         final static int UNDEFINED= -1;
41
42         String JavaDoc fAnnotationType;
43         String JavaDoc fMarkerType;
44         int fMarkerSeverity= UNDEFINED;
45
46         boolean isMarkerSeverityDefined() {
47             return fMarkerSeverity != UNDEFINED;
48         }
49     }
50
51     /** The lookup table for marker to annotation type mappings. */
52     private Map JavaDoc fMapping;
53
54     /**
55      * Creates a new annotation lookup object.
56      */

57     public AnnotationTypeLookup() {
58     }
59
60     /**
61      * Computes the annotation type that corresponds to the state of
62      * the given marker.
63      *
64      * @param marker the marker
65      * @return the annotation type or <code>null</code>
66      */

67     public String JavaDoc getAnnotationType(IMarker marker) {
68         String JavaDoc markerType= MarkerUtilities.getMarkerType(marker);
69         if (markerType != null) {
70             int severity= MarkerUtilities.getSeverity(marker);
71             return getAnnotationType(markerType, severity);
72         }
73         return null;
74     }
75
76     /**
77      * Computes the annotation type that corresponds to the given marker type and
78      * the given marker severity.
79      *
80      * @param markerType the marker type
81      * @param markerSeverity the marker severity
82      * @return the annotation type or <code>null</code>
83      */

84     public String JavaDoc getAnnotationType(String JavaDoc markerType, int markerSeverity) {
85         String JavaDoc annotationType= lookupAnnotationType(markerType, markerSeverity);
86         if (annotationType != null)
87             return annotationType;
88         String JavaDoc[] superTypes= MarkerUtilities.getSuperTypes(markerType);
89         for (int i= 0; i < superTypes.length; i++) {
90             annotationType= lookupAnnotationType(superTypes[i], markerSeverity);
91             if (annotationType != null)
92                 return annotationType;
93         }
94         return null;
95     }
96
97     /**
98      * Returns the annotation type for the given marker type and the given
99      * marker severity.
100      *
101      * @param markerType the marker type
102      * @param severity the marker severity
103      * @return the annotation type
104      */

105     private String JavaDoc lookupAnnotationType(String JavaDoc markerType, int severity) {
106         if (fMapping == null)
107             initializeMapping();
108
109         Object JavaDoc value= fMapping.get(markerType);
110
111         if (value instanceof String JavaDoc)
112             return (String JavaDoc) value;
113
114         if (value instanceof Map JavaDoc) {
115             Map JavaDoc severityMap= (Map JavaDoc) value;
116             return (String JavaDoc) severityMap.get(new Integer JavaDoc(severity));
117         }
118
119         return null;
120     }
121
122     /**
123      * Initializes the mapping between markers and their property values and
124      * annotation types.
125      */

126     private void initializeMapping() {
127         fMapping= new HashMap JavaDoc();
128         List JavaDoc mappings= getAnnotationTypeMappings();
129         for (int i= 0, l= mappings.size(); i < l; i++) {
130             AnnotationTypeMapping atm= (AnnotationTypeMapping) mappings.get(i);
131             if (atm.isMarkerSeverityDefined()) {
132                 Object JavaDoc severityMap= fMapping.get(atm.fMarkerType);
133                 if (!(severityMap instanceof Map JavaDoc)) {
134                     severityMap= new HashMap JavaDoc();
135                     fMapping.put(atm.fMarkerType, severityMap);
136                 }
137                 Map JavaDoc map= (Map JavaDoc) severityMap;
138                 map.put(new Integer JavaDoc(atm.fMarkerSeverity), atm.fAnnotationType);
139             } else {
140                 fMapping.put(atm.fMarkerType, atm.fAnnotationType);
141             }
142         }
143     }
144
145     /**
146      * Returns the list of annotation type mappings generated from the
147      * extensions provided for the annotation type extension point.
148      *
149      * @return a list of annotation type mappings
150      */

151     private List JavaDoc getAnnotationTypeMappings() {
152         List JavaDoc annotationTypeMappings= new ArrayList JavaDoc();
153         // read compatibility mode
154
readExtensionPoint(annotationTypeMappings, "markerAnnotationSpecification", "annotationType"); //$NON-NLS-1$ //$NON-NLS-2$
155
// read new extension point
156
readExtensionPoint(annotationTypeMappings, "annotationTypes", "name"); //$NON-NLS-1$ //$NON-NLS-2$
157
return annotationTypeMappings;
158     }
159
160     /**
161      * Reads the extensions provided for the given extension point name. Uses
162      * the given type attribute name to create annotation type mappings that
163      * are appended to the given list.
164      *
165      * @param annotationTypeMappings the list to be populated
166      * @param extensionPointName the name of the extension point to read
167      * @param typeAttributeName the name of attribute specifying the annotation
168      * type
169      */

170     private void readExtensionPoint(List JavaDoc annotationTypeMappings, String JavaDoc extensionPointName, String JavaDoc typeAttributeName) {
171         IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, extensionPointName);
172         if (extensionPoint != null) {
173             IConfigurationElement[] elements= extensionPoint.getConfigurationElements();
174             for (int i= 0; i < elements.length; i++) {
175                 AnnotationTypeMapping mapping= createMapping(elements[i], typeAttributeName);
176                 if (mapping != null)
177                     annotationTypeMappings.add(mapping);
178             }
179         }
180     }
181
182     /**
183      * Creates an annotation type mapping from the given configuration element.
184      *
185      * @param element the configuration element
186      * @param typeAttributeName the name of the attribute specifying the
187      * annotation type
188      * @return the annotation type mapping or <code>null</code>
189      */

190     private AnnotationTypeMapping createMapping(IConfigurationElement element, String JavaDoc typeAttributeName) {
191
192         AnnotationTypeMapping mapping= new AnnotationTypeMapping();
193
194         String JavaDoc s= element.getAttribute(typeAttributeName);
195         if (s == null || s.trim().length() == 0) return null;
196         mapping.fAnnotationType= s;
197
198         s= element.getAttribute("markerType"); //$NON-NLS-1$
199
if (s == null || s.trim().length() == 0) return null;
200         mapping.fMarkerType= s;
201
202         s= element.getAttribute("markerSeverity"); //$NON-NLS-1$
203
if (s != null && s.trim().length() > 0)
204             mapping.fMarkerSeverity= StringConverter.asInt(s, AnnotationTypeMapping.UNDEFINED);
205
206         return mapping;
207     }
208 }
209
Popular Tags