Skip to content

Commit

Permalink
Menu Contributions Example migrated from wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
vogella committed Feb 8, 2024
1 parent 6d0c74a commit 4030e25
Show file tree
Hide file tree
Showing 15 changed files with 1,779 additions and 13 deletions.
26 changes: 13 additions & 13 deletions docs/Menu_Contributions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ A (hopefully) growing list of menu contribution examples.

| Example | comments |
| --- | --- |
| [Menu Contributions/Dropdown Command](/Menu_Contributions/Dropdown_Command "Menu Contributions/Dropdown Command") | Dropdown tool items can have their menus filled in using menu contributions |
| [Menu Contributions/Problems View Example](/Menu_Contributions/Problems_View_Example "Menu Contributions/Problems View Example") | An example showing how the Problems View might be converted |
| [Menu Contributions/Populating a dynamic submenu](/Menu_Contributions/Populating_a_dynamic_submenu "Menu Contributions/Populating a dynamic submenu") | A menu contribution to populate a Problems View dynamic submenu |
| [Menu Contributions/Toggle Mark Occurrences](/Menu_Contributions/Toggle_Mark_Occurrences "Menu Contributions/Toggle Mark Occurrences") | Placing the toggle mark occurrences button |
| [Menu Contributions/Toggle Button Command](/Menu_Contributions/Toggle_Button_Command "Menu Contributions/Toggle Button Command") | Contribute a toggle state menu item thru commands |
| [Menu Contributions/Radio Button Command](/Menu_Contributions/Radio_Button_Command "Menu Contributions/Radio Button Command") | Similar to updating toggle state, you can create radio buttons using menu contributions |
| [Menu Contributions/Update checked state](/Menu_Contributions/Update_checked_state "Menu Contributions/Update checked state") | The active handler can update the checked state (and other attributes) of its button |
| [Menu Contributions/Search Menu](/Menu_Contributions/Search_Menu "Menu Contributions/Search Menu") | Adding the Java Search options to the Search menu |
| [Menu Contributions/IFile objectContribution](/Menu_Contributions/IFile_objectContribution "Menu Contributions/IFile objectContribution") | A menu contribution for context menus when the selection is an IFile |
| [Menu Contributions/TextEditor viewerContribution](/Menu_Contributions/TextEditor_viewerContribution "Menu Contributions/TextEditor viewerContribution") | A menu contribution for the text editor context menu |
| [Menu Contributions/Widget in a toolbar](/Menu_Contributions/Widget_in_a_toolbar "Menu Contributions/Widget in a toolbar") | A menu contribution adding a control into the main toolbar |
| [Menu Contributions/RCP removes the Project menu](/Menu_Contributions/RCP_removes_the_Project_menu "Menu Contributions/RCP removes the Project menu") | An RCP application removes the Project menu. Note: this will probably not be in 3.3 |
| [Menu Contributions/Workbench wizard contribution](/Menu_Contributions/Workbench_wizard_contribution "Menu Contributions/Workbench wizard contribution") | Contributing workbench wizards to Menu |
| [Menu Contributions/Dropdown Command](./Menu_Contributions/Dropdown_Command.md "Menu Contributions/Dropdown Command") | Dropdown tool items can have their menus filled in using menu contributions |
| [Menu Contributions/Problems View Example](./Menu_Contributions/Problems_View_Example.md "Menu Contributions/Problems View Example") | An example showing how the Problems View might be converted |
| [Menu Contributions/Populating a dynamic submenu](./Menu_Contributions/Populating_a_dynamic_submenu.md "Menu Contributions/Populating a dynamic submenu") | A menu contribution to populate a Problems View dynamic submenu |
| [Menu Contributions/Toggle Mark Occurrences](./Menu_Contributions/Toggle_Mark_Occurrences.md "Menu Contributions/Toggle Mark Occurrences") | Placing the toggle mark occurrences button |
| [Menu Contributions/Toggle Button Command](./Menu_Contributions/Toggle_Button_Command.md "Menu Contributions/Toggle Button Command") | Contribute a toggle state menu item thru commands |
| [Menu Contributions/Radio Button Command](./Menu_Contributions/Radio_Button_Command.md "Menu Contributions/Radio Button Command") | Similar to updating toggle state, you can create radio buttons using menu contributions |
| [Menu Contributions/Update checked state](./Menu_Contributions/Update_checked_state.md "Menu Contributions/Update checked state") | The active handler can update the checked state (and other attributes) of its button |
| [Menu Contributions/Search Menu](./Menu_Contributions/Search_Menu.md "Menu Contributions/Search Menu") | Adding the Java Search options to the Search menu |
| [Menu Contributions/IFile objectContribution](./Menu_Contributions/IFile_objectContribution.md "Menu Contributions/IFile objectContribution") | A menu contribution for context menus when the selection is an IFile |
| [Menu Contributions/TextEditor viewerContribution](./Menu_Contributions/TextEditor_viewerContribution.md "Menu Contributions/TextEditor viewerContribution") | A menu contribution for the text editor context menu |
| [Menu Contributions/Widget in a toolbar](./Menu_Contributions/Widget_in_a_toolbar.md "Menu Contributions/Widget in a toolbar") | A menu contribution adding a control into the main toolbar |
| [Menu Contributions/RCP removes the Project menu](./Menu_Contributions/RCP_removes_the_Project_menu.md "Menu Contributions/RCP removes the Project menu") | An RCP application removes the Project menu. Note: this will probably not be in 3.3 |
| [Menu Contributions/Workbench wizard contribution](./Menu_Contributions/Workbench_wizard_contribution.md "Menu Contributions/Workbench wizard contribution") | Contributing workbench wizards to Menu |

Menu XML
========
Expand Down
119 changes: 119 additions & 0 deletions docs/Menu_Contributions/Dropdown_Command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
Menu Contributions/Dropdown Command
===================================

You can create dropdown commands using menu contributions, and then use multiple menu contributions to create the dropdown menu.

Contents
--------

* [1 Command Definition](#Command-Definition)
* [2 Handler](#Handler)
* [3 Menu Contribution](#Menu-Contribution)
* [3.1 Toolbar declaration](#Toolbar-declaration)
* [3.2 Dropdown menu declaration](#Dropdown-menu-declaration)

Command Definition
------------------

Any command can be used as a toolbar dropdown ... the command itself is not aware of its dropdown rendering. If it were to need that information, 2 common ways of sharing it are:

1. Create a parameterized command. The commands inserted in the dropdown menu would specify the parameter, and the dropdown tool item command would not.
2. Back your handler with a model and rely on that information

This command declaration defines a parameter:



<extension point="org.eclipse.ui.commands">
<category id="z.ex.dropdown.category1" name="DropDown Examples">
</category>
<command categoryId="z.ex.dropdown.category1" defaultHandler="z.ex.dropdown.internal.DropDownHandler"
id="z.ex.dropdown.command1" name="Drop">
<commandParameter id="z.ex.dropdown.msg" name="Message" optional="true">
</commandParameter>
</command>
</extension>
<extension point="org.eclipse.ui.commandImages">
<image commandId="z.ex.dropdown.command1" icon="icons/change_obj.gif">
</image>
</extension>




I've thrown in a default image for fun.

Handler
-------

The command example includes a default handler, which is common for simple global commands. The handler needs to check for the parameter and then do its stuff. Use org.eclipse.core.commands.AbstractHandler to use its default methods for most of the IHandler interface.

public class DropDownHandler extends AbstractHandler {
private static final String PARM_MSG = "z.ex.dropdown.msg";

public Object execute(ExecutionEvent event) throws ExecutionException {
String msg = event.getParameter(PARM_MSG);
if (msg == null) {
System.out.println("No message");
} else {
System.out.println("msg: " + msg);
}
return null;
}
}




As with all handlers, you can extract most of the workbench useful information out of the ExecutionEvent using HandlerUtil.

Menu Contribution
-----------------

Dropdown declarations come in two parts, the tool item declaration and a separate dropdown menu declaration.

### Toolbar declaration

You are just placing the command in the toolbar.



<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
<toolbar id="z.ex.dropdown.toolbar2">
<command commandId="z.ex.dropdown.command1" id="z.ex.dropdown.toolbar.command1" style="pulldown"
tooltip="Send them a message">
</command>
</toolbar>
</menuContribution>
</extension>




You can use an existing toolbar id or create a new toolbar to hold the command (as this example did).

### Dropdown menu declaration

Now you can provide one or more menu contributions to the dropdown menu. Here is an example of the main one:



<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="menu:z.ex.dropdown.toolbar.command1">
<command commandId="z.ex.dropdown.command1" label="Msg - hi" style="push">
<parameter name="z.ex.dropdown.msg" value="Hello">
</parameter>
</command>
<separator name="additions" visible="false">
</separator>
<command commandId="z.ex.dropdown.command1" label="Msg - bye" style="push">
<parameter name="z.ex.dropdown.msg" value="Goodbye">
</parameter>
</command>
</menuContribution>
</extension>




113 changes: 113 additions & 0 deletions docs/Menu_Contributions/IFile_objectContribution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
Menu Contributions/IFile objectContribution
===========================================


IFile object contribution
=========================

We also have to provide object contributions (which in the past were scoped by objectClass).

Here's an example from one of our plugin.xml:

<objectContribution adaptable="true"
objectClass="org.eclipse.core.resources.IFile"
nameFilter="*.xml"
id="org.eclipse.jdt.internal.ui.javadocexport.JavadocWizard">
<visibility>
<objectState name="contentTypeId"
value="org.eclipse.ant.core.antBuildFile" />
</visibility>
<action label="Create Javadoc"
class="org.eclipse.jdt.internal.ui.CreateJavadocActionDelegate"
enablesFor="1" id="LaunchJavadocWizard"/>
</objectContribution>

**enablesFor** is now a property of the active handler, not the visible GUI element.

Menus
-----

There will be a reserved popup ID, "org.eclipse.ui.popup.any" that will allow contributions to any popup menu.

<extension point="org.eclipse.core.expressions.definitions">
<definition id="org.eclipse.ui.example.antFile">
<iterate ifEmpty="false">
<adapt type="org.eclipse.core.resources.IFile">
<test property="org.eclipse.core.resources.name" value="*.xml"/>
<test property="org.eclipse.core.resources.contentTypeId" value="org.eclipse.ant.core.antBuildFile"/>
</adapt>
</iterate>
</definition>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:org.eclipse.ui.popup.any">
<command commandId="org.eclipse.jdt.ui.launchJavadocWizard" id="LaunchJavadocWizard" label="Create Javadoc" style="push">
<visibleWhen checkEnabled="false">
<or>
<with variable="activeMenuSelection">
<reference definitionId="org.eclipse.ui.example.antFile"/>
</with>
<with variable="activeMenuEditorInput">
<reference definitionId="org.eclipse.ui.example.antFile"/>
</with>
</or>
</visibleWhen>
</command>
</menuContribution>
</extension>


The default variable for visibleWhen/activeWhen/enabledWhen expressions is **selection**. But it's better to be specific and use `<with variable="selection".../>` if that's what you need.

Menus API
---------

Here is a similar example programmatically.

public static void addFileContribution() {
final IMenuService menuService = (IMenuService) PlatformUI
.getWorkbench().getService(IMenuService.class);
// an expression that walks the selection looking for objectclasses
final ObjectClassExpression ifileExpression = new ObjectClassExpression(
"org.eclipse.core.resources.IFile");
 
final ImageDescriptor postIcon = AbstractUIPlugin
.imageDescriptorFromPlugin("org.eclise.ui.tests",
"icons/full/elcl16/post_wiki.gif");
final ImageDescriptor loadIcon = AbstractUIPlugin
.imageDescriptorFromPlugin("org.eclise.ui.tests",
"icons/full/elcl16/load_wiki.gif");
AbstractContributionFactory factory = new AbstractContributionFactory(
"popup:org.eclipse.ui.popup.any?after=additions") {
public void createContributionItems(IMenuService menuService,
List additions) {
CommandContributionItem item = new CommandContributionItem(
"org.eclipse.ui.examples.wiki.post",
"org.eclipse.ui.examples.wiki.post", null, postIcon,
null, null, null, "P", null,
CommandContributionItem.STYLE_PUSH);
menuService.registerVisibleWhen(item, ifileExpression);
additions.add(item);
 
item = new CommandContributionItem(
"org.eclipse.ui.examples.wiki.load",
"org.eclipse.ui.examples.wiki.load", null, loadIcon,
null, null, null, "L", null,
CommandContributionItem.STYLE_PUSH);
menuService.registerVisibleWhen(item, ifileExpression);
additions.add(item);
}
 
public void releaseContributionItems(IMenuService menuService,
List items) {
}
};
menuService.addContributionFactory(factory);
}

The location of org.eclipse.ui.popup.any specifies any context menu, and the expression ties it to a specific objectClass. Using the new expression syntax you can make your conditions more complex.

You can set your visibleWhen expression on each item as you create it.

In **3.3M6** registerVisibleWhen(*) method might be changing.

35 changes: 35 additions & 0 deletions docs/Menu_Contributions/Populating_a_dynamic_submenu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Menu Contributions/Populating a dynamic submenu
===============================================

Add a dynamic submenu to the ProblemView menu
=============================================

In [Menu Contributions/Problems View Example](./Menu_Contributions/Problems_View_Example.md "Menu Contributions/Problems View Example") we added 2 dynamic menus. You then have to extend the abstract [CompoundContributionItem](http://help.eclipse.org/latest/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/actions/CompoundContributionItem.html) class in your provided class.

<menu id="org.eclipse.ui.views.problems.groupBy.menu"
label="%ProblemView.GroupBy.label"
mnemonic="%ProblemView.GroupBy.mnemonic">
<dynamic class="org.eclipse.ui.views.markers.internal.GroupByItems"
id="org.eclipse.ui.views.problems.groupBy.items"/>
</menu>

When your menu is populated, you'll have your getContributionItems() method called:

protected IContributionItem\[\] getContributionItems() {
IContributionItem\[\] list = new IContributionItem\[2\];
Map parms = new HashMap();
parms.put("groupBy", "Severity");
list\[0\] = new CommandContributionItem(null,
"org.eclipse.ui.views.problems.grouping",
parms, null, null, null, "Severity", null,
null, CommandContributionItem.STYLE_PUSH);
 
parms = new HashMap();
parms.put("groupBy", "None");
list\[1\] = new CommandContributionItem(null,
"org.eclipse.ui.views.problems.grouping",
parms, null, null, null, "None", null, null,
CommandContributionItem.STYLE_PUSH);
return list;
}

Loading

0 comments on commit 4030e25

Please sign in to comment.