001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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 org.eclipse.core.expressions.EvaluationResult;
013: import org.eclipse.core.expressions.Expression;
014: import org.eclipse.core.expressions.ExpressionInfo;
015: import org.eclipse.core.expressions.IEvaluationContext;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.ui.ISources;
019: import org.eclipse.ui.IWorkbenchWindow;
020: import org.eclipse.ui.internal.ActionExpression;
021:
022: /**
023: * <p>
024: * This wrappers the old {@link ActionExpression} class so that it can
025: * communicate via the {@link Expression} contract.
026: * </p>
027: * <p>
028: * This class is not intended for use outside of the
029: * <code>org.eclipse.ui.workbench</code> plug-in.
030: * </p>
031: *
032: * @since 3.2
033: */
034: public final class LegacyActionExpressionWrapper extends
035: WorkbenchWindowExpression {
036:
037: /**
038: * The seed for the hash code for all schemes.
039: */
040: private static final int HASH_INITIAL = LegacyActionExpressionWrapper.class
041: .getName().hashCode();
042:
043: /**
044: * The legacy action expression being wrapped; never <code>null</code>.
045: */
046: private final ActionExpression expression;
047:
048: /**
049: * Constructs a new instance of {@link LegacyActionExpressionWrapper}.
050: *
051: * @param expression
052: * The expression to wrap; must not be <code>null</code>.
053: * @param window
054: * The workbench window which must be active for this expression
055: * to evaluate to <code>true</code>; may be <code>null</code>
056: * if the window should be disregarded.
057: */
058: public LegacyActionExpressionWrapper(
059: final ActionExpression expression,
060: final IWorkbenchWindow window) {
061: super (window);
062:
063: if (expression == null) {
064: throw new NullPointerException(
065: "The action expression cannot be null"); //$NON-NLS-1$
066: }
067: this .expression = expression;
068: }
069:
070: public final void collectExpressionInfo(final ExpressionInfo info) {
071: super .collectExpressionInfo(info);
072: info.markDefaultVariableAccessed();
073: }
074:
075: protected final int computeHashCode() {
076: int hashCode = HASH_INITIAL * HASH_FACTOR
077: + hashCode(getWindow());
078: hashCode = hashCode * HASH_FACTOR + hashCode(expression);
079: return hashCode;
080: }
081:
082: public final boolean equals(final Object object) {
083: if (object instanceof LegacyActionExpressionWrapper) {
084: final LegacyActionExpressionWrapper that = (LegacyActionExpressionWrapper) object;
085: return equals(this .expression, that.expression)
086: && equals(this .getWindow(), that.getWindow());
087: }
088:
089: return false;
090: }
091:
092: public final EvaluationResult evaluate(
093: final IEvaluationContext context) throws CoreException {
094: final EvaluationResult result = super .evaluate(context);
095: if (result == EvaluationResult.FALSE) {
096: return result;
097: }
098:
099: 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("); //$NON-NLS-1$
116: buffer.append(expression);
117: buffer.append(',');
118: buffer.append(getWindow());
119: buffer.append(')');
120: return buffer.toString();
121: }
122: }
|