c699b5e1d9fdddfccfa8804e332ec67dfbf0ba4e
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / HTMLFileWizard.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  www.phpeclipse.de
13  **********************************************************************/
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.lang.reflect.InvocationTargetException;
19
20 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
21 import net.sourceforge.phpdt.internal.corext.template.php.CodeTemplateContext;
22 import net.sourceforge.phpdt.internal.corext.template.php.CodeTemplateContextType;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.IWorkspaceRoot;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.Path;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.operation.IRunnableWithProgress;
37 import org.eclipse.jface.text.templates.Template;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.IStructuredSelection;
40 import org.eclipse.jface.wizard.Wizard;
41 import org.eclipse.ui.INewWizard;
42 import org.eclipse.ui.IWorkbench;
43 import org.eclipse.ui.IWorkbenchPage;
44 import org.eclipse.ui.IWorkbenchWizard;
45 import org.eclipse.ui.PartInitException;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.ide.IDE;
48
49 /**
50  * This wizard creates one file with the extension "html".
51  */
52 public class HTMLFileWizard extends Wizard implements INewWizard {
53
54   private HTMLFileWizardPage page;
55
56   private ISelection selection;
57
58   // the name of the file to create
59   private String fFileName;
60
61   public HTMLFileWizard() {
62     super();
63     setNeedsProgressMonitor(true);
64   }
65
66   /**
67    * Adding the page to the wizard.
68    */
69   public void addPages() {
70     page = new HTMLFileWizardPage(selection);
71     addPage(page);
72   }
73
74   /**
75    * This method is called when 'Finish' button is pressed in the wizard. We will create an operation and run it using wizard as
76    * execution context.
77    */
78   public boolean performFinish() {
79     final String containerName = page.getContainerName();
80     final String fileName = page.getFileName();
81     IRunnableWithProgress op = new IRunnableWithProgress() {
82       public void run(IProgressMonitor monitor) throws InvocationTargetException {
83         try {
84           doFinish(containerName, fileName, monitor);
85         } catch (CoreException e) {
86           throw new InvocationTargetException(e);
87         } finally {
88           monitor.done();
89         }
90       }
91     };
92     try {
93       getContainer().run(true, false, op);
94     } catch (InterruptedException e) {
95       return false;
96     } catch (InvocationTargetException e) {
97       Throwable realException = e.getTargetException();
98       MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
99       return false;
100     }
101     return true;
102   }
103
104   /**
105    * The worker method. It will find the container, create the file if missing or just replace its contents, and open the editor on
106    * the newly created file.
107    */
108   private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
109     // create a sample file
110     monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
111     IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
112     IResource resource = root.findMember(new Path(containerName));
113     if (!resource.exists() || !(resource instanceof IContainer)) {
114       throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
115     }
116     IContainer container = (IContainer) resource;
117     final IFile file = container.getFile(new Path(fileName));
118     try {
119       InputStream stream;
120       stream = openContentStream(fileName);
121       if (file.exists()) {
122         file.setContents(stream, true, true, monitor);
123       } else {
124         file.create(stream, true, monitor);
125       }
126       stream.close();
127     } catch (IOException e) {
128     }
129     monitor.worked(1);
130     monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
131     getShell().getDisplay().asyncExec(new Runnable() {
132       public void run() {
133         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
134         try {
135           IDE.openEditor(page, file, true);
136         } catch (PartInitException e) {
137         }
138       }
139     });
140     monitor.worked(1);
141   }
142
143   /**
144    * We will initialize file contents with a sample text.
145    */
146   private InputStream openContentStream(String fileName) {
147     try {
148       Template template = PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWHTML);
149       if (template == null) {
150         return null;
151       }
152       String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
153       CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, lineDelimiter);
154       context.setFileNameVariable(fileName);
155       return new ByteArrayInputStream(StubUtility.evaluateTemplate(context, template).getBytes());
156     } catch (CoreException e) {
157       e.printStackTrace();
158       return null;
159     }
160   }
161
162   private void throwCoreException(String message) throws CoreException {
163     IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
164     throw new CoreException(status);
165   }
166
167   /**
168    * We will accept the selection in the workbench to see if we can initialize from it.
169    * 
170    * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
171    */
172   public void init(IWorkbench workbench, IStructuredSelection selection) {
173     this.selection = selection;
174   }
175
176   /**
177    * Sets the name of the file to create (used to set the class name in the new file)
178    */
179   public void setFileName(String name) {
180     fFileName = name;
181   }
182 }