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
53 public class PHPFileWizard extends Wizard implements INewWizard {
55 private PHPFileWizardPage page;
56 private ISelection selection;
58 // the name of the file to create
59 private String fileName;
62 public PHPFileWizard() {
64 setNeedsProgressMonitor(true);
68 * Adding the page to the wizard.
70 public void addPages() {
71 page = new PHPFileWizardPage(selection);
76 * This method is called when 'Finish' button is pressed in
78 * We will create an operation and run it
79 * using wizard as execution context.
81 public boolean performFinish() {
82 final String containerName = page.getContainerName();
83 final String fileName = page.getFileName();
84 IRunnableWithProgress op = new IRunnableWithProgress() {
85 public void run(IProgressMonitor monitor) throws InvocationTargetException {
87 doFinish(containerName, fileName, monitor);
88 } catch (CoreException e) {
89 throw new InvocationTargetException(e);
96 getContainer().run(true, false, op);
97 } catch (InterruptedException e) {
99 } catch (InvocationTargetException e) {
100 Throwable realException = e.getTargetException();
101 MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
108 * The worker method. It will find the container, create the
109 * file if missing or just replace its contents, and open
110 * the editor on the newly created file.
112 private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
113 // create a sample file
114 monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
115 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
116 IResource resource = root.findMember(new Path(containerName));
117 if (!resource.exists() || !(resource instanceof IContainer)) {
118 throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
120 IContainer container = (IContainer) resource;
121 final IFile file = container.getFile(new Path(fileName));
122 String className = getClassName(fileName);
126 if (className == null) {
127 stream = openContentStream();
129 stream = openContentStreamClass(className);
132 file.setContents(stream, true, true, monitor);
134 file.create(stream, true, monitor);
137 } catch (IOException e) {
140 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
141 getShell().getDisplay().asyncExec(new Runnable() {
143 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
145 IDE.openEditor(page,file,true);
146 } catch (PartInitException e) {
154 * Check if the filename is like this anyname.class.php
155 * @param fileName the filename
156 * @return the anyname or null
158 private static final String getClassName(final String fileName) {
159 final int lastDot = fileName.lastIndexOf('.');
160 if (lastDot == -1) return null;
161 final int precLastDot = fileName.lastIndexOf('.',lastDot-1);
162 if (precLastDot == -1) return null;
163 if (!fileName.substring(precLastDot+1,lastDot).toUpperCase().equals("CLASS")) return null;
164 return fileName.substring(0,precLastDot);
168 * We will initialize file contents for a class
169 * @param className the classname
171 private InputStream openContentStreamClass(final String className) {
172 StringBuffer contents = new StringBuffer("<?php\n\n");
173 contents.append("class ");
174 contents.append(className);
175 contents.append(" {\n\n");
176 contents.append(" function ");
177 contents.append(className);
178 contents.append("() {\n");
179 contents.append(" }\n}\n?>");
180 return new ByteArrayInputStream(contents.toString().getBytes());
184 * We will initialize file contents with a sample text.
186 private InputStream openContentStream() {
188 Template template= PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWTYPE);
189 if (template == null) {
192 String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
193 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, lineDelimiter);
194 // context.setCompilationUnitVariables(cu);
195 return new ByteArrayInputStream(StubUtility.evaluateTemplate(context, template).getBytes());
196 } catch (CoreException e) {
201 // StringBuffer contents = new StringBuffer("<?php\n\n");
202 // contents.append("function f0() {\n\n");
203 // contents.append("}\n\n");
204 // contents.append("function f1() {\n\n");
205 // contents.append("}\n\n");
206 // contents.append("switch($func) {\n");
207 // contents.append(" case \"f1\":\n");
208 // contents.append(" f1();\n");
209 // contents.append(" break;\n\n");
210 // contents.append(" default:\n");
211 // contents.append(" f0();\n");
212 // contents.append(" break;\n\n");
213 // contents.append("}\n\n?>");
214 // return new ByteArrayInputStream(contents.toString().getBytes());
217 private void throwCoreException(String message) throws CoreException {
218 IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
219 throw new CoreException(status);
223 * We will accept the selection in the workbench to see if
224 * we can initialize from it.
225 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
227 public void init(IWorkbench workbench, IStructuredSelection selection) {
228 this.selection = selection;
232 * Sets the name of the file to create
233 * (used to set the class name in the new file)
235 public void setFileName(String name) {