Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Using commands in a view toolbar
Using commands in a view toolbar [message #659936] Wed, 16 March 2011 09:09 Go to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
Hi all,

I've converted the actions of my view into commands.
Very happy with the result Smile but.....

The handler for these commands is always enabled (no expressions for enabled or visible).
But when another view in the same perspective becomes enabled, they became disabled.
I don't like this behaviour, Confused because I have first to activate a view in order to click a toolbar button, instead of clicking it directly.

The handlers a registered with the IHandlerService obtained from IViewSite.

This effect doesn't happen if I use the IHandlerService from PlatformUI.getWorkbench(), but the handlers from different views conflict with each other.

Is this behaviour by design?
Can be avoided?

Thanks in advance for any clue.

David
Re: Using commands in a view toolbar [message #659992 is a reply to message #659936] Wed, 16 March 2011 14:01 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 03/16/2011 05:09 AM, David Pérez wrote:
>
> Is this behaviour by design?
> Can be avoided?

This behaviour is by design. If you activate a handler from you view,
it is only active while your view is the active part.

If you really only have one handler for the command and it should always
be enabled, you can contribute it using defaultHandler in the
org.eclipse.ui.commands extension ... if you are generating commands for
your views.

The when your handler is executed, you use
org.eclipse.ui.handlers.HandlerUtil.getActivePart(ExecutionE vent) (or
checked) to get your view.

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: Using commands in a view toolbar [message #660006 is a reply to message #659992] Wed, 16 March 2011 14:24 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
Thanks Paul.

This behaviour is good for commands in the global toolbar or menus, but not so good for commands inserted in a view toolbar.

If the handler is unique for a view, your solution is ok, but this isn't my case.

I think I'll switch back to actions.

David

[quote title=Paul Webster wrote on Wed, 16 March 2011 10:01]On 03/16/2011 05:09 AM, David Pérez wrote:
>
> Is this behaviour by design?
> Can be avoided?

This behaviour is by design. If you activate a handler from you view,
it is only active while your view is the active part.

If you really only have one handler for the command and it should always
be enabled, you can contribute it using defaultHandler in the
org.eclipse.ui.commands extension ... if you are generating commands for
your views.

PW
Re: Using commands in a view toolbar [message #660011 is a reply to message #659992] Wed, 16 March 2011 14:39 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
Paul, do you think this is the best behaviour for toolbar-specific items?

In order to execute a command in a toolbar view, you have to activate the view and then click the command.

For instance, the standard views supplied with JDT doesn't exhibit this behaviour.
I suppose, commands aren't used (or aren't registered with the IViewSite).

Paul Webster wrote on Wed, 16 March 2011 10:01


This behaviour is by design. If you activate a handler from you view,
it is only active while your view is the active part.


Re: Using commands in a view toolbar [message #660207 is a reply to message #660011] Thu, 17 March 2011 11:19 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 03/16/2011 10:39 AM, David Pérez wrote:
> Paul, do you think this is the best behaviour for toolbar-specific items?
>
> In order to execute a command in a toolbar view, you have to activate
> the view and then click the command.

Yes, that's the correct behaviour. You cannot act within a view (from a
UI point of view) without activating it. So acting within a view will
put the system in the correct state.

ex: View A contributes a refresh handler, View B contributes a refresh
handler. When a view contributes a handler, you only want that handler
active when the view is active.

If it makes sense for your command to be active all the time, then you
would use plugin.xml to contribute it. It would use the application
context to extract the active part (if that's a good behaviour) or get
the active window and find the part it cares about.

> I suppose, commands aren't used (or aren't registered with the IViewSite).

Right. Programmatically activating handlers within a view will restrict
those handlers to that view. Handlers that apply at the window level
are usually contributed through plugin.xml, as are most of the platform
handlers (if we can).

There is a simplification of enablement that's part of the architecture
of 3.x that I think can be improved (it is actually an attribute of each
global command instance). Right now everybody operates against the
global application state. That makes it hard to localize certain kinds
of information, like your view handler really only cares about the state
provided by your view.

In Eclipse 4, the view toolbar checks enablement by asking its view, as
opposed to in Eclipse 3.x where it goes to the global application state.

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: Using commands in a view toolbar [message #661361 is a reply to message #660207] Thu, 24 March 2011 11:34 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
Finally, I have solved what I consider a problem. Razz
The solution is to use this class instead of a CommandContributionItem to populate the view items.

public class LocalHandlerContributionItem extends Comman dContributionItem {
    protected IHandler handler;

    public LocalHandlerContributionItem(CommandContribu tionItemParameter contributionParameters, IHandler handle r) {
        super(contributionParameters);
        this.handler = handler;
    }

    public boolean isEnabled() {
        if (handler instanceof IHandler2) {
            ((IHandler2) handler).setEnabled(me nuService.getCurrentState());
        }
        return handler.isEnabled();
    }

    @Override
    protected void handleWidgetSelection(Event event)  {
        Command cmd = command.getCommand();
        IHandler oldHandler = cmd.getHandler();
        cmd.setHandler(handler);
        command.getCommand().setHandler(handler);
        try {
            super.handleWidgetSelection(event);
        } finally {
            cmd.setHandler(oldHandler);
        }
    }
}

You have to pass the handler that you have registered previously with IHandlerService#activateHandler()

The trick is to use the supplied handler, instead of the current handler of the command.

I hope this is useful for other people having the same problem like me.

This is the last problem I had to use commands everywhere, instead of actions.

David

Paul Webster wrote on Thu, 17 March 2011 07:19
On 03/16/2011 10:39 AM, David Pérez wrote:
> Paul, do you think this is the best behaviour for toolbar-specific items?
>
> In order to execute a command in a toolbar view, you have to activate
> the view and then click the command.

Yes, that's the correct behaviour. You cannot act within a view (from a
UI point of view) without activating it. So acting within a view will
put the system in the correct state.

ex: View A contributes a refresh handler, View B contributes a refresh
handler. When a view contributes a handler, you only want that handler
active when the view is active.

If it makes sense for your command to be active all the time, then you
would use plugin.xml to contribute it. It would use the application
context to extract the active part (if that's a good behaviour) or get
the active window and find the part it cares about.

> I suppose, commands aren't used (or aren't registered with the IViewSite).

Right. Programmatically activating handlers within a view will restrict
those handlers to that view. Handlers that apply at the window level
are usually contributed through plugin.xml, as are most of the platform
handlers (if we can).

There is a simplification of enablement that's part of the architecture
of 3.x that I think can be improved (it is actually an attribute of each
global command instance). Right now everybody operates against the
global application state. That makes it hard to localize certain kinds
of information, like your view handler really only cares about the state
provided by your view.

In Eclipse 4, the view toolbar checks enablement by asking its view, as
opposed to in Eclipse 3.x where it goes to the global application state.

PW

Re: Using commands in a view toolbar [message #661922 is a reply to message #661361] Mon, 28 March 2011 14:16 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 03/24/2011 07:35 AM, David Pérez wrote:
> Finally, I have solved what I consider a problem. :p The solution is to
> use this class instead of a CommandContributionItem to populate the view
> items.
>

Just remember you'll possibly run into problems with this in the 4.x SDK
as the handler will be chosen by the system and the method of
determining the state to use is also different.

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/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: Using commands in a view toolbar [message #662308 is a reply to message #661922] Wed, 30 March 2011 08:35 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
Thanks Paul for your valuable advice.

I hope in e4 I won't need this kind of patches, otherwise, I'll redo my patches.

Here is an improved version, that handles correctly enabling expressions:

public class LocalHandlerContributionItem extends Comman dContributionItem {
    protected IHandler handler;
    protected IEvaluationService evalServ;
    protected Expression expr;
    protected boolean exprTrue = true;
    protected IHandlerListener handlerListener = new  IHandlerListener() {
        @Override
        public void handlerChanged(HandlerEvent h andlerEvent) {
            update();
        }
    };

    public LocalHandlerContributionItem(CommandContribu tionItemParameter params, IHandler handler, Expression  expr) {
        super(params);
        this.handler = handler;
        handler.addHandlerListener(handlerListener);
        if (expr != null) {
            this.expr = expr;
            evalServ = SWTUtil.getService(para ms.serviceLocator, IEvaluationService.class);
            evalServ.addEvaluationListener(expr,  new IPropertyChangeListener() {
                public void propertyChange (PropertyChangeEvent event) {
                    exprTrue = (Boolea n)event.getNewValue();
                    update();
                }
            }, "enabled");
        }
    }

    @Override
    public boolean isEnabled() {
        if (!exprTrue) {
            return false;
        }
        if (handler instanceof IHandler2) {
            ((IHandler2)handler).setEnabled(menu Service.getCurrentState());
        }
        return handler.isHandled() && han dler.isEnabled();
    }

    @Override
    protected void handleWidgetSelection(Event event)  {
        Command cmd = command.getCommand();
        IHandler oldHandler = cmd.getHandler();
        cmd.setHandler(handler);
        try {
            super.handleWidgetSelection(event);
        } finally {
            cmd.setHandler(oldHandler);
        }
    }

    @Override
    public void dispose() {
        if (handlerListener != null) {
            handler.removeHandlerListener(handle rListener);
            handlerListener = null;
        }
        super.dispose();
    }
}

David

Paul Webster wrote on Mon, 28 March 2011 10:16
On 03/24/2011 07:35 AM, David Pérez wrote:
> Finally, I have solved what I consider a problem. Razz The solution is to
> use this class instead of a CommandContributionItem to populate the view
> items.
>

Just remember you'll possibly run into problems with this in the 4.x SDK
as the handler will be chosen by the system and the method of
determining the state to use is also different.

PW

Re: Using commands in a view toolbar [message #662366 is a reply to message #662308] Wed, 30 March 2011 11:49 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 03/30/2011 04:35 AM, David Pérez wrote:
> @Override
> public void dispose() {
> if (handlerListener != null) {
> handler.removeHandlerListener(handle rListener);
> handlerListener = null;
> }
> super.dispose();
> }
> }


Glad your solution is working for you. I just have one more comment.
You should save the ref from addEvaluationListener(*) and remove it in
your dispose, just like the handler listener. Otherwise it looks good.

Later,
Paul


--
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/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: Using commands in a view toolbar [message #665329 is a reply to message #660207] Thu, 14 April 2011 10:59 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
I'm not the only one who's complaining about the standard behaviour.

Here is another way of solving this:

http://www.google.de/codesearch/p?hl=de#qFWjsxJ50LM/de.walwa re.ecommons.uimisc/src/de/walware/ecommons/ui/actions/Handle rContributionItem.java&q=STYLE_PULLDOWN&sa=N&cd= 8&ct=rc

I've discovered it by accident.

I find very interesting to integrate this kind of functionality into the code.

Paul Webster wrote on Thu, 17 March 2011 07:19
On 03/16/2011 10:39 AM, David Pérez wrote:
> Paul, do you think this is the best behaviour for toolbar-specific items?
>
> In order to execute a command in a toolbar view, you have to activate
> the view and then click the command.

Yes, that's the correct behaviour. You cannot act within a view (from a
UI point of view) without activating it. So acting within a view will
put the system in the correct state.
[/url]

Re: Using commands in a view toolbar [message #665363 is a reply to message #665329] Thu, 14 April 2011 12:41 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 04/14/2011 06:59 AM, David Pérez wrote:
> I'm not the only one who's complaining about the standard behaviour.
>
> Here is another way of solving this:
>
> http://www.google.de/codesearch/p?hl=de#qFWjsxJ50LM/de.walwa re.ecommons.uimisc/src/de/walware/ecommons/ui/actions/Handle rContributionItem.java&q=STYLE_PULLDOWN&sa=N&cd= 8&ct=rc
>
>
> I've discovered it by accident.
>
> I find very interesting to integrate this kind of functionality into the
> code.

I suspect that won't work reliably ... it certainly won't work in
Eclipse 4 and I can't guess what it would do in Eclipse 4.1 (where there
is such a thing as local state).

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/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Previous Topic:Local history for custom ediotr
Next Topic:get file location using IFile and IProject
Goto Forum:
  


Current Time: Sat Apr 27 01:09:22 GMT 2024

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

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

Back to the top