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.Map;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.preferences.IPreferenceFilter;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.ui.IPluginContribution;
019: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
020: import org.eclipse.ui.internal.registry.PreferenceTransferRegistryReader;
021: import org.eclipse.ui.internal.registry.RegistryReader;
022: import org.eclipse.ui.model.WorkbenchAdapter;
023: import org.eclipse.ui.plugin.AbstractUIPlugin;
024:
025: /**
026: * Instances represent registered preference transfers.
027: *
028: * @since 3.1
029: */
030: public class PreferenceTransferElement extends WorkbenchAdapter
031: implements IPluginContribution {
032: private String id;
033:
034: private ImageDescriptor imageDescriptor;
035:
036: private IConfigurationElement configurationElement;
037:
038: private IPreferenceFilter filter;
039:
040: /**
041: * Create a new instance of this class
042: *
043: * @param configurationElement
044: *
045: */
046: public PreferenceTransferElement(
047: IConfigurationElement configurationElement) {
048: this .configurationElement = configurationElement;
049: id = configurationElement
050: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
051: }
052:
053: /**
054: * @return IConfigurationElement
055: */
056: public IConfigurationElement getConfigurationElement() {
057: return configurationElement;
058: }
059:
060: /**
061: * Answer the preference filter of this element
062: * If the class attribute is specified it will be used, if not then look to the
063: *
064: * @return java.lang.String
065: * @throws CoreException
066: */
067: public IPreferenceFilter getFilter() throws CoreException {
068: //TODO: can the CoreException be removed?
069: if (filter == null) {
070: IConfigurationElement[] mappings = PreferenceTransferRegistryReader
071: .getMappings(configurationElement);
072: PreferenceFilter prefFilter = new PreferenceFilter();
073: prefFilter.scopes = new String[mappings.length];
074: prefFilter.maps = new Map[mappings.length];
075: for (int i = 0; i < mappings.length; i++) {
076: prefFilter.scopes[i] = PreferenceTransferRegistryReader
077: .getScope(mappings[i]);
078: prefFilter.maps[i] = PreferenceTransferRegistryReader
079: .getEntry(mappings[i]);
080: }
081: filter = prefFilter;
082: }
083: return filter;
084: }
085:
086: /**
087: * Answer the description parameter of this element
088: *
089: * @return java.lang.String
090: */
091: public String getDescription() {
092: return RegistryReader.getDescription(configurationElement);
093: }
094:
095: /**
096: * Answer the id as specified in the extension.
097: *
098: * @return java.lang.String
099: */
100: public String getID() {
101: return id;
102: }
103:
104: /**
105: * Answer the icon of this element.
106: *
107: * @return an image descriptor
108: */
109: public ImageDescriptor getImageDescriptor() {
110: if (imageDescriptor == null) {
111: String iconName = configurationElement
112: .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
113: if (iconName == null) {
114: return null;
115: }
116: imageDescriptor = AbstractUIPlugin
117: .imageDescriptorFromPlugin(configurationElement
118: .getNamespace(), iconName);
119: }
120: return imageDescriptor;
121: }
122:
123: /**
124: * Returns the name of this preference transfer element.
125: * @return the name of the element
126: */
127: public String getName() {
128: return configurationElement
129: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
130: }
131:
132: /* (non-Javadoc)
133: * @see org.eclipse.ui.IPluginContribution#getLocalId()
134: */
135: public String getLocalId() {
136: return getID();
137: }
138:
139: /* (non-Javadoc)
140: * @see org.eclipse.ui.IPluginContribution#getPluginId()
141: */
142: public String getPluginId() {
143: return (configurationElement != null) ? configurationElement
144: .getNamespace() : null;
145: }
146:
147: class PreferenceFilter implements IPreferenceFilter {
148:
149: protected String[] scopes;
150: protected Map[] maps;
151:
152: public String[] getScopes() {
153: return scopes;
154: }
155:
156: public Map getMapping(String scope) {
157: for (int i = 0; i < scopes.length; i++) {
158: String item = scopes[i];
159: if (item.equals(scope)) {
160: return maps[i];
161: }
162: }
163: return null;
164: }
165:
166: }
167: }
|