KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > wizards > CommonWizardDescriptor


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
12 package org.eclipse.ui.internal.navigator.wizards;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.expressions.ElementHandler;
17 import org.eclipse.core.expressions.EvaluationContext;
18 import org.eclipse.core.expressions.EvaluationResult;
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.core.expressions.ExpressionConverter;
21 import org.eclipse.core.expressions.IEvaluationContext;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IConfigurationElement;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.ui.IPluginContribution;
27 import org.eclipse.ui.WorkbenchException;
28 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
29 import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
30
31 /**
32  * <p>
33  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
34  * part of a work in progress. There is a guarantee neither that this API will
35  * work nor that it will remain the same. Please do not use this API without
36  * consulting with the Platform/UI team.
37  * </p>
38  *
39  * @since 3.2
40  */

41 public class CommonWizardDescriptor implements INavigatorContentExtPtConstants, IPluginContribution {
42     
43     /** The default menu group id for commonWizards without a menuGroupId attribute. */
44     public static final String JavaDoc DEFAULT_MENU_GROUP_ID = "all-uncategorized"; //$NON-NLS-1$
45

46     private String JavaDoc id;
47     
48     private String JavaDoc wizardId;
49     
50     private String JavaDoc menuGroupId;
51
52     private String JavaDoc type;
53
54     private Expression enablement;
55
56     private IConfigurationElement configElement;
57
58     /**
59      * @param aConfigElement The configuration element from the extension point.
60      * @throws WorkbenchException
61      * if the configuration element could not be parsed. Reasons
62      * include:
63      * <ul>
64      * <li>A required attribute is missing.</li>
65      * <li>More elements are define than is allowed.</li>
66      * </ul>
67      */

68     public CommonWizardDescriptor(IConfigurationElement aConfigElement)
69             throws WorkbenchException {
70         super();
71         configElement = aConfigElement;
72         init();
73     }
74
75     /**
76      * @param aConfigElement The configuration element from the extension point.
77      * @param anId the identifier for visibility purposes.
78      *
79      * @throws WorkbenchException
80      * if the configuration element could not be parsed. Reasons
81      * include:
82      * <ul>
83      * <li>A required attribute is missing.</li>
84      * <li>More elements are define than is allowed.</li>
85      * </ul>
86      */

87     public CommonWizardDescriptor(IConfigurationElement aConfigElement, String JavaDoc anId)
88             throws WorkbenchException {
89         super();
90         configElement = aConfigElement;
91         id = anId;
92         init();
93         
94     }
95
96     /**
97      * Determine if this content extension is enabled for the given selection.
98      * The content extension is enabled for the selection if and only if it is
99      * enabled for each element in the selection.
100      *
101      * @param aStructuredSelection
102      * The selection from the viewer
103      * @return True if and only if the extension is enabled for each element in
104      * the selection.
105      */

106     public boolean isEnabledFor(IStructuredSelection aStructuredSelection) {
107         if (enablement == null) {
108             return false;
109         }
110
111         IEvaluationContext context = null;
112
113         Iterator JavaDoc elements = aStructuredSelection.iterator();
114         while (elements.hasNext()) {
115             context = new EvaluationContext(null, elements.next());
116             context.setAllowPluginActivation(true);
117             try {
118                 if (enablement.evaluate(context) == EvaluationResult.FALSE) {
119                     return false;
120                 }
121             } catch (CoreException e) {
122                 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
123                 return false;
124             }
125         }
126         return true;
127     }
128
129     /**
130      * Determine if this content extension is enabled for the given element.
131      *
132      * @param anElement
133      * The element that should be used for the evaluation.
134      * @return True if and only if the extension is enabled for the element.
135      */

136     public boolean isEnabledFor(Object JavaDoc anElement) {
137
138         if (enablement == null) {
139             return false;
140         }
141
142         try {
143             EvaluationContext context = new EvaluationContext(null, anElement);
144             context.setAllowPluginActivation(true);
145             return (enablement.evaluate(context) == EvaluationResult.TRUE);
146         } catch (CoreException e) {
147             NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
148         }
149         return false;
150     }
151
152     void init() throws WorkbenchException {
153         wizardId = configElement.getAttribute(ATT_WIZARD_ID);
154         type = configElement.getAttribute(ATT_TYPE);
155         
156         menuGroupId = configElement.getAttribute(ATT_MENU_GROUP_ID);
157         if(menuGroupId == null) {
158             menuGroupId = DEFAULT_MENU_GROUP_ID;
159         }
160         
161         /*
162          * The id defaults to the id of the enclosing navigatorContent extension, if any
163          * If not enclosed, this can be null initially, so it will default to the
164          * value of the associatedExtensionId
165          *
166          * Code elsewhere anticipates that this attribute may be null, so there is
167          * no need to set it to a default non-null value. Indeed, this will cause
168          * incorrect behavior.
169          * */

170         if(id == null) {
171             id = configElement.getAttribute(ATT_ASSOCIATED_EXTENSION_ID);
172         }
173
174         if (wizardId == null || wizardId.length() == 0) {
175             throw new WorkbenchException("Missing attribute: " + //$NON-NLS-1$
176
ATT_WIZARD_ID + " in common wizard extension: " + //$NON-NLS-1$
177
configElement.getDeclaringExtension().getNamespaceIdentifier());
178         }
179
180         if (type == null || type.length() == 0) {
181             throw new WorkbenchException("Missing attribute: " + //$NON-NLS-1$
182
ATT_TYPE + " in common wizard extension: " + //$NON-NLS-1$
183
configElement.getDeclaringExtension().getNamespaceIdentifier());
184         }
185
186         IConfigurationElement[] children = configElement
187                 .getChildren(TAG_ENABLEMENT);
188         if (children.length == 1) {
189             try {
190                 enablement = ElementHandler.getDefault().create(
191                         ExpressionConverter.getDefault(), children[0]);
192             } catch (CoreException e) {
193                 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
194             }
195         } else if (children.length > 1) {
196             throw new WorkbenchException("More than one element: " + //$NON-NLS-1$
197
TAG_ENABLEMENT + " in common wizard extension: " + //$NON-NLS-1$
198
configElement.getDeclaringExtension().getUniqueIdentifier());
199         }
200     }
201
202     /**
203      *
204      * @return Returns the common wizard wizardId
205      */

206     public String JavaDoc getWizardId() {
207         return wizardId;
208     }
209
210     /**
211      * @return Returns the type.
212      */

213     public String JavaDoc getType() {
214         return type;
215     }
216     
217     /**
218      * @return the declaring namespace.
219      */

220     public String JavaDoc getNamespace() {
221         return configElement.getDeclaringExtension().getNamespaceIdentifier();
222     }
223     
224     /**
225      *
226      * @return The identifier of the wizard descriptor for visiblity purposes (or null) if none.
227      */

228     public String JavaDoc getId() {
229         return id;
230     }
231     
232
233     /**
234      *
235      * @return A developer-defined logical group that this wizard menu option and
236      * others like it should be rendered in a localized manner.
237      */

238     public String JavaDoc getMenuGroupId() {
239         return menuGroupId;
240     }
241
242     
243     public String JavaDoc toString() {
244         return "CommonWizardDescriptor["+getId()+", wizardId="+getWizardId()+"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
245
}
246
247     /* (non-Javadoc)
248      * @see org.eclipse.ui.IPluginContribution#getLocalId()
249      */

250     public String JavaDoc getLocalId() {
251         return getId();
252     }
253     
254     /* (non-Javadoc)
255      * @see org.eclipse.ui.IPluginContribution#getPluginId()
256      */

257     public String JavaDoc getPluginId() {
258         return (configElement != null) ? configElement.getNamespaceIdentifier() : null;
259     }
260 }
261
Popular Tags