1 11 package org.eclipse.ui.internal.navigator.extensions; 12 13 import java.util.Iterator ; 14 import java.util.Set ; 15 import java.util.TreeSet ; 16 17 import org.eclipse.core.expressions.ElementHandler; 18 import org.eclipse.core.expressions.EvaluationContext; 19 import org.eclipse.core.expressions.EvaluationResult; 20 import org.eclipse.core.expressions.Expression; 21 import org.eclipse.core.expressions.ExpressionConverter; 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.ILabelProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.ITreeContentProvider; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.ui.IPluginContribution; 30 import org.eclipse.ui.WorkbenchException; 31 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages; 32 import org.eclipse.ui.internal.navigator.CustomAndExpression; 33 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 34 import org.eclipse.ui.navigator.ICommonContentProvider; 35 import org.eclipse.ui.navigator.ICommonLabelProvider; 36 import org.eclipse.ui.navigator.INavigatorContentDescriptor; 37 import org.eclipse.ui.navigator.Priority; 38 39 51 public final class NavigatorContentDescriptor implements 52 INavigatorContentDescriptor, INavigatorContentExtPtConstants { 53 54 private static final int HASH_CODE_NOT_COMPUTED = -1; 55 private String id; 56 57 private String name; 58 59 private IConfigurationElement configElement; 60 61 private int priority = Priority.NORMAL_PRIORITY_VALUE; 62 63 private Expression enablement; 64 65 private Expression possibleChildren; 66 67 private String icon; 68 69 private boolean activeByDefault; 70 71 private IPluginContribution contribution; 72 73 private Set overridingExtensions; 74 75 private OverridePolicy overridePolicy; 76 77 private String suppressedExtensionId; 78 79 private INavigatorContentDescriptor overriddenDescriptor; 80 81 private int hashCode = HASH_CODE_NOT_COMPUTED; 82 83 private boolean providesSaveables; 84 85 99 NavigatorContentDescriptor(IConfigurationElement configElement) 100 throws WorkbenchException { 101 super(); 102 this.configElement = configElement; 103 init(); 104 } 105 106 111 public String getId() { 112 return id; 113 } 114 115 120 public String getName() { 121 return name; 122 } 123 124 129 public int getPriority() { 130 return priority; 131 } 132 133 144 private void init() throws WorkbenchException { 145 id = configElement.getAttribute(ATT_ID); 146 name = configElement.getAttribute(ATT_NAME); 147 String priorityString = configElement.getAttribute(ATT_PRIORITY); 148 icon = configElement.getAttribute(ATT_ICON); 149 150 String activeByDefaultString = configElement 151 .getAttribute(ATT_ACTIVE_BY_DEFAULT); 152 activeByDefault = (activeByDefaultString != null && activeByDefaultString 153 .length() > 0) ? Boolean.valueOf( 154 configElement.getAttribute(ATT_ACTIVE_BY_DEFAULT)) 155 .booleanValue() : true; 156 157 String providesSaveablesString = configElement 158 .getAttribute(ATT_PROVIDES_SAVEABLES); 159 providesSaveables = (providesSaveablesString != null && providesSaveablesString 160 .length() > 0) ? Boolean.valueOf(providesSaveablesString) 161 .booleanValue() : false; 162 163 if (priorityString != null) { 164 try { 165 Priority p = Priority.get(priorityString); 166 priority = p != null ? p.getValue() 167 : Priority.NORMAL_PRIORITY_VALUE; 168 } catch (NumberFormatException exception) { 169 priority = Priority.NORMAL_PRIORITY_VALUE; 170 } 171 } 172 if (id == null) { 173 throw new WorkbenchException(NLS.bind( 174 CommonNavigatorMessages.Attribute_Missing_Warning, 175 new Object [] { 176 ATT_ID, 177 id, 178 configElement.getDeclaringExtension() 179 .getNamespaceIdentifier() })); 180 } 181 182 IConfigurationElement[] children = configElement 183 .getChildren(TAG_ENABLEMENT); 184 if (children.length == 0) { 185 186 children = configElement.getChildren(TAG_TRIGGER_POINTS); 187 if (children.length == 1) { 188 enablement = new CustomAndExpression(children[0]); 189 } else { 190 throw new WorkbenchException(NLS.bind( 191 CommonNavigatorMessages.Attribute_Missing_Warning, 192 new Object [] { 193 TAG_TRIGGER_POINTS, 194 id, 195 configElement.getDeclaringExtension() 196 .getNamespaceIdentifier() })); 197 } 198 199 children = configElement.getChildren(TAG_POSSIBLE_CHILDREN); 200 if (children.length == 1) { 201 possibleChildren = new CustomAndExpression(children[0]); 202 } else if(children.length > 1){ 203 throw new WorkbenchException(NLS.bind( 204 CommonNavigatorMessages.Attribute_Missing_Warning, 205 new Object [] { 206 TAG_POSSIBLE_CHILDREN, 207 id, 208 configElement.getDeclaringExtension() 209 .getNamespaceIdentifier() })); 210 } 211 } else if (children.length == 1) { 212 try { 213 enablement = ElementHandler.getDefault().create( 214 ExpressionConverter.getDefault(), children[0]); 215 } catch (CoreException e) { 216 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 217 } 218 } else if (children.length > 1) { 219 throw new WorkbenchException(NLS.bind( 220 CommonNavigatorMessages.Attribute_Missing_Warning, 221 new Object [] { 222 TAG_ENABLEMENT, 223 id, 224 configElement.getDeclaringExtension() 225 .getNamespaceIdentifier() })); 226 } 227 228 contribution = new IPluginContribution() { 229 230 public String getLocalId() { 231 return getId(); 232 } 233 234 public String getPluginId() { 235 return configElement.getDeclaringExtension().getNamespaceIdentifier(); 236 } 237 238 }; 239 240 children = configElement.getChildren(TAG_OVERRIDE); 241 if (children.length == 1) { 242 suppressedExtensionId = children[0] 243 .getAttribute(ATT_SUPPRESSED_EXT_ID); 244 overridePolicy = OverridePolicy.get(children[0] 245 .getAttribute(ATT_POLICY)); 246 } 247 } 248 249 252 public String getIcon() { 253 return icon; 254 } 255 256 259 public String getSuppressedExtensionId() { 260 return suppressedExtensionId; 261 } 262 263 267 public OverridePolicy getOverridePolicy() { 268 return overridePolicy; 269 } 270 271 274 public IPluginContribution getContribution() { 275 return contribution; 276 } 277 278 290 public ITreeContentProvider createContentProvider() throws CoreException { 291 return (ITreeContentProvider) configElement 292 .createExecutableExtension(ATT_CONTENT_PROVIDER); 293 } 294 295 305 public ILabelProvider createLabelProvider() throws CoreException { 306 return (ILabelProvider) configElement 307 .createExecutableExtension(ATT_LABEL_PROVIDER); 308 } 309 310 315 public boolean isActiveByDefault() { 316 return activeByDefault; 317 } 318 319 327 public boolean isTriggerPoint(Object anElement) { 328 329 if (enablement == null || anElement == null) { 330 return false; 331 } 332 333 try { 334 EvaluationContext context = new EvaluationContext(null, anElement); 335 context.setAllowPluginActivation(true); 336 return (enablement.evaluate(context) == EvaluationResult.TRUE); 337 } catch (CoreException e) { 338 NavigatorPlugin.logError(0, e.getMessage(), e); 339 } 340 return false; 341 } 342 343 357 public boolean isPossibleChild(Object anElement) { 358 359 if ((enablement == null && possibleChildren == null) 360 || anElement == null) { 361 return false; 362 } else if(anElement instanceof IStructuredSelection) { 363 return arePossibleChildren((IStructuredSelection) anElement); 364 } 365 366 try { 367 EvaluationContext context = new EvaluationContext(null, anElement); 368 context.setAllowPluginActivation(true); 369 if (possibleChildren != null) { 370 return (possibleChildren.evaluate(context) == EvaluationResult.TRUE); 371 } else if (enablement != null) { 372 return (enablement.evaluate(context) == EvaluationResult.TRUE); 373 } 374 } catch (CoreException e) { 375 NavigatorPlugin.logError(0, e.getMessage(), e); 376 } 377 return false; 378 } 379 380 386 public boolean arePossibleChildren(IStructuredSelection aSelection) { 387 if(aSelection.isEmpty()) { 388 return false; 389 } 390 for (Iterator iter = aSelection.iterator(); iter.hasNext();) { 391 Object element = iter.next(); 392 if(!isPossibleChild(element)) { 393 return false; 394 } 395 } 396 return true; 397 } 398 399 405 public boolean hasOverridingExtensions() { 406 return overridingExtensions != null && overridingExtensions.size() > 0; 407 } 408 409 413 public Set getOverriddingExtensions() { 414 if (overridingExtensions == null) { 415 overridingExtensions = new TreeSet (ExtensionPriorityComparator.DESCENDING); 416 } 417 return overridingExtensions; 418 } 419 420 425 public String toString() { 426 return "Content[" + id + ", \"" + name + "\"]"; } 428 429 432 public int hashCode() { 433 if (hashCode == HASH_CODE_NOT_COMPUTED) { 434 String hashCodeString = configElement.getNamespaceIdentifier() + getId(); 435 hashCode = hashCodeString.hashCode(); 436 if (hashCode == HASH_CODE_NOT_COMPUTED) 437 hashCode++; 438 } 439 return hashCode; 440 } 441 442 446 public INavigatorContentDescriptor getOverriddenDescriptor() { 447 return overriddenDescriptor; 448 } 449 450 454 void setOverriddenDescriptor( 455 INavigatorContentDescriptor theOverriddenDescriptor) { 456 overriddenDescriptor = theOverriddenDescriptor; 457 } 458 459 462 public boolean hasSaveablesProvider() { 463 return providesSaveables; 464 } 465 466 } 467 | Popular Tags |