Thursday, March 22, 2012

How to add Action and Menu in Eclipse Toolbar and Menu bar respectively


To add Action and Menu in eclipse workbench toolbar and menu bar, do the following:

1. Add new "Extension" in Extensions tab of plugin.xml file - org.eclipse.ui.actionSets

Adding "actionSets" extension point
2. Create new "actionSet" and provide the id*, label* and set visible to "true".

New"actionSets" properties
3. Right click on "actionSet" and select New -> menu option.

Add new "menu" option
Provide the value of id*, label* as give in the below screenshot.

Field of "path" value is depend on the developer. It may you keep it as blank. If it is blank, then newly created menu will be appeared at the end of the eclipse menu bar. If you want to keep your menu after specific eclipse standard menu item (like: File, Edit, Search, Window, Help etc.) then you have to provide their id in the "path" field. Like below screenshot, I provide "path" value as "run" which means, Sample Code Menu will be appear after "Run" menu.

"menu" properties
4. R-click on "menu" item and select New -> "groupMarker" option. Provide the name* as SampleCode.groupMarker

5. R-click on "actionSet" and create another "menu" item. Provide the id*, label*, path and icon value like below screenshot. "path" value should be [id of the menu item]/[group marker name]

Sample Menu properties
6. R-click on "actionSet" and select New -> action option.

Add new action option
7. After creating a new 'action" provide the respective properties of action like below screenshot.

"action" properties
Provide unique id*, label*, menubarPath (same as Show Menu View menu path value), toolbarPath (like: additions), icon and other properties keep as default.

8. Now, click on class link of this action properties, a New Java Class wizard will be opened. Provide the necessary data like below screenshot and click "Finish" button.

New Java Class wizard for creating new action class
A new java class will be created in "samplecode.source.action" package with default implementation of "IWorkbenchWindowActionDelegate" interface.
Modify the class using the below code snippet.





9. Save all the projects and run the project.

The new Menu will be created as well as a new action in toolbar. If this action is clicked, the Sample View will be visible in the current perspective.


Run the application. Output is highlighted.
10. If we provide this action as "toggle" style and modify this action class with below code snippet, then we can  apply this action as show or hide the Sample View in the current perspective.



And, change the action style property as "toggle" like below screenshot:


Action style property
11. If we run the current project, this action button now work as show/hide the Sample View in the current perspective.


Output window with toggle action button implementation
Now the button will be in toggle mode. If it is checked state, the Sample View will be visible or else it will be hidden.

Attachment: To Download this project click here.

No comments:

Post a Comment