forked from eclipse-platform/eclipse.platform.ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Menu Contributions Example migrated from wiki
- Loading branch information
Showing
15 changed files
with
1,779 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
Oops, something went wrong.