Well, I've had a painful time working out how to make visible/not
visible and enable/disable menus supplied to the workbench by a plugin
given certain conditions. I'm posting this in the hope it might save
someone some time when attempting to do the same -and for others to
hopefully correct me if I've gone astray (if you read to the end of this
post, you will *know* I have). Unfortunately searching the newsgroup
does reveal some posts requesting similar help, but they are not
answered or I've found some of the replies do not seem to work.
Background:
-----------
I have a plugin ("testplugin"). In my actionset ("testactionset") I
have a view ("testview") and a menu ("testmenu") and an action related
to the menu ("testaction"). When "testview" comes up, I want the
"testmenu" to be visible on the workbench menu (similar to if you select
the java perspective or cvs perspective in Eclipse, you get some new
menu options unique to that perspective or view). Likewise if
"testview" is closed, I want the "testmenu" menu to disappear.
Additionally, I want to sometimes disable or enable the "testmenu"
option based on some other logic -e.g. another menu option was pressed
or a logical condition was met meaning my "testmenu" is disabled.
Assumptions
-----------
You've added the necessary menus and related actions already. You have
know the relationship between actionsets, actions, menus, etc.
The Role of the Plugin.xml
--------------------------
Editing the plugin.xml will allow you to add (i.e. configure) the menu
and action items and set the initial states of the menus. From reading
the documentation, enablements seemed the way to go!
HOWEVER: enablements are mostly(see caveat below this paragraph) a "one
time" deal of being read and initializing the menus. This is contrary
to the help documentation (see http://help.eclipse.org/help31/index.jsp,
Platform Plug-in Developer Guide, Programmers Guide, Advanced Workbench
Concepts, Boolean expressions and action filters) which can be
(easily!!) read so that you might think an enablement condition may be
set to govern visibility and enablement as the application is used.
Caveat here: I did not test using selection criteria, but I did try this
using an enablement with a systemProperty, pluginState and objectState
(yes, I did implement the IActionFilter interface in my action class
there!) and indeed confirmed these values had changed by logging.
I'm sure there are some Eclipse developers out there who are shaking
their heads and saying "But of course...!". Let me just say, I didn't
find it to be obviously stated! A simple sentence in the help about
this could have saved me alot of time. Here is a post that made me
first realize this:
http://dev.eclipse.org/newslists/news.eclipse.platform.rcp/msg05661.html
(Many thanks to Nick Edgar on this).
Making a Menu Visible or Not Visible Depending on Which View is Open
--------------------------------------------------------------------
Initially I wanted to make my "testmenu" menu visibility associated with
a perspective, but in the end I associated the menu visibility to my
"testview" view (and of course you can add other dependent views as well
to effectively build up a perspective anyway). I associated a view to
menu visibility by doing the following:
i. in the plugin.xml for my "testplugin", add a
actionSetPartAssociations extension.
ii. Add a new actionSetPartAssociation to this. Set the target id to
appopriate actionset (e.g. "testactionset").
iii. Add a new part to the setpartassociation, and set the id to the
view id we wish to relate the menu and action to (e.g. "testview").
This will mean that whenever the "testview" is open, the menu "testmenu"
related to the action "testaction" is visible, and likewise is not
visible when the "testview" is closed.
Here is a snippet of my plugin.xml file for this (testview definition
excluded for brevity):
<!-- defining the actionset with action and menu -->
<extension point="org.eclipse.ui.actionSets">
<actionSet label="testactionset" id="testactionset">
<action label="testaction option" class="my.code.Testaction"
style="push" menubarPath="file/additions" id="testaction">
</action>
<menu label="testmenu option" path="additions" id="testmenu"/>
</actionSet>
</extension>
<!-- defining the part association -->
<extension point="org.eclipse.ui.actionSetPartAssociations">
<actionSetPartAssociation targetID="testactionset">
<part id="testview"/>
</actionSetPartAssociation>
</extension>
Making a Menu Enabled or Disabled Depending on a Programmatical Condition
-------------------------------------------------------------------------
This was pretty easy, though I think I'm not doing it the easy way -I
just couldn't find the appropriate method to interrogate the workbench
contents to obtain all the Action ids (if anyone reads this far, I'm
open to suggestions and an example on this one). I'm still working on
this -at the moment I am populating a Map of Action ids and IAction
objects when the IWorkbenchWindowActionDelegate class's run(IAction
action) method is invoked..... the IAction is added to the Map if it
does not already exist.... ugh, not the way to do it I know! Anyway,
once I have references to the IAction objects, I can then easily enable
or disable them programmatically based on a condition.
Please feel free to correct/help/add to this.