1 11 12 package org.eclipse.ui.internal.navigator.extensions; 13 14 import org.eclipse.core.expressions.EvaluationContext; 15 import org.eclipse.core.expressions.EvaluationResult; 16 import org.eclipse.core.expressions.Expression; 17 import org.eclipse.core.expressions.IEvaluationContext; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.ui.IEditorInput; 23 import org.eclipse.ui.internal.navigator.CustomAndExpression; 24 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 25 import org.eclipse.ui.navigator.ILinkHelper; 26 27 34 public class LinkHelperDescriptor implements ILinkHelperExtPtConstants { 35 36 private final IConfigurationElement configElement; 37 38 private String id; 39 40 private Expression editorInputEnablement; 41 42 43 private Expression selectionEnablement; 44 45 private boolean hasLinkHelperFailedCreation; 46 47 LinkHelperDescriptor(IConfigurationElement aConfigElement) { 48 Assert.isNotNull(aConfigElement, 49 "LinkHelperRegistry.Descriptor objects cannot be null."); Assert 51 .isLegal(LINK_HELPER.equals(aConfigElement.getName()), 52 "LinkHelperRegistry.Descriptor objects must have the name \"linkHelper\"."); configElement = aConfigElement; 54 init(); 55 } 56 57 void init() { 58 id = configElement.getAttribute(ATT_ID); 59 IConfigurationElement[] expressions = this.configElement 60 .getChildren(EDITOR_INPUT_ENABLEMENT); 61 Assert 62 .isLegal( 63 expressions.length == 1, 64 "The linkHelper extension point requires exactly one editorInputEnablement child."); 66 editorInputEnablement = new CustomAndExpression(expressions[0]); 67 68 expressions = configElement.getChildren(SELECTION_ENABLEMENT); 69 if (expressions.length > 0) { 70 71 if (expressions[0].getChildren() != null 74 && expressions[0].getChildren().length > 0) { 75 76 selectionEnablement = new CustomAndExpression(expressions[0]); 77 78 } 79 } 80 } 81 82 85 public String getId() { 86 return id; 87 } 88 89 95 public ILinkHelper createLinkHelper() { 96 if (hasLinkHelperFailedCreation) 97 return SkeletonLinkHelper.INSTANCE; 98 try { 99 return (ILinkHelper) configElement 100 .createExecutableExtension(ATT_CLASS); 101 } catch (Throwable t) { 102 hasLinkHelperFailedCreation = true; 103 NavigatorPlugin.logError(0, t.getMessage(), t); 104 } 105 return SkeletonLinkHelper.INSTANCE; 106 } 107 108 115 public boolean isEnabledFor(IEditorInput anInput) { 116 117 if (editorInputEnablement == null || anInput == null) { 118 return false; 119 } 120 121 try { 122 EvaluationContext context = new EvaluationContext(null, anInput); 123 context.setAllowPluginActivation(true); 124 return (editorInputEnablement.evaluate(context) == EvaluationResult.TRUE); 125 } catch (CoreException e) { 126 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 127 } 128 return false; 129 } 130 131 137 public boolean isEnabledFor(Object anObject) { 138 if (selectionEnablement == null) { 139 return false; 140 } 141 142 IEvaluationContext context = new EvaluationContext(null, anObject); 143 context.setAllowPluginActivation(true); 144 try { 145 if (selectionEnablement.evaluate(context) != EvaluationResult.TRUE) { 146 return false; 147 } 148 } catch (CoreException e) { 149 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e); 150 return false; 151 } 152 return true; 153 } 154 } 155 | Popular Tags |