1 17 package org.apache.forrest.eclipse.wizards; 18 19 import java.lang.reflect.InvocationTargetException ; 20 21 import org.apache.forrest.eclipse.ForrestPlugin; 22 import org.eclipse.ant.core.AntRunner; 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IProject; 26 import org.eclipse.core.resources.IResource; 27 import org.eclipse.core.resources.IWorkspaceRoot; 28 import org.eclipse.core.resources.ResourcesPlugin; 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.core.runtime.IProgressMonitor; 31 import org.eclipse.core.runtime.IStatus; 32 import org.eclipse.core.runtime.Path; 33 import org.eclipse.core.runtime.Status; 34 import org.eclipse.jface.dialogs.MessageDialog; 35 import org.eclipse.jface.operation.IRunnableWithProgress; 36 import org.eclipse.jface.viewers.ISelection; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.wizard.Wizard; 39 import org.eclipse.ui.INewWizard; 40 import org.eclipse.ui.IWorkbench; 41 import org.eclipse.ui.IWorkbenchPage; 42 import org.eclipse.ui.IWorkbenchWizard; 43 import org.eclipse.ui.PartInitException; 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.ide.IDE; 46 47 55 56 public class NewXDoc extends Wizard implements INewWizard { 57 protected NewXdocPage page; 58 protected ISelection selection; 59 protected String resourceAntScript = "/src/org/apache/forrest/template/template_build.xml"; 60 61 64 public NewXDoc() { 65 super(); 66 setNeedsProgressMonitor(true); 67 } 68 69 72 public void addPages() { 73 page = new NewXdocPage(selection); 74 addPage(page); 75 } 76 77 82 public boolean performFinish() { 83 final String containerName = page.getContainerName(); 84 final String fileName = page.getFileName(); 85 IRunnableWithProgress op = new IRunnableWithProgress() { 86 public void run(IProgressMonitor monitor) throws InvocationTargetException { 87 try { 88 doFinish(containerName, fileName, monitor); 89 } catch (CoreException e) { 90 throw new InvocationTargetException (e); 91 } finally { 92 monitor.done(); 93 } 94 } 95 }; 96 try { 97 getContainer().run(true, false, op); 98 } catch (InterruptedException e) { 99 return false; 100 } catch (InvocationTargetException e) { 101 Throwable realException = e.getTargetException(); 102 MessageDialog.openError(getShell(), "Error", realException.getMessage()); 103 return false; 104 } 105 return true; 106 } 107 108 113 114 private void doFinish( 115 String containerName, 116 String fileName, 117 IProgressMonitor monitor) 118 throws CoreException { 119 monitor.beginTask("Creating " + fileName, 2); 121 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 122 IResource resource = root.findMember(new Path(containerName)); 123 if (!resource.exists() || !(resource instanceof IContainer)) { 124 throwCoreException("Container \"" + containerName + "\" does not exist."); 125 } 126 127 createFile(resource, fileName, monitor); 128 129 IContainer container = (IContainer) resource; 130 final IFile file = container.getFile(new Path(fileName)); 131 132 monitor.worked(1); 133 monitor.setTaskName("Opening file for editing..."); 134 getShell().getDisplay().asyncExec(new Runnable () { 135 public void run() { 136 IWorkbenchPage page = 137 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 138 try { 139 IDE.openEditor(page, file, true); 140 } catch (PartInitException e) { 141 } 142 } 143 }); 144 monitor.worked(1); 145 } 146 147 150 protected void createFile( 151 IResource resource, 152 String fileName, 153 IProgressMonitor monitor) 154 throws CoreException { 155 AntRunner runner = new AntRunner(); 156 ForrestPlugin plugin = ForrestPlugin.getDefault(); 157 String strPluginDir = plugin.getBundle().getLocation(); 158 if (strPluginDir.startsWith("update@")) { 159 strPluginDir = strPluginDir.substring(8); 160 } 161 runner.setBuildFileLocation(strPluginDir + resourceAntScript); 162 String strPath = resource.getLocation().toOSString(); 163 StringBuffer sb = new StringBuffer ("-Dresource.dir="); 164 sb.append(strPath); 165 sb.append(" "); 166 sb.append("-Dresource.name="); 167 sb.append(fileName); 168 runner.setArguments(sb.toString()); 169 runner.run(monitor); 170 resource.refreshLocal(IProject.DEPTH_INFINITE, monitor); 171 } 172 173 private void throwCoreException(String message) throws CoreException { 174 IStatus status = 175 new Status(IStatus.ERROR, "org.apache.forrest.eclipse", IStatus.OK, message, null); 176 throw new CoreException(status); 177 } 178 179 184 public void init(IWorkbench workbench, IStructuredSelection selection) { 185 this.selection = selection; 186 } 187 } | Popular Tags |