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.

 

More articles :

» Progress Bars in Eclipse UI

In the "" article, I said the top most mistake is running long running operations in the UI thread. Assuming that you are running it in a non-UI thread, how to show the progress of the execution? Obviously thru progress monitors. But how many...

» How to add TrayItem in Eclipse RCP application?

In an application like Yahoo Messenger or Skype, you don't want to quit the application when the main window is closed. Most of these applications will move themselves to System Tray when the main window is closed and clicking on the Tray icon would...

» A glimpse at the Faceted Project Framework

If you haven't noticed, the Faceted Project Framework from WTP is now proposed as a separate project. I like the framework very much, probably because my very first Eclipse Plug-in is simply a WTP Facet :-) When I wrote that plug-in, there was no...

» A short tutorial on Intro/Welcome

 Welcome page in Eclipse provides you a unique way to introduce your product to the new users. Its very flexible and can be extended by other plugins. You can create the entire Welcome view by yourself by extending the org.eclipse.ui.intro and...

» Extending FilteredItemsSelectionDialog

In a previous tip, you have seen various . One thing which was not explained in it was FilteredItemsSelectionDialog, as it deserves a tip on its own. In this tip, I'll explain how to extend that class.

Subscribe To

Unless stated, all the text contents of this site is available under Eclipse Public License