Project

General

Profile

Download (7.73 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2010 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;
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 DEFAULT_MENU_GROUP_ID = "all-uncategorized"; //$NON-NLS-1$
45

    
46
	private String id;
47
	
48
	private String wizardId;
49
	
50
	private String menuGroupId; 
51

    
52
	private String 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 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
		IEvaluationContext parentContext = NavigatorPlugin.getApplicationContext();
113

    
114
		Iterator elements = aStructuredSelection.iterator();
115
		while (elements.hasNext()) {
116
			context = new EvaluationContext(parentContext, elements.next());
117
			context.setAllowPluginActivation(true);
118
			if (NavigatorPlugin.safeEvaluate(enablement, context) == EvaluationResult.FALSE) {
119
				return false;
120
			}
121
		}
122
		return true;
123
	}
124

    
125
	/**
126
	 * Determine if this content extension is enabled for the given element.
127
	 * 
128
	 * @param anElement
129
	 *            The element that should be used for the evaluation.
130
	 * @return True if and only if the extension is enabled for the element.
131
	 */
132
	public boolean isEnabledFor(Object anElement) {
133
		if (enablement == null) {
134
			return false;
135
		}
136

    
137
		IEvaluationContext context = NavigatorPlugin.getEvalContext(anElement);
138
		return (NavigatorPlugin.safeEvaluate(enablement, context) == EvaluationResult.TRUE);
139
	}
140

    
141
	void init() throws WorkbenchException { 
142
		wizardId = configElement.getAttribute(ATT_WIZARD_ID);
143
		type = configElement.getAttribute(ATT_TYPE);
144
		
145
		menuGroupId = configElement.getAttribute(ATT_MENU_GROUP_ID);
146
		if(menuGroupId == null) {
147
			menuGroupId = DEFAULT_MENU_GROUP_ID;
148
		}
149
		
150
		/* 
151
		 * The id defaults to the id of the enclosing navigatorContent extension, if any
152
		 * If not enclosed, this can be null initially, so it will default to the 
153
		 * value of the associatedExtensionId
154
		 *  
155
		 * Code elsewhere anticipates that this attribute may be null, so there is 
156
		 * no need to set it to a default non-null value. Indeed, this will cause
157
		 * incorrect behavior. 
158
		 * */
159
		if(id == null) {
160
			id = configElement.getAttribute(ATT_ASSOCIATED_EXTENSION_ID);
161
		}
162

    
163
		if (wizardId == null || wizardId.length() == 0) {
164
			throw new WorkbenchException("Missing attribute: " + //$NON-NLS-1$
165
					ATT_WIZARD_ID + " in common wizard extension: " + //$NON-NLS-1$
166
					configElement.getDeclaringExtension().getNamespaceIdentifier());
167
		}
168

    
169
		if (type == null || type.length() == 0) {
170
			throw new WorkbenchException("Missing attribute: " + //$NON-NLS-1$
171
					ATT_TYPE + " in common wizard extension: " + //$NON-NLS-1$
172
					configElement.getDeclaringExtension().getNamespaceIdentifier());
173
		}
174

    
175
		IConfigurationElement[] children = configElement
176
				.getChildren(TAG_ENABLEMENT);
177
		if (children.length == 1) {
178
			try {
179
				enablement = ElementHandler.getDefault().create(
180
						ExpressionConverter.getDefault(), children[0]);
181
			} catch (CoreException e) {
182
				NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
183
			}
184
		} else if (children.length > 1) {
185
			throw new WorkbenchException("More than one element: " + //$NON-NLS-1$
186
					TAG_ENABLEMENT + " in common wizard extension: " + //$NON-NLS-1$
187
					configElement.getDeclaringExtension().getUniqueIdentifier());
188
		} 
189
	}
190

    
191
	/**
192
	 * 
193
	 * @return Returns the common wizard wizardId
194
	 */
195
	public String getWizardId() {
196
		return wizardId;
197
	}
198

    
199
	/**
200
	 * @return Returns the type.
201
	 */
202
	public String getType() {
203
		return type;
204
	}
205
	
206
	/**
207
	 * @return the declaring namespace.
208
	 */
209
	public String getNamespace() {
210
		return configElement.getDeclaringExtension().getNamespaceIdentifier(); 
211
	}
212
	
213
	/**
214
	 * 
215
	 * @return The identifier of the wizard descriptor for visiblity purposes (or null) if none.
216
	 */
217
	public String getId() {
218
		return id;
219
	}
220
	
221

    
222
	/**
223
	 * 
224
	 * @return A developer-defined logical group that this wizard menu option and 
225
	 * 	others like it should be rendered in a localized manner.
226
	 */
227
	public String getMenuGroupId() {
228
		return menuGroupId;
229
	}
230

    
231
	
232
	public String toString() {
233
		return "CommonWizardDescriptor["+getId()+", wizardId="+getWizardId()+"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
234
	}
235

    
236
	/* (non-Javadoc)
237
	 * @see org.eclipse.ui.IPluginContribution#getLocalId()
238
	 */
239
	public String getLocalId() {
240
		return getWizardId();
241
	}
242
	
243
	/* (non-Javadoc)
244
	 * @see org.eclipse.ui.IPluginContribution#getPluginId()
245
	 */
246
	public String getPluginId() {
247
        return (configElement != null) ? configElement.getNamespaceIdentifier() : null;
248
	}
249
}
(1-1/3)