Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Enable/Disable menu item in eclipse plugin
Enable/Disable menu item in eclipse plugin [message #895275] Thu, 12 July 2012 10:42 Go to next message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
I've made a pop up menu with one menu item, I want to enable it only when I do a right click on a tree item of a certain class type otherwise disable it.

How can I achieve this ?
Re: Enable/Disable menu item in eclipse plugin [message #895279 is a reply to message #895275] Thu, 12 July 2012 11:26 Go to previous messageGo to next message
Alexander Haag is currently offline Alexander HaagFriend
Messages: 119
Registered: July 2009
Senior Member
Hi,

if your menu item is defined in the plugin.xml you should add an <enabledWhen>-condition to your commandhandler.
If you defined a 'default'-handler in your command-definition you must delete it there and define the handler using 'org.eclipse.ui.handlers'-extension point instead where you can add the enabledWhen-condition.

condition should look like

<enabledWhen>
   <with
      variable="selection">
        <and>
          <count
                value="1">
          </count>
          <iterate>
             <instanceof
                   value="..yourclass...">
              </instanceof>
          </iterate>
       </and>
   </with>
</enabledWhen>


Hope this helps
Greets Alex
Re: Enable/Disable menu item in eclipse plugin [message #895288 is a reply to message #895279] Thu, 12 July 2012 11:44 Go to previous messageGo to next message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
Alexander Haag wrote on Thu, 12 July 2012 07:26
Hi,

if your menu item is defined in the plugin.xml you should add an <enabledWhen>-condition to your commandhandler.
If you defined a 'default'-handler in your command-definition you must delete it there and define the handler using 'org.eclipse.ui.handlers'-extension point instead where you can add the enabledWhen-condition.

condition should look like

<enabledWhen>
   <with
      variable="selection">
        <and>
          <count
                value="1">
          </count>
          <iterate>
             <instanceof
                   value="..yourclass...">
              </instanceof>
          </iterate>
       </and>
   </with>
</enabledWhen>


Hope this helps
Greets Alex



Thanks Alex for your reply,

I've made what you've told but I still get the menu item enabled even when I select on empty space.
What I've made with menu and handler is attached and what I get when I do right click on empty space.

Am I missing something ?
  • Attachment: 1.png
    (Size: 2.69KB, Downloaded 322 times)
  • Attachment: 2.png
    (Size: 5.81KB, Downloaded 272 times)
  • Attachment: 3.png
    (Size: 3.21KB, Downloaded 291 times)
Re: Enable/Disable menu item in eclipse plugin [message #895289 is a reply to message #895288] Thu, 12 July 2012 11:45 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You have to set ifEmpty to false on the iterate, no?

PW


Re: Enable/Disable menu item in eclipse plugin [message #895291 is a reply to message #895289] Thu, 12 July 2012 11:47 Go to previous messageGo to next message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
I did it, but I get the same behavior
Re: Enable/Disable menu item in eclipse plugin [message #895293 is a reply to message #895291] Thu, 12 July 2012 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

What do you mean you right-click in "empty space" What does the rest of your view look like when you do that?

Is that view correctly registering a selection provider when it is created?

PW


Re: Enable/Disable menu item in eclipse plugin [message #895295 is a reply to message #895293] Thu, 12 July 2012 11:54 Go to previous messageGo to next message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
It is basically a tree which have some tree items and the menu I'mtalking about is a pop up menu, I want to enable the button only when I do select a certain items (items with certain class)

and disable the button when I select any other item or I select the anywhere in my view other than the tree items.

but the case here, is when I do right cliclk on any tree item or any blank space I get the button enabled.
Re: Enable/Disable menu item in eclipse plugin [message #895298 is a reply to message #895295] Thu, 12 July 2012 11:58 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

1) did you register the tree viewer as the selection provider for that view?

2) What enabledWhen clause are you using, exactly?

3) Let's make sure it works when you right-click on a tree node that's the incorrect type ...

Also, right-clicking in an empty part of a tree widget has no effect on selection, it would only matter if you've cleared the selection some other way.

PW


Re: Enable/Disable menu item in eclipse plugin [message #895301 is a reply to message #895298] Thu, 12 July 2012 12:07 Go to previous messageGo to next message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
Yes, I did register the tree viewer as the selection provider.

I don't know what you mean by enablewhen clause
but this is the part from plugin.xml if it could help
<extension
         point="org.eclipse.ui.handlers">
      <handler
            class="copycmd"
            commandId="copycmd">
         <enabledWhen>
            <with
                  variable="selection">
               <and>
                  <count
                        value="1">
                  </count>
                  <iterate
                        ifEmpty="false"
                        operator="and">
                     <instanceof
                           value="document">
                     </instanceof>
                  </iterate>
               </and>
            </with>
         </enabledWhen>
      </handler>
   </extension>


The menu item I want to enable or disable has dynamic menu items that show up or now show up according to selection of certain tree items in the tree.

so, the behavior is:

When I do right click on item of incorrect type I get the copy menu enabled ! but when I do click on it, it gets disabled!

and for the correct type , if the copy menu is disabled from the first selection m it gets enabled when I do click on the copy menu.

This is weird behavior.

[Updated on: Thu, 12 July 2012 12:08]

Report message to a moderator

Re: Enable/Disable menu item in eclipse plugin [message #895363 is a reply to message #895301] Thu, 12 July 2012 15:03 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Does your view implement setFocus()?

PW


Re: Enable/Disable menu item in eclipse plugin [message #895405 is a reply to message #895363] Thu, 12 July 2012 17:39 Go to previous messageGo to next message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
Yes
public void setFocus() {
		Viewer.getControl().setFocus();
	}
Re: Enable/Disable menu item in eclipse plugin [message #895406 is a reply to message #895405] Thu, 12 July 2012 17:45 Go to previous message
becks m is currently offline becks mFriend
Messages: 59
Registered: June 2012
Member
Should I implement a class for this command handler even if I don't need it ?
Previous Topic:How can I add submenus for pop up menu programatically?
Next Topic:Plugin unable to find class - ClassNotFoundException
Goto Forum:
  


Current Time: Fri Mar 29 06:05:20 GMT 2024

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

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

Back to the top