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 org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.wizard.Wizard;
17 import org.eclipse.ui.INewWizard;
18 import org.eclipse.ui.IWorkbench;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.operation.*;
21 import java.lang.reflect.InvocationTargetException;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.core.resources.*;
25 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.ui.*;
30 * This wizard creates one file with the extension
33 public class PHPFileWizard extends Wizard implements INewWizard {
35 private PHPFileWizardPage page;
36 private ISelection selection;
38 // the name of the file to create
39 private String fileName;
42 public PHPFileWizard() {
44 setNeedsProgressMonitor(true);
48 * Adding the page to the wizard.
50 public void addPages() {
51 page = new PHPFileWizardPage(selection);
56 * This method is called when 'Finish' button is pressed in
58 * We will create an operation and run it
59 * using wizard as execution context.
61 public boolean performFinish() {
62 final String containerName = page.getContainerName();
63 final String fileName = page.getFileName();
64 IRunnableWithProgress op = new IRunnableWithProgress() {
65 public void run(IProgressMonitor monitor) throws InvocationTargetException {
67 doFinish(containerName, fileName, monitor);
68 } catch (CoreException e) {
69 throw new InvocationTargetException(e);
76 getContainer().run(true, false, op);
77 } catch (InterruptedException e) {
79 } catch (InvocationTargetException e) {
80 Throwable realException = e.getTargetException();
81 MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
88 * The worker method. It will find the container, create the
89 * file if missing or just replace its contents, and open
90 * the editor on the newly created file.
92 private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
93 // create a sample file
94 monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
95 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
96 IResource resource = root.findMember(new Path(containerName));
97 if (!resource.exists() || !(resource instanceof IContainer)) {
98 throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
100 IContainer container = (IContainer) resource;
101 final IFile file = container.getFile(new Path(fileName));
103 InputStream stream = openContentStream();
105 file.setContents(stream, true, true, monitor);
107 file.create(stream, true, monitor);
110 } catch (IOException e) {
113 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
114 getShell().getDisplay().asyncExec(new Runnable() {
116 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
118 page.openEditor(file);
119 } catch (PartInitException e) {
127 * We will initialize file contents with a sample text.
129 private InputStream openContentStream() {
130 String className = fileName.substring(0, fileName.length() - 3);
131 StringBuffer contents = new StringBuffer("<?php\n\n");
132 contents.append("function f0() {\n\n");
133 contents.append("}\n\n");
134 contents.append("function f1() {\n\n");
135 contents.append("}\n\n");
136 contents.append("switch($func) {\n");
137 contents.append(" case \"f1\":\n");
138 contents.append(" f1();\n");
139 contents.append(" break;\n\n");
140 contents.append(" default:\n");
141 contents.append(" f0();\n");
142 contents.append(" break;\n\n");
143 contents.append("}\n\n?>");
144 return new ByteArrayInputStream(contents.toString().getBytes());
147 private void throwCoreException(String message) throws CoreException {
148 IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
149 throw new CoreException(status);
153 * We will accept the selection in the workbench to see if
154 * we can initialize from it.
155 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
157 public void init(IWorkbench workbench, IStructuredSelection selection) {
158 this.selection = selection;
162 * Sets the name of the file to create
163 * (used to set the class name in the new file)
165 public void setFileName(String name) {