1 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 41 public class CommonWizardDescriptor implements INavigatorContentExtPtConstants, IPluginContribution { 42 43 44 public static final String DEFAULT_MENU_GROUP_ID = "all-uncategorized"; 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 68 public CommonWizardDescriptor(IConfigurationElement aConfigElement) 69 throws WorkbenchException { 70 super(); 71 configElement = aConfigElement; 72 init(); 73 } 74 75 87 public CommonWizardDescriptor(IConfigurationElement aConfigElement, String anId) 88 throws WorkbenchException { 89 super(); 90 configElement = aConfigElement; 91 id = anId; 92 init(); 93 94 } 95 96 106 public boolean isEnabledFor(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.log(IStatus.ERROR, 0, e.getMessage(), e); 123 return false; 124 } 125 } 126 return true; 127 } 128 129 136 public boolean isEnabledFor(Object 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 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: " + ATT_WIZARD_ID + " in common wizard extension: " + configElement.getDeclaringExtension().getNamespaceIdentifier()); 178 } 179 180 if (type == null || type.length() == 0) { 181 throw new WorkbenchException("Missing attribute: " + ATT_TYPE + " in common wizard extension: " + 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: " + TAG_ENABLEMENT + " in common wizard extension: " + configElement.getDeclaringExtension().getUniqueIdentifier()); 199 } 200 } 201 202 206 public String getWizardId() { 207 return wizardId; 208 } 209 210 213 public String getType() { 214 return type; 215 } 216 217 220 public String getNamespace() { 221 return configElement.getDeclaringExtension().getNamespaceIdentifier(); 222 } 223 224 228 public String getId() { 229 return id; 230 } 231 232 233 238 public String getMenuGroupId() { 239 return menuGroupId; 240 } 241 242 243 public String toString() { 244 return "CommonWizardDescriptor["+getId()+", wizardId="+getWizardId()+"]"; } 246 247 250 public String getLocalId() { 251 return getId(); 252 } 253 254 257 public String getPluginId() { 258 return (configElement != null) ? configElement.getNamespaceIdentifier() : null; 259 } 260 } 261 | Popular Tags |