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