Thursday, December 20, 2007

Eclipse RCP, add new wizard to the workbench File menu and toolbar at the root level

Here is how you add your wizard as an option under the File > New menu, and toolbar drop down button.

If you were like me, you should have been easily be able to add a new wizard, but when you added it to the file menu, it added your wizard under the Others... category.

Here is how you get it at the root level, i.e. the same level as the new File and Folder wizards.


Step 1: Add the newWizards extension and your new wizard under it.
Step 2: Add the perspectivesExtensions extension and a newWizardShortcut extension under it.

Here is how the plugin.xml looks:

<plugin>

<extension id="application" point="org.eclipse.core.runtime.applications">
<application>
<run class="edu.pitt.dbmi.odie.Application">
</run>
</application>
</extension>
<extension point="org.eclipse.ui.perspectives">
<perspective name="RCP Perspective" class="edu.pitt.dbmi.odie.Perspective" id="edu.pitt.dbmi.odie.perspective">
</perspective>
</extension>
<extension point="org.eclipse.ui.newWizards">
<wizard class="edu.pitt.dbmi.odie.NewDocumentCollectionWizard"
id="edu.pitt.dbmi.odie.NewDocumentCollectionWizard"
name="Document Collection">
</wizard>
</extension>
<extension point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension targetID="edu.pitt.dbmi.odie.perspective">
<newWizardShortcut id="edu.pitt.dbmi.odie.NewDocumentCollectionWizard">
</newWizardShortcut>
</perspectiveExtension>
</extension>

</plugin>


Step 3: create the actions and add it to the menu and toolbar.
You can edit the ApplicationActionBarAdvisor.java. You will need to override the makeActions, fillMenuBar and fillCoolBar methods.

Here are how they look:

IWorkbenchAction quitAction;
private IWorkbenchAction newWizardDropDownAction;
private IContributionItem newWizardMenu;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}


protected void makeActions(IWorkbenchWindow window) {
newWizardDropDownAction = ActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
register(newWizardDropDownAction);

newWizardMenu = ContributionItemFactory.NEW_WIZARD_SHORTLIST.create(window);
}

protected void fillMenuBar(IMenuManager menuBar) {


MenuManager menu = new MenuManager("File", IWorkbenchActionConstants.M_FILE);

{
// create the New submenu, using the same id for it as the New action
MenuManager newMenu = new MenuManager("New", "new");
newMenu.add(this.newWizardMenu);
menu.add(newMenu);
}

menuBar.add(menu);

}

@Override
protected void fillCoolBar(ICoolBarManager coolBar) {
ToolBarManager toolbar = new ToolBarManager(SWT.FLAT);
toolbar.add(newWizardDropDownAction);

coolBar.add(toolbar);
}


Note, Step 2 can also be done in code. Just add this line to the createInitialLayout method of your perspective

public void createInitialLayout(IPageLayout layout) {
layout.addNewWizardShortcut("edu.pitt.dbmi.odie.NewDocumentCollectionWizard");
}

IMPORTANT: Donot forget to clear workspace data to see your changes.