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.IProject;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.IWorkspaceRoot;
30 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.Path;
35 import org.eclipse.core.runtime.Status;
36 import org.eclipse.jface.dialogs.MessageDialog;
37 import org.eclipse.jface.operation.IRunnableWithProgress;
38 import org.eclipse.jface.text.templates.Template;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.wizard.Wizard;
42 import org.eclipse.ui.INewWizard;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchPage;
45 import org.eclipse.ui.IWorkbenchWizard;
46 import org.eclipse.ui.PartInitException;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.ide.IDE;
51 * This wizard creates one file with the extension "php".
53 public class PHPFileWizard extends Wizard implements INewWizard {
55 private PHPFileWizardPage page;
57 private ISelection selection;
59 // the name of the file to create
60 private String fFileName;
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 the wizard. We will create an operation and run it using wizard as
79 public boolean performFinish() {
80 final String containerName = page.getContainerName();
81 final String fileName = page.getFileName();
82 IRunnableWithProgress op = new IRunnableWithProgress() {
83 public void run(IProgressMonitor monitor) throws InvocationTargetException {
85 doFinish(containerName, fileName, monitor);
86 } catch (CoreException e) {
87 throw new InvocationTargetException(e);
94 getContainer().run(true, false, op);
95 } catch (InterruptedException e) {
97 } catch (InvocationTargetException e) {
98 Throwable realException = e.getTargetException();
99 MessageDialog.openError(getShell(), PHPWizardMessages.getString("Wizard.error"), realException.getMessage());
106 * The worker method. It will find the container, create the file if missing or just replace its contents, and open the editor on
107 * the newly created file.
109 private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
110 // create a sample file
111 monitor.beginTask(PHPWizardMessages.getString("Wizard.Monitor.creating") + " " + fileName, 2);
112 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
113 IResource resource = root.findMember(new Path(containerName));
114 if (!resource.exists() || !(resource instanceof IContainer)) {
115 throwCoreException(PHPWizardMessages.getString("Wizard.Monitor.containerDoesNotExistException"));
117 IContainer container = (IContainer) resource;
118 final IFile file = container.getFile(new Path(fileName));
119 IProject project = file.getProject();
120 String projectName = project.getName();
121 String className = getClassName(fileName);
125 if (className == null) {
126 stream = openContentStream(fileName, projectName);
128 stream = openContentStreamClass(className);
131 file.setContents(stream, true, true, monitor);
133 file.create(stream, true, monitor);
136 } catch (IOException e) {
139 monitor.setTaskName(PHPWizardMessages.getString("Wizard.Monitor.openingFile"));
140 getShell().getDisplay().asyncExec(new Runnable() {
142 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
144 IDE.openEditor(page, file, true);
145 } catch (PartInitException e) {
153 * Check if the filename is like this anyname.class.php
157 * @return the anyname or null
159 private static final String getClassName(final String fileName) {
160 final int lastDot = fileName.lastIndexOf('.');
163 final int precLastDot = fileName.lastIndexOf('.', lastDot - 1);
164 if (precLastDot == -1)
166 if (!fileName.substring(precLastDot + 1, lastDot).toUpperCase().equals("CLASS"))
168 return fileName.substring(0, precLastDot);
172 * We will initialize file contents for a class
177 private InputStream openContentStreamClass(final String className) {
178 StringBuffer contents = new StringBuffer("<?php\n\n");
179 contents.append("class ");
180 contents.append(className);
181 contents.append(" {\n\n");
182 contents.append(" function ");
183 contents.append(className);
184 contents.append("() {\n");
185 contents.append(" }\n}\n?>");
186 return new ByteArrayInputStream(contents.toString().getBytes());
190 * We will initialize file contents with a sample text.
192 private InputStream openContentStream(String fileName, String projectname) {
194 Template template = PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWTYPE);
195 if (template == null) {
198 String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
199 CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, lineDelimiter);
200 context.setFileNameVariable(fileName, projectname);
201 String content=StubUtility.evaluateTemplate(context, template);
205 return new ByteArrayInputStream(content.getBytes());
206 } catch (CoreException e) {
213 private void throwCoreException(String message) throws CoreException {
214 IStatus status = new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
215 throw new CoreException(status);
219 * We will accept the selection in the workbench to see if we can initialize from it.
221 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
223 public void init(IWorkbench workbench, IStructuredSelection selection) {
224 this.selection = selection;
228 * Sets the name of the file to create (used to set the class name in the new file)
230 public void setFileName(String name) {