KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.texteditor;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jface.text.source.Annotation;
18
19 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
20 import org.eclipse.ui.internal.texteditor.*;
21
22 /**
23  * Provides the strategy for finding the annotation preference for a given
24  * annotation.
25  *
26  * @since 3.0
27  */

28 public class AnnotationPreferenceLookup {
29
30     /** The map between annotation types and annotation preference fragments. */
31     private Map JavaDoc fFragments;
32
33     /**
34      * Creates a new annotation preference lookup object.
35      */

36     public AnnotationPreferenceLookup() {
37     }
38
39     /**
40      * Returns the annotation preference of a given annotation.
41      *
42      * @param annotation the annotation
43      * @return the annotation preference for the given annotation or <code>null</code>
44      */

45     public AnnotationPreference getAnnotationPreference(Annotation annotation) {
46         return getAnnotationPreference(annotation.getType());
47     }
48
49     /**
50      * Returns the annotation preference defined for the given annotation type.
51      *
52      * @param annotationType the annotation type
53      * @return the annotation preference for the given annotation type or <code>null</code>
54      */

55     public AnnotationPreference getAnnotationPreference(String JavaDoc annotationType) {
56         if (annotationType == null || annotationType == Annotation.TYPE_UNKNOWN)
57             return null;
58
59         AnnotationTypeHierarchy hierarchy= getAnnotationTypeHierarchy();
60         AnnotationType type= hierarchy.getAnnotationType(annotationType);
61         AnnotationPreference preference= type.getPreference();
62         if (preference == null) {
63             preference= new DelegatingAnnotationPreference(type, this);
64             type.setAnnotationPreference(preference);
65         }
66
67         return preference;
68     }
69
70     /**
71      * Returns the annotation preference fragment defined for the given
72      * annotation type.
73      * <p>
74      * For internal use only. Not intended to be called by clients.
75      *
76      * @param annotationType the annotation type
77      * @return the defined annotation preference fragment
78      */

79     public AnnotationPreference getAnnotationPreferenceFragment(String JavaDoc annotationType) {
80         Map JavaDoc fragments= getPreferenceFragments();
81         return (AnnotationPreference) fragments.get(annotationType);
82     }
83
84     /**
85      * Returns the annotation type hierarchy and creates it when not yet done.
86      *
87      * @return the annotation type hierarchy
88      */

89     private AnnotationTypeHierarchy getAnnotationTypeHierarchy() {
90         return EditorsPlugin.getDefault().getAnnotationTypeHierarchy();
91     }
92
93     /**
94      * Returns a map between annotation type names and annotation preference
95      * fragments and creates it if not yet done.
96      *
97      * @return the map between annotation type names and annotation preference fragments
98      */

99     private synchronized Map JavaDoc getPreferenceFragments() {
100         if (fFragments == null) {
101             fFragments= new HashMap JavaDoc();
102             MarkerAnnotationPreferences p= new MarkerAnnotationPreferences();
103             Iterator JavaDoc e= p.getAnnotationPreferenceFragments().iterator();
104             while (e.hasNext()) {
105                 AnnotationPreference fragment= (AnnotationPreference) e.next();
106                 Object JavaDoc annotationType = fragment.getAnnotationType();
107                 AnnotationPreference preference= (AnnotationPreference) fFragments.get(annotationType);
108                 if (preference == null)
109                     fFragments.put(annotationType, fragment);
110                 else
111                     preference.merge(fragment);
112             }
113         }
114         return fFragments;
115     }
116 }
117
Popular Tags