1 package $packageName$; 2 3 import org.eclipse.jface.viewers.IStructuredSelection; 4 import org.eclipse.jface.wizard.Wizard; 5 import org.eclipse.ui.INewWizard; 6 import org.eclipse.ui.IWorkbench; 7 import org.eclipse.core.runtime.*; 8 import org.eclipse.jface.operation.*; 9 import java.lang.reflect.InvocationTargetException ; 10 import org.eclipse.jface.dialogs.MessageDialog; 11 import org.eclipse.jface.viewers.ISelection; 12 import org.eclipse.core.resources.*; 13 import org.eclipse.core.runtime.CoreException; 14 import java.io.*; 15 import org.eclipse.ui.*; 16 17 27 28 public class $wizardClassName$ extends Wizard implements INewWizard { 29 private $wizardPageClassName$ page; 30 private ISelection selection; 31 32 35 public $wizardClassName$() { 36 super(); 37 setNeedsProgressMonitor(true); 38 } 39 40 43 44 public void addPages() { 45 page = new $wizardPageClassName$(selection); 46 addPage(page); 47 } 48 49 54 public boolean performFinish() { 55 final String containerName = page.getContainerName(); 56 final String fileName = page.getFileName(); 57 IRunnableWithProgress op = new IRunnableWithProgress() { 58 public void run(IProgressMonitor monitor) throws InvocationTargetException { 59 try { 60 doFinish(containerName, fileName, monitor); 61 } catch (CoreException e) { 62 throw new InvocationTargetException (e); 63 } finally { 64 monitor.done(); 65 } 66 } 67 }; 68 try { 69 getContainer().run(true, false, op); 70 } catch (InterruptedException e) { 71 return false; 72 } catch (InvocationTargetException e) { 73 Throwable realException = e.getTargetException(); 74 MessageDialog.openError(getShell(), "Error", realException.getMessage()); 75 return false; 76 } 77 return true; 78 } 79 80 85 86 private void doFinish( 87 String containerName, 88 String fileName, 89 IProgressMonitor monitor) 90 throws CoreException { 91 monitor.beginTask("Creating " + fileName, 2); 93 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 94 IResource resource = root.findMember(new Path(containerName)); 95 if (!resource.exists() || !(resource instanceof IContainer)) { 96 throwCoreException("Container \"" + containerName + "\" does not exist."); 97 } 98 IContainer container = (IContainer) resource; 99 final IFile file = container.getFile(new Path(fileName)); 100 try { 101 InputStream stream = openContentStream(); 102 if (file.exists()) { 103 file.setContents(stream, true, true, monitor); 104 } else { 105 file.create(stream, true, monitor); 106 } 107 stream.close(); 108 } catch (IOException e) { 109 } 110 monitor.worked(1); 111 monitor.setTaskName("Opening file for editing..."); 112 getShell().getDisplay().asyncExec(new Runnable () { 113 public void run() { 114 IWorkbenchPage page = 115 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 116 try { 117 page.openEditor(file); 118 } catch (PartInitException e) { 119 } 120 } 121 }); 122 monitor.worked(1); 123 } 124 125 128 129 private InputStream openContentStream() { 130 String contents = 131 "This is the initial file contents for *.$extension$ file that should be word-sorted in the Preview page of the multi-page editor"; 132 return new ByteArrayInputStream(contents.getBytes()); 133 } 134 135 private void throwCoreException(String message) throws CoreException { 136 IStatus status = 137 new Status(IStatus.ERROR, "$pluginId$", IStatus.OK, message, null); 138 throw new CoreException(status); 139 } 140 141 146 public void init(IWorkbench workbench, IStructuredSelection selection) { 147 this.selection = selection; 148 } 149 } | Popular Tags |