alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui;

import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionInfo;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.swt.widgets.Shell;

/**
 * <p>
 * An expression that checks the active shell variable. The variable name is
 * <code>ISources.ACTIVE_SHELL_NAME and falls back to
 * <code>ISources.ACTIVE_WORKBENCH_WINDOW. That is, if the active
 * shell doesn't match, then it will be allowed to match the active workbench
 * window.
 * </p>
 * 
 * @since 3.1
 */
public final class ActiveShellExpression extends Expression {

	/**
	 * The seed for the hash code for all schemes.
	 */
	private static final int HASH_INITIAL = ActiveShellExpression.class
			.getName().hashCode();

	/**
	 * The sources value to use with this expression.
	 */
	public static final int SOURCES = ISources.ACTIVE_SHELL
			| ISources.ACTIVE_WORKBENCH_WINDOW;

	/**
	 * The shell that must be active for this expression to evaluate to
	 * <code>true. If this value is null, then any
	 * shell may be active.
	 */
	private final Shell activeShell;

	/**
	 * Constructs a new instance of <code>ActiveShellExpression
	 * 
	 * @param activeShell
	 *            The shell to match with the active shell; <code>null
	 *            if it will match any active shell.
	 */
	public ActiveShellExpression(final Shell activeShell) {
		this.activeShell = activeShell;
	}

	/**
	 * Expression information for this expression.  Namely active shell and 
	 * active workbench window name.
	 * 
	 * @since 3.2
	 */
	public final void collectExpressionInfo(final ExpressionInfo info) {
		info.addVariableNameAccess(ISources.ACTIVE_SHELL_NAME);
		info.addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
	}

	protected final int computeHashCode() {
		return HASH_INITIAL * HASH_FACTOR + hashCode(activeShell);
	}

	public final boolean equals(final Object object) {
		if (object instanceof ActiveShellExpression) {
			final ActiveShellExpression that = (ActiveShellExpression) object;
			return equals(this.activeShell, that.activeShell);
		}

		return false;
	}

	/**
	 * Evaluates this expression. If the active shell defined by the context
	 * matches the shell from this expression, then this evaluates to
	 * <code>EvaluationResult.TRUE. Similarly, if the active workbench
	 * window shell defined by the context matches the shell from this
	 * expression, then this evaluates to <code>EvaluationResult.TRUE.
	 * 
	 * @param context
	 *            The context from which the current state is determined; must
	 *            not be <code>null.
	 * @return <code>EvaluationResult.TRUE if the shell is active;
	 *         <code>EvaluationResult.FALSE otherwise.
	 */
	public final EvaluationResult evaluate(final IEvaluationContext context) {
		if (activeShell != null) {
			Object value = context.getVariable(ISources.ACTIVE_SHELL_NAME);
			if (!activeShell.equals(value)) {
				value = context
						.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
				if (!activeShell.equals(value)) {
					return EvaluationResult.FALSE;
				}
			}
		}

		return EvaluationResult.TRUE;
	}

	public final String toString() {
		final StringBuffer buffer = new StringBuffer();
		buffer.append("ActiveShellExpression("); //$NON-NLS-1$
		buffer.append(activeShell);
		buffer.append(')');
		return buffer.toString();
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.