KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.internal.preferences;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.preferences.IPreferenceFilter;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.ui.IPluginContribution;
20 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
21 import org.eclipse.ui.internal.registry.PreferenceTransferRegistryReader;
22 import org.eclipse.ui.internal.registry.RegistryReader;
23 import org.eclipse.ui.model.WorkbenchAdapter;
24 import org.eclipse.ui.plugin.AbstractUIPlugin;
25
26 /**
27  * Instances represent registered preference transfers.
28  *
29  * @since 3.1
30  */

31 public class PreferenceTransferElement extends WorkbenchAdapter implements
32         IPluginContribution {
33     private String JavaDoc id;
34     
35     private ImageDescriptor imageDescriptor;
36
37     private IConfigurationElement configurationElement;
38
39     private IPreferenceFilter filter;
40
41     /**
42      * Create a new instance of this class
43      *
44      * @param configurationElement
45      *
46      */

47     public PreferenceTransferElement(IConfigurationElement configurationElement) {
48         this.configurationElement = configurationElement;
49         id = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
50     }
51
52     /**
53      * @return IConfigurationElement
54      */

55     public IConfigurationElement getConfigurationElement() {
56         return configurationElement;
57     }
58
59     /**
60      * Answer the preference filter of this element
61      * If the class attribute is specified it will be used, if not then look to the
62      *
63      * @return java.lang.String
64      * @throws CoreException
65      */

66     public IPreferenceFilter getFilter() throws CoreException {
67         //TODO: can the CoreException be removed?
68
if (filter == null) {
69             IConfigurationElement[] mappings = PreferenceTransferRegistryReader.getMappings(configurationElement);
70             PreferenceFilter prefFilter = new PreferenceFilter();
71             prefFilter.scopes = new String JavaDoc[mappings.length];
72             prefFilter.maps = new Map JavaDoc[mappings.length];
73             for (int i = 0; i < mappings.length; i++) {
74                 prefFilter.scopes[i] = PreferenceTransferRegistryReader.getScope(mappings[i]);
75                 prefFilter.maps[i] = PreferenceTransferRegistryReader.getEntry(mappings[i]);
76             }
77             filter = prefFilter;
78         }
79         return filter;
80     }
81
82     /**
83      * Answer the description parameter of this element
84      *
85      * @return java.lang.String
86      */

87     public String JavaDoc getDescription() {
88         return RegistryReader.getDescription(configurationElement);
89     }
90     
91     /**
92      * Answer the id as specified in the extension.
93      *
94      * @return java.lang.String
95      */

96     public String JavaDoc getID() {
97         return id;
98     }
99
100     /**
101      * Answer the icon of this element.
102      *
103      * @return an image descriptor
104      */

105     public ImageDescriptor getImageDescriptor() {
106         if (imageDescriptor == null) {
107             String JavaDoc iconName = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
108             if (iconName == null) {
109                 return null;
110             }
111             imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
112                     configurationElement.getNamespace(), iconName);
113         }
114         return imageDescriptor;
115     }
116     
117     /**
118      * Returns the name of this preference transfer element.
119      * @return the name of the element
120      */

121     public String JavaDoc getName() {
122         return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.ui.IPluginContribution#getLocalId()
127      */

128     public String JavaDoc getLocalId() {
129         return getID();
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.ui.IPluginContribution#getPluginId()
134      */

135     public String JavaDoc getPluginId() {
136         return (configurationElement != null) ? configurationElement
137                 .getNamespace() : null;
138     }
139
140     class PreferenceFilter implements IPreferenceFilter {
141
142         protected String JavaDoc[] scopes;
143         protected Map JavaDoc[] maps;
144         
145         public String JavaDoc[] getScopes() {
146             return scopes;
147         }
148
149         public Map JavaDoc getMapping(String JavaDoc scope) {
150             for (int i = 0; i < scopes.length; i++) {
151                 String JavaDoc item = scopes[i];
152                 if (item.equals(scope)) {
153                     return maps[i];
154                 }
155             }
156             return null;
157         }
158         
159     }
160 }
161
Popular Tags