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 :

» Types of Breakpoints in Eclipse

All of know the importance of the breakpoints during debugging. But do you know apart from setting a breakpoint on a line, there are other ways to set a breakpoint? Lets explore the breakpoints supported by Eclipse.

» Opening an editor without IFile

When we use Eclipse, we are used to the notion of editors operating on a workspace file. But workspace and resources are optional for an RCP application. You might have to invoke an editor on a non-file object like a database record or an in-memory...

» How to open an external file in Eclipse

Background Based on work in SWT () and the native launcher (), the Eclipse IDE now supports opening a file from the command line, or opening a file that has been associated with the Eclipse executable ().

» 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...

» 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...

Subscribe To

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