1+ package org .nodeclipse .ui .wizards ;
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 ;
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+ import org .eclipse .ui .ide .IDE ;
17+
18+ /**
19+ * This is a sample new wizard. Its role is to create a new file resource in the
20+ * provided container. If the container resource (a folder or a project) is
21+ * selected in the workspace when the wizard is opened, it will accept it as the
22+ * target container. The wizard creates one file with the extension "js". If a
23+ * sample multi-page editor (also available as a template) is registered for the
24+ * same extension, it will be able to open it.
25+ *
26+ * @author Paul Verest
27+ * @since 0.5
28+ */
29+ public class CoffeeFileWizard extends Wizard implements INewWizard {
30+
31+ private final String WINDOW_TITLE = "New CoffeeScript Source File" ;
32+
33+ private CoffeeFileWizardPage page ;
34+ private ISelection selection ;
35+
36+ /**
37+ * Constructor for CoffeeWizard.
38+ */
39+ public CoffeeFileWizard () {
40+ setWindowTitle (WINDOW_TITLE );
41+ setNeedsProgressMonitor (true );
42+ }
43+
44+ /**
45+ * Adding the page to the wizard.
46+ */
47+
48+ public void addPages () {
49+ page = new CoffeeFileWizardPage (selection );
50+ addPage (page );
51+ }
52+
53+ /**
54+ * This method is called when 'Finish' button is pressed in the wizard. We
55+ * will create an operation and run it using wizard as execution context.
56+ */
57+ public boolean performFinish () {
58+ final String containerName = page .getContainerName ();
59+ final String fileName = page .getFileName ();
60+ IRunnableWithProgress op = new IRunnableWithProgress () {
61+ public void run (IProgressMonitor monitor ) throws InvocationTargetException {
62+ try {
63+ doFinish (containerName , fileName , monitor );
64+ } catch (CoreException e ) {
65+ throw new InvocationTargetException (e );
66+ } finally {
67+ monitor .done ();
68+ }
69+ }
70+ };
71+ try {
72+ getContainer ().run (true , false , op );
73+ } catch (InterruptedException e ) {
74+ return false ;
75+ } catch (InvocationTargetException e ) {
76+ Throwable realException = e .getTargetException ();
77+ MessageDialog .openError (getShell (), "Error" , realException .getMessage ());
78+ return false ;
79+ }
80+ return true ;
81+ }
82+
83+ /**
84+ * The worker method. It will find the container, create the file if missing
85+ * or just replace its contents, and open the editor on the newly created
86+ * file.
87+ */
88+
89+ private void doFinish (String containerName , String fileName , IProgressMonitor monitor ) throws CoreException {
90+ // create a sample file
91+ monitor .beginTask ("Creating " + fileName , 2 );
92+ IWorkspaceRoot root = ResourcesPlugin .getWorkspace ().getRoot ();
93+ IResource resource = root .findMember (new Path (containerName ));
94+ if (!resource .exists () || !(resource instanceof IContainer )) {
95+ throwCoreException ("Container \" " + containerName + "\" does not exist." );
96+ }
97+ IContainer container = (IContainer ) resource ;
98+ final IFile file = container .getFile (new Path (fileName ));
99+ try {
100+ InputStream stream = openContentStream ();
101+ if (file .exists ()) {
102+ file .setContents (stream , true , true , monitor );
103+ } else {
104+ file .create (stream , true , monitor );
105+ }
106+ stream .close ();
107+ } catch (IOException e ) {
108+ }
109+ monitor .worked (1 );
110+ monitor .setTaskName ("Opening file for editing..." );
111+ getShell ().getDisplay ().asyncExec (new Runnable () {
112+ public void run () {
113+ IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
114+ try {
115+ IDE .openEditor (page , file , true );
116+ } catch (PartInitException e ) {
117+ }
118+ }
119+ });
120+ monitor .worked (1 );
121+ }
122+
123+ /**
124+ * We will initialize file contents with a sample text.
125+ */
126+
127+ private InputStream openContentStream () {
128+ String contents = "/**\n * New coffeescript file\n */\n " ;
129+ return new ByteArrayInputStream (contents .getBytes ());
130+ }
131+
132+ private void throwCoreException (String message ) throws CoreException {
133+ IStatus status = new Status (IStatus .ERROR , "org.nodeclipse.ui" , IStatus .OK , message , null );
134+ throw new CoreException (status );
135+ }
136+
137+ /**
138+ * We will accept the selection in the workbench to see if we can initialize
139+ * from it.
140+ *
141+ * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
142+ */
143+ public void init (IWorkbench workbench , IStructuredSelection selection ) {
144+ this .selection = selection ;
145+ }
146+ }
0 commit comments