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.navigator.wizards;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.expressions.ElementHandler;
015: import org.eclipse.core.expressions.EvaluationContext;
016: import org.eclipse.core.expressions.EvaluationResult;
017: import org.eclipse.core.expressions.Expression;
018: import org.eclipse.core.expressions.ExpressionConverter;
019: import org.eclipse.core.expressions.IEvaluationContext;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IConfigurationElement;
022: import org.eclipse.core.runtime.IStatus;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.ui.IPluginContribution;
025: import org.eclipse.ui.WorkbenchException;
026: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
027: import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
028:
029: /**
030: * <p>
031: * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
032: * part of a work in progress. There is a guarantee neither that this API will
033: * work nor that it will remain the same. Please do not use this API without
034: * consulting with the Platform/UI team.
035: * </p>
036: *
037: * @since 3.2
038: */
039: public class CommonWizardDescriptor implements
040: INavigatorContentExtPtConstants, IPluginContribution {
041:
042: /** The default menu group id for commonWizards without a menuGroupId attribute. */
043: public static final String DEFAULT_MENU_GROUP_ID = "all-uncategorized"; //$NON-NLS-1$
044:
045: private String id;
046:
047: private String wizardId;
048:
049: private String menuGroupId;
050:
051: private String type;
052:
053: private Expression enablement;
054:
055: private IConfigurationElement configElement;
056:
057: /**
058: * @param aConfigElement The configuration element from the extension point.
059: * @throws WorkbenchException
060: * if the configuration element could not be parsed. Reasons
061: * include:
062: * <ul>
063: * <li>A required attribute is missing.</li>
064: * <li>More elements are define than is allowed.</li>
065: * </ul>
066: */
067: public CommonWizardDescriptor(IConfigurationElement aConfigElement)
068: throws WorkbenchException {
069: super ();
070: configElement = aConfigElement;
071: init();
072: }
073:
074: /**
075: * @param aConfigElement The configuration element from the extension point.
076: * @param anId the identifier for visibility purposes.
077: *
078: * @throws WorkbenchException
079: * if the configuration element could not be parsed. Reasons
080: * include:
081: * <ul>
082: * <li>A required attribute is missing.</li>
083: * <li>More elements are define than is allowed.</li>
084: * </ul>
085: */
086: public CommonWizardDescriptor(IConfigurationElement aConfigElement,
087: String anId) throws WorkbenchException {
088: super ();
089: configElement = aConfigElement;
090: id = anId;
091: init();
092:
093: }
094:
095: /**
096: * Determine if this content extension is enabled for the given selection.
097: * The content extension is enabled for the selection if and only if it is
098: * enabled for each element in the selection.
099: *
100: * @param aStructuredSelection
101: * The selection from the viewer
102: * @return True if and only if the extension is enabled for each element in
103: * the selection.
104: */
105: public boolean isEnabledFor(
106: IStructuredSelection aStructuredSelection) {
107: if (enablement == null) {
108: return false;
109: }
110:
111: IEvaluationContext context = null;
112:
113: Iterator 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
123: .log(IStatus.ERROR, 0, e.getMessage(), e);
124: return false;
125: }
126: }
127: return true;
128: }
129:
130: /**
131: * Determine if this content extension is enabled for the given element.
132: *
133: * @param anElement
134: * The element that should be used for the evaluation.
135: * @return True if and only if the extension is enabled for the element.
136: */
137: public boolean isEnabledFor(Object anElement) {
138:
139: if (enablement == null) {
140: return false;
141: }
142:
143: try {
144: EvaluationContext context = new EvaluationContext(null,
145: anElement);
146: context.setAllowPluginActivation(true);
147: return (enablement.evaluate(context) == EvaluationResult.TRUE);
148: } catch (CoreException e) {
149: NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
150: }
151: return false;
152: }
153:
154: void init() throws WorkbenchException {
155: wizardId = configElement.getAttribute(ATT_WIZARD_ID);
156: type = configElement.getAttribute(ATT_TYPE);
157:
158: menuGroupId = configElement.getAttribute(ATT_MENU_GROUP_ID);
159: if (menuGroupId == null) {
160: menuGroupId = DEFAULT_MENU_GROUP_ID;
161: }
162:
163: /*
164: * The id defaults to the id of the enclosing navigatorContent extension, if any
165: * If not enclosed, this can be null initially, so it will default to the
166: * value of the associatedExtensionId
167: *
168: * Code elsewhere anticipates that this attribute may be null, so there is
169: * no need to set it to a default non-null value. Indeed, this will cause
170: * incorrect behavior.
171: * */
172: if (id == null) {
173: id = configElement
174: .getAttribute(ATT_ASSOCIATED_EXTENSION_ID);
175: }
176:
177: if (wizardId == null || wizardId.length() == 0) {
178: throw new WorkbenchException("Missing attribute: " + //$NON-NLS-1$
179: ATT_WIZARD_ID
180: + " in common wizard extension: " + //$NON-NLS-1$
181: configElement.getDeclaringExtension()
182: .getNamespaceIdentifier());
183: }
184:
185: if (type == null || type.length() == 0) {
186: throw new WorkbenchException("Missing attribute: " + //$NON-NLS-1$
187: ATT_TYPE
188: + " in common wizard extension: " + //$NON-NLS-1$
189: configElement.getDeclaringExtension()
190: .getNamespaceIdentifier());
191: }
192:
193: IConfigurationElement[] children = configElement
194: .getChildren(TAG_ENABLEMENT);
195: if (children.length == 1) {
196: try {
197: enablement = ElementHandler.getDefault().create(
198: ExpressionConverter.getDefault(), children[0]);
199: } catch (CoreException e) {
200: NavigatorPlugin
201: .log(IStatus.ERROR, 0, e.getMessage(), e);
202: }
203: } else if (children.length > 1) {
204: throw new WorkbenchException("More than one element: " + //$NON-NLS-1$
205: TAG_ENABLEMENT
206: + " in common wizard extension: " + //$NON-NLS-1$
207: configElement.getDeclaringExtension()
208: .getUniqueIdentifier());
209: }
210: }
211:
212: /**
213: *
214: * @return Returns the common wizard wizardId
215: */
216: public String getWizardId() {
217: return wizardId;
218: }
219:
220: /**
221: * @return Returns the type.
222: */
223: public String getType() {
224: return type;
225: }
226:
227: /**
228: * @return the declaring namespace.
229: */
230: public String getNamespace() {
231: return configElement.getDeclaringExtension()
232: .getNamespaceIdentifier();
233: }
234:
235: /**
236: *
237: * @return The identifier of the wizard descriptor for visiblity purposes (or null) if none.
238: */
239: public String getId() {
240: return id;
241: }
242:
243: /**
244: *
245: * @return A developer-defined logical group that this wizard menu option and
246: * others like it should be rendered in a localized manner.
247: */
248: public String getMenuGroupId() {
249: return menuGroupId;
250: }
251:
252: public String toString() {
253: return "CommonWizardDescriptor[" + getId() + ", wizardId=" + getWizardId() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
254: }
255:
256: /* (non-Javadoc)
257: * @see org.eclipse.ui.IPluginContribution#getLocalId()
258: */
259: public String getLocalId() {
260: return getId();
261: }
262:
263: /* (non-Javadoc)
264: * @see org.eclipse.ui.IPluginContribution#getPluginId()
265: */
266: public String getPluginId() {
267: return (configElement != null) ? configElement
268: .getNamespaceIdentifier() : null;
269: }
270: }
|