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;
40 import org.eclipse.ui.ide.IDE;
43 * This wizard creates one file with the extension
46 public class PHPFileWizard extends Wizard implements INewWizard {
48 private PHPFileWizardPage page;
49 private ISelection selection;
51 // the name of the file to create
52 private String fileName;
55 public PHPFileWizard() {
57 setNeedsProgressMonitor(true);
61 * Adding the page to the wizard.
63 public void addPages() {
64 page = new PHPFileWizardPage(selection);
69 * This method is called when 'Finish' button is pressed in
71 * We will create an operation and run it
72 * using wizard as execution context.
74 public boolean performFinish() {
75 final String containerName = page.getContainerName();
76 final String fileName = page.getFileName();
77 IRunnableWithProgress op = new IRunnableWithProgress() {
78 public void run(IProgressMonitor monitor) throws InvocationTargetException {
80 doFinish(containerName, fileName, monitor);
81 } catch (CoreException e) {
82 throw new InvocationTargetException(e);
89 getContainer().run(true, false, op);
90 } catch (InterruptedException e) {
92 } catch (InvocationTargetException e) {
93 Throwable realException = e.getTargetException();
94 MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
101 * The worker method. It will find the container, create the
102 * file if missing or just replace its contents, and open
103 * the editor on the newly created file.
105 private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
106 // create a sample file
107 monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
108 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
109 IResource resource = root.findMember(new Path(containerName));
110 if (!resource.exists() || !(resource instanceof IContainer)) {
111 throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
113 IContainer container = (IContainer) resource;
114 final IFile file = container.getFile(new Path(fileName));
115 String className = getClassName(fileName);
119 if (className == null) {
120 stream = openContentStream();
122 stream = openContentStreamClass(className);
125 file.setContents(stream, true, true, monitor);
127 file.create(stream, true, monitor);
130 } catch (IOException e) {
133 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
134 getShell().getDisplay().asyncExec(new Runnable() {
136 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
138 IDE.openEditor(page,file,true);
139 } catch (PartInitException e) {
147 * Check if the filename is like this anyname.class.php
148 * @param fileName the filename
149 * @return the anyname or null
151 private static final String getClassName(final String fileName) {
152 final int lastDot = fileName.lastIndexOf('.');
153 if (lastDot == -1) return null;
154 final int precLastDot = fileName.lastIndexOf('.',lastDot-1);
155 if (precLastDot == -1) return null;
156 if (!fileName.substring(precLastDot+1,lastDot).toUpperCase().equals("CLASS")) return null;
157 return fileName.substring(0,precLastDot);
161 * We will initialize file contents for a class
162 * @param className the classname
164 private InputStream openContentStreamClass(final String className) {
165 StringBuffer contents = new StringBuffer("<?php\n\n");
166 contents.append("class ");
167 contents.append(className);
168 contents.append(" {\n\n");
169 contents.append(" function ");
170 contents.append(className);
171 contents.append("() {\n");
172 contents.append(" }\n}\n?>");
173 return new ByteArrayInputStream(contents.toString().getBytes());
177 * We will initialize file contents with a sample text.
179 private InputStream openContentStream() {
180 StringBuffer contents = new StringBuffer("<?php\n\n");
181 contents.append("function f0() {\n\n");
182 contents.append("}\n\n");
183 contents.append("function f1() {\n\n");
184 contents.append("}\n\n");
185 contents.append("switch($func) {\n");
186 contents.append(" case \"f1\":\n");
187 contents.append(" f1();\n");
188 contents.append(" break;\n\n");
189 contents.append(" default:\n");
190 contents.append(" f0();\n");
191 contents.append(" break;\n\n");
192 contents.append("}\n\n?>");
193 return new ByteArrayInputStream(contents.toString().getBytes());
196 private void throwCoreException(String message) throws CoreException {
197 IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
198 throw new CoreException(status);
202 * We will accept the selection in the workbench to see if
203 * we can initialize from it.
204 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
206 public void init(IWorkbench workbench, IStructuredSelection selection) {
207 this.selection = selection;
211 * Sets the name of the file to create
212 * (used to set the class name in the new file)
214 public void setFileName(String name) {