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
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 "php".
52 public class PHPFileWizard extends Wizard implements INewWizard {
54 private PHPFileWizardPage page;
56 private ISelection selection;
58 // the name of the file to create
59 private String fFileName;
61 public PHPFileWizard() {
63 setNeedsProgressMonitor(true);
67 * Adding the page to the wizard.
69 public void addPages() {
70 page = new PHPFileWizardPage(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));
118 String className = getClassName(fileName);
122 if (className == null) {
123 stream = openContentStream(fileName);
125 stream = openContentStreamClass(className);
128 file.setContents(stream, true, true, monitor);
130 file.create(stream, true, monitor);
133 } catch (IOException e) {
136 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
137 getShell().getDisplay().asyncExec(new Runnable() {
139 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
141 IDE.openEditor(page, file, true);
142 } catch (PartInitException e) {
150 * Check if the filename is like this anyname.class.php
154 * @return the anyname or null
156 private static final String getClassName(final String fileName) {
157 final int lastDot = fileName.lastIndexOf('.');
160 final int precLastDot = fileName.lastIndexOf('.', lastDot - 1);
161 if (precLastDot == -1)
163 if (!fileName.substring(precLastDot + 1, lastDot).toUpperCase().equals("CLASS"))
165 return fileName.substring(0, precLastDot);
169 * We will initialize file contents for a class
174 private InputStream openContentStreamClass(final String className) {
175 StringBuffer contents = new StringBuffer("<?php\n\n");
176 contents.append("class ");
177 contents.append(className);
178 contents.append(" {\n\n");
179 contents.append(" function ");
180 contents.append(className);
181 contents.append("() {\n");
182 contents.append(" }\n}\n?>");
183 return new ByteArrayInputStream(contents.toString().getBytes());
187 * We will initialize file contents with a sample text.
189 private InputStream openContentStream(String fileName) {
191 Template template = PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWTYPE);
192 if (template == null) {
195 String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
196 CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, lineDelimiter);
197 context.setFileNameVariable(fileName);
198 return new ByteArrayInputStream(StubUtility.evaluateTemplate(context, template).getBytes());
199 } catch (CoreException e) {
206 private void throwCoreException(String message) throws CoreException {
207 IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
208 throw new CoreException(status);
212 * We will accept the selection in the workbench to see if we can initialize from it.
214 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
216 public void init(IWorkbench workbench, IStructuredSelection selection) {
217 this.selection = selection;
221 * Sets the name of the file to create (used to set the class name in the new file)
223 public void setFileName(String name) {