Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 173213 | Differences between
and this patch

Collapse All | Expand All

(-)Eclipse UI/org/eclipse/ui/internal/handlers/HandlerAuthority.java (-10 / +87 lines)
Lines 12-17 Link Here
12
package org.eclipse.ui.internal.handlers;
12
package org.eclipse.ui.internal.handlers;
13
13
14
import java.util.Collection;
14
import java.util.Collection;
15
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.Iterator;
Lines 23-28 Link Here
23
import org.eclipse.core.commands.Command;
24
import org.eclipse.core.commands.Command;
24
import org.eclipse.core.commands.IHandler;
25
import org.eclipse.core.commands.IHandler;
25
import org.eclipse.core.commands.util.Tracing;
26
import org.eclipse.core.commands.util.Tracing;
27
import org.eclipse.core.expressions.EvaluationContext;
26
import org.eclipse.core.expressions.EvaluationResult;
28
import org.eclipse.core.expressions.EvaluationResult;
27
import org.eclipse.core.expressions.Expression;
29
import org.eclipse.core.expressions.Expression;
28
import org.eclipse.core.expressions.IEvaluationContext;
30
import org.eclipse.core.expressions.IEvaluationContext;
Lines 517-529 Link Here
517
	}
519
	}
518
520
519
	/**
521
	/**
520
	 * Currently this is a kludge.
522
	 * Currently this is a an internal method to help locate a handler.
521
	 * <p>
523
	 * <p>
522
	 * DO NOT CALL THIS METHOD.
524
	 * DO NOT CALL THIS METHOD.
523
	 * </p>
525
	 * </p>
524
	 * 
526
	 * 
525
	 * @param commandId the command id to check
527
	 * @param commandId
526
	 * @param the context to use for activations
528
	 *            the command id to check
529
	 * @param context
530
	 *            the context to use for activations
527
	 * @since 3.3
531
	 * @since 3.3
528
	 */
532
	 */
529
	public final IHandler findHandler(String commandId,
533
	public final IHandler findHandler(String commandId,
Lines 532-538 Link Here
532
		if (o instanceof IHandlerActivation) {
536
		if (o instanceof IHandlerActivation) {
533
			IHandlerActivation activation = (IHandlerActivation) o;
537
			IHandlerActivation activation = (IHandlerActivation) o;
534
			try {
538
			try {
535
				if (activation.getExpression().evaluate(context) == EvaluationResult.TRUE) {
539
				if (eval(context, activation)) {
536
					return activation.getHandler();
540
					return activation.getHandler();
537
				}
541
				}
538
			} catch (CoreException e) {
542
			} catch (CoreException e) {
Lines 543-552 Link Here
543
			IHandlerActivation lastActivation = null;
547
			IHandlerActivation lastActivation = null;
544
			IHandlerActivation currentActivation = null;
548
			IHandlerActivation currentActivation = null;
545
			Iterator i = activations.iterator();
549
			Iterator i = activations.iterator();
546
			while (i.hasNext()) {
550
			while (i.hasNext() && lastActivation==null) {
547
				IHandlerActivation activation = (IHandlerActivation) i.next();
551
				IHandlerActivation activation = (IHandlerActivation) i.next();
548
				try {
552
				try {
549
					if (activation.getExpression().evaluate(context) == EvaluationResult.TRUE) {
553
					if (eval(context, activation)) {
550
						lastActivation = currentActivation;
554
						lastActivation = currentActivation;
551
						currentActivation = activation;
555
						currentActivation = activation;
552
					}
556
					}
Lines 554-568 Link Here
554
					// OK, this one is out of the running
558
					// OK, this one is out of the running
555
				}
559
				}
556
			}
560
			}
557
			if (currentActivation!=null) {
561
			if (currentActivation != null) {
558
				if (lastActivation==null) {
562
				if (lastActivation == null) {
559
					return currentActivation.getHandler();
563
					return currentActivation.getHandler();
560
				}
564
				}
561
				if (lastActivation.getSourcePriority()!=currentActivation.getSourcePriority()) {
565
				if (lastActivation.getSourcePriority() != currentActivation
562
					return currentActivation.getHandler();
566
						.getSourcePriority()) {
567
					return lastActivation.getHandler();
563
				}
568
				}
564
			}
569
			}
565
		}
570
		}
566
		return null;
571
		return null;
567
	}
572
	}
573
574
	/**
575
	 * Evaluate the expression for the handler and bypass the result cache.
576
	 * <p>
577
	 * DO NOT CALL THIS METHOD.
578
	 * </p>
579
	 * 
580
	 * @param context
581
	 * @param activation
582
	 * @return <code>true</code> if the handler expression can evaluate to
583
	 *         true.
584
	 * @throws CoreException
585
	 * @since 3.3
586
	 */
587
	private boolean eval(IEvaluationContext context,
588
			IHandlerActivation activation) throws CoreException {
589
		Expression expression = activation.getExpression();
590
		if (expression == null) {
591
			return true;
592
		}
593
		return expression.evaluate(context) == EvaluationResult.TRUE;
594
	}
595
596
	/**
597
	 * Normally the context returned from getCurrentState() still tracks the
598
	 * application state. This method creates a copy and fills it in with the
599
	 * variables that we know about. Currently it does not fill in the active
600
	 * selection.
601
	 * <p>
602
	 * DO NOT CALL THIS METHOD. It is experimental in 3.3.
603
	 * </p>
604
	 * 
605
	 * @return an evaluation context with no parent.
606
	 * @since 3.3
607
	 */
608
	public IEvaluationContext getContextSnapshot() {
609
		EvaluationContext context = new EvaluationContext(null,
610
				Collections.EMPTY_LIST);
611
		IEvaluationContext tmpContext = getCurrentState();
612
		context.addVariable(ISources.ACTIVE_ACTION_SETS_NAME, tmpContext
613
				.getVariable(ISources.ACTIVE_ACTION_SETS_NAME));
614
		context.addVariable(ISources.ACTIVE_CONTEXT_NAME, tmpContext
615
				.getVariable(ISources.ACTIVE_CONTEXT_NAME));
616
		context.addVariable(ISources.ACTIVE_EDITOR_ID_NAME, tmpContext
617
				.getVariable(ISources.ACTIVE_EDITOR_ID_NAME));
618
		context.addVariable(ISources.ACTIVE_EDITOR_NAME, tmpContext
619
				.getVariable(ISources.ACTIVE_EDITOR_NAME));
620
		context.addVariable(ISources.ACTIVE_PART_ID_NAME, tmpContext
621
				.getVariable(ISources.ACTIVE_PART_ID_NAME));
622
		context.addVariable(ISources.ACTIVE_PART_NAME, tmpContext
623
				.getVariable(ISources.ACTIVE_PART_NAME));
624
		context.addVariable(ISources.ACTIVE_SITE_NAME, tmpContext
625
				.getVariable(ISources.ACTIVE_SITE_NAME));
626
		context
627
				.addVariable(
628
						ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME,
629
						tmpContext
630
								.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME));
631
		context
632
				.addVariable(
633
						ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME,
634
						tmpContext
635
								.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME));
636
		context.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, tmpContext
637
				.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME));
638
		context
639
				.addVariable(
640
						ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME,
641
						tmpContext
642
								.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME));
643
		return context;
644
	}
568
}
645
}
(-)Eclipse UI/org/eclipse/ui/internal/handlers/HandlerProxy.java (-10 / +28 lines)
Lines 19-25 Link Here
19
import org.eclipse.core.commands.HandlerEvent;
19
import org.eclipse.core.commands.HandlerEvent;
20
import org.eclipse.core.commands.IHandler;
20
import org.eclipse.core.commands.IHandler;
21
import org.eclipse.core.commands.IHandlerListener;
21
import org.eclipse.core.commands.IHandlerListener;
22
import org.eclipse.core.expressions.EvaluationResult;
22
import org.eclipse.core.expressions.Expression;
23
import org.eclipse.core.expressions.Expression;
24
import org.eclipse.core.expressions.IEvaluationContext;
23
import org.eclipse.core.runtime.CoreException;
25
import org.eclipse.core.runtime.CoreException;
24
import org.eclipse.core.runtime.IConfigurationElement;
26
import org.eclipse.core.runtime.IConfigurationElement;
25
import org.eclipse.core.runtime.IStatus;
27
import org.eclipse.core.runtime.IStatus;
Lines 158-167 Link Here
158
		this.enabledWhenExpression = enabledWhenExpression;
160
		this.enabledWhenExpression = enabledWhenExpression;
159
		this.evaluationService = evaluationService;
161
		this.evaluationService = evaluationService;
160
		if (enabledWhenExpression != null) {
162
		if (enabledWhenExpression != null) {
161
			proxyEnabled = false;
163
			setProxyEnabled(false);
162
			registerEnablement();
164
			registerEnablement();
163
		} else {
165
		} else {
164
			proxyEnabled = true;
166
			setProxyEnabled(true);
165
		}
167
		}
166
	}
168
	}
167
169
Lines 170-176 Link Here
170
	 */
172
	 */
171
	private void registerEnablement() {
173
	private void registerEnablement() {
172
		enablementRef = evaluationService.addEvaluationListener(
174
		enablementRef = evaluationService.addEvaluationListener(
173
				enabledWhenExpression, getEnablementListener(), PROP_ENABLED, null);
175
				enabledWhenExpression, getEnablementListener(), PROP_ENABLED,
176
				null);
177
	}
178
179
	void setEnabledFor(IEvaluationContext context) throws ExecutionException {
180
		if (enabledWhenExpression != null) {
181
			try {
182
				setProxyEnabled(enabledWhenExpression.evaluate(context) == EvaluationResult.TRUE);
183
			} catch (CoreException e) {
184
				throw new ExecutionException(e.getMessage(), e);
185
			}
186
		}
187
	}
188
189
	void setProxyEnabled(boolean enabled) {
190
		proxyEnabled = enabled;
191
	}
192
193
	boolean getProxyEnabled() {
194
		return proxyEnabled;
174
	}
195
	}
175
196
176
	/**
197
	/**
Lines 181-192 Link Here
181
			enablementListener = new IPropertyChangeListener() {
202
			enablementListener = new IPropertyChangeListener() {
182
				public void propertyChange(PropertyChangeEvent event) {
203
				public void propertyChange(PropertyChangeEvent event) {
183
					if (event.getProperty() == PROP_ENABLED) {
204
					if (event.getProperty() == PROP_ENABLED) {
184
						if (event.getNewValue() != null) {
205
						setProxyEnabled(event.getNewValue() == null ? false
185
							proxyEnabled = ((Boolean) event.getNewValue())
206
								: ((Boolean) event.getNewValue())
186
									.booleanValue();
207
										.booleanValue());
187
						} else {
188
							proxyEnabled = false;
189
						}
190
						fireHandlerChanged(new HandlerEvent(HandlerProxy.this,
208
						fireHandlerChanged(new HandlerEvent(HandlerProxy.this,
191
								true, false));
209
								true, false));
192
					}
210
					}
Lines 227-233 Link Here
227
	public final boolean isEnabled() {
245
	public final boolean isEnabled() {
228
		if (enabledWhenExpression != null) {
246
		if (enabledWhenExpression != null) {
229
			// proxyEnabled reflects the enabledWhen clause
247
			// proxyEnabled reflects the enabledWhen clause
230
			if (!proxyEnabled) {
248
			if (!getProxyEnabled()) {
231
				return false;
249
				return false;
232
			}
250
			}
233
			if (isOkToLoad() && loadHandler()) {
251
			if (isOkToLoad() && loadHandler()) {
(-)Eclipse UI/org/eclipse/ui/internal/handlers/HandlerService.java (-6 / +77 lines)
Lines 75-81 Link Here
75
	 * @param evaluationService
75
	 * @param evaluationService
76
	 *            The evaluation service to use; must not be <code>null</code>.
76
	 *            The evaluation service to use; must not be <code>null</code>.
77
	 */
77
	 */
78
	public HandlerService(final ICommandService commandService, 
78
	public HandlerService(final ICommandService commandService,
79
			final IEvaluationService evaluationService) {
79
			final IEvaluationService evaluationService) {
80
		if (commandService == null) {
80
		if (commandService == null) {
81
			throw new NullPointerException(
81
			throw new NullPointerException(
Lines 83-89 Link Here
83
		}
83
		}
84
		this.commandService = commandService;
84
		this.commandService = commandService;
85
		this.handlerAuthority = new HandlerAuthority(commandService);
85
		this.handlerAuthority = new HandlerAuthority(commandService);
86
		this.handlerPersistence = new HandlerPersistence(this, evaluationService);
86
		this.handlerPersistence = new HandlerPersistence(this,
87
				evaluationService);
87
	}
88
	}
88
89
89
	public final IHandlerActivation activateHandler(
90
	public final IHandlerActivation activateHandler(
Lines 225-243 Link Here
225
					ISources.ACTIVE_SHELL_NAME, shell);
226
					ISources.ACTIVE_SHELL_NAME, shell);
226
		}
227
		}
227
	}
228
	}
228
	
229
229
	/**
230
	/**
230
	 * Currently this is a kludge.
231
	 * Currently this is a an internal method to help locate a handler.
231
	 * <p>
232
	 * <p>
232
	 * DO NOT CALL THIS METHOD.
233
	 * DO NOT CALL THIS METHOD.
233
	 * </p>
234
	 * </p>
234
	 * 
235
	 * 
235
	 * @param commandId the command id to check
236
	 * @param commandId
236
	 * @param the context to use for activations
237
	 *            the command id to check
238
	 * @param context
239
	 *            the context to use for activations
237
	 * @since 3.3
240
	 * @since 3.3
238
	 */
241
	 */
239
	public final IHandler findHandler(String commandId,
242
	public final IHandler findHandler(String commandId,
240
			IEvaluationContext context) {
243
			IEvaluationContext context) {
241
		return handlerAuthority.findHandler(commandId, context);
244
		return handlerAuthority.findHandler(commandId, context);
242
	}
245
	}
246
247
	/**
248
	 * Normally the context returned from getCurrentState() still tracks the
249
	 * application state. This method creates a copy and fills it in with the
250
	 * variables that we know about. Currently it does not fill in the active
251
	 * selection.
252
	 * <p>
253
	 * DO NOT CALL THIS METHOD. It is experimental in 3.3.
254
	 * </p>
255
	 * 
256
	 * @return an evaluation context with no parent.
257
	 * @since 3.3
258
	 */
259
	public final IEvaluationContext getContextSnapshot() {
260
		return handlerAuthority.getContextSnapshot();
261
	}
262
263
	/**
264
	 * Execute the command using the provided context. It takes care of finding
265
	 * the correct active handler given the context, and executes with that
266
	 * handler.
267
	 * <p>
268
	 * It currently cannot effect the enablement of the handler.
269
	 * </p>
270
	 * <p>
271
	 * DO NOT CALL THIS METHOD. It is experimental in 3.3.
272
	 * </p>
273
	 * 
274
	 * @param command
275
	 *            the parameterized command to execute
276
	 * @param trigger
277
	 *            the SWT event trigger ... can be null
278
	 * @param context
279
	 *            the evaluation context to run against.
280
	 * @return
281
	 * @throws ExecutionException
282
	 * @throws NotDefinedException
283
	 * @throws NotEnabledException
284
	 * @throws NotHandledException
285
	 * @since 3.3
286
	 * @see #getContextSnapshot()
287
	 */
288
	public final Object executeCommandInContext(
289
			final ParameterizedCommand command, final Event trigger,
290
			IEvaluationContext context) throws ExecutionException,
291
			NotDefinedException, NotEnabledException, NotHandledException {
292
		IHandler oldHandler = command.getCommand().getHandler();
293
294
		IHandler handler = findHandler(command.getId(), context);
295
		boolean enabled = true;
296
		if (handler instanceof HandlerProxy) {
297
			enabled = ((HandlerProxy) handler).getProxyEnabled();
298
		}
299
300
		try {
301
			command.getCommand().setHandler(handler);
302
			if (handler instanceof HandlerProxy) {
303
				((HandlerProxy) handler).setEnabledFor(context);
304
			}
305
306
			return command.executeWithChecks(trigger, context);
307
		} finally {
308
			if (handler instanceof HandlerProxy) {
309
				((HandlerProxy) handler).setProxyEnabled(enabled);
310
			}
311
			command.getCommand().setHandler(oldHandler);
312
		}
313
	}
243
}
314
}

Return to bug 173213