KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ActiveShellExpression


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

11
12 package org.eclipse.ui;
13
14 import org.eclipse.core.expressions.EvaluationResult;
15 import org.eclipse.core.expressions.Expression;
16 import org.eclipse.core.expressions.ExpressionInfo;
17 import org.eclipse.core.expressions.IEvaluationContext;
18 import org.eclipse.swt.widgets.Shell;
19
20 /**
21  * <p>
22  * An expression that checks the active shell variable. The variable name is
23  * <code>ISources.ACTIVE_SHELL_NAME</code> and falls back to
24  * <code>ISources.ACTIVE_WORKBENCH_WINDOW</code>. That is, if the active
25  * shell doesn't match, then it will be allowed to match the active workbench
26  * window.
27  * </p>
28  *
29  * @since 3.1
30  */

31 public final class ActiveShellExpression extends Expression {
32
33     /**
34      * The seed for the hash code for all schemes.
35      */

36     private static final int HASH_INITIAL = ActiveShellExpression.class
37             .getName().hashCode();
38
39     /**
40      * The sources value to use with this expression.
41      */

42     public static final int SOURCES = ISources.ACTIVE_SHELL
43             | ISources.ACTIVE_WORKBENCH_WINDOW;
44
45     /**
46      * The shell that must be active for this expression to evaluate to
47      * <code>true</code>. If this value is <code>null</code>, then any
48      * shell may be active.
49      */

50     private final Shell activeShell;
51
52     /**
53      * Constructs a new instance of <code>ActiveShellExpression</code>
54      *
55      * @param activeShell
56      * The shell to match with the active shell; <code>null</code>
57      * if it will match any active shell.
58      */

59     public ActiveShellExpression(final Shell activeShell) {
60         this.activeShell = activeShell;
61     }
62
63     /**
64      * Expression information for this expression. Namely active shell and
65      * active workbench window name.
66      *
67      * @since 3.2
68      */

69     public final void collectExpressionInfo(final ExpressionInfo info) {
70         info.addVariableNameAccess(ISources.ACTIVE_SHELL_NAME);
71         info.addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
72     }
73
74     protected final int computeHashCode() {
75         return HASH_INITIAL * HASH_FACTOR + hashCode(activeShell);
76     }
77
78     public final boolean equals(final Object JavaDoc object) {
79         if (object instanceof ActiveShellExpression) {
80             final ActiveShellExpression that = (ActiveShellExpression) object;
81             return equals(this.activeShell, that.activeShell);
82         }
83
84         return false;
85     }
86
87     /**
88      * Evaluates this expression. If the active shell defined by the context
89      * matches the shell from this expression, then this evaluates to
90      * <code>EvaluationResult.TRUE</code>. Similarly, if the active workbench
91      * window shell defined by the context matches the shell from this
92      * expression, then this evaluates to <code>EvaluationResult.TRUE</code>.
93      *
94      * @param context
95      * The context from which the current state is determined; must
96      * not be <code>null</code>.
97      * @return <code>EvaluationResult.TRUE</code> if the shell is active;
98      * <code>EvaluationResult.FALSE</code> otherwise.
99      */

100     public final EvaluationResult evaluate(final IEvaluationContext context) {
101         if (activeShell != null) {
102             Object JavaDoc value = context.getVariable(ISources.ACTIVE_SHELL_NAME);
103             if (!activeShell.equals(value)) {
104                 value = context
105                         .getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
106                 if (!activeShell.equals(value)) {
107                     return EvaluationResult.FALSE;
108                 }
109             }
110         }
111
112         return EvaluationResult.TRUE;
113     }
114
115     public final String JavaDoc toString() {
116         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
117         buffer.append("ActiveShellExpression("); //$NON-NLS-1$
118
buffer.append(activeShell);
119         buffer.append(')');
120         return buffer.toString();
121     }
122 }
123
Popular Tags