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 :

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

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

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

» API Tooling Tutorial

IntroductionRecently, while fixing a bug, there was a discussion in the emails going on whether adding a new field to an interface in the API would break the binary compatibility to the existing clients. I strongly believed that only adding a method...

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

Subscribe To

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