KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 java.util.Collection JavaDoc;
15
16 import org.eclipse.core.expressions.EvaluationResult;
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.core.expressions.ExpressionInfo;
19 import org.eclipse.core.expressions.IEvaluationContext;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.ui.ISources;
22 import org.eclipse.ui.IWorkbenchWindow;
23
24 /**
25  * <p>
26  * An expression that can control the activation of a handler derived from a
27  * viewer contribution. The viewer contribution is linked to a menu with a
28  * particular identifier, as well as some other criteria. This expression checks
29  * the target menu id, and contains a child expression for the other criteria.
30  * </p>
31  * <p>
32  * This class is not intended for use outside of the
33  * <code>org.eclipse.ui.workbench</code> plug-in.
34  * </p>
35  *
36  * @see ISources#ACTIVE_MENU_NAME
37  * @since 3.2
38  */

39 public final class LegacyViewerContributionExpression extends
40         WorkbenchWindowExpression {
41
42     /**
43      * The seed for the hash code for all schemes.
44      */

45     private static final int HASH_INITIAL = LegacyViewerContributionExpression.class
46             .getName().hashCode();
47
48     /**
49      * The child expression for this viewer contribution. This value may be
50      * <code>null</code> if there are no criteria beyond the menu id.
51      */

52     private final Expression expression;
53
54     /**
55      * The identifier of the menu to which this viewer contribution applies.
56      * This value is never <code>null</code>.
57      */

58     private final String JavaDoc targetId;
59
60     /**
61      * Constructs a new {@link LegacyViewerContributionExpression}.
62      *
63      * @param targetId
64      * The identifier of the menu to which this viewer contribution
65      * applies; never <code>null</code>.
66      * @param window
67      * The workbench window which must be active for this expression
68      * to evaluate to <code>true</code>; may be <code>null</code>
69      * if the window should be disregarded.
70      * @param childExpression
71      * The child expression for this viewer contribution; may be
72      * <code>null</code>.
73      */

74     public LegacyViewerContributionExpression(final String JavaDoc targetId,
75             final IWorkbenchWindow window, final Expression childExpression) {
76         super(window);
77
78         if (targetId == null) {
79             throw new NullPointerException JavaDoc("The targetId cannot be null"); //$NON-NLS-1$
80
}
81         this.targetId = targetId;
82         this.expression = childExpression;
83     }
84
85     public final void collectExpressionInfo(final ExpressionInfo info) {
86         super.collectExpressionInfo(info);
87         info.addVariableNameAccess(ISources.ACTIVE_MENU_NAME);
88         if (expression != null) {
89             expression.collectExpressionInfo(info);
90         }
91     }
92
93     protected final int computeHashCode() {
94         int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(getWindow());
95         hashCode = hashCode * HASH_FACTOR + hashCode(expression);
96         hashCode = hashCode * HASH_FACTOR + hashCode(targetId);
97         return hashCode;
98     }
99
100     public final boolean equals(final Object JavaDoc object) {
101         if (object instanceof LegacyViewerContributionExpression) {
102             final LegacyViewerContributionExpression that = (LegacyViewerContributionExpression) object;
103             return equals(this.targetId, that.targetId)
104                     && equals(this.expression, that.expression)
105                     && equals(this.getWindow(), that.getWindow());
106         }
107
108         return false;
109     }
110
111     public final EvaluationResult evaluate(final IEvaluationContext context)
112             throws CoreException {
113         final EvaluationResult result = super.evaluate(context);
114         if (result == EvaluationResult.FALSE) {
115             return result;
116         }
117
118         final Object JavaDoc value = context.getVariable(ISources.ACTIVE_MENU_NAME);
119         if (value instanceof String JavaDoc) {
120             final String JavaDoc menuId = (String JavaDoc) value;
121             if (targetId.equals(menuId)) {
122                 if (expression == null) {
123                     return EvaluationResult.TRUE;
124                 }
125
126                 return expression.evaluate(context);
127             }
128         } else if (value instanceof Collection JavaDoc) {
129             final Collection JavaDoc menuIds = (Collection JavaDoc) value;
130             if (menuIds.contains(targetId)) {
131                 if (expression == null) {
132                     return EvaluationResult.TRUE;
133                 }
134
135                 return expression.evaluate(context);
136             }
137         }
138
139         return EvaluationResult.FALSE;
140     }
141
142     public final String JavaDoc toString() {
143         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
144         buffer.append("ViewerContributionExpression("); //$NON-NLS-1$
145
buffer.append(targetId);
146         buffer.append(',');
147         buffer.append(expression);
148         buffer.append(',');
149         buffer.append(getWindow());
150         buffer.append(')');
151         return buffer.toString();
152     }
153 }
154
Popular Tags