1 11 12 package org.eclipse.ui.internal.expressions; 13 14 import org.eclipse.core.expressions.EvaluationResult; 15 import org.eclipse.core.expressions.Expression; 16 import org.eclipse.core.expressions.ExpressionInfo; 17 import org.eclipse.core.expressions.IEvaluationContext; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.ui.ISources; 21 import org.eclipse.ui.IWorkbenchWindow; 22 import org.eclipse.ui.internal.ActionExpression; 23 24 36 public final class LegacyActionExpressionWrapper extends 37 WorkbenchWindowExpression { 38 39 42 private static final int HASH_INITIAL = LegacyActionExpressionWrapper.class 43 .getName().hashCode(); 44 45 48 private final ActionExpression expression; 49 50 60 public LegacyActionExpressionWrapper(final ActionExpression expression, 61 final IWorkbenchWindow window) { 62 super(window); 63 64 if (expression == null) { 65 throw new NullPointerException ( 66 "The action expression cannot be null"); } 68 this.expression = expression; 69 } 70 71 public final void collectExpressionInfo(final ExpressionInfo info) { 72 super.collectExpressionInfo(info); 73 info.markDefaultVariableAccessed(); 74 } 75 76 protected final int computeHashCode() { 77 int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(getWindow()); 78 hashCode = hashCode * HASH_FACTOR + hashCode(expression); 79 return hashCode; 80 } 81 82 public final boolean equals(final Object object) { 83 if (object instanceof LegacyActionExpressionWrapper) { 84 final LegacyActionExpressionWrapper that = (LegacyActionExpressionWrapper) object; 85 return equals(this.expression, that.expression) 86 && equals(this.getWindow(), that.getWindow()); 87 } 88 89 return false; 90 } 91 92 public final EvaluationResult evaluate(final IEvaluationContext context) 93 throws CoreException { 94 final EvaluationResult result = super.evaluate(context); 95 if (result == EvaluationResult.FALSE) { 96 return result; 97 } 98 99 final Object defaultVariable = context 100 .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME); 101 if (defaultVariable instanceof IStructuredSelection) { 102 final IStructuredSelection selection = (IStructuredSelection) defaultVariable; 103 if (expression.isEnabledFor(selection)) { 104 return EvaluationResult.TRUE; 105 } 106 } else if (expression.isEnabledFor(defaultVariable)) { 107 return EvaluationResult.TRUE; 108 } 109 110 return EvaluationResult.FALSE; 111 } 112 113 public final String toString() { 114 final StringBuffer buffer = new StringBuffer (); 115 buffer.append("LegacyActionExpressionWrapper("); buffer.append(expression); 117 buffer.append(','); 118 buffer.append(getWindow()); 119 buffer.append(')'); 120 return buffer.toString(); 121 } 122 } 123 | Popular Tags |