1 package net.sourceforge.phpeclipse.wizards;
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
11 IBM Corporation - Initial implementation
13 **********************************************************************/
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.lang.reflect.InvocationTargetException;
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;
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;
51 * This wizard creates one file with the extension "html".
53 public class HTMLFileWizard extends Wizard implements INewWizard {
55 private HTMLFileWizardPage page;
57 private ISelection selection;
59 public HTMLFileWizard() {
61 setNeedsProgressMonitor(true);
62 setWindowTitle(PHPWizardMessages
63 .getString("WizardNewProjectCreationPage.html.windowTitle"));
67 * Adding the page to the wizard.
69 public void addPages() {
70 page = new HTMLFileWizardPage(selection);
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.
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 {
85 doFinish(containerName, fileName, monitor);
86 } catch (CoreException e) {
87 throw new InvocationTargetException(e);
94 getContainer().run(true, false, op);
95 } catch (InterruptedException e) {
97 } catch (InvocationTargetException e) {
98 Throwable realException = e.getTargetException();
99 MessageDialog.openError(getShell(), PHPWizardMessages
100 .getString("Wizard.error"), realException.getMessage());
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
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"));
123 IContainer container = (IContainer) resource;
124 final IFile file = container.getFile(new Path(fileName));
125 IProject project = file.getProject();
126 String projectName = project.getName();
129 stream = openContentStream(fileName, projectName);
131 file.setContents(stream, true, true, monitor);
133 file.create(stream, true, monitor);
136 } catch (IOException e) {
139 monitor.setTaskName(PHPWizardMessages
140 .getString("Wizard.Monitor.openingFile"));
141 getShell().getDisplay().asyncExec(new Runnable() {
143 IWorkbenchPage page = PlatformUI.getWorkbench()
144 .getActiveWorkbenchWindow().getActivePage();
146 IDE.openEditor(page, file, true);
147 } catch (PartInitException e) {
155 * We will initialize file contents with a sample text.
157 private InputStream openContentStream(String fileName, String projectname) {
159 Template template = PHPeclipsePlugin.getDefault()
160 .getCodeTemplateStore().findTemplate(
161 CodeTemplateContextType.NEWHTML);
162 if (template == null) {
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) {
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);
184 * We will accept the selection in the workbench to see if we can initialize
187 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
189 public void init(IWorkbench workbench, IStructuredSelection selection) {
190 this.selection = selection;