001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Collections;
015: import java.util.HashSet;
016: import java.util.Iterator;
017:
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.jface.preference.PreferenceNode;
020: import org.eclipse.jface.resource.ImageDescriptor;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.ui.IPluginContribution;
023: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
024: import org.eclipse.ui.internal.registry.KeywordRegistry;
025: import org.eclipse.ui.plugin.AbstractUIPlugin;
026:
027: /**
028: * The WorkbenchPreferenceExtensionNode is the abstract class for all property
029: * and page nodes in the workbench.
030: *
031: * @since 3.1
032: */
033: public abstract class WorkbenchPreferenceExtensionNode extends
034: PreferenceNode implements IPluginContribution {
035:
036: private static final String TAG_KEYWORD_REFERENCE = "keywordReference"; //$NON-NLS-1$
037:
038: private Collection keywordReferences;
039:
040: private IConfigurationElement configurationElement;
041:
042: private ImageDescriptor imageDescriptor;
043:
044: private Image image;
045:
046: private Collection keywordLabelCache;
047:
048: /**
049: * Create a new instance of the reciever.
050: *
051: * @param id
052: * @param configurationElement
053: */
054: public WorkbenchPreferenceExtensionNode(String id,
055: IConfigurationElement configurationElement) {
056: super (id);
057: this .configurationElement = configurationElement;
058: }
059:
060: /**
061: * Get the ids of the keywords the receiver is bound to.
062: *
063: * @return Collection of <code>String</code>. Never <code>null</code>.
064: */
065: public Collection getKeywordReferences() {
066: if (keywordReferences == null) {
067: IConfigurationElement[] references = getConfigurationElement()
068: .getChildren(TAG_KEYWORD_REFERENCE);
069: HashSet list = new HashSet(references.length);
070: for (int i = 0; i < references.length; i++) {
071: IConfigurationElement page = references[i];
072: String id = page
073: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
074: if (id != null) {
075: list.add(id);
076: }
077: }
078:
079: if (!list.isEmpty()) {
080: keywordReferences = list;
081: } else {
082: keywordReferences = Collections.EMPTY_SET;
083: }
084:
085: }
086: return keywordReferences;
087: }
088:
089: /**
090: * Get the labels of all of the keywords of the receiver.
091: *
092: * @return Collection of <code>String</code>. Never <code>null</code>.
093: */
094: public Collection getKeywordLabels() {
095: if (keywordLabelCache != null) {
096: return keywordLabelCache;
097: }
098:
099: Collection refs = getKeywordReferences();
100:
101: if (refs == Collections.EMPTY_SET) {
102: keywordLabelCache = Collections.EMPTY_SET;
103: return keywordLabelCache;
104: }
105:
106: keywordLabelCache = new ArrayList(refs.size());
107: Iterator referenceIterator = refs.iterator();
108: while (referenceIterator.hasNext()) {
109: Object label = KeywordRegistry.getInstance()
110: .getKeywordLabel((String) 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: /* (non-Javadoc)
151: * @see org.eclipse.jface.preference.IPreferenceNode#getLabelText()
152: */
153: public String getLabelText() {
154: return getConfigurationElement().getAttribute(
155: 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 imageName = getConfigurationElement().getAttribute(
169: IWorkbenchRegistryConstants.ATT_ICON);
170: if (imageName != null) {
171: String contributingPluginId = getConfigurationElement()
172: .getNamespace();
173: imageDescriptor = AbstractUIPlugin
174: .imageDescriptorFromPlugin(contributingPluginId,
175: imageName);
176: }
177: return imageDescriptor;
178: }
179:
180: /**
181: * Return the configuration element.
182: *
183: * @return the configuration element
184: */
185: public IConfigurationElement getConfigurationElement() {
186: return configurationElement;
187: }
188:
189: /* (non-Javadoc)
190: * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
191: */
192: public String getLocalId() {
193: return getId();
194: }
195:
196: /* (non-Javadoc)
197: * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
198: */
199: public String getPluginId() {
200: return getConfigurationElement().getNamespace();
201: }
202: }
|