KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > $packageName$ > $wizardClassName$


1 package $packageName$;
2
3 import org.eclipse.jface.viewers.IStructuredSelection;
4 import org.eclipse.jface.wizard.Wizard;
5 import org.eclipse.ui.INewWizard;
6 import org.eclipse.ui.IWorkbench;
7 import org.eclipse.core.runtime.*;
8 import org.eclipse.jface.operation.*;
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.core.resources.*;
13 import org.eclipse.core.runtime.CoreException;
14 import java.io.*;
15 import org.eclipse.ui.*;
16
17 /**
18  * This is a sample new wizard. Its role is to create a new file
19  * resource in the provided container. If the container resource
20  * (a folder or a project) is selected in the workspace
21  * when the wizard is opened, it will accept it as the target
22  * container. The wizard creates one file with the extension
23  * "$extension$". If a sample multi-page editor (also available
24  * as a template) is registered for the same extension, it will
25  * be able to open it.
26  */

27
28 public class $wizardClassName$ extends Wizard implements INewWizard {
29     private $wizardPageClassName$ page;
30     private ISelection selection;
31
32     /**
33      * Constructor for $wizardClassName$.
34      */

35     public $wizardClassName$() {
36         super();
37         setNeedsProgressMonitor(true);
38     }
39     
40     /**
41      * Adding the page to the wizard.
42      */

43
44     public void addPages() {
45         page = new $wizardPageClassName$(selection);
46         addPage(page);
47     }
48
49     /**
50      * This method is called when 'Finish' button is pressed in
51      * the wizard. We will create an operation and run it
52      * using wizard as execution context.
53      */

54     public boolean performFinish() {
55         final String JavaDoc containerName = page.getContainerName();
56         final String JavaDoc fileName = page.getFileName();
57         IRunnableWithProgress op = new IRunnableWithProgress() {
58             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
59                 try {
60                     doFinish(containerName, fileName, monitor);
61                 } catch (CoreException e) {
62                     throw new InvocationTargetException JavaDoc(e);
63                 } finally {
64                     monitor.done();
65                 }
66             }
67         };
68         try {
69             getContainer().run(true, false, op);
70         } catch (InterruptedException JavaDoc e) {
71             return false;
72         } catch (InvocationTargetException JavaDoc e) {
73             Throwable JavaDoc realException = e.getTargetException();
74             MessageDialog.openError(getShell(), "Error", realException.getMessage());
75             return false;
76         }
77         return true;
78     }
79     
80     /**
81      * The worker method. It will find the container, create the
82      * file if missing or just replace its contents, and open
83      * the editor on the newly created file.
84      */

85
86     private void doFinish(
87         String JavaDoc containerName,
88         String JavaDoc fileName,
89         IProgressMonitor monitor)
90         throws CoreException {
91         // create a sample file
92
monitor.beginTask("Creating " + fileName, 2);
93         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
94         IResource resource = root.findMember(new Path(containerName));
95         if (!resource.exists() || !(resource instanceof IContainer)) {
96             throwCoreException("Container \"" + containerName + "\" does not exist.");
97         }
98         IContainer container = (IContainer) resource;
99         final IFile file = container.getFile(new Path(fileName));
100         try {
101             InputStream stream = openContentStream();
102             if (file.exists()) {
103                 file.setContents(stream, true, true, monitor);
104             } else {
105                 file.create(stream, true, monitor);
106             }
107             stream.close();
108         } catch (IOException e) {
109         }
110         monitor.worked(1);
111         monitor.setTaskName("Opening file for editing...");
112         getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
113             public void run() {
114                 IWorkbenchPage page =
115                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
116                 try {
117                     page.openEditor(file);
118                 } catch (PartInitException e) {
119                 }
120             }
121         });
122         monitor.worked(1);
123     }
124     
125     /**
126      * We will initialize file contents with a sample text.
127      */

128
129     private InputStream openContentStream() {
130         String JavaDoc contents =
131             "This is the initial file contents for *.$extension$ file that should be word-sorted in the Preview page of the multi-page editor";
132         return new ByteArrayInputStream(contents.getBytes());
133     }
134
135     private void throwCoreException(String JavaDoc message) throws CoreException {
136         IStatus status =
137             new Status(IStatus.ERROR, "$pluginId$", IStatus.OK, message, null);
138         throw new CoreException(status);
139     }
140
141     /**
142      * We will accept the selection in the workbench to see if
143      * we can initialize from it.
144      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
145      */

146     public void init(IWorkbench workbench, IStructuredSelection selection) {
147         this.selection = selection;
148     }
149 }
Popular Tags