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;
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.swt.widgets.Shell;
017:
018: /**
019: * <p>
020: * An expression that checks the active shell variable. The variable name is
021: * <code>ISources.ACTIVE_SHELL_NAME</code> and falls back to
022: * <code>ISources.ACTIVE_WORKBENCH_WINDOW</code>. That is, if the active
023: * shell doesn't match, then it will be allowed to match the active workbench
024: * window.
025: * </p>
026: *
027: * @since 3.1
028: */
029: public final class ActiveShellExpression extends Expression {
030:
031: /**
032: * The seed for the hash code for all schemes.
033: */
034: private static final int HASH_INITIAL = ActiveShellExpression.class
035: .getName().hashCode();
036:
037: /**
038: * The sources value to use with this expression.
039: */
040: public static final int SOURCES = ISources.ACTIVE_SHELL
041: | ISources.ACTIVE_WORKBENCH_WINDOW;
042:
043: /**
044: * The shell that must be active for this expression to evaluate to
045: * <code>true</code>. If this value is <code>null</code>, then any
046: * shell may be active.
047: */
048: private final Shell activeShell;
049:
050: /**
051: * Constructs a new instance of <code>ActiveShellExpression</code>
052: *
053: * @param activeShell
054: * The shell to match with the active shell; <code>null</code>
055: * if it will match any active shell.
056: */
057: public ActiveShellExpression(final Shell activeShell) {
058: this .activeShell = activeShell;
059: }
060:
061: /**
062: * Expression information for this expression. Namely active shell and
063: * active workbench window name.
064: *
065: * @since 3.2
066: */
067: public final void collectExpressionInfo(final ExpressionInfo info) {
068: info.addVariableNameAccess(ISources.ACTIVE_SHELL_NAME);
069: info
070: .addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
071: }
072:
073: protected final int computeHashCode() {
074: return HASH_INITIAL * HASH_FACTOR + hashCode(activeShell);
075: }
076:
077: public final boolean equals(final Object object) {
078: if (object instanceof ActiveShellExpression) {
079: final ActiveShellExpression that = (ActiveShellExpression) object;
080: return equals(this .activeShell, that.activeShell);
081: }
082:
083: return false;
084: }
085:
086: /**
087: * Evaluates this expression. If the active shell defined by the context
088: * matches the shell from this expression, then this evaluates to
089: * <code>EvaluationResult.TRUE</code>. Similarly, if the active workbench
090: * window shell defined by the context matches the shell from this
091: * expression, then this evaluates to <code>EvaluationResult.TRUE</code>.
092: *
093: * @param context
094: * The context from which the current state is determined; must
095: * not be <code>null</code>.
096: * @return <code>EvaluationResult.TRUE</code> if the shell is active;
097: * <code>EvaluationResult.FALSE</code> otherwise.
098: */
099: public final EvaluationResult evaluate(
100: final IEvaluationContext context) {
101: if (activeShell != null) {
102: Object value = context
103: .getVariable(ISources.ACTIVE_SHELL_NAME);
104: if (!activeShell.equals(value)) {
105: value = context
106: .getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
107: if (!activeShell.equals(value)) {
108: return EvaluationResult.FALSE;
109: }
110: }
111: }
112:
113: return EvaluationResult.TRUE;
114: }
115:
116: public final String toString() {
117: final StringBuffer buffer = new StringBuffer();
118: buffer.append("ActiveShellExpression("); //$NON-NLS-1$
119: buffer.append(activeShell);
120: buffer.append(')');
121: return buffer.toString();
122: }
123: }
|