1 package org.eclipse.emf.examples.jet.article2.ui; 2 3 4 import java.lang.reflect.InvocationTargetException ; 5 6 7 import org.eclipse.core.resources.IFile; 8 import org.eclipse.core.resources.IResource; 9 import org.eclipse.core.resources.IWorkspaceRunnable; 10 import org.eclipse.core.runtime.CoreException; 11 import org.eclipse.core.runtime.IProgressMonitor; 12 import org.eclipse.core.runtime.IStatus; 13 import org.eclipse.core.runtime.OperationCanceledException; 14 import org.eclipse.core.runtime.Status; 15 import org.eclipse.jdt.core.JavaCore; 16 import org.eclipse.jface.dialogs.DialogSettings; 17 import org.eclipse.jface.dialogs.ErrorDialog; 18 import org.eclipse.jface.dialogs.IDialogSettings; 19 import org.eclipse.jface.operation.IRunnableWithProgress; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.wizard.Wizard; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.ui.INewWizard; 25 import org.eclipse.ui.IWorkbench; 26 import org.eclipse.ui.IWorkbenchPage; 27 import org.eclipse.ui.PartInitException; 28 import org.eclipse.ui.ide.IDE; 29 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; 30 31 import org.eclipse.emf.examples.jet.article2.TypesafeEnumPlugin; 32 import org.eclipse.emf.examples.jet.article2.codegen.Config; 33 import org.eclipse.emf.examples.jet.article2.codegen.JETGateway; 34 35 36 43 public class NewTypesafeEnumCreationWizard extends Wizard implements INewWizard 44 { 45 46 private IWorkbench mWorkbench; 47 48 private IStructuredSelection mSelection; 49 50 private static final String DIALOG_SETTINGS_KEY = "NewTypesafeEnumCreationWizard"; 51 52 private NewTypesafeEnumCreationWizardPage mPage1; 53 54 private NewTypesafeEnumCreationWizardPageAttributes mPage2; 55 56 private NewTypesafeEnumCreationWizardPageInstances mPage3; 57 58 61 public NewTypesafeEnumCreationWizard() 62 { 63 super(); 64 setNeedsProgressMonitor(true); 65 setDefaultPageImageDescriptor(TypesafeEnumPlugin.getImageDescriptor("icons/newclass_wiz.gif")); 66 initDialogSettings(); 67 setWindowTitle(WizardMessages.getString("Wizard.title.new")); } 69 70 76 public void init(IWorkbench workbench, IStructuredSelection currentSelection) 77 { 78 mWorkbench = workbench; 79 mSelection = currentSelection; 80 } 81 82 85 public void addPages() 86 { 87 super.addPages(); 88 89 mPage1 = new NewTypesafeEnumCreationWizardPage(); 90 addPage(mPage1); 91 mPage1.init(getSelection()); 92 93 mPage2 = new NewTypesafeEnumCreationWizardPageAttributes(); 94 addPage(mPage2); 95 96 mPage3 = new NewTypesafeEnumCreationWizardPageInstances(); 97 addPage(mPage3); 98 } 99 100 106 protected void finishPage(IProgressMonitor monitor) 107 throws InterruptedException , CoreException 108 { 109 Config config = new Config(); 110 config.setModel(mPage1.getTypesafeEnumModel()); 111 config.setPackageName(mPage1.getPackageText()); 112 config.setTargetFile(mPage1.getTypeName() + ".java"); 113 config.setTargetFolder(mPage1.getPackageFragmentRootText()); 114 115 config.setClasspathVariable("JET_TUTORIAL"); 116 config.setPluginId(TypesafeEnumPlugin.getPluginId()); 117 config.setTemplateRelativeUri("templates/TypeSafeEnumeration.javajet"); 118 119 JETGateway gateway = new JETGateway(config); 120 String content = gateway.generate(monitor); 121 IFile file = gateway.save(monitor, content.getBytes()); 122 123 selectAndReveal(file); 124 openResource(file); 125 } 126 127 130 public boolean performFinish() 131 { 132 IWorkspaceRunnable op = new IWorkspaceRunnable() 133 { 134 public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException 135 { 136 try 137 { 138 finishPage(monitor); 139 } 140 catch (InterruptedException e) 141 { 142 throw new OperationCanceledException(e.getMessage()); 143 } 144 } 145 }; 146 147 try 148 { 149 getContainer().run(false, true, new WorkbenchRunnableAdapter(op)); 150 } 151 catch (InvocationTargetException e) 152 { 153 handleFinishException(getShell(), e); 154 return false; 155 } 156 catch (InterruptedException e) 157 { 158 return false; 159 } 160 return true; 161 } 162 163 170 private static class WorkbenchRunnableAdapter implements IRunnableWithProgress 171 { 172 173 private IWorkspaceRunnable fWorkspaceRunnable; 174 175 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) 176 { 177 fWorkspaceRunnable = runnable; 178 } 179 180 183 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException 184 { 185 try 186 { 187 JavaCore.run(fWorkspaceRunnable, monitor); 188 } 189 catch (OperationCanceledException e) 190 { 191 throw new InterruptedException (e.getMessage()); 192 } 193 catch (CoreException e) 194 { 195 throw new InvocationTargetException (e); 196 } 197 } 198 } 199 200 protected void handleFinishException(Shell shell, InvocationTargetException e) 201 { 202 String exceptionMessage = "Error when generating the Typesafe Enumeration Class."; 203 if(e.getCause() != null && e.getCause().getMessage() != null) 204 { 205 exceptionMessage = e.getCause().getMessage(); 206 } 207 else if(e.getMessage() != null) 208 { 209 exceptionMessage = e.getMessage(); 210 } 211 212 IStatus status = new Status(IStatus.ERROR, TypesafeEnumPlugin.getPluginId(), IStatus.ERROR, exceptionMessage, e); 213 TypesafeEnumPlugin.log(status); 214 215 String title = WizardMessages.getString("Wizard.op_error.title"); String message = WizardMessages.getString("Wizard.op_error.message"); ErrorDialog.openError(shell, title, message, status); 218 } 219 220 protected void openResource(final IResource resource) 221 { 222 if (resource.getType() == IResource.FILE) 223 { 224 final IWorkbenchPage activePage = TypesafeEnumPlugin.getActivePage(); 225 if (activePage != null) 226 { 227 final Display display = getShell().getDisplay(); 228 if (display != null) 229 { 230 display.asyncExec(new Runnable () 231 { 232 public void run() 233 { 234 try 235 { 236 IDE.openEditor(activePage, (IFile)resource, true); 237 } 238 catch (PartInitException e) 239 { 240 TypesafeEnumPlugin.log(e); 241 } 242 } 243 }); 244 } 245 } 246 } 247 } 248 249 protected void selectAndReveal(IResource newResource) 250 { 251 BasicNewResourceWizard.selectAndReveal(newResource, mWorkbench.getActiveWorkbenchWindow()); 252 } 253 254 public IStructuredSelection getSelection() 255 { 256 return mSelection; 257 } 258 259 protected void initDialogSettings() 260 { 261 IDialogSettings pluginSettings = TypesafeEnumPlugin.getDefault().getDialogSettings(); 262 IDialogSettings wizardSettings = pluginSettings.getSection(DIALOG_SETTINGS_KEY); 263 if (wizardSettings == null) 264 { 265 wizardSettings = new DialogSettings(DIALOG_SETTINGS_KEY); 266 pluginSettings.addSection(wizardSettings); 267 } 268 setDialogSettings(wizardSettings); 269 } 270 } | Popular Tags |