Over a million developers have joined DZone.

Contributing Workbench Wizards Through Commands

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

You must have been used this JDT toolbar quite often.

The New Class tool item is contributed as an Action in plugin.xml (id "org.eclipse.jdt.ui.actions.NewTypeDropDown"), and then the Menu on the Action is contributed via code. In this tip, let us see how to do the same via command framework and without any code. The first step is to add a ToolBar and then the New Wizard command to it:

<extension point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.eclipsetips.commands.workbenchWizards.jdtToolbar"
label="Java Creation Elements">
<command
commandId="org.eclipse.ui.newWizard"
style="push">
</command>
</toolbar>
</menuContribution>
</extension>

Now we get:

Two issues with this. First, what we get is simply the New Wizard Dialog, (The one which you get when you press Ctrl+N). We want the New Java Class wizard. To make that happen, we need to pass on a parameter - which New Wizard to show to the user. Second, its just a toolbar item. There is no drop down menu associated with it. To have that we need to change the style of the command from 'push' to 'pulldown'.

            <command
commandId="org.eclipse.ui.newWizard"
style="pulldown">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewClassCreationWizard">
</parameter>
</command>

Now we can get the New Class Wizard when we click the tool item, still nothing is there in the dropdown menu of it. The command framework assigns menu:<id-of-the-toolitem> as the uri for menu contributions. So if we create a menu contribution to that URI:

 <menuContribution
allPopups="false"
locationURI="menu:com.eclipsetips.commands.workbenchWizards.jdtNewClass">
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewClassCreationWizard">
</parameter>
</command>
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewAnnotationCreationWizard">
</parameter>
</command>
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewInterfaceCreationWizard">
</parameter>
</command>
</menuContribution>
</extension>

then we get this:

The beauty of this is any other plugin can now contribute to this drop down. Consider some other plugin which wants to add another command to this drop down. It can also use the same URI:

      <menuContribution
allPopups="false"
locationURI="menu:com.eclipsetips.commands.workbenchWizards.jdtNewClass">
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewPackageCreationWizard">
</parameter>
</command>
</menuContribution>

There is however one annoying thing about this is that the toolbar is shown in always. You can limit the visibility using visibleWhen and expressions. Say if we want to see this only in Java Perspective:

<command
commandId="org.eclipse.ui.newWizard"
id="com.eclipsetips.commands.workbenchWizards.jdtNewClass"
style="pulldown">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewClassCreationWizard">
</parameter>
<visibleWhen
checkEnabled="false">
<with
variable="activeWorkbenchWindow.activePerspective">
<equals
value="org.eclipse.jdt.ui.JavaPerspective">
</equals>
</with>
</visibleWhen>
</command>

If you have multiple toolitems in the toolbar and want the visibleWhen on the entire toolbar, you have to wait till this bug gets fixed.

You must have been used this JDT toolbar quite often.

The New Class tool item is contributed as an Action in plugin.xml (id "org.eclipse.jdt.ui.actions.NewTypeDropDown"), and then the Menu on the Action is contributed via code. In this tip, let us see how to do the same via command framework and without any code. The first step is to add a ToolBar and then the New Wizard command to it:

<extension point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.eclipsetips.commands.workbenchWizards.jdtToolbar"
label="Java Creation Elements">
<command
commandId="org.eclipse.ui.newWizard"
style="push">
</command>
</toolbar>
</menuContribution>
</extension>

Now we get:

Two issues with this. First, what we get is simply the New Wizard Dialog, (The one which you get when you press Ctrl+N). We want the New Java Class wizard. To make that happen, we need to pass on a parameter - which New Wizard to show to the user. Second, its just a toolbar item. There is no drop down menu associated with it. To have that we need to change the style of the command from 'push' to 'pulldown'.

            <command
commandId="org.eclipse.ui.newWizard"
style="pulldown">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewClassCreationWizard">
</parameter>
</command>

Now we can get the New Class Wizard when we click the tool item, still nothing is there in the dropdown menu of it. The command framework assigns menu:<id-of-the-toolitem> as the uri for menu contributions. So if we create a menu contribution to that URI:

 <menuContribution
allPopups="false"
locationURI="menu:com.eclipsetips.commands.workbenchWizards.jdtNewClass">
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewClassCreationWizard">
</parameter>
</command>
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewAnnotationCreationWizard">
</parameter>
</command>
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewInterfaceCreationWizard">
</parameter>
</command>
</menuContribution>
</extension>

then we get this:

The beauty of this is any other plugin can now contribute to this drop down. Consider some other plugin which wants to add another command to this drop down. It can also use the same URI:

      <menuContribution
allPopups="false"
locationURI="menu:com.eclipsetips.commands.workbenchWizards.jdtNewClass">
<command
commandId="org.eclipse.ui.newWizard"
style="push">
<parameter
name="newWizardId"
value="org.eclipse.jdt.ui.wizards.NewPackageCreationWizard">
</parameter>
</command>
</menuContribution>

There is however one annoying thing about this is that the toolbar is shown in always. You can limit the visibility using visibleWhen and expressions. Say if we want to see this only in Java Perspective:

  Download

 

From http://blog.eclipse-tips.com/2010/12/contributing-workbench-wizards-thru.html

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}