1 11 package org.eclipse.ui.internal.navigator.actions; 12 13 import java.util.Collections ; 14 import java.util.Comparator ; 15 import java.util.Iterator ; 16 import java.util.LinkedHashSet ; 17 import java.util.Set ; 18 import java.util.TreeSet ; 19 20 import org.eclipse.core.expressions.ElementHandler; 21 import org.eclipse.core.expressions.EvaluationContext; 22 import org.eclipse.core.expressions.EvaluationResult; 23 import org.eclipse.core.expressions.Expression; 24 import org.eclipse.core.expressions.ExpressionConverter; 25 import org.eclipse.core.expressions.IEvaluationContext; 26 import org.eclipse.core.runtime.Assert; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IConfigurationElement; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.jface.viewers.IStructuredSelection; 32 import org.eclipse.ui.internal.navigator.CustomAndExpression; 33 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 34 import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants; 35 import org.eclipse.ui.internal.navigator.extensions.SkeletonActionProvider; 36 import org.eclipse.ui.navigator.CommonActionProvider; 37 import org.eclipse.ui.navigator.Priority; 38 39 44 public class CommonActionProviderDescriptor implements 45 INavigatorContentExtPtConstants { 46 47 private static final String DEFAULT_ID = "org.eclipse.ui.navigator.actionProvider"; 49 private static int count = 0; 50 51 private final IConfigurationElement configurationElement; 52 53 private final boolean isNested; 54 55 private Set dependentDescriptors; 56 57 private Set overridingDescriptors; 58 59 private IConfigurationElement enablementElement; 60 61 private Expression enablement; 62 63 private boolean hasLoadingFailed; 64 65 private String definedId; 66 67 private String visibilityId; 68 69 private String dependsOnId; 70 71 private String overridesId; 72 73 private String toString; 74 75 private Priority priority; 76 77 83 public CommonActionProviderDescriptor(IConfigurationElement aConfigElement) { 84 super(); 85 Assert.isTrue(TAG_ACTION_PROVIDER.equals(aConfigElement.getName())); 86 configurationElement = aConfigElement; 87 isNested = false; 88 init(); 89 } 90 91 109 public CommonActionProviderDescriptor(IConfigurationElement aConfigElement, 110 IConfigurationElement anEnablementExpression, Priority defaultPriority, String anOverrideId, 111 boolean nestedUnderNavigatorContent) { 112 super(); 113 Assert.isTrue(TAG_ACTION_PROVIDER.equals(aConfigElement.getName())); 114 Assert.isTrue(TAG_POSSIBLE_CHILDREN.equals(anEnablementExpression 115 .getName()) 116 || TAG_ENABLEMENT.equals(anEnablementExpression.getName())); 117 configurationElement = aConfigElement; 118 enablementElement = anEnablementExpression; 119 visibilityId = anOverrideId; 120 isNested = nestedUnderNavigatorContent; 121 priority = defaultPriority; 122 init(); 123 } 124 125 private void init() { 126 127 try { 128 129 definedId = configurationElement.getAttribute(ATT_ID); 130 131 if (definedId == null) { 133 definedId = DEFAULT_ID + "." + count++; } 135 136 if (visibilityId == null) { 138 visibilityId = definedId; 139 } 140 141 dependsOnId = configurationElement.getAttribute(ATT_DEPENDS_ON); 142 143 overridesId = configurationElement.getAttribute(ATT_OVERRIDES); 144 145 if(priority == null) { 146 String prio = configurationElement.getAttribute(ATT_PRIORITY); 147 if(prio != null) 148 priority = Priority.get(prio); 149 else 150 priority = Priority.NORMAL; 151 } 152 153 IConfigurationElement[] children = configurationElement 154 .getChildren(TAG_ENABLEMENT); 155 if (children.length == 0 && enablementElement != null) { 158 enablement = new CustomAndExpression(enablementElement); 159 } else if (children.length == 1) { 161 enablement = ElementHandler.getDefault().create( 162 ExpressionConverter.getDefault(), children[0]); 163 164 } else { 165 System.err.println("Incorrect number of expressions: " + TAG_ENABLEMENT 167 + " in navigator extension: " + configurationElement.getDeclaringExtension() 169 .getUniqueIdentifier() + " in plugin " + configurationElement.getDeclaringExtension().getNamespaceIdentifier()); 171 } 172 } catch (CoreException e) { 173 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 174 } 175 } 176 177 184 public CommonActionProvider createActionProvider() { 185 if (hasLoadingFailed) { 186 return SkeletonActionProvider.INSTANCE; 187 } 188 CommonActionProvider provider = null; 189 try { 190 provider = (CommonActionProvider) configurationElement 191 .createExecutableExtension(ATT_CLASS); 192 } catch (CoreException exception) { 193 NavigatorPlugin.log(exception.getStatus()); 194 hasLoadingFailed = true; 195 provider = SkeletonActionProvider.INSTANCE; 196 } catch (Exception e) { 197 NavigatorPlugin.log(new Status(IStatus.ERROR, 198 NavigatorPlugin.PLUGIN_ID, 0, e.getMessage(), e)); 199 hasLoadingFailed = true; 200 provider = SkeletonActionProvider.INSTANCE; 201 } 202 203 return provider; 204 } 205 206 216 public boolean isEnabledFor(IStructuredSelection aStructuredSelection) { 217 if (enablement == null) { 218 return false; 219 } 220 221 if(aStructuredSelection.isEmpty()) { 222 IEvaluationContext context = null; 223 context = new EvaluationContext(null, Collections.EMPTY_LIST); 224 context.setAllowPluginActivation(true); 225 try { 226 if (enablement.evaluate(context) != EvaluationResult.TRUE) { 227 return false; 228 } 229 } catch (CoreException e) { 230 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 231 return false; 232 } 233 } else { 234 235 IEvaluationContext context = null; 236 Iterator elements = aStructuredSelection.iterator(); 237 while (elements.hasNext()) { 238 context = new EvaluationContext(null, elements.next()); 239 context.setAllowPluginActivation(true); 240 try { 241 if (enablement.evaluate(context) != EvaluationResult.TRUE) { 242 return false; 243 } 244 } catch (CoreException e) { 245 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 246 return false; 247 } 248 } 249 } 250 return true; 251 } 252 253 260 public boolean isEnabledFor(Object anElement) { 261 if (enablement == null || anElement == null) { 262 return false; 263 } 264 265 try { 266 EvaluationContext context = new EvaluationContext(null, anElement); 267 context.setAllowPluginActivation(true); 268 return (enablement.evaluate(context) == EvaluationResult.TRUE); 269 } catch (CoreException e) { 270 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 271 } 272 return false; 273 } 274 275 282 public String getId() { 283 return visibilityId; 284 } 285 286 292 public String getDefinedId() { 293 return definedId; 294 } 295 296 300 public boolean isNested() { 301 return isNested; 302 } 303 304 309 public String getDependsOnId() { 310 return dependsOnId; 311 } 312 313 318 public String getOverridesId() { 319 return overridesId; 320 } 321 322 327 public Priority getPriority() { 328 return priority; 329 } 330 331 332 public int hashCode() { 333 final int PRIME = 31; 334 int result = 1; 335 result = PRIME * result + ((definedId == null) ? 0 : definedId.hashCode()); 336 result = PRIME * result + ((visibilityId == null) ? 0 : visibilityId.hashCode()); 337 return result; 338 } 339 340 public boolean equals(Object obj) { 341 if (this == obj) 342 return true; 343 if (!super.equals(obj)) 344 return false; 345 if (getClass() != obj.getClass()) 346 return false; 347 final CommonActionProviderDescriptor other = (CommonActionProviderDescriptor) obj; 348 if (definedId == null) { 349 if (other.definedId != null) 350 return false; 351 } else if (!definedId.equals(other.definedId)) 352 return false; 353 if (visibilityId == null) { 354 if (other.visibilityId != null) 355 return false; 356 } else if (!visibilityId.equals(other.visibilityId)) 357 return false; 358 return true; 359 } 360 361 362 363 protected void addDependentDescriptor( 364 CommonActionProviderDescriptor dependentDescriptor) { 365 Assert.isTrue(this != dependentDescriptor); 366 if (dependentDescriptors == null) { 367 dependentDescriptors = new LinkedHashSet (); 368 } 369 dependentDescriptors.add(dependentDescriptor); 370 } 371 372 protected void addOverridingDescriptor( 373 CommonActionProviderDescriptor overridingDescriptor) { 374 Assert.isTrue(this != overridingDescriptor); 375 if (overridingDescriptors == null) { 376 overridingDescriptors = new TreeSet (CommonActionProviderDescriptorCompator.INSTANCE); 377 } 378 overridingDescriptors.add(overridingDescriptor); 379 } 380 381 protected boolean hasDependentDescriptors() { 382 return dependentDescriptors != null && !dependentDescriptors.isEmpty(); 383 } 384 385 protected boolean hasOverridingDescriptors() { 386 return overridingDescriptors != null 387 && !overridingDescriptors.isEmpty(); 388 } 389 390 protected Iterator dependentDescriptors() { 391 return dependentDescriptors.iterator(); 392 } 393 394 protected Iterator overridingDescriptors() { 395 return overridingDescriptors.iterator(); 396 } 397 398 public String toString() { 399 if (toString == null) { 400 toString = "CommonActionProviderDescriptor[definedId=" + getDefinedId() + ", visibilityId=" + getId() + ", dependsOn=" + getDependsOnId() + ", overrides=" + getOverridesId() + "]"; } 402 return toString; 403 } 404 405 406 411 public static class CommonActionProviderDescriptorCompator implements Comparator { 412 413 416 public static final CommonActionProviderDescriptorCompator INSTANCE = new CommonActionProviderDescriptorCompator(); 417 418 private static final int LESS_THAN = -1; 419 private static final int EQUALS = 0; 420 421 424 public int compare(Object o1, Object o2) { 425 CommonActionProviderDescriptor lvalue= null, rvalue= null; 426 427 if(o1 instanceof CommonActionProviderDescriptor) 428 lvalue = (CommonActionProviderDescriptor) o1; 429 430 if(o2 instanceof CommonActionProviderDescriptor) 431 rvalue = (CommonActionProviderDescriptor) o2; 432 433 if(lvalue == null || rvalue == null) 434 return LESS_THAN; 435 if(lvalue.equals(rvalue)) 436 return EQUALS; 437 int comparison = lvalue.getPriority().getValue() - rvalue.getPriority().getValue(); 438 if(comparison == 0) 439 return lvalue.getDefinedId().compareTo(rvalue.getDefinedId()); 440 return comparison; 441 442 } 443 444 445 } 446 447 } 448 449 | Popular Tags |