KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > expressions > WorkbenchWindowExpression


1 /*******************************************************************************
2  * Copyright (c) 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.internal.expressions;
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.core.runtime.CoreException;
19 import org.eclipse.ui.ISources;
20 import org.eclipse.ui.IWorkbenchWindow;
21
22 /**
23  * <p>
24  * An expression that evaluates to {@link EvaluationResult#TRUE} when the active
25  * workbench window matches the window held by this expression.
26  * </p>
27  *
28  * @since 3.2
29  */

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

35     private static final int HASH_INITIAL = WorkbenchWindowExpression.class
36             .getName().hashCode();
37
38     /**
39      * The workbench window that must be active for this expression to evaluate
40      * to <code>true</code>. If this value is <code>null</code>, then any
41      * workbench window may be active.
42      */

43     private final IWorkbenchWindow window;
44
45     /**
46      * Constructs a new instance.
47      *
48      * @param window
49      * The workbench window which must be active for this expression
50      * to evaluate to <code>true</code>; may be <code>null</code>
51      * if this expression is always <code>true</code>.
52      */

53     public WorkbenchWindowExpression(final IWorkbenchWindow window) {
54         this.window = window;
55     }
56
57     public void collectExpressionInfo(final ExpressionInfo info) {
58         if (window != null) {
59             info.addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
60         }
61     }
62
63     protected int computeHashCode() {
64         return HASH_INITIAL * HASH_FACTOR + hashCode(window);
65     }
66
67     public boolean equals(final Object JavaDoc object) {
68         if (object instanceof WorkbenchWindowExpression) {
69             final WorkbenchWindowExpression that = (WorkbenchWindowExpression) object;
70             return equals(this.window, that.window);
71         }
72
73         return false;
74     }
75
76     public EvaluationResult evaluate(final IEvaluationContext context)
77             throws CoreException {
78         if (window != null) {
79             Object JavaDoc value = context
80                     .getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
81             if (window.equals(value)) {
82                 return EvaluationResult.TRUE;
83             }
84         }
85
86         return EvaluationResult.FALSE;
87     }
88
89     /**
90      * Returns the workbench window to which this expression applies.
91      *
92      * @return The workbench window to which this expression applies; may be
93      * <code>null</code>.
94      */

95     protected final IWorkbenchWindow getWindow() {
96         return window;
97     }
98
99     public String JavaDoc toString() {
100         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101         buffer.append("WorkbenchWindowExpression("); //$NON-NLS-1$
102
buffer.append(window);
103         buffer.append(')');
104         return buffer.toString();
105     }
106 }
107
Popular Tags