KickJava   Java API By Example, From Geeks To Geeks.

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


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 encapsulating all of the information from legacy handler
23  * submissions.
24  * </p>
25  *
26  * @since 3.1
27  */

28 public final class LegacyHandlerSubmissionExpression extends Expression {
29
30     /**
31      * The seed for the hash code for all schemes.
32      */

33     private static final int HASH_INITIAL = LegacyHandlerSubmissionExpression.class
34             .getName().hashCode();
35
36     /**
37      * The identifier for the part that must be active for this expression to
38      * evaluate to <code>true</code>. If this value is <code>null</code>,
39      * then any part may be active.
40      */

41     private final String JavaDoc activePartId;
42
43     /**
44      * The shell that must be active for this expression to evaluate to
45      * <code>true</code>. If this value is <code>null</code>, then any
46      * shell may be active.
47      */

48     private final Shell activeShell;
49
50     /**
51      * The site that must be active for this expression to evaluate to
52      * <code>true</code>. If this value is <code>null</code>, then any
53      * site may be active.
54      */

55     private final IWorkbenchPartSite activeSite;
56
57     /**
58      * Constructs a new instance of
59      * <code>LegacyHandlerSubmissionExpression</code>
60      *
61      * @param activePartId
62      * The part identifier to match with the active part;
63      * <code>null</code> if it will match any active part.
64      * @param activeShell
65      * The shell to match with the active shell; <code>null</code>
66      * if it will match any active shell.
67      * @param activeSite
68      * The site to match with the active site; <code>null</code> if
69      * it will match any active site.
70      */

71     public LegacyHandlerSubmissionExpression(final String JavaDoc activePartId,
72             final Shell activeShell, final IWorkbenchPartSite activeSite) {
73
74         this.activePartId = activePartId;
75         this.activeShell = activeShell;
76         this.activeSite = activeSite;
77     }
78
79     /**
80      * Collect expression info for a legacy handler submission. Namely
81      * the active part id and name, active shell name, active workbench
82      * window shell name and the active site name.
83      *
84      * @since 3.2
85      */

86     public final void collectExpressionInfo(final ExpressionInfo info) {
87         if (activePartId != null) {
88             info.addVariableNameAccess(ISources.ACTIVE_PART_ID_NAME);
89         }
90         if (activeShell != null) {
91             info.addVariableNameAccess(ISources.ACTIVE_SHELL_NAME);
92             info
93                     .addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
94         }
95         if (activeSite != null) {
96             info.addVariableNameAccess(ISources.ACTIVE_SITE_NAME);
97         }
98     }
99
100     protected final int computeHashCode() {
101         int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(activePartId);
102         hashCode = hashCode * HASH_FACTOR + hashCode(activeShell);
103         hashCode = hashCode * HASH_FACTOR + hashCode(activeSite);
104         return hashCode;
105     }
106
107     public final boolean equals(final Object JavaDoc object) {
108         if (object instanceof LegacyHandlerSubmissionExpression) {
109             final LegacyHandlerSubmissionExpression that = (LegacyHandlerSubmissionExpression) object;
110             return equals(this.activePartId, that.activePartId)
111                     && equals(this.activeShell, that.activeShell)
112                     && equals(this.activeSite, that.activeSite);
113         }
114
115         return false;
116     }
117
118     /**
119      * Evaluates this expression. This tests the three conditions against the
120      * current state of the application (as defined by <code>context</code>).
121      * If a condition is <code>null</code>, then it matches any possible
122      * value (i.e., it is not tested at all).
123      *
124      * @param context
125      * The context providing the current workbench state; must not be
126      * <code>null</code>.
127      * @return <code>EvaluationResult.TRUE</code> if the conditions all
128      * matches; <code>EvaluationResult.FALSE</code> otherwise.
129      */

130     public final EvaluationResult evaluate(final IEvaluationContext context) {
131         if (activePartId != null) {
132             final Object JavaDoc value = context
133                     .getVariable(ISources.ACTIVE_PART_ID_NAME);
134             if (!activePartId.equals(value)) {
135                 return EvaluationResult.FALSE;
136             }
137         }
138
139         if (activeShell != null) {
140             Object JavaDoc value = context.getVariable(ISources.ACTIVE_SHELL_NAME);
141             if (!activeShell.equals(value)) {
142                 value = context
143                         .getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
144                 if (!activeShell.equals(value)) {
145                     return EvaluationResult.FALSE;
146                 }
147             }
148         }
149
150         if (activeSite != null) {
151             final Object JavaDoc value = context.getVariable(ISources.ACTIVE_SITE_NAME);
152             if (!activeSite.equals(value)) {
153                 return EvaluationResult.FALSE;
154             }
155         }
156
157         return EvaluationResult.TRUE;
158     }
159
160     public final String JavaDoc toString() {
161         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
162         buffer.append("LegacyHandlerSubmission("); //$NON-NLS-1$
163
buffer.append(activeShell);
164         buffer.append(',');
165         buffer.append(activePartId);
166         buffer.append(',');
167         buffer.append(activeSite);
168         buffer.append(')');
169         return buffer.toString();
170     }
171 }
172
Popular Tags