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;
 
  24 import net.sourceforge.phpeclipse.ui.WebUI;
 
  26 import org.eclipse.core.resources.IContainer;
 
  27 import org.eclipse.core.resources.IFile;
 
  28 import org.eclipse.core.resources.IProject;
 
  29 import org.eclipse.core.resources.IResource;
 
  30 import org.eclipse.core.resources.IWorkspaceRoot;
 
  31 import org.eclipse.core.resources.ResourcesPlugin;
 
  32 import org.eclipse.core.runtime.CoreException;
 
  33 import org.eclipse.core.runtime.IProgressMonitor;
 
  34 import org.eclipse.core.runtime.IStatus;
 
  35 import org.eclipse.core.runtime.Path;
 
  36 import org.eclipse.core.runtime.Status;
 
  37 import org.eclipse.jface.dialogs.MessageDialog;
 
  38 import org.eclipse.jface.operation.IRunnableWithProgress;
 
  39 import org.eclipse.jface.text.templates.Template;
 
  40 import org.eclipse.jface.viewers.ISelection;
 
  41 import org.eclipse.jface.viewers.IStructuredSelection;
 
  42 import org.eclipse.jface.wizard.Wizard;
 
  43 import org.eclipse.ui.INewWizard;
 
  44 import org.eclipse.ui.IWorkbench;
 
  45 import org.eclipse.ui.IWorkbenchPage;
 
  46 import org.eclipse.ui.IWorkbenchWizard;
 
  47 import org.eclipse.ui.PartInitException;
 
  48 import org.eclipse.ui.PlatformUI;
 
  49 import org.eclipse.ui.ide.IDE;
 
  52  * This wizard creates one file with the extension "html".
 
  54 public class HTMLFileWizard extends Wizard implements INewWizard {
 
  56         private HTMLFileWizardPage page;
 
  58         private ISelection selection;
 
  60         public HTMLFileWizard() {
 
  62                 setNeedsProgressMonitor(true);
 
  63                 setWindowTitle(PHPWizardMessages
 
  64                                 .getString("WizardNewProjectCreationPage.html.windowTitle"));
 
  68          * Adding the page to the wizard.
 
  70         public void addPages() {
 
  71                 page = new HTMLFileWizardPage(selection);
 
  76          * This method is called when 'Finish' button is pressed in the wizard. We
 
  77          * will create an operation and run it using wizard as execution context.
 
  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)
 
  84                                         throws InvocationTargetException {
 
  86                                         doFinish(containerName, fileName, monitor);
 
  87                                 } catch (CoreException e) {
 
  88                                         throw new InvocationTargetException(e);
 
  95                         getContainer().run(true, false, op);
 
  96                 } catch (InterruptedException e) {
 
  98                 } catch (InvocationTargetException e) {
 
  99                         Throwable realException = e.getTargetException();
 
 100                         MessageDialog.openError(getShell(), PHPWizardMessages
 
 101                                         .getString("Wizard.error"), realException.getMessage());
 
 108          * The worker method. It will find the container, create the file if missing
 
 109          * or just replace its contents, and open the editor on the newly created
 
 112         private void doFinish(String containerName, String fileName,
 
 113                         IProgressMonitor monitor) throws CoreException {
 
 114                 // create a sample file
 
 115                 monitor.beginTask(PHPWizardMessages
 
 116                                 .getString("Wizard.Monitor.creating")
 
 117                                 + " " + fileName, 2);
 
 118                 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
 
 119                 IResource resource = root.findMember(new Path(containerName));
 
 120                 if (!resource.exists() || !(resource instanceof IContainer)) {
 
 121                         throwCoreException(PHPWizardMessages
 
 122                                         .getString("Wizard.Monitor.containerDoesNotExistException"));
 
 124                 IContainer container = (IContainer) resource;
 
 125                 final IFile file = container.getFile(new Path(fileName));
 
 126                 IProject project = file.getProject();
 
 127                 String projectName = project.getName();
 
 130                         stream = openContentStream(fileName, projectName);
 
 132                                 file.setContents(stream, true, true, monitor);
 
 134                                 file.create(stream, true, monitor);
 
 137                 } catch (IOException e) {
 
 140                 monitor.setTaskName(PHPWizardMessages
 
 141                                 .getString("Wizard.Monitor.openingFile"));
 
 142                 getShell().getDisplay().asyncExec(new Runnable() {
 
 144                                 IWorkbenchPage page = PlatformUI.getWorkbench()
 
 145                                                 .getActiveWorkbenchWindow().getActivePage();
 
 147                                         IDE.openEditor(page, file, true);
 
 148                                 } catch (PartInitException e) {
 
 156          * We will initialize file contents with a sample text.
 
 158         private InputStream openContentStream(String fileName, String projectname) {
 
 160                         Template template = WebUI.getDefault()
 
 161                                         .getCodeTemplateStore().findTemplate(
 
 162                                                         CodeTemplateContextType.NEWHTML);
 
 163                         if (template == null) {
 
 166                         String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 
 167                         CodeTemplateContext context = new CodeTemplateContext(template
 
 168                                         .getContextTypeId(), null, lineDelimiter);
 
 169                         context.setFileNameVariable(fileName, projectname);
 
 170                         return new ByteArrayInputStream(StubUtility.evaluateTemplate(
 
 171                                         context, template).getBytes());
 
 172                 } catch (CoreException e) {
 
 178         private void throwCoreException(String message) throws CoreException {
 
 179                 IStatus status = new Status(IStatus.ERROR,
 
 180                                 "net.sourceforge.phpeclipse.wizards", IStatus.OK, message, null);
 
 181                 throw new CoreException(status);
 
 185          * We will accept the selection in the workbench to see if we can initialize
 
 188          * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
 
 190         public void init(IWorkbench workbench, IStructuredSelection selection) {
 
 191                 this.selection = selection;