KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > HandlerActivation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.handlers;
13
14 import java.io.BufferedWriter JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.StringWriter JavaDoc;
17
18 import org.eclipse.core.commands.IHandler;
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.core.expressions.IEvaluationContext;
21 import org.eclipse.ui.ISources;
22 import org.eclipse.ui.handlers.IHandlerActivation;
23 import org.eclipse.ui.handlers.IHandlerService;
24 import org.eclipse.ui.internal.services.EvaluationResultCache;
25
26 /**
27  * <p>
28  * A token representing the activation of a handler. This token can later be
29  * used to cancel that activation. Without this token, then handler will only
30  * become inactive if the component in which the handler was activated is
31  * destroyed.
32  * </p>
33  * <p>
34  * This caches the command id and the handler, so that they can later be
35  * identified.
36  * </p>
37  * <p>
38  * <b>Note:</b> this class has a natural ordering that is inconsistent with
39  * equals.
40  * </p>
41  *
42  * @since 3.1
43  */

44 final class HandlerActivation extends EvaluationResultCache implements
45         IHandlerActivation {
46
47     /**
48      * The identifier for the command which the activated handler handles. This
49      * value is never <code>null</code>.
50      */

51     private final String JavaDoc commandId;
52
53     /**
54      * The depth of services at which this token was created. This is used as a
55      * final tie-breaker if all other things are equivalent.
56      */

57     private final int depth;
58
59     /**
60      * The handler that has been activated. This value may be <code>null</code>.
61      */

62     private final IHandler handler;
63
64     /**
65      * The handler service from which this handler activation was request. This
66      * value is never <code>null</code>.
67      */

68     private final IHandlerService handlerService;
69
70     /**
71      * Constructs a new instance of <code>HandlerActivation</code>.
72      *
73      * @param commandId
74      * The identifier for the command which the activated handler
75      * handles. This value must not be <code>null</code>.
76      * @param handler `
77      * The handler that has been activated. This value may be
78      * <code>null</code>.
79      * @param expression
80      * The expression that must evaluate to <code>true</code>
81      * before this handler is active. This value may be
82      * <code>null</code> if it is always active.</code>.
83      * @param depth
84      * The depth at which this activation was created within the
85      * services hierarchy. This is used as the final tie-breaker if
86      * all other conditions are equal. This should be a positive
87      * integer.
88      * @param handlerService
89      * The handler service from which the handler activation was
90      * requested; must not be <code>null</code>.
91      * @see ISources
92      */

93     HandlerActivation(final String JavaDoc commandId, final IHandler handler,
94             final Expression expression, final int depth,
95             final IHandlerService handlerService) {
96         super(expression);
97
98         if (commandId == null) {
99             throw new NullPointerException JavaDoc(
100                     "The command identifier for a handler activation cannot be null"); //$NON-NLS-1$
101
}
102
103         if (handlerService == null) {
104             throw new NullPointerException JavaDoc(
105                     "The handler service for an activation cannot be null"); //$NON-NLS-1$
106
}
107
108         this.commandId = commandId;
109         this.depth = depth;
110         this.handler = handler;
111         this.handlerService = handlerService;
112     }
113
114     public final void clearActive() {
115         clearResult();
116     }
117
118     /**
119      * Implement {@link Comparable#compareTo(Object)}.
120      * <p>
121      * <b>Note:</b> this class has a natural ordering that is inconsistent with
122      * equals.
123      * </p>
124      */

125     public final int compareTo(final Object JavaDoc object) {
126         final IHandlerActivation activation = (IHandlerActivation) object;
127         int difference;
128
129         // Check the priorities
130
int thisPriority = this.getSourcePriority();
131         int thatPriority = activation.getSourcePriority();
132         
133         // rogue bit problem - ISources.ACTIVE_MENU
134
int thisLsb = 0;
135         int thatLsb = 0;
136         
137         if (((thisPriority & ISources.ACTIVE_MENU) | (thatPriority & ISources.ACTIVE_MENU)) != 0) {
138             thisLsb = thisPriority & 1;
139             thisPriority = (thisPriority >> 1) & 0x7fffffff;
140             thatLsb = thatPriority & 1;
141             thatPriority = (thatPriority >> 1) & 0x7fffffff;
142         }
143         
144         difference = thisPriority - thatPriority;
145         if (difference != 0) {
146             return difference;
147         }
148         
149         // if all of the higher bits are the same, check the
150
// difference of the LSB
151
difference = thisLsb - thatLsb;
152         if (difference != 0) {
153             return difference;
154         }
155
156         // Check depth
157
final int thisDepth = this.getDepth();
158         final int thatDepth = activation.getDepth();
159         difference = thisDepth - thatDepth;
160         return difference;
161     }
162
163     public final String JavaDoc getCommandId() {
164         return commandId;
165     }
166
167     public final int getDepth() {
168         return depth;
169     }
170
171     public final IHandler getHandler() {
172         return handler;
173     }
174
175     public final IHandlerService getHandlerService() {
176         return handlerService;
177     }
178
179     public final boolean isActive(final IEvaluationContext context) {
180         return evaluate(context);
181     }
182
183     public final String JavaDoc toString() {
184         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
185         final BufferedWriter JavaDoc buffer = new BufferedWriter JavaDoc(sw);
186         
187         try {
188             buffer.write("HandlerActivation(commandId="); //$NON-NLS-1$
189
buffer.write(commandId);
190             buffer.write(',');
191             buffer.newLine();
192             buffer.write("\thandler="); //$NON-NLS-1$
193
buffer.write(handler==null?"":handler.toString()); //$NON-NLS-1$
194
buffer.write(',');
195             buffer.newLine();
196             buffer.write("\texpression="); //$NON-NLS-1$
197
Expression exp = getExpression();
198             buffer.write(exp==null?"":exp.toString()); //$NON-NLS-1$
199
buffer.write(",sourcePriority="); //$NON-NLS-1$
200
buffer.write(Integer.toString(getSourcePriority()));
201             buffer.write(')');
202             buffer.flush();
203         } catch (IOException JavaDoc e) {
204             // we're a string buffer, there should be no IO exception
205
}
206         return sw.toString();
207     }
208 }
209
Popular Tags