001: /*******************************************************************************
002: * Copyright (c) 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.ui.ISources;
018: import org.eclipse.ui.IWorkbenchWindow;
019:
020: /**
021: * <p>
022: * An expression that evaluates to {@link EvaluationResult#TRUE} when the active
023: * workbench window matches the window held by this expression.
024: * </p>
025: *
026: * @since 3.2
027: */
028: public class WorkbenchWindowExpression extends Expression {
029:
030: /**
031: * The seed for the hash code for all schemes.
032: */
033: private static final int HASH_INITIAL = WorkbenchWindowExpression.class
034: .getName().hashCode();
035:
036: /**
037: * The workbench window that must be active for this expression to evaluate
038: * to <code>true</code>. If this value is <code>null</code>, then any
039: * workbench window may be active.
040: */
041: private final IWorkbenchWindow window;
042:
043: /**
044: * Constructs a new instance.
045: *
046: * @param window
047: * The workbench window which must be active for this expression
048: * to evaluate to <code>true</code>; may be <code>null</code>
049: * if this expression is always <code>true</code>.
050: */
051: public WorkbenchWindowExpression(final IWorkbenchWindow window) {
052: this .window = window;
053: }
054:
055: public void collectExpressionInfo(final ExpressionInfo info) {
056: if (window != null) {
057: info
058: .addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
059: }
060: }
061:
062: protected int computeHashCode() {
063: return HASH_INITIAL * HASH_FACTOR + hashCode(window);
064: }
065:
066: public boolean equals(final Object object) {
067: if (object instanceof WorkbenchWindowExpression) {
068: final WorkbenchWindowExpression that = (WorkbenchWindowExpression) object;
069: return equals(this .window, that.window);
070: }
071:
072: return false;
073: }
074:
075: public EvaluationResult evaluate(final IEvaluationContext context)
076: throws CoreException {
077: if (window != null) {
078: Object value = context
079: .getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
080: if (window.equals(value)) {
081: return EvaluationResult.TRUE;
082: }
083: }
084:
085: return EvaluationResult.FALSE;
086: }
087:
088: /**
089: * Returns the workbench window to which this expression applies.
090: *
091: * @return The workbench window to which this expression applies; may be
092: * <code>null</code>.
093: */
094: protected final IWorkbenchWindow getWindow() {
095: return window;
096: }
097:
098: public String toString() {
099: final StringBuffer buffer = new StringBuffer();
100: buffer.append("WorkbenchWindowExpression("); //$NON-NLS-1$
101: buffer.append(window);
102: buffer.append(')');
103: return buffer.toString();
104: }
105: }
|