Eclipse Search

Loading

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

4 comments:

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

    ReplyDelete
  2. 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

    ReplyDelete
  3. Hi,

    I have a problem with this bit of code:

    IFile file = newFileWizardPage.createNewFile();
    if (file != null)
    return true;
    else
    return false;

    When I place breakpoints on the return true and return false statements, I notice that they never get invoked. Thus I believe that it never leaves createNewFile(). It does not give an error, it just stops and does nothing.
    I am stuck on this bit of code, can someone explain me what happens? and how to solve it.

    greetz

    B.

    ReplyDelete
  4. @B,
    that means an exception is thrown in createNewFile() method. You are potentially hitting an NPE/SWTException I guess. Try debugging into that code put that line of code inside a try-catch block

    ReplyDelete