Problem is simple. Implement a INewWizard for creating some file type in your application. For this problem, I have seen people start creating the WizardPage on their own. But luckily there is a reusable WizardPage which is specifically meant for this - WizardNewFileCreationPage. In this tip, lets see how to use the class. Lets assume that we need to create a new properties file with the extension .config First we need to create a sub class of WizardNewFileCreationPage.

public class NewConfigFileWizardPage extends WizardNewFileCreationPage {

public NewConfigFileWizardPage(IStructuredSelection selection) {
super("NewConfigFileWizardPage", selection);
setTitle("Config File");
setDescription("Creates a new Config File");
setFileExtension("config"); }
}

@Override
protected InputStream getInitialContents() {
try {
return Activator.getDefault().getBundle().getEntry("/resources/newFileContents.config").openStream();
} catch (IOException e) {
return null; // ignore and create empty comments
}
}
}

The constructor sets the initial selection, wizard page title & description and the extension of the file to be created. Not just extension, you can even set the initial name for the file also using the setFileName() method. If you want to create some default contents for this file, override the getInitialContents(). I've taken the initial contents from a file, but you can have your own logic there. WizardPage is done, lets see the code for Wizard:

public class NewConfigFileWizard extends Wizard implements INewWizard {

private IStructuredSelection selection;
private NewConfigFileWizardPage newFileWizardPage;
private IWorkbench workbench;

public NewConfigFileWizard() {
setWindowTitle("New Config File");
}

@Override
public void addPages() {
newFileWizardPage = new NewConfigFileWizardPage(selection);
addPage(newFileWizardPage);
}

@Override
public boolean performFinish() {
IFile file = newFileWizardPage.createNewFile();
if (file != null)
return true;
else
return false;
}

public void init(IWorkbench workbench, IStructuredSelection selection) {
this.workbench = workbench;
this.selection = selection;
}
}

Nothing fancy in this. Just call the wizardPage's createNewFile() method when Finish button is pressed. Thats it. Now what are the advantages that we get by reusing WizardNewFileCreationPage rather than the one hand crafted by you?

  • Live error notification if a file already exists in the selected folder for the name you typed
  • It understand the selection in the Package Explorer/Navigator and selects it for you
  • If the parent folder structure you have entered is not present, it will automatically create it for you
  • Allows you to link to a different file (in this case your getInitialContents() will not be called)
  • Undo/Redo support - You can undo/redo the file creation

Now next time you want to create a new file, don't reinvent the wheel. Just use the WizardNewFileCreationPage. The above code works just fine. Only thing is that the file is not opened in an editor. So the user will have no clue of whether its properly created or not. So in the performFinish() method, open the newly created file in an editor using one of the IDE.openEditor() methods

More articles :

» Opening an Editor programmatically

When you click on a file in the Package Explorer or Navigator, the file will open the associated editor. If you are looking for a way to do the same action through code, this tip is for you.

» Making good Eclipse RCP apps for Mac

Eclipse isn't a perfect app on Mac platform. Right from unzipping to "install" to not being packaged as a single .app file, its far from being accepted as a perfect application. But beyond these things, Eclipse does provides some support for Mac...

» Creating the defacto RCP Mail application

  In many of the RCP related tips in this website and other sites, the starting point would be this one liner "Create the RCP Mail application". Instead of repeatedly this step in those tips, I've put the steps in this item. Step 1: Open the "New"...

» Reload your plugins without restarting Eclipse

When you are developing Eclipse plugins, sometimes its annoying that the changes in the plugin.xml won't reflect immediately. You need to restart the target Eclipse to see the changes. This will be painful if you are playing with trial-n-error stuff...

» Adding a new editorAction for Orion

In case you have not heard it yet,  is the new Web IDE from Eclipse. In a blog entry , I said that this one is going to stand out from the crowd and going to rule the world. Why can't the other Web IDEs do that? Because, Orion also would be walking...

Subscribe To

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