Recipe 13.2 Customizing a Wizard

     

13.2.1 Problem

You want to customize a template you created for a plug-in that includes a wizard.

13.2.2 Solution

Edit the wizard's support file ( SampleNewWizard.java in the example created in the previous recipe) to customize the code.

13.2.3 Discussion

In the plug-in created in the previous recipe, the wizard creates files with the default name document.new , inserting default text into those files. This wizard is supported by the two files SampleNewWizard.java and SampleNewWizardPage.java . In SampleNewWizard.java , the code begins by creating the page the wizard will show:

 public class SampleNewWizard extends Wizard implements INewWizard {     private SampleNewWizardPage page;     private ISelection selection;     /**      * Constructor for SampleNewWizard.      */     public SampleNewWizard( ) {         super( );         setNeedsProgressMonitor(true);     }          /**      * Adding the page to the wizard.      */  public void addPages( ) {   page = new SampleNewWizardPage(selection);   addPage(page);   }  .         .         . 

In the createControl method of SampleNewWizardPage.java , the wizard's appearance is created. As you can see here, the SWT controlslabels, text controls, and so onare added and positioned:

 public void createControl(Composite parent) {     Composite container = new Composite(parent, SWT.NULL);     GridLayout layout = new GridLayout( );     container.setLayout(layout);     layout.numColumns = 3;     layout.verticalSpacing = 9;     Label label = new Label(container, SWT.NULL);     label.setText("&Container:");     containerText = new Text(container, SWT.BORDER  SWT.SINGLE);     GridData gd = new GridData(GridData.FILL_HORIZONTAL);     containerText.setLayoutData(gd);     containerText.addModifyListener(new ModifyListener( ) {         public void modifyText(ModifyEvent e) {             dialogChanged( );         }     });     Button button = new Button(container, SWT.PUSH);     button.setText("Browse...");     button.addSelectionListener(new SelectionAdapter( ) {         public void widgetSelected(SelectionEvent e) {             handleBrowse( );         }     });     label = new Label(container, SWT.NULL);     label.setText("&File name:");     fileText = new Text(container, SWT.BORDER  SWT.SINGLE);     gd = new GridData(GridData.FILL_HORIZONTAL);     fileText.setLayoutData(gd);     fileText.addModifyListener(new ModifyListener( ) {         public void modifyText(ModifyEvent e) {             dialogChanged( );         }     });     initialize( );     dialogChanged( );     setControl(container); } 

If you want to customize the wizard's page, do it here. This is where that page is created; if you want a different appearance and different controls, this is the place to configure things as you want them.

The actual work that the wizard does for your plug-in begins when the Finish button is clicked in the wizard's page, calling the doFinish method in SampleNewWizard.java . We'll customize this code to insert our own text into the newly created document. That method runs in a worker thread, and it begins by finding the path of the file to create and creates that file this way; if you want to create files in another location, this is the spot to customize:

 private void doFinish(     String containerName,     String fileName,     IProgressMonitor monitor)     throws CoreException {      // create a sample file      monitor.beginTask("Creating " + fileName, 2);      IWorkspaceRoot root = ResourcesPlugin.getWorkspace( ).getRoot( );      IResource resource = root.findMember(new Path(containerName));      if (!resource.exists( )  !(resource instanceof IContainer)) {          throwCoreException("Container \"" + containerName +          "\" does not exist.");     }     IContainer container = (IContainer) resource;     final IFile file = container.getFile(new Path(fileName));         .         .         . 

To set the contents of the new file, the code next calls a method named openContentStream and uses that method to fill the file:

 try {     InputStream stream = openContentStream( );     if (file.exists( )) {         file.setContents(stream, true, true, monitor);     } else {         file.create(stream, true, monitor);     }     stream.close( );               .               .               . 

We'll customize the openContentStream method to change the default text put into the document.new file created by the wizard. Here's what that method looks like now:

 private InputStream openContentStream( ) {     String contents =      "This is the initial file contents for *.new file that should                be word-sorted in the Preview page of the multi-page editor";     return new ByteArrayInputStream(contents.getBytes( )); } 

Change that to this, where we're installing our own custom text:

 private InputStream openContentStream( ) {     String contents =      "  Welcome to document.new";  return new ByteArrayInputStream(contents.getBytes( )); } 

Making this change customizes the wizard for our purposes. In Recipe 13.3, we will customize the editor this plug-in displays.

13.2.4 See Also

Recipe 13.1 on creating plug-ins that support wizards and editors; Recipe 13.3 on customizing an editor.



Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net