KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > AnnotationTypeHierarchy


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.internal.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.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtensionPoint;
20 import org.eclipse.core.runtime.Platform;
21
22 import org.eclipse.ui.editors.text.EditorsUI;
23
24
25 /**
26  * Internal annotation super type hierarchy cache.
27  * TODO this cache is currently unbound, i.e. only limited by the number of annotation types
28  *
29  * @since 3.0
30  */

31 public final class AnnotationTypeHierarchy {
32
33     private Map JavaDoc fTypeMap;
34     private Map JavaDoc fTypesCache= new HashMap JavaDoc();
35
36     public AnnotationTypeHierarchy() {
37     }
38
39     public AnnotationType getAnnotationType(String JavaDoc typeName) {
40         AnnotationType type= (AnnotationType) fTypesCache.get(typeName);
41         if (type == null) {
42             String JavaDoc[] superTypes= computeSuperTypes(typeName);
43             type= new AnnotationType(typeName, superTypes);
44             fTypesCache.put(typeName, type);
45         }
46         return type;
47     }
48
49     public boolean isSubtype(String JavaDoc superType, String JavaDoc subtypeCandidate) {
50         AnnotationType type= getAnnotationType(subtypeCandidate);
51         return type.isSubtype(superType);
52     }
53
54     private String JavaDoc[] computeSuperTypes(String JavaDoc typeName) {
55         ArrayList JavaDoc types= new ArrayList JavaDoc();
56         append(types, getDirectSuperType(typeName));
57         int index= 0;
58         while (index < types.size()) {
59             String JavaDoc type= (String JavaDoc) types.get(index++);
60             append(types, getDirectSuperType(type));
61         }
62
63         String JavaDoc[] superTypes= new String JavaDoc[types.size()];
64         types.toArray(superTypes);
65         return superTypes;
66     }
67
68     private String JavaDoc getDirectSuperType(String JavaDoc typeName) {
69         return (String JavaDoc) getTypeMap().get(typeName);
70     }
71
72     private void append(List JavaDoc list, String JavaDoc string) {
73         if (string == null || string.trim().length() == 0)
74             return;
75
76         if (!list.contains(string))
77             list.add(string);
78     }
79
80     private Map JavaDoc getTypeMap() {
81         if (fTypeMap == null)
82             fTypeMap= readTypes();
83         return fTypeMap;
84     }
85
86     private Map JavaDoc readTypes() {
87         HashMap JavaDoc allTypes= new HashMap JavaDoc();
88
89         IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, "annotationTypes"); //$NON-NLS-1$
90
if (extensionPoint != null) {
91             IConfigurationElement[] elements= extensionPoint.getConfigurationElements();
92             for (int i= 0; i < elements.length; i++) {
93                 IConfigurationElement element= elements[i];
94
95                 String JavaDoc name= element.getAttribute("name"); //$NON-NLS-1$
96
if (name == null || name.trim().length() == 0)
97                     continue;
98
99                 String JavaDoc parent= element.getAttribute("super"); //$NON-NLS-1$
100
if (parent == null || parent.trim().length() == 0)
101                     parent= ""; //$NON-NLS-1$
102

103                 allTypes.put(name, parent);
104             }
105         }
106
107         return allTypes;
108     }
109 }
110
Popular Tags