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
12 Klaus Hartlage - www.eclipseproject.de
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.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;
50 * This wizard creates one file with the extension "html".
52 public class HTMLFileWizard extends Wizard implements INewWizard {
54 private HTMLFileWizardPage page;
56 private ISelection selection;
58 // the name of the file to create
59 private String fFileName;
61 public HTMLFileWizard() {
63 setNeedsProgressMonitor(true);
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 will create an operation and run it using wizard as
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 {
84 doFinish(containerName, fileName, monitor);
85 } catch (CoreException e) {
86 throw new InvocationTargetException(e);
93 getContainer().run(true, false, op);
94 } catch (InterruptedException e) {
96 } catch (InvocationTargetException e) {
97 Throwable realException = e.getTargetException();
98 MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
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.
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"));
116 IContainer container = (IContainer) resource;
117 final IFile file = container.getFile(new Path(fileName));
120 stream = openContentStream(fileName);
122 file.setContents(stream, true, true, monitor);
124 file.create(stream, true, monitor);
127 } catch (IOException e) {
130 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
131 getShell().getDisplay().asyncExec(new Runnable() {
133 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
135 IDE.openEditor(page, file, true);
136 } catch (PartInitException e) {
144 * We will initialize file contents with a sample text.
146 private InputStream openContentStream(String fileName) {
148 Template template = PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWHTML);
149 if (template == null) {
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) {
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);
168 * We will accept the selection in the workbench to see if we can initialize from it.
170 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
172 public void init(IWorkbench workbench, IStructuredSelection selection) {
173 this.selection = selection;
177 * Sets the name of the file to create (used to set the class name in the new file)
179 public void setFileName(String name) {