Home » Eclipse Projects » Eclipse Platform » Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3)
Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #490529] |
Fri, 09 October 2009 01:58 |
Gary Miguel Messages: 15 Registered: October 2009 |
Junior Member |
|
|
I am posting here because the closest thing I've found to an answer so far was posted on the RCP newsgroup, in a thread titled Command handler enabled state with dynamic data model. Sorry if it should go somewhere else.
I have zoom in and zoom out, each with a toolbar button, command, and handler. The handler is delcared in plugin.xml and it has an enabledWhen condition.
When I zoom in or out, I want to re-evaluate that enabledWhen expression and update the enabled state of the handler. I've attempted to do this like so:
each of the zoomIn and zoomOut handlers adds a listener to the canvas so that on any zoom event, fireHandlerChanged(HandlerEvent) gets called (See the code I've included at the end of the comment).
However, this doesn't actually get the enabledWhen condition to be re-evaluated, and the HandlerProxy.setProxyEnabled(boolean) doesn't get called.
I noticed that selection changes will force the condition to be re-evaluated, and if in the canvasZoomed listener method I have a canvas.fireSelectionChanged(), that does what I want it to do. Unfortunately, this would violate my project's abstractions, so I can't have an Canvas.fireSelectionChanged method.
I saw in the old thread I referenced that maybe I could use org.eclipse.ui.services.IEvaluationService.requestEvaluation (String), but that doesn't exist in Eclipse 3.3.
I also foresee another problem, even if I get the above working:
if zoomOut starts disabled, it can never execute, so it won't get a chance to add a listener to the canvas. Thus I need a way for the zoomIn handler to add a listener that can re-enable zoomOut, and vice-versa.
Many thanks for any help for a first-time poster.
Gary
public Object execute(ExecutionEvent arg0) throws ExecutionException {
Canvas canvas = getCanvas();
if (listener == null) {
listener = new CanvasListener() {
@Override
public void canvasZoomed(DrawCanvasEvent event) {
Object[] listeners = getListeners();
HandlerEvent handlerEvent = new HandlerEvent(ZoomInHandler.this, true, false);
for (int i = 0; i < listeners.length; i++) {
final IHandlerListener listener = (IHandlerListener) listeners[i];
listener.handlerChanged(handlerEvent);
}
ZoomInHandler.this.fireHandlerChanged(handlerEvent);
}
};
}
canvas.addCanvasListener(listener);
return null;
}
|
|
| | |
Re: Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #631780 is a reply to message #490529] |
Fri, 08 October 2010 17:48 |
|
Is there a way to programmatically enable a handler declared with an enabledWhen clause (using default selection variable), e.g.
<extension point="...handlers"
<handler class="MyHandler" commandId="my.command.id"
<enabledWhen>
<and>
<count="+">
</count>
<iterate>
<adapt type="MyOtherClass">
</adapt>
</iterate>
</and>
</enabledWhen>
</handler>
</extension>
In code:
ICommandService cService = (ICommandService) Platform.getWorkbench().getService(ICommandService.class);
Command myCommand = cService.getCommand("my.command.id");
List<Object> objectsToEvaluate = new ArrayList<Object>();
objectsToEvaluate.add(new ObjectThatAdapts());
ISelection selection = new StructuredSelection(objectsToEvaluate);
EvaluationContext context = new EvaluationContext(null, selection);
context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
ExecutionEvent event = new ExecutionEvent(myCommand, Collections.EMPTY_MAP, null, context);
myCommand.executeWithChecks(event);
This works when the handler was declared under the commands extension as the default handler but not when it was moved out to a handlers extension with the enabledWhen clause. Is there something different I can do to programmatically enable the handler now that it has an enabledWhen clause?
Thanks,
Craig
|
|
| | | | |
Goto Forum:
Current Time: Thu Apr 25 14:03:56 GMT 2024
Powered by FUDForum. Page generated in 0.04043 seconds
|