1 11 12 package org.eclipse.ui.internal.expressions; 13 14 import java.util.Collection ; 15 16 import org.eclipse.core.expressions.EvaluationResult; 17 import org.eclipse.core.expressions.Expression; 18 import org.eclipse.core.expressions.ExpressionInfo; 19 import org.eclipse.core.expressions.IEvaluationContext; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.ui.ISources; 22 import org.eclipse.ui.IWorkbenchWindow; 23 24 39 public final class LegacyViewerContributionExpression extends 40 WorkbenchWindowExpression { 41 42 45 private static final int HASH_INITIAL = LegacyViewerContributionExpression.class 46 .getName().hashCode(); 47 48 52 private final Expression expression; 53 54 58 private final String targetId; 59 60 74 public LegacyViewerContributionExpression(final String targetId, 75 final IWorkbenchWindow window, final Expression childExpression) { 76 super(window); 77 78 if (targetId == null) { 79 throw new NullPointerException ("The targetId cannot be null"); } 81 this.targetId = targetId; 82 this.expression = childExpression; 83 } 84 85 public final void collectExpressionInfo(final ExpressionInfo info) { 86 super.collectExpressionInfo(info); 87 info.addVariableNameAccess(ISources.ACTIVE_MENU_NAME); 88 if (expression != null) { 89 expression.collectExpressionInfo(info); 90 } 91 } 92 93 protected final int computeHashCode() { 94 int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(getWindow()); 95 hashCode = hashCode * HASH_FACTOR + hashCode(expression); 96 hashCode = hashCode * HASH_FACTOR + hashCode(targetId); 97 return hashCode; 98 } 99 100 public final boolean equals(final Object object) { 101 if (object instanceof LegacyViewerContributionExpression) { 102 final LegacyViewerContributionExpression that = (LegacyViewerContributionExpression) object; 103 return equals(this.targetId, that.targetId) 104 && equals(this.expression, that.expression) 105 && equals(this.getWindow(), that.getWindow()); 106 } 107 108 return false; 109 } 110 111 public final EvaluationResult evaluate(final IEvaluationContext context) 112 throws CoreException { 113 final EvaluationResult result = super.evaluate(context); 114 if (result == EvaluationResult.FALSE) { 115 return result; 116 } 117 118 final Object value = context.getVariable(ISources.ACTIVE_MENU_NAME); 119 if (value instanceof String ) { 120 final String menuId = (String ) value; 121 if (targetId.equals(menuId)) { 122 if (expression == null) { 123 return EvaluationResult.TRUE; 124 } 125 126 return expression.evaluate(context); 127 } 128 } else if (value instanceof Collection ) { 129 final Collection menuIds = (Collection ) value; 130 if (menuIds.contains(targetId)) { 131 if (expression == null) { 132 return EvaluationResult.TRUE; 133 } 134 135 return expression.evaluate(context); 136 } 137 } 138 139 return EvaluationResult.FALSE; 140 } 141 142 public final String toString() { 143 final StringBuffer buffer = new StringBuffer (); 144 buffer.append("ViewerContributionExpression("); buffer.append(targetId); 146 buffer.append(','); 147 buffer.append(expression); 148 buffer.append(','); 149 buffer.append(getWindow()); 150 buffer.append(')'); 151 return buffer.toString(); 152 } 153 } 154 | Popular Tags |