Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when one c
3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when one c [message #467652] Wed, 09 May 2007 13:51 Go to next message
Eclipse UserFriend
I am using the new 3.3 menu extension point (entries below). I have a
single handler that handles the "record navigation" command. The
command takes a single parameter (an enum) that indicates the navigation
type such as MoveFirst(move to the first record), MoveLast, Add (add a
record) etc. This is in contrast to having 8 different commands and
handlers and implementing the handlers in classes multiple times.

The handler handles the command and uses the parameter to determine
which method to call on an appropriately tagged editor part. When the
right type of editor is active, I need to keep all the record navigation
menu entries visible but dis/enable their states based on the state of
the edited record e.g. the save button should only be enabled when the
record has been modified in some way.

How do I control the enablement state of the visible menu entry on the
screen via programming when I only have one handler handling all of the
commands and I want to keep all of the menu entries visible? The main
record navigation menu entry is only visible when an editor that
implements the interface tag is active but I need to enable at the menu
contribution level as well.

All of the menu/command stuff works fine, I just can't seem to get hold
an IContributionItem that allows me to set the enablement state of a
particular menu entry. IContributionItem does not have a setEnable()
method on it only a isEnabled() method.

The words navigation below refers to record navigation and the concept
of actions below refers to the action of navigation not an IAction object.



========org.eclipse.ui.menu extension point entry
<menuContribution locationURI="menu:org.eclipse.ui.main.menu?after=edit">
<menu id="navigation" label="Navigation">
<command id="navigation.moveFirst"
commandId="navigation.navigate" label="Move First">
<parameter name="action" value="MoveFirst" />
</command>
<command id="navigation.movePrevious"
commandId="navigation.navigate" label="Move Previous">
<parameter name="action" value="MovePrevious" />
</command>
<command id="navigation.moveNext"
commandId="navigation.navigate" label="Move Next">
<parameter name="action" value="MoveNext" />
</command>
<command id="navigation.moveFirst"
commandId="navigation.navigate" label="Move Last">
<parameter name="action" value="MoveLast" />
</command>
<separator name="sep1" visible="true" />
<command id="navigation.save" commandId="navigation.navigate"
label="Save Changes">
<parameter name="action" value="Save" />
</command>
<command id="navigation.add" commandId="navigation.navigate"
label="Add New">
<parameter name="action" value="Add" />
</command>
<separator name="sep2" visible="true" />
<command id="navigation.revert" commandId="navigation.navigate"
label="Revert Changes">
<parameter name="action" value="Revert" />
</command>
<separator name="sep3" visible="true" />
<command id="delete" commandId="navigation.navigate"
label="Delete Current">
<parameter name="action" value="Delete" />
</command>
<visibleWhen>
<with variable="activePart">
<instanceof
value="org.myapp.ui.actions.IRecordNavigationCapable" />
</with>
</visibleWhen>
</menu>
</menuContribution>


================the command
<command
categoryId="navigator"
description="Perform a navigation action."
id="navigation.navigate"
name="Perform Navigation Action"
defaultHandler="org.myapp.ui.actions.RecordNavigationHandler ">
<commandParameter
id="action"
name="Navigation Action"
values="org.myapp.ui.actions.RecordNavigationValues"/>
</command>


===================the handler
public class RecordNavigationHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);

if (part instanceof IRecordNavigationCapable) {
IRecordNavigationCapable p = (IRecordNavigationCapable)part;
String actionParam = event.getParameter("action");
RecordNavigationAction actionEnum =
RecordNavigationAction.valueOf(actionParam);
p.navigate(actionEnum);
}
return null;
}
}

================interface tag
public interface IRecordNavigationCapable {
void navigate(RecordNavigationAction action);
}
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467740 is a reply to message #467652] Thu, 10 May 2007 09:42 Go to previous messageGo to next message
Eclipse UserFriend
This is done by updating the UIElement. Check out IElementUpdater and
org.eclipse.ui.tests.menus.ToggleContextHandler in org.eclipse.ui.tests
(CVS) as an example.

For each "UI" element public void updateElement(UIElement element, Map
parameters) is called with a map of parameters that match the
parameterized commands.

You would extract your parameter from the map and check your enum (so
you know which thing you are updating).

Later,
PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467765 is a reply to message #467740] Thu, 10 May 2007 11:34 Go to previous messageGo to next message
Eclipse UserFriend
I'll look into those parts although at first blush it looked a bit
scary. Ideally, the CommandContributionItem would have a
setEnabled(boolean) method on it because I think you can get the
ContributionItem fairly easily given menu path navigation. You would
have to change the IContributionItem interface as well.

I think this issue only comes up when you reuse handlers. Because the
enabled state is determined by default based on whether a handler is
attached and is enabled, you can toggle the handle attachment to achieve
this effect.


Paul Webster wrote:
> This is done by updating the UIElement. Check out IElementUpdater and
> org.eclipse.ui.tests.menus.ToggleContextHandler in org.eclipse.ui.tests
> (CVS) as an example.
>
> For each "UI" element public void updateElement(UIElement element, Map
> parameters) is called with a map of parameters that match the
> parameterized commands.
>
> You would extract your parameter from the map and check your enum (so
> you know which thing you are updating).
>
> Later,
> PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467768 is a reply to message #467740] Thu, 10 May 2007 11:34 Go to previous messageGo to next message
Eclipse UserFriend
I'll look into those parts although at first blush it looked a bit
scary. Ideally, the CommandContributionItem would have a
setEnabled(boolean) method on it because I think you can get the
ContributionItem fairly easily given menu path navigation. You would
have to change the IContributionItem interface as well.

I think this issue only comes up when you reuse handlers. Because the
enabled state is determined by default based on whether a handler is
attached and is enabled, you can toggle the handle attachment to achieve
this effect.


Paul Webster wrote:
> This is done by updating the UIElement. Check out IElementUpdater and
> org.eclipse.ui.tests.menus.ToggleContextHandler in org.eclipse.ui.tests
> (CVS) as an example.
>
> For each "UI" element public void updateElement(UIElement element, Map
> parameters) is called with a map of parameters that match the
> parameterized commands.
>
> You would extract your parameter from the map and check your enum (so
> you know which thing you are updating).
>
> Later,
> PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467770 is a reply to message #467740] Thu, 10 May 2007 12:30 Go to previous messageGo to next message
Eclipse UserFriend
BTW, I did find a way to get a contribution item but it only returns a
CommandContributionItem which has no setEnabled() interface.


Inside my editor I could do something like:

IMenuManager c = (IMenuManager)getEditorSite().getActionBars()
.getMenuManager().find("navigation");
if(c != null) {
log.info("Found navigation menu manager item correctly");
IContributionItem cc = c.find("delete");
if(cc != null)
log.info("found delete navigation item");
}



Paul Webster wrote:
> This is done by updating the UIElement. Check out IElementUpdater and
> org.eclipse.ui.tests.menus.ToggleContextHandler in org.eclipse.ui.tests
> (CVS) as an example.
>
> For each "UI" element public void updateElement(UIElement element, Map
> parameters) is called with a map of parameters that match the
> parameterized commands.
>
> You would extract your parameter from the map and check your enum (so
> you know which thing you are updating).
>
> Later,
> PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467771 is a reply to message #467740] Thu, 10 May 2007 12:33 Go to previous messageGo to next message
Eclipse UserFriend
I took a deeper look at the recommendation but there is no obvious way
to get to contribution item that somehow understands setEnabled().
UIElement enablement is all based on the handler presence. While
UIElement has some setText() type methods, etc. it lacks setEnabled().

Any other ideas? It looks like I need to create 8 command and handler
subclasses or log this as a feature request.


Paul Webster wrote:
> This is done by updating the UIElement. Check out IElementUpdater and
> org.eclipse.ui.tests.menus.ToggleContextHandler in org.eclipse.ui.tests
> (CVS) as an example.
>
> For each "UI" element public void updateElement(UIElement element, Map
> parameters) is called with a map of parameters that match the
> parameterized commands.
>
> You would extract your parameter from the map and check your enum (so
> you know which thing you are updating).
>
> Later,
> PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467779 is a reply to message #467771] Thu, 10 May 2007 13:33 Go to previous messageGo to next message
Eclipse UserFriend
aappddeevv wrote:
> I took a deeper look at the recommendation but there is no obvious way
> to get to contribution item that somehow understands setEnabled().
> UIElement enablement is all based on the handler presence. While
> UIElement has some setText() type methods, etc. it lacks setEnabled().
>
> Any other ideas? It looks like I need to create 8 command and handler
> subclasses or log this as a feature request.

Sorry, I went back and re-read your post.

You are correct, it won't work for enablement which is a property of the
command itself by way of the current active handler (and all of your
parameterized commands are really just pointing at one command). My
UIElement example is about updating properties of the UI element
(MenuItem or ToolItem) like label, icon, things that can be rendered.


You can (and probably should) log a feature request (although I'm not
sure what can be done because of the architecture). In any event it
won't be looked at until 3.4 (June 2008)

In the short term you will need to create multiple commands. You can
reuse your handler, however, by making it an IExecutableExtension and
then for each time you specify the handler class append a :MoveFirst or
so. This will be passed to your handler's setInitializationData(*) method.

BTW: Save, Add, Revert, and Delete are are not navigation actions and
deserve their own command anyway :-)

PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467812 is a reply to message #467779] Thu, 10 May 2007 17:53 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for the feedback and I will look into the approach you mention.

True they are not navigation events :-)

I am thinking in the spirit of the BindingNavigator that .Net has for
navigating between records and doing things to lists of records in the
interface.


Paul Webster wrote:
> aappddeevv wrote:
>> I took a deeper look at the recommendation but there is no obvious way
>> to get to contribution item that somehow understands setEnabled().
>> UIElement enablement is all based on the handler presence. While
>> UIElement has some setText() type methods, etc. it lacks setEnabled().
>>
>> Any other ideas? It looks like I need to create 8 command and handler
>> subclasses or log this as a feature request.
>
> Sorry, I went back and re-read your post.
>
> You are correct, it won't work for enablement which is a property of the
> command itself by way of the current active handler (and all of your
> parameterized commands are really just pointing at one command). My
> UIElement example is about updating properties of the UI element
> (MenuItem or ToolItem) like label, icon, things that can be rendered.
>
>
> You can (and probably should) log a feature request (although I'm not
> sure what can be done because of the architecture). In any event it
> won't be looked at until 3.4 (June 2008)
>
> In the short term you will need to create multiple commands. You can
> reuse your handler, however, by making it an IExecutableExtension and
> then for each time you specify the handler class append a :MoveFirst or
> so. This will be passed to your handler's setInitializationData(*) method.
>
> BTW: Save, Add, Revert, and Delete are are not navigation actions and
> deserve their own command anyway :-)
>
> PW
Re: 3.3: org.eclipse.ui.menus and parameterized commands - how to en/disable the visible menu when o [message #467815 is a reply to message #467812] Thu, 10 May 2007 20:44 Go to previous message
Eclipse UserFriend
BTW, so far after looking into IExecutableExtension as well as
manipulating the execution context/evaluation context, I am frustrated
that it so hard to changed the enablement state in a menu in a straight
forward, programmatic way.

After more browsing, another way appears could work is to use the "test"
expression which when combined with the <with variable="activeEditor">
may allow me to use an intercepting class that knows about special
methods on my editor subclass reflecting the state of the "navigation"
menu options. This way, I can decouple my editor class from handlers
and commands altogether which is what I want.

Ouch!


aappddeevv wrote:
> Thanks for the feedback and I will look into the approach you mention.
>
> True they are not navigation events :-)
>
> I am thinking in the spirit of the BindingNavigator that .Net has for
> navigating between records and doing things to lists of records in the
> interface.
>
>
> Paul Webster wrote:
>> aappddeevv wrote:
>>> I took a deeper look at the recommendation but there is no obvious way
>>> to get to contribution item that somehow understands setEnabled().
>>> UIElement enablement is all based on the handler presence. While
>>> UIElement has some setText() type methods, etc. it lacks setEnabled().
>>>
>>> Any other ideas? It looks like I need to create 8 command and
>>> handler subclasses or log this as a feature request.
>>
>> Sorry, I went back and re-read your post.
>>
>> You are correct, it won't work for enablement which is a property of
>> the command itself by way of the current active handler (and all of
>> your parameterized commands are really just pointing at one command).
>> My UIElement example is about updating properties of the UI element
>> (MenuItem or ToolItem) like label, icon, things that can be rendered.
>>
>>
>> You can (and probably should) log a feature request (although I'm not
>> sure what can be done because of the architecture). In any event it
>> won't be looked at until 3.4 (June 2008)
>>
>> In the short term you will need to create multiple commands. You can
>> reuse your handler, however, by making it an IExecutableExtension and
>> then for each time you specify the handler class append a :MoveFirst
>> or so. This will be passed to your handler's setInitializationData(*)
>> method.
>>
>> BTW: Save, Add, Revert, and Delete are are not navigation actions and
>> deserve their own command anyway :-)
>>
>> PW
Previous Topic:RCP Based Standalone Help
Next Topic:Plug-in Development
Goto Forum:
  


Current Time: Thu May 15 13:13:35 EDT 2025

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

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

Back to the top