Sometimes I find the need to programmatically invoke an Eclipse wizard. I’ve been using various ways of doing so, but have finally ended up with a more generic function. This will search through all the wizard registries and try to find a wizard with the specified identifier. If found the wizard will be opened. The code is shown below:
public void openWizard(String id) { // First see if this is a "new wizard". IWizardDescriptor descriptor = PlatformUI.getWorkbench() .getNewWizardRegistry().findWizard(id); // If not check if it is an "import wizard". if (descriptor == null) { descriptor = PlatformUI.getWorkbench().getImportWizardRegistry() .findWizard(id); } // Or maybe an export wizard if (descriptor == null) { descriptor = PlatformUI.getWorkbench().getExportWizardRegistry() .findWizard(id); } try { // Then if we have a wizard, open it. if (descriptor != null) { IWizard wizard = descriptor.createWizard(); WizardDialog wd = new WizardDialog(getStandardDisplay() .getActiveShell(), wizard); wd.setTitle(wizard.getWindowTitle()); wd.open(); } } catch (CoreException e) { e.printStackTrace(); } }
I hope you’ll find this snippet useful.
The Eclipse 3.x API shows its age… So much code for so less functionality.
Hi Matthias.
Well it could be that there is a better way that I have not discovered yet. However it is most common to invoke wizards using existing mechanisms. If you want to do it programmatically you normally reference the wizard directly which requires much less code. This method is intentionally quite generic.
Platform UI provides commands (org.eclipse.ui.file.export, org.eclipse.ui.file.import & org.eclipse.ui.newWizard) handlers for invoking the workbench wizards. You can just invoke these commands with correct parameter ids.
Thanks, that is a very useful code snippet. Would it be ok for you if I integrate it into my Plugin Tutorial http://www.vogella.de/articles/EclipsePlugIn/article.html ?
@Author: Yes you can, but in my eyes that would be a bit more laborious and less obvious.
@Lars: Thank you. Please go ahead 🙂
Hi!
I have some questions:
1.)I want to invoke the ecore diagram model wizard. Does anyone know how to find out its id?
2.)I tried to invoke the ecore model wizard using the id “org.eclipse.emf.ecore.presentation.EcoreModelWizardID”. The wizard is found but as soon as the code reaches wd.open() it throws a nullPointerException and I do not know why. Can anybody help me with that?
Thanks in advance!
@Matthias I'm having the exact same problem(no. 2) … did you fix it ?
Thanks! This did exactly what I needed it to!
Can you just tell me the id of New Java Class Wizard.??? I want to open that wizard programatically
-Vallabh
thanks it helped me.
good stuff, only getStandardDisplay() is not found 🙁
Hello, I'm wondering how to call New Project wizard. I don't see the command.
@Matthias A.
Hope this link will help: http://www.eclipse.org/forums/index.php/t/150928/
UMLCreationWizard wizard = new UMLCreationWizard();
// init wizard
wizard.init(PlatformUI.getWorkbench(), new StructuredSelection());
WizardDialog dialog = new WizardDialog(wizard.getShell(), wizard);
dialog.open();
How can I get the ID of File –> New –> Other… wizard?
When I invoke org.eclipse.ui.newWizard it opens Other wizard window but I need to open New Wizard window instead of it.
Any help would be appreciated, thanks
I believe the wizard you’re looking for is the
org.eclipse.ui.internal.dialogs.NewWizard
. I recommend that you install the Eclipse Plug-in Spy. I may help you with such questions. See http://www.eclipse.org/pde/incubator/spy/