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