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 org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.operation.IRunnableWithProgress;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.wizard.Wizard;
35 import org.eclipse.ui.INewWizard;
36 import org.eclipse.ui.IWorkbench;
37 import org.eclipse.ui.IWorkbenchPage;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.PlatformUI;
42 * This wizard creates one file with the extension
45 public class PHPFileWizard extends Wizard implements INewWizard {
47 private PHPFileWizardPage page;
48 private ISelection selection;
50 // the name of the file to create
51 private String fileName;
54 public PHPFileWizard() {
56 setNeedsProgressMonitor(true);
60 * Adding the page to the wizard.
62 public void addPages() {
63 page = new PHPFileWizardPage(selection);
68 * This method is called when 'Finish' button is pressed in
70 * We will create an operation and run it
71 * using wizard as execution context.
73 public boolean performFinish() {
74 final String containerName = page.getContainerName();
75 final String fileName = page.getFileName();
76 IRunnableWithProgress op = new IRunnableWithProgress() {
77 public void run(IProgressMonitor monitor) throws InvocationTargetException {
79 doFinish(containerName, fileName, monitor);
80 } catch (CoreException e) {
81 throw new InvocationTargetException(e);
88 getContainer().run(true, false, op);
89 } catch (InterruptedException e) {
91 } catch (InvocationTargetException e) {
92 Throwable realException = e.getTargetException();
93 MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
100 * The worker method. It will find the container, create the
101 * file if missing or just replace its contents, and open
102 * the editor on the newly created file.
104 private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
105 // create a sample file
106 monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
107 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
108 IResource resource = root.findMember(new Path(containerName));
109 if (!resource.exists() || !(resource instanceof IContainer)) {
110 throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
112 IContainer container = (IContainer) resource;
113 final IFile file = container.getFile(new Path(fileName));
115 InputStream stream = openContentStream();
117 file.setContents(stream, true, true, monitor);
119 file.create(stream, true, monitor);
122 } catch (IOException e) {
125 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
126 getShell().getDisplay().asyncExec(new Runnable() {
128 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
130 page.openEditor(file);
131 } catch (PartInitException e) {
139 * We will initialize file contents with a sample text.
141 private InputStream openContentStream() {
142 String className = fileName.substring(0, fileName.length() - 3);
143 StringBuffer contents = new StringBuffer("<?php\n\n");
144 contents.append("function f0() {\n\n");
145 contents.append("}\n\n");
146 contents.append("function f1() {\n\n");
147 contents.append("}\n\n");
148 contents.append("switch($func) {\n");
149 contents.append(" case \"f1\":\n");
150 contents.append(" f1();\n");
151 contents.append(" break;\n\n");
152 contents.append(" default:\n");
153 contents.append(" f0();\n");
154 contents.append(" break;\n\n");
155 contents.append("}\n\n?>");
156 return new ByteArrayInputStream(contents.toString().getBytes());
159 private void throwCoreException(String message) throws CoreException {
160 IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
161 throw new CoreException(status);
165 * We will accept the selection in the workbench to see if
166 * we can initialize from it.
167 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
169 public void init(IWorkbench workbench, IStructuredSelection selection) {
170 this.selection = selection;
174 * Sets the name of the file to create
175 * (used to set the class name in the new file)
177 public void setFileName(String name) {