KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > registry > Capability


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ide.registry;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IProjectNatureDescriptor;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.ui.ICapabilityInstallWizard;
23 import org.eclipse.ui.ICapabilityUninstallWizard;
24 import org.eclipse.ui.WorkbenchException;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
27 import org.eclipse.ui.model.IWorkbenchAdapter;
28 import org.eclipse.ui.model.WorkbenchAdapter;
29 import org.eclipse.ui.plugin.AbstractUIPlugin;
30
31 /**
32  * A capability is the user interface aspect of a project's nature. There is
33  * a 1-1 relationship between a capability and a nature. It is possible for
34  * a nature to not have a capability, but the opposite is not true - that is,
35  * a capability must represent a nature.
36  * <p>
37  * A capability can take control of the user interface of other capabilities. It
38  * is then responsible for collecting the necessary information and adding the
39  * natures represented by these capabilites.
40  * </p>
41  */

42 public class Capability extends WorkbenchAdapter implements IAdaptable {
43     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
44

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

47     private static final String JavaDoc ATT_NATURE_ID = "natureId"; //$NON-NLS-1$
48

49     private static final String JavaDoc ATT_CATEGORY = "category"; //$NON-NLS-1$
50

51     private static final String JavaDoc ATT_DESCRIPTION = "description"; //$NON-NLS-1$
52

53     private static final String JavaDoc ATT_INSTALL_WIZARD = "installWizard"; //$NON-NLS-1$
54

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

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

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

61     private String JavaDoc id;
62
63     private String JavaDoc natureId;
64
65     private IProjectNatureDescriptor natureDescriptor;
66
67     private ImageDescriptor icon;
68
69     private IConfigurationElement element;
70
71     private ArrayList JavaDoc handleUIs;
72
73     private ArrayList JavaDoc perspectiveChoices;
74
75     /**
76      * Creates an instance of <code>Capability</code> using the
77      * information provided by the configuration element.
78      *
79      * @param configElement the <code>IConfigurationElement<code> containing
80      * the attributes
81      * @param reader the <code>CapabilityRegistryReader<code> used to log missing attributes
82      * @throws a <code>WorkbenchException</code> if the ID, nature, or wizard is <code>null</code>
83      */

84     public Capability(IConfigurationElement configElement,
85             CapabilityRegistryReader reader) throws WorkbenchException {
86         super();
87
88         boolean missingAttribute = false;
89         String JavaDoc attr_id = configElement.getAttribute(ATT_ID);
90         String JavaDoc attr_nature = configElement.getAttribute(ATT_NATURE_ID);
91
92         if (attr_id == null) {
93             reader.logMissingAttribute(configElement, ATT_ID);
94             missingAttribute = true;
95         }
96         if (attr_nature == null) {
97             reader.logMissingAttribute(configElement, ATT_NATURE_ID);
98             missingAttribute = true;
99         }
100         if (configElement.getAttribute(ATT_INSTALL_WIZARD) == null) {
101             reader.logMissingAttribute(configElement, ATT_INSTALL_WIZARD);
102             missingAttribute = true;
103         }
104
105         if (missingAttribute)
106             throw new WorkbenchException(
107                     "Capability missing required attributes."); //$NON-NLS-1$
108

109         id = attr_id;
110         natureId = attr_nature;
111         element = configElement;
112         natureDescriptor = ResourcesPlugin.getWorkspace().getNatureDescriptor(
113                 natureId);
114     }
115
116     /**
117      * Creates an instance of <code>Capability</code> as an unknown one
118      * for a given nature id.
119      *
120      * @param natureId the nature id for the unknown capbility
121      */

122     public Capability(String JavaDoc natureId) {
123         super();
124         this.id = natureId;
125         this.natureId = natureId;
126     }
127
128     /**
129      * Adds the id of a capability for which this capability handles
130      * the user interface.
131      */

132     public void addHandleUI(String JavaDoc capabilityId) {
133         if (handleUIs == null)
134             handleUIs = new ArrayList JavaDoc(4);
135         handleUIs.add(capabilityId);
136     }
137
138     /**
139      * Adds the id of a perspective for which this capability
140      * wants to present as a choice in the user interface.
141      */

142     public void addPerspectiveChoice(String JavaDoc perspId) {
143         if (perspectiveChoices == null)
144             perspectiveChoices = new ArrayList JavaDoc(4);
145         perspectiveChoices.add(perspId);
146     }
147
148     public String JavaDoc getId() {
149         return id;
150     }
151
152     /* (non-Javadoc)
153      * Method declared on IWorkbenchAdapter.
154      */

155     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
156         return getIconDescriptor();
157     }
158
159     /* (non-Javadoc)
160      * Method declared on IWorkbenchAdapter.
161      */

162     public String JavaDoc getLabel(Object JavaDoc o) {
163         return getName();
164     }
165
166     public String JavaDoc getName() {
167         if (isValid())
168             return natureDescriptor.getLabel();
169         else
170             return NLS.bind(IDEWorkbenchMessages.Capability_nameMissing, id);
171     }
172
173     public ImageDescriptor getIconDescriptor() {
174         if (icon == null && isValid()) {
175             String JavaDoc extendingPluginId = element.getNamespace();
176             String JavaDoc location = element.getAttribute(ATT_ICON);
177             if (location != null && location.length() > 0)
178                 icon = AbstractUIPlugin.imageDescriptorFromPlugin(
179                         extendingPluginId, location);
180         }
181         return icon;
182     }
183
184     /**
185      * Returns the nature descriptor or <code>null</code> if
186      * none exist.
187      */

188     public IProjectNatureDescriptor getNatureDescriptor() {
189         return natureDescriptor;
190     }
191
192     public String JavaDoc getNatureId() {
193         return natureId;
194     }
195
196     /* (non-Javadoc)
197      * Method declared on IAdaptable.
198      */

199     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
200         if (adapter == IWorkbenchAdapter.class)
201             return this;
202         else
203             return null;
204     }
205
206     public String JavaDoc getCategoryPath() {
207         if (element == null)
208             return ""; //$NON-NLS-1$;
209
else
210             return element.getAttribute(ATT_CATEGORY);
211     }
212
213     /**
214      * Returns a new instance of the capability install
215      * wizard. Caller is responsible for calling the init
216      * method. If the wizard cannot be created, <code>null</code>
217      * is returned.
218      *
219      * @return the non-initialized capability wizard or
220      * <code>null</code> if the wizard cannot be created.
221      */

222     public ICapabilityInstallWizard getInstallWizard() {
223         if (!isValid())
224             return null;
225
226         try {
227             return (ICapabilityInstallWizard) element
228                     .createExecutableExtension(ATT_INSTALL_WIZARD);
229         } catch (CoreException e) {
230             IDEWorkbenchPlugin
231                     .log(
232                             "Could not create capability install wizard.", e.getStatus()); //$NON-NLS-1$
233
return null;
234         }
235     }
236
237     /**
238      * Returns the description for the install wizard
239      * or <code>null</code> if none supplied.
240      */

241     public String JavaDoc getInstallDetails() {
242         if (!isValid())
243             return null;
244         return element.getAttribute(ATT_INSTALL_DETAILS);
245     }
246
247     /**
248      * Returns a new instance of the capability uninstall
249      * wizard. Caller is responsible for calling the init
250      * method. If the wizard cannot be created, <code>null</code>
251      * is returned.
252      *
253      * @return the non-initialized capability wizard or
254      * <code>null</code> if the wizard cannot be created.
255      */

256     public ICapabilityUninstallWizard getUninstallWizard() {
257         if (!isValid())
258             return null;
259
260         try {
261             return (ICapabilityUninstallWizard) element
262                     .createExecutableExtension(ATT_UNINSTALL_WIZARD);
263         } catch (CoreException e) {
264             IDEWorkbenchPlugin
265                     .log(
266                             "Could not create capability uninstall wizard.", e.getStatus()); //$NON-NLS-1$
267
return null;
268         }
269     }
270
271     /**
272      * Returns the description for the uninstall wizard
273      * or <code>null</code> if none supplied.
274      */

275     public String JavaDoc getUninstallDetails() {
276         if (!isValid())
277             return null;
278         return element.getAttribute(ATT_UNINSTALL_DETAILS);
279     }
280
281     public String JavaDoc getDescription() {
282         if (!isValid())
283             return ""; //$NON-NLS-1$;
284
String JavaDoc description = element.getAttribute(ATT_DESCRIPTION);
285         if (description == null)
286             description = ""; //$NON-NLS-1$
287
return description;
288     }
289
290     /**
291      * Returns a list of ids of other capabilities for which this
292      * capability handles the user interface, or <code>null</code>
293      * if not applicable.
294      */

295     public ArrayList JavaDoc getHandleUIs() {
296         return handleUIs;
297     }
298
299     /**
300      * Returns a list of ids of perspectives for which this
301      * capability wants to present as choices, or <code>null</code>
302      * if not applicable.
303      */

304     public ArrayList JavaDoc getPerspectiveChoices() {
305         return perspectiveChoices;
306     }
307
308     /**
309      * Returns whether this capability is valid
310      */

311     public boolean isValid() {
312         return natureDescriptor != null;
313     }
314 }
315
Popular Tags