Jul 19, 2008

How to create a new File Wizard?

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

2 comments:

Udo said...

Hi,
how can create more than 1 file at once with this API?

Prakash G.R. said...

hmmmmm. This API is aimed in creating a single file. I guess it doesn't support creating multiple files. For that either you create you own wizard or hack the API somehow to support. If I were you, I would opt for the first way