a0e659d28aaedb9f35865f3f07516f28a8ffb7a9
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / PHPFileWizard.java
1 package net.sourceforge.phpeclipse.wizards;
2
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
9
10 Contributors:
11     IBM Corporation - Initial implementation
12     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.wizard.Wizard;
17 import org.eclipse.ui.INewWizard;
18 import org.eclipse.ui.IWorkbench;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.operation.*;
21 import java.lang.reflect.InvocationTargetException;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.core.resources.*;
25 import org.eclipse.core.runtime.CoreException;
26 import java.io.*;
27 import org.eclipse.ui.*;
28
29 /**
30  * This wizard creates one file with the extension
31  * "php". 
32  */
33 public class PHPFileWizard extends Wizard implements INewWizard {
34
35   private PHPFileWizardPage page;
36   private ISelection selection;
37
38   // the name of the file to create
39   private String fileName;
40
41   
42   public PHPFileWizard() {
43     super();
44     setNeedsProgressMonitor(true);
45   }
46
47   /**
48    * Adding the page to the wizard.
49    */
50   public void addPages() {
51     page = new PHPFileWizardPage(selection);
52     addPage(page);
53   }
54
55   /**
56    * This method is called when 'Finish' button is pressed in
57    * the wizard. 
58    * We will create an operation and run it
59    * using wizard as execution context.
60    */
61   public boolean performFinish() {
62     final String containerName = page.getContainerName();
63     final String fileName = page.getFileName();
64     IRunnableWithProgress op = new IRunnableWithProgress() {
65       public void run(IProgressMonitor monitor) throws InvocationTargetException {
66         try {
67           doFinish(containerName, fileName, monitor);
68         } catch (CoreException e) {
69           throw new InvocationTargetException(e);
70         } finally {
71           monitor.done();
72         }
73       }
74     };
75     try {
76       getContainer().run(true, false, op);
77     } catch (InterruptedException e) {
78       return false;
79     } catch (InvocationTargetException e) {
80       Throwable realException = e.getTargetException();
81       MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
82       return false;
83     }
84     return true;
85   }
86
87   /**
88    * The worker method. It will find the container, create the
89    * file if missing or just replace its contents, and open
90    * the editor on the newly created file.
91    */
92   private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
93     // create a sample file
94     monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
95     IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
96     IResource resource = root.findMember(new Path(containerName));
97     if (!resource.exists() || !(resource instanceof IContainer)) {
98       throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
99     }
100     IContainer container = (IContainer) resource;
101     final IFile file = container.getFile(new Path(fileName));
102     try {
103       InputStream stream = openContentStream();
104       if (file.exists()) {
105         file.setContents(stream, true, true, monitor);
106       } else {
107         file.create(stream, true, monitor);
108       }
109       stream.close();
110     } catch (IOException e) {
111     }
112     monitor.worked(1);
113     monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
114     getShell().getDisplay().asyncExec(new Runnable() {
115       public void run() {
116         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
117         try {
118           page.openEditor(file);
119         } catch (PartInitException e) {
120         }
121       }
122     });
123     monitor.worked(1);
124   }
125
126   /**
127    * We will initialize file contents with a sample text.
128    */
129   private InputStream openContentStream() {
130     String className = fileName.substring(0, fileName.length() - 3);
131     StringBuffer contents = new StringBuffer("<?php\n\n");
132     contents.append("function f0() {\n\n");
133     contents.append("}\n\n");
134     contents.append("function f1() {\n\n");
135     contents.append("}\n\n");
136     contents.append("switch($func) {\n");
137     contents.append("    case \"f1\":\n");
138     contents.append("    f1();\n");
139     contents.append("    break;\n\n");
140     contents.append("    default:\n");
141     contents.append("    f0();\n");
142     contents.append("    break;\n\n");
143     contents.append("}\n\n?>");
144     return new ByteArrayInputStream(contents.toString().getBytes());
145   }
146
147   private void throwCoreException(String message) throws CoreException {
148     IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
149     throw new CoreException(status);
150   }
151
152   /**
153    * We will accept the selection in the workbench to see if
154    * we can initialize from it.
155    * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
156    */
157   public void init(IWorkbench workbench, IStructuredSelection selection) {
158     this.selection = selection;
159   }
160
161   /**
162    * Sets the name of the file to create
163    * (used to set the class name in the new file)
164    */
165   public void setFileName(String name) {
166     fileName = name;
167   }
168 }