KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > WorkingSetDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtension;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.ui.IPluginContribution;
24 import org.eclipse.ui.IWorkingSetElementAdapter;
25 import org.eclipse.ui.IWorkingSetUpdater;
26 import org.eclipse.ui.dialogs.IWorkingSetPage;
27 import org.eclipse.ui.internal.WorkbenchPlugin;
28 import org.eclipse.ui.plugin.AbstractUIPlugin;
29
30 /**
31  * A working set descriptor stores the plugin registry data for
32  * a working set page extension.
33  *
34  * @since 2.0
35  */

36 public class WorkingSetDescriptor implements IPluginContribution {
37     private String JavaDoc id;
38
39     private String JavaDoc name;
40
41     private String JavaDoc icon;
42
43     private String JavaDoc pageClassName;
44     
45     private String JavaDoc updaterClassName;
46
47     private IConfigurationElement configElement;
48     
49     private String JavaDoc[] classTypes;
50
51     private String JavaDoc[] adapterTypes;
52
53     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
54

55     private static final String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
56

57     private static final String JavaDoc ATT_ICON = "icon"; //$NON-NLS-1$
58

59     private static final String JavaDoc ATT_PAGE_CLASS = "pageClass"; //$NON-NLS-1$
60

61     private static final String JavaDoc ATT_UPDATER_CLASS = "updaterClass"; //$NON-NLS-1$
62

63     private static final String JavaDoc ATT_ELEMENT_ADAPTER_CLASS = "elementAdapterClass"; //$NON-NLS-1$
64

65     private static final String JavaDoc TAG_APPLICABLE_TYPE = "applicableType"; //$NON-NLS-1$
66

67     /**
68      * Creates a descriptor from a configuration element.
69      *
70      * @param configElement configuration element to create a descriptor from
71      */

72     public WorkingSetDescriptor(IConfigurationElement configElement)
73             throws CoreException {
74         super();
75         this.configElement = configElement;
76         id = configElement.getAttribute(ATT_ID);
77         name = configElement.getAttribute(ATT_NAME);
78         icon = configElement.getAttribute(ATT_ICON);
79         pageClassName = configElement.getAttribute(ATT_PAGE_CLASS);
80         updaterClassName = configElement.getAttribute(ATT_UPDATER_CLASS);
81
82         if (name == null) {
83             throw new CoreException(new Status(IStatus.ERROR,
84                     WorkbenchPlugin.PI_WORKBENCH, 0,
85                     "Invalid extension (missing class name): " + id, //$NON-NLS-1$
86
null));
87         }
88         
89         IConfigurationElement[] containsChildren = configElement
90                 .getChildren(TAG_APPLICABLE_TYPE);
91         if (containsChildren.length > 0) {
92             List JavaDoc byClassList = new ArrayList JavaDoc(containsChildren.length);
93             List JavaDoc byAdapterList = new ArrayList JavaDoc(containsChildren.length);
94             for (int i = 0; i < containsChildren.length; i++) {
95                 IConfigurationElement child = containsChildren[i];
96                 String JavaDoc className = child
97                         .getAttribute(IWorkbenchRegistryConstants.ATT_CLASS);
98                 if (className != null)
99                     byClassList.add(className);
100                 if ("true".equals(child.getAttribute(IWorkbenchRegistryConstants.ATT_ADAPTABLE))) //$NON-NLS-1$
101
byAdapterList.add(className);
102             }
103             if (!byClassList.isEmpty()) {
104                 classTypes = (String JavaDoc[]) byClassList.toArray(new String JavaDoc[byClassList
105                         .size()]);
106                 Arrays.sort(classTypes);
107             }
108             
109             if (!byAdapterList.isEmpty()) {
110                 adapterTypes = (String JavaDoc[]) byAdapterList.toArray(new String JavaDoc[byAdapterList
111                         .size()]);
112                 Arrays.sort(adapterTypes);
113             }
114         }
115     }
116     
117     /**
118      * Returns the name space that declares this working set.
119      *
120      * @return the name space declaring this working set
121      */

122     public String JavaDoc getDeclaringNamespace() {
123         return configElement.getNamespace();
124     }
125     
126     /**
127      * Return the namespace that contains the class referenced by the
128      * updaterClass. May be the bundle that declared this extension or another
129      * bundle that contains the referenced class.
130      *
131      * @return the namespace
132      * @since 3.3
133      */

134     public String JavaDoc getUpdaterNamespace() {
135         return WorkbenchPlugin.getBundleForExecutableExtension(configElement,
136                 ATT_UPDATER_CLASS).getSymbolicName();
137     }
138     
139     /**
140      * Return the namespace that contains the class referenced by the
141      * elementAdapterClass. May be the bundle that declared this extension or
142      * another bundle that contains the referenced class.
143      *
144      * @return the namespace
145      * @since 3.3
146      */

147     public String JavaDoc getElementAdapterNamespace() {
148         return WorkbenchPlugin.getBundleForExecutableExtension(configElement,
149                 ATT_UPDATER_CLASS).getSymbolicName();
150     }
151
152     /**
153      * Creates a working set page from this extension descriptor.
154      *
155      * @return a working set page created from this extension descriptor.
156      */

157     public IWorkingSetPage createWorkingSetPage() {
158         Object JavaDoc page = null;
159
160         if (pageClassName != null) {
161             try {
162                 page = WorkbenchPlugin.createExtension(configElement,
163                         ATT_PAGE_CLASS);
164             } catch (CoreException exception) {
165                 WorkbenchPlugin.log("Unable to create working set page: " + //$NON-NLS-1$
166
pageClassName, exception.getStatus());
167             }
168         }
169         return (IWorkingSetPage) page;
170     }
171
172     /**
173      * Returns the page's icon
174      *
175      * @return the page's icon
176      */

177     public ImageDescriptor getIcon() {
178         if (icon == null) {
179             return null;
180         }
181
182         IExtension extension = configElement.getDeclaringExtension();
183         String JavaDoc extendingPluginId = extension.getNamespace();
184         return AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId,
185                 icon);
186     }
187
188     /**
189      * Returns the working set page id.
190      *
191      * @return the working set page id.
192      */

193     public String JavaDoc getId() {
194         return id;
195     }
196
197     /**
198      * Returns the working set page class name
199      *
200      * @return the working set page class name or <code>null</code> if
201      * no page class name has been provided by the extension
202      */

203     public String JavaDoc getPageClassName() {
204         return pageClassName;
205     }
206
207     /**
208      * Returns the name of the working set element type the
209      * page works with.
210      *
211      * @return the working set element type name
212      */

213     public String JavaDoc getName() {
214         return name;
215     }
216     
217     /**
218      * Returns the working set updater class name
219      *
220      * @return the working set updater class name or <code>null</code> if
221      * no updater class name has been provided by the extension
222      */

223     public String JavaDoc getUpdaterClassName() {
224         return updaterClassName;
225     }
226     
227     /**
228      * Creates a working set element adapter.
229      *
230      * @return the element adapter or <code>null</code> if no adapter has been
231      * declared
232      */

233     public IWorkingSetElementAdapter createWorkingSetElementAdapter() {
234         if (!WorkbenchPlugin.hasExecutableExtension(configElement, ATT_ELEMENT_ADAPTER_CLASS))
235             return null;
236         IWorkingSetElementAdapter result = null;
237         try {
238             result = (IWorkingSetElementAdapter) WorkbenchPlugin
239                     .createExtension(configElement, ATT_ELEMENT_ADAPTER_CLASS);
240         } catch (CoreException exception) {
241             WorkbenchPlugin.log("Unable to create working set element adapter: " + //$NON-NLS-1$
242
result, exception.getStatus());
243         }
244         return result;
245     }
246     
247     /**
248      * Creates a working set updater.
249      *
250      * @return the working set updater or <code>null</code> if no updater has
251      * been declared
252      */

253     public IWorkingSetUpdater createWorkingSetUpdater() {
254         if (updaterClassName == null) {
255             return null;
256         }
257         IWorkingSetUpdater result = null;
258         try {
259             result = (IWorkingSetUpdater)WorkbenchPlugin.createExtension(configElement, ATT_UPDATER_CLASS);
260         } catch (CoreException exception) {
261             WorkbenchPlugin.log("Unable to create working set updater: " + //$NON-NLS-1$
262
updaterClassName, exception.getStatus());
263         }
264         return result;
265     }
266     
267     public boolean isUpdaterClassLoaded() {
268         return WorkbenchPlugin.isBundleLoadedForExecutableExtension(configElement, ATT_UPDATER_CLASS);
269     }
270     
271     public boolean isElementAdapterClassLoaded() {
272         return WorkbenchPlugin.isBundleLoadedForExecutableExtension(configElement, ATT_ELEMENT_ADAPTER_CLASS);
273     }
274     
275     /**
276      * Returns whether working sets based on this descriptor are editable.
277      *
278      * @return <code>true</code> if working sets based on this descriptor are editable; otherwise
279      * <code>false</code>
280      *
281      * @since 3.1
282      */

283     public boolean isEditable() {
284         return getPageClassName() != null;
285     }
286
287     public String JavaDoc getLocalId() {
288         return getId();
289     }
290
291     public String JavaDoc getPluginId() {
292         return getDeclaringNamespace();
293     }
294     
295     /**
296      * Return the config element for this descriptor.
297      *
298      * @return the config element
299      * @since 3.3
300      */

301     public IConfigurationElement getConfigurationElement() {
302         return configElement;
303     }
304 }
305
Popular Tags