Disabling a context-menu action [message #63614] |
Thu, 05 June 2003 08:07  |
Eclipse User |
|
|
|
Originally posted by: n.czempin.printcom.deutschepost.de
I want to disable an action depending on the selection in the viewer.
For some reason, it doesn't work.
When initializing my view, I'm using this to add the menu listener:
[..]
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(final IMenuManager manager) {
System.out.println("****menu about to show!");
AbstractAPSTreeView.this.fillContextMenu(manager);
}
});
[..]
in fillContextMenu I do this:
[..]
System.out.println("action 1 enabled? " +
this.action1.isEnabled());
manager.add(this.action1);
[..]
In the action I do this:
protected boolean updateSelection(IStructuredSelection selection) {
this.setEnabled(false);
System.out.println("ReadyLBAction.updateSelection: set enabled
false");
if (!super.updateSelection(selection)) {
return false;
}
System.out.println("**********Selection changed: " + selection);
final Object obj = ((IStructuredSelection)
selection).getFirstElement();
System.out.println("this(action).isEnabled: " + this.isEnabled());
if (obj == null) {
return true;
}
TreeObject to = (TreeObject) obj;
System.out.println("this(action).isEnabled: " + this.isEnabled());
if (to.getName().startsWith("incoming: ")) {
this.setEnabled(true);
System.out.println(
"ReadyLBAction.updateSelection: set enabled true");
}
return true;
}
And my console shows this:
[after the selection]
ReadyLBAction.updateSelection: set enabled false
this action: de.printcom.psi.gui.actions.ReadyLBAction@7b40fe
**********Selection changed: [ready: PB_XYZ_1 Thu Jun 05 13:59:16 CEST
2003]
this(action).isEnabled: false
[now I click the right mouse button]
****menu about to show!
starting fillContextMenu
action 2 enabled? true
action 2: de.printcom.psi.gui.actions.ReadyLBAction@7b40fe
********
So some[thing, body] is changing the status of the action in between the
updateSelection and the menuListener.
What's happening?
How can I disable a menu item depending on my selection??
TIA
|
|
|
|
Re: Disabling a context-menu action [message #63702 is a reply to message #63614] |
Thu, 05 June 2003 08:38  |
Eclipse User |
|
|
|
Originally posted by: n.czempin.printcom.deutschepost.de
Nicolai Czempin wrote:
> I want to disable an action depending on the selection in the viewer.
> For some reason, it doesn't work.
okay, my thinking was too complicated. And my vision impaired :-)
javadoc for SelectionListenerAction:
updateSelection protected boolean updateSelection(IStructuredSelection
selection)
Updates this action in response to the given selection.
The SelectionListenerAction implementation of this method returns true.
Subclasses may extend to react to selection changes; however, if the super
method returns false, the overriding method must also return false.
Parameters:
selection - the new selection
Returns:
true if the action should be enabled for this selection, and false
otherwise
I only read the main part, not the "returns" part. It should make clear
that the return value of this function resolves whether the action is
enabled or not.
If anyone is interested, here's what works:
>protected boolean updateSelection(IStructuredSelection selection) {
> if (!super.updateSelection(selection)) {
> return false;
> }
> final Object obj = ((IStructuredSelection)selection).getFirstElement();
> if (obj == null) {
** return false;
> }
> TreeObject to = (TreeObject) obj;
** return (to.getName().startsWith("incoming: "));
>}
I found this by looking at org.eclipse.ui.actions.AddTaskAction
in the org.eclipse.ui.views plug-in
Thanx for your patience.
|
|
|
Powered by
FUDForum. Page generated in 0.04703 seconds