Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3)
icon5.gif  Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #490529] Fri, 09 October 2009 01:58 Go to next message
Gary Miguel is currently offline Gary MiguelFriend
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 #490716 is a reply to message #490529] Fri, 09 October 2009 19:12 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

To get a core expression re-evaluated (enabledWhen, activeWhen, visibleWhen) one of the variables in the application context has to change. That's why you notice it is re-evaluated when the selection changes, the default variable is the current selection.

If you really are in 3.3, you would need to (probably manually) add your own ISourceProvider with an appropriate variable to the correct places, and then use it in your expressions. When it changes, your expressions will get re-evaluated.

That's why org.eclipse.ui.services.IEvaluationService.requestEvaluation (String) was added in 3.4. Properties weren't covered by variable change events. This method will request all expressions using the propertyName be re-evaluated.

PW


Re: Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #490856 is a reply to message #490716] Sun, 11 October 2009 22:34 Go to previous messageGo to next message
Gary Miguel is currently offline Gary MiguelFriend
Messages: 15
Registered: October 2009
Junior Member
Thanks for your help, Paul.

To get it working, I had to create a new SourceProvider and register it with both the HandlerService and the EvaluationService. When there was a change to the state, I called fireSourceChanged(int sourcePriority, Map) with sourcePriority == 0.

Gary
icon5.gif  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 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
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
Re: Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #632311 is a reply to message #631780] Tue, 12 October 2010 14:23 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

The system works on a global application context, so your it depends on
your handler being active and enabled in that context.

however, you can try
org.eclipse.ui.handlers.IHandlerService.executeCommandInCont ext(ParameterizedCommand,
Event, IEvaluationContext)

There's a chance that might work.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #632442 is a reply to message #632311] Tue, 12 October 2010 23:28 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
Thanks Paul, I'll give that a try. I saw that method but as my command is not parameterized, I thought it inappropriate.

Craig
Re: Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #632847 is a reply to message #632442] Thu, 14 October 2010 13:34 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Craig Foote wrote:
> Thanks Paul, I'll give that a try. I saw that method but as my command
> is not parameterized, I thought it inappropriate.

:-) That's just new ParameterizedCommand(command, null)

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Force re-evaluation of handler enabledWhen expressions from Handler (eclipse 3.3) [message #632996 is a reply to message #632847] Thu, 14 October 2010 23:02 Go to previous message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
Sweet!
Previous Topic:Eclipse freezes on show view
Next Topic:Add menu to team in popup
Goto Forum:
  


Current Time: Thu Apr 25 14:03:56 GMT 2024

Powered by FUDForum. Page generated in 0.04043 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top