1 11 package org.eclipse.ui.internal; 12 13 import java.util.List ; 14 15 import org.eclipse.core.expressions.EvaluationContext; 16 import org.eclipse.core.expressions.EvaluationResult; 17 import org.eclipse.core.expressions.Expression; 18 import org.eclipse.core.expressions.ExpressionConverter; 19 import org.eclipse.core.expressions.IEvaluationContext; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IAdaptable; 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.ISafeRunnable; 24 import org.eclipse.core.runtime.SafeRunner; 25 import org.eclipse.jface.action.IMenuManager; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.ISelectionProvider; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 import org.eclipse.ui.IWorkbenchPart; 30 import org.eclipse.ui.SelectionEnabler; 31 import org.eclipse.ui.internal.misc.Policy; 32 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 33 import org.eclipse.ui.internal.util.Util; 34 import org.eclipse.ui.model.IWorkbenchAdapter; 35 36 40 public class ObjectActionContributor extends PluginActionBuilder implements 41 IObjectActionContributor, IAdaptable { 42 43 private static final String P_TRUE = "true"; 45 private IConfigurationElement config; 46 47 private boolean configRead = false; 48 49 private boolean adaptable = false; 50 51 private String objectClass; 52 53 58 public ObjectActionContributor(IConfigurationElement config) { 59 this.config = config; 60 this.adaptable = P_TRUE.equalsIgnoreCase(config 61 .getAttribute(IWorkbenchRegistryConstants.ATT_ADAPTABLE)); 62 this.objectClass = config.getAttribute(IWorkbenchRegistryConstants.ATT_OBJECTCLASS); 63 } 64 65 68 public boolean canAdapt() { 69 return adaptable; 70 } 71 72 77 public String getObjectClass() { 78 return objectClass; 79 } 80 81 84 public void contributeObjectActionIdOverrides(List actionIdOverrides) { 85 if (!configRead) { 86 readConfigElement(); 87 } 88 89 if (currentContribution.actions != null) { 91 for (int i = 0; i < currentContribution.actions.size(); i++) { 92 ActionDescriptor ad = (ActionDescriptor) currentContribution.actions 93 .get(i); 94 String id = ad.getAction().getOverrideActionId(); 95 if (id != null) { 96 actionIdOverrides.add(id); 97 } 98 } 99 } 100 } 101 102 105 public boolean contributeObjectActions(final IWorkbenchPart part, 106 IMenuManager menu, ISelectionProvider selProv, 107 List actionIdOverrides) { 108 if (!configRead) { 109 readConfigElement(); 110 } 111 112 if (currentContribution.actions == null) { 114 return false; 115 } 116 117 ISelection sel = selProv.getSelection(); 119 if ((sel == null) || !(sel instanceof IStructuredSelection)) { 120 return false; 121 } 122 IStructuredSelection ssel = (IStructuredSelection) sel; 123 124 if(canAdapt()) { 125 IStructuredSelection newSelection = LegacyResourceSupport.adaptSelection(ssel, getObjectClass()); 126 if(newSelection.size() != ssel.size()) { 127 if (Policy.DEBUG_CONTRIBUTIONS) { 128 WorkbenchPlugin.log("Error adapting selection to " + getObjectClass() + ". Contribution " + getID(config) + " is being ignored"); } 131 return false; 132 } 133 ssel = newSelection; 134 } 135 136 final IStructuredSelection selection = ssel; 137 138 for (int i = 0; i < currentContribution.actions.size(); i++) { 140 ActionDescriptor ad = (ActionDescriptor) currentContribution.actions 141 .get(i); 142 if (!actionIdOverrides.contains(ad.getId())) { 143 currentContribution.contributeMenuAction(ad, menu, true); 144 if (ad.getAction() instanceof ObjectPluginAction) { 146 final ObjectPluginAction action = (ObjectPluginAction) ad 147 .getAction(); 148 ISafeRunnable runnable = new ISafeRunnable() { 149 public void handleException(Throwable exception) { 150 WorkbenchPlugin.log("Failed to update action " + action.getId(), exception); 152 } 153 154 public void run() throws Exception { 155 action.setActivePart(part); 156 action.selectionChanged(selection); 157 } 158 }; 159 SafeRunner.run(runnable); 160 } 161 } 162 } 163 return true; 164 } 165 166 169 public boolean contributeObjectMenus(IMenuManager menu, 170 ISelectionProvider selProv) { 171 if (!configRead) { 172 readConfigElement(); 173 } 174 175 if (currentContribution.menus == null) { 177 return false; 178 } 179 180 ISelection sel = selProv.getSelection(); 182 if ((sel == null) || !(sel instanceof IStructuredSelection)) { 183 return false; 184 } 185 186 for (int i = 0; i < currentContribution.menus.size(); i++) { 188 IConfigurationElement menuElement = (IConfigurationElement) currentContribution.menus 189 .get(i); 190 currentContribution.contributeMenu(menuElement, menu, true); 191 } 192 return true; 193 } 194 195 198 protected ActionDescriptor createActionDescriptor( 199 IConfigurationElement element) { 200 return new ActionDescriptor(element, ActionDescriptor.T_POPUP); 201 } 202 203 206 protected BasicContribution createContribution() { 207 return new ObjectContribution(); 208 } 209 210 214 public boolean isApplicableTo(Object object) { 215 if (!configRead) { 216 readConfigElement(); 217 } 218 219 if (canAdapt()) { 222 Object adapted = LegacyResourceSupport.getAdapter(object, getObjectClass()); 223 if (adapted == null) { 224 if (Policy.DEBUG_CONTRIBUTIONS) { 225 WorkbenchPlugin 226 .log("Error adapting " + object.getClass().getName() + " to " + getObjectClass() 229 + ". Contribution " + getID(config) + " is being ignored"); } 231 } else { 232 object = adapted; 233 } 234 } 235 236 if (!testName(object)) { 237 return false; 238 } 239 240 return ((ObjectContribution) currentContribution) 241 .isApplicableTo(object); 242 } 243 244 248 private void readConfigElement() { 249 currentContribution = createContribution(); 250 readElementChildren(config); 251 configRead = true; 252 } 253 254 257 protected boolean readElement(IConfigurationElement element) { 258 String tag = element.getName(); 259 260 if (tag.equals(IWorkbenchRegistryConstants.TAG_VISIBILITY)) { 262 ((ObjectContribution) currentContribution) 263 .setVisibilityTest(element); 264 return true; 265 } 266 267 if (tag.equals(IWorkbenchRegistryConstants.TAG_FILTER)) { 269 ((ObjectContribution) currentContribution).addFilterTest(element); 270 return true; 271 } 272 273 if (tag.equals(IWorkbenchRegistryConstants.TAG_ENABLEMENT)) { 274 ((ObjectContribution) currentContribution) 275 .setEnablementTest(element); 276 return true; 277 } 278 279 return super.readElement(element); 280 } 281 282 285 private boolean testName(Object object) { 286 String nameFilter = config.getAttribute(IWorkbenchRegistryConstants.ATT_NAME_FILTER); 287 if (nameFilter == null) { 288 return true; 289 } 290 String objectName = null; 291 IWorkbenchAdapter de = (IWorkbenchAdapter)Util.getAdapter(object, IWorkbenchAdapter.class); 292 if (de != null) { 293 objectName = de.getLabel(object); 294 } 295 if (objectName == null) { 296 objectName = object.toString(); 297 } 298 return SelectionEnabler.verifyNameMatch(objectName, nameFilter); 299 } 300 301 305 private static class ObjectContribution extends BasicContribution { 306 private ObjectFilterTest filterTest; 307 308 private ActionExpression visibilityTest; 309 310 private Expression enablement; 311 312 317 public void addFilterTest(IConfigurationElement element) { 318 if (filterTest == null) { 319 filterTest = new ObjectFilterTest(); 320 } 321 filterTest.addFilterElement(element); 322 } 323 324 329 public void setVisibilityTest(IConfigurationElement element) { 330 visibilityTest = new ActionExpression(element); 331 } 332 333 338 public void setEnablementTest(IConfigurationElement element) { 339 try { 340 enablement = ExpressionConverter.getDefault().perform(element); 341 } catch (CoreException e) { 342 WorkbenchPlugin.log(e); 343 } 344 } 345 346 353 public boolean isApplicableTo(Object object) { 354 boolean result = true; 355 if (visibilityTest != null) { 356 result = result && visibilityTest.isEnabledFor(object); 357 if (!result) { 358 return result; 359 } 360 } else if (filterTest != null) { 361 result = result && filterTest.matches(object, true); 362 if (!result) { 363 return result; 364 } 365 } 366 if (enablement != null) { 367 try { 368 IEvaluationContext context = new EvaluationContext(null, 369 object); 370 context.setAllowPluginActivation(true); 371 context.addVariable("selection", object); EvaluationResult evalResult = enablement.evaluate(context); 373 if (evalResult == EvaluationResult.FALSE) { 374 return false; 375 } 376 } catch (CoreException e) { 377 enablement = null; 378 WorkbenchPlugin.log(e); 379 result = false; 380 } 381 } 382 return result; 383 } 384 } 385 386 390 public String toString() { 391 StringBuffer buffer = new StringBuffer (); 392 IConfigurationElement[] children = config.getChildren(); 393 for (int i = 0; i < children.length; i++) { 394 IConfigurationElement element = children[i]; 395 String label = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 396 if(label != null) { 397 buffer.append(label); 398 buffer.append('\n'); 399 } 400 } 401 return buffer.toString(); 402 } 403 404 407 public Object getAdapter(Class adapter) { 408 if (adapter.equals(IConfigurationElement.class)) { 409 return config; 410 } 411 return null; 412 } 413 } 414 | Popular Tags |