KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > WorkbenchPreferenceExtensionNode


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.jface.preference.PreferenceNode;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.ui.IPluginContribution;
25 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
26 import org.eclipse.ui.internal.registry.KeywordRegistry;
27 import org.eclipse.ui.plugin.AbstractUIPlugin;
28
29 /**
30  * The WorkbenchPreferenceExtensionNode is the abstract class for all property
31  * and page nodes in the workbench.
32  *
33  * @since 3.1
34  */

35 public abstract class WorkbenchPreferenceExtensionNode extends PreferenceNode implements IPluginContribution {
36     
37     private static final String JavaDoc TAG_KEYWORD_REFERENCE = "keywordReference"; //$NON-NLS-1$
38

39     private Collection JavaDoc keywordReferences;
40     
41     private IConfigurationElement configurationElement;
42
43     private ImageDescriptor imageDescriptor;
44
45     private Image image;
46
47     private Collection JavaDoc keywordLabelCache;
48     
49
50     /**
51      * Create a new instance of the reciever.
52      *
53      * @param id
54      * @param configurationElement
55      */

56     public WorkbenchPreferenceExtensionNode(String JavaDoc id, IConfigurationElement configurationElement) {
57         super(id);
58         this.configurationElement = configurationElement;
59     }
60
61     /**
62      * Get the ids of the keywords the receiver is bound to.
63      *
64      * @return Collection of <code>String</code>. Never <code>null</code>.
65      */

66     public Collection JavaDoc getKeywordReferences() {
67         if (keywordReferences == null) {
68             IConfigurationElement[] references = getConfigurationElement()
69                     .getChildren(TAG_KEYWORD_REFERENCE);
70             HashSet JavaDoc list = new HashSet JavaDoc(references.length);
71             for (int i = 0; i < references.length; i++) {
72                 IConfigurationElement page = references[i];
73                 String JavaDoc id = page.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
74                 if (id != null) {
75                     list.add(id);
76                 }
77             }
78
79             if (!list.isEmpty()) {
80                 keywordReferences = list;
81             } else {
82                 keywordReferences = Collections.EMPTY_SET;
83             }
84             
85         }
86         return keywordReferences;
87     }
88
89     /**
90      * Get the labels of all of the keywords of the receiver.
91      *
92      * @return Collection of <code>String</code>. Never <code>null</code>.
93      */

94     public Collection JavaDoc getKeywordLabels() {
95         if (keywordLabelCache != null) {
96             return keywordLabelCache;
97         }
98         
99         Collection JavaDoc refs = getKeywordReferences();
100         
101         if(refs == Collections.EMPTY_SET) {
102             keywordLabelCache = Collections.EMPTY_SET;
103             return keywordLabelCache;
104         }
105         
106         keywordLabelCache = new ArrayList JavaDoc(refs.size());
107         Iterator JavaDoc referenceIterator = refs.iterator();
108         while(referenceIterator.hasNext()){
109             Object JavaDoc label = KeywordRegistry.getInstance().getKeywordLabel(
110                     (String JavaDoc) referenceIterator.next());
111             if(label != null) {
112                 keywordLabelCache.add(label);
113             }
114         }
115         
116         return keywordLabelCache;
117     }
118     
119     /**
120      * Clear the keyword cache, if any.
121      */

122     public void clearKeywords() {
123         keywordLabelCache = null;
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.jface.preference.IPreferenceNode#disposeResources()
128      */

129     public void disposeResources() {
130         if (image != null) {
131             image.dispose();
132             image = null;
133         }
134         super.disposeResources();
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.jface.preference.IPreferenceNode#getLabelImage()
139      */

140     public Image getLabelImage() {
141         if (image == null) {
142             ImageDescriptor desc = getImageDescriptor();
143             if (desc != null) {
144                 image = imageDescriptor.createImage();
145             }
146         }
147         return image;
148     }
149
150
151     /* (non-Javadoc)
152      * @see org.eclipse.jface.preference.IPreferenceNode#getLabelText()
153      */

154     public String JavaDoc getLabelText() {
155         return getConfigurationElement().getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
156     }
157
158     /**
159      * Returns the image descriptor for this node.
160      *
161      * @return the image descriptor
162      */

163     public ImageDescriptor getImageDescriptor() {
164         if (imageDescriptor != null) {
165             return imageDescriptor;
166         }
167         
168         String JavaDoc imageName = getConfigurationElement().getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
169         if (imageName != null) {
170             String JavaDoc contributingPluginId = getConfigurationElement().getNamespace();
171             imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(contributingPluginId, imageName);
172         }
173         return imageDescriptor;
174     }
175     
176     /**
177      * Return the configuration element.
178      *
179      * @return the configuration element
180      */

181     public IConfigurationElement getConfigurationElement() {
182         return configurationElement;
183     }
184     
185     /* (non-Javadoc)
186      * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
187      */

188     public String JavaDoc getLocalId() {
189         return getId();
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
194      */

195     public String JavaDoc getPluginId() {
196         return getConfigurationElement().getNamespace();
197     }
198 }
199
Popular Tags