001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.expressions;
011:
012: import java.util.Collection;
013:
014: import org.eclipse.core.expressions.EvaluationResult;
015: import org.eclipse.core.expressions.Expression;
016: import org.eclipse.core.expressions.ExpressionInfo;
017: import org.eclipse.core.expressions.IEvaluationContext;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.ui.ISources;
020: import org.eclipse.ui.IWorkbenchWindow;
021:
022: /**
023: * <p>
024: * An expression that can control the activation of a handler derived from a
025: * viewer contribution. The viewer contribution is linked to a menu with a
026: * particular identifier, as well as some other criteria. This expression checks
027: * the target menu id, and contains a child expression for the other criteria.
028: * </p>
029: * <p>
030: * This class is not intended for use outside of the
031: * <code>org.eclipse.ui.workbench</code> plug-in.
032: * </p>
033: *
034: * @see ISources#ACTIVE_MENU_NAME
035: * @since 3.2
036: */
037: public final class LegacyViewerContributionExpression extends
038: WorkbenchWindowExpression {
039:
040: /**
041: * The seed for the hash code for all schemes.
042: */
043: private static final int HASH_INITIAL = LegacyViewerContributionExpression.class
044: .getName().hashCode();
045:
046: /**
047: * The child expression for this viewer contribution. This value may be
048: * <code>null</code> if there are no criteria beyond the menu id.
049: */
050: private final Expression expression;
051:
052: /**
053: * The identifier of the menu to which this viewer contribution applies.
054: * This value is never <code>null</code>.
055: */
056: private final String targetId;
057:
058: /**
059: * Constructs a new {@link LegacyViewerContributionExpression}.
060: *
061: * @param targetId
062: * The identifier of the menu to which this viewer contribution
063: * applies; never <code>null</code>.
064: * @param window
065: * The workbench window which must be active for this expression
066: * to evaluate to <code>true</code>; may be <code>null</code>
067: * if the window should be disregarded.
068: * @param childExpression
069: * The child expression for this viewer contribution; may be
070: * <code>null</code>.
071: */
072: public LegacyViewerContributionExpression(final String targetId,
073: final IWorkbenchWindow window,
074: final Expression childExpression) {
075: super (window);
076:
077: if (targetId == null) {
078: throw new NullPointerException(
079: "The targetId cannot be null"); //$NON-NLS-1$
080: }
081: this .targetId = targetId;
082: this .expression = childExpression;
083: }
084:
085: public final void collectExpressionInfo(final ExpressionInfo info) {
086: super .collectExpressionInfo(info);
087: info.addVariableNameAccess(ISources.ACTIVE_MENU_NAME);
088: if (expression != null) {
089: expression.collectExpressionInfo(info);
090: }
091: }
092:
093: protected final int computeHashCode() {
094: int hashCode = HASH_INITIAL * HASH_FACTOR
095: + hashCode(getWindow());
096: hashCode = hashCode * HASH_FACTOR + hashCode(expression);
097: hashCode = hashCode * HASH_FACTOR + hashCode(targetId);
098: return hashCode;
099: }
100:
101: public final boolean equals(final Object object) {
102: if (object instanceof LegacyViewerContributionExpression) {
103: final LegacyViewerContributionExpression that = (LegacyViewerContributionExpression) object;
104: return equals(this .targetId, that.targetId)
105: && equals(this .expression, that.expression)
106: && equals(this .getWindow(), that.getWindow());
107: }
108:
109: return false;
110: }
111:
112: public final EvaluationResult evaluate(
113: final IEvaluationContext context) throws CoreException {
114: final EvaluationResult result = super .evaluate(context);
115: if (result == EvaluationResult.FALSE) {
116: return result;
117: }
118:
119: final Object value = context
120: .getVariable(ISources.ACTIVE_MENU_NAME);
121: if (value instanceof String) {
122: final String menuId = (String) value;
123: if (targetId.equals(menuId)) {
124: if (expression == null) {
125: return EvaluationResult.TRUE;
126: }
127:
128: return expression.evaluate(context);
129: }
130: } else if (value instanceof Collection) {
131: final Collection menuIds = (Collection) value;
132: if (menuIds.contains(targetId)) {
133: if (expression == null) {
134: return EvaluationResult.TRUE;
135: }
136:
137: return expression.evaluate(context);
138: }
139: }
140:
141: return EvaluationResult.FALSE;
142: }
143:
144: public final String toString() {
145: final StringBuffer buffer = new StringBuffer();
146: buffer.append("ViewerContributionExpression("); //$NON-NLS-1$
147: buffer.append(targetId);
148: buffer.append(',');
149: buffer.append(expression);
150: buffer.append(',');
151: buffer.append(getWindow());
152: buffer.append(')');
153: return buffer.toString();
154: }
155: }
|