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.ISelection;
018: import org.eclipse.ui.ISources;
019: import org.eclipse.ui.IWorkbenchWindow;
020: import org.eclipse.ui.SelectionEnabler;
021:
022: /**
023: * <p>
024: * An expression wrapper for the legacy {@link SelectionEnabler}. This emulates
025: * an {@link Expression} using an instance of <code>SelectionEnabler</code>.
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 LegacySelectionEnablerWrapper extends
035: WorkbenchWindowExpression {
036:
037: /**
038: * The seed for the hash code for all schemes.
039: */
040: private static final int HASH_INITIAL = LegacySelectionEnablerWrapper.class
041: .getName().hashCode();
042:
043: /**
044: * The enabler for this expression; never <code>null</code>.
045: */
046: private final SelectionEnabler enabler;
047:
048: /**
049: * Constructs a new instance of <code>SelectionEnablerExpression</code>.
050: *
051: * @param enabler
052: * The enabler; 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 LegacySelectionEnablerWrapper(
059: final SelectionEnabler enabler,
060: final IWorkbenchWindow window) {
061: super (window);
062:
063: if (enabler == null) {
064: throw new NullPointerException("There is no enabler"); //$NON-NLS-1$
065: }
066: this .enabler = enabler;
067: }
068:
069: public final void collectExpressionInfo(final ExpressionInfo info) {
070: super .collectExpressionInfo(info);
071: info.markDefaultVariableAccessed();
072: }
073:
074: protected final int computeHashCode() {
075: int hashCode = HASH_INITIAL * HASH_FACTOR
076: + hashCode(getWindow());
077: hashCode = hashCode * HASH_FACTOR + hashCode(enabler);
078: return hashCode;
079: }
080:
081: public final boolean equals(final Object object) {
082: if (object instanceof LegacySelectionEnablerWrapper) {
083: final LegacySelectionEnablerWrapper that = (LegacySelectionEnablerWrapper) object;
084: return equals(this .enabler, that.enabler)
085: && equals(this .getWindow(), that.getWindow());
086: }
087:
088: return false;
089: }
090:
091: public final EvaluationResult evaluate(
092: final IEvaluationContext context) throws CoreException {
093: final EvaluationResult result = super .evaluate(context);
094: if (result == EvaluationResult.FALSE) {
095: return result;
096: }
097:
098: final Object defaultVariable = context
099: .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
100: if (defaultVariable instanceof ISelection) {
101: final ISelection selection = (ISelection) defaultVariable;
102: if (enabler.isEnabledForSelection(selection)) {
103: return EvaluationResult.TRUE;
104: }
105: }
106:
107: return EvaluationResult.FALSE;
108: }
109:
110: public final String toString() {
111: final StringBuffer buffer = new StringBuffer();
112: buffer.append("LegacySelectionEnablerWrapper("); //$NON-NLS-1$
113: buffer.append(enabler);
114: buffer.append(',');
115: buffer.append(getWindow());
116: buffer.append(')');
117: return buffer.toString();
118: }
119: }
|