1 17 package org.eclipse.emf.importer.ui.contribution.base; 18 19 import java.util.List ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.jface.dialogs.ErrorDialog; 27 import org.eclipse.jface.dialogs.IPageChangeProvider; 28 import org.eclipse.jface.dialogs.IPageChangedListener; 29 import org.eclipse.jface.dialogs.MessageDialog; 30 import org.eclipse.jface.dialogs.PageChangedEvent; 31 import org.eclipse.jface.viewers.ISelection; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.jface.viewers.StructuredSelection; 34 import org.eclipse.jface.wizard.IWizardContainer; 35 import org.eclipse.jface.wizard.Wizard; 36 import org.eclipse.jface.wizard.WizardDialog; 37 import org.eclipse.ui.IWorkbench; 38 import org.eclipse.ui.IWorkbenchPage; 39 import org.eclipse.ui.IWorkbenchPart; 40 import org.eclipse.ui.PartInitException; 41 import org.eclipse.ui.actions.WorkspaceModifyOperation; 42 import org.eclipse.ui.part.FileEditorInput; 43 import org.eclipse.ui.part.ISetSelectionTarget; 44 45 import org.eclipse.emf.edit.provider.IDisposable; 46 import org.eclipse.emf.importer.ImporterPlugin; 47 import org.eclipse.emf.importer.ModelImporter; 48 import org.eclipse.emf.importer.ui.contribution.IModelImporterWizard; 49 import org.eclipse.emf.importer.util.ImporterUtil; 50 51 52 55 public abstract class ModelImporterWizard extends Wizard implements IModelImporterWizard 56 { 57 public static class PageHelper implements IPageChangedListener, IDisposable 58 { 59 protected Object oldPage; 60 protected IPageChangeProvider pageChangeProvider; 61 62 public void dispose() 63 { 64 oldPage = null; 65 pageChangeProvider = null; 66 } 67 68 public void pageChanged(PageChangedEvent event) 69 { 70 pageChangeProvider = event.getPageChangeProvider(); 71 pageChanged(event.getSelectedPage()); 72 } 73 74 protected void pageChanged(Object currentPage) 75 { 76 if (oldPage != currentPage) 77 { 78 int cause = ModelImporterPage.CAUSE_UNKNOWN; 79 if (oldPage instanceof ModelImporterPage) 80 { 81 ModelImporterPage page = (ModelImporterPage)oldPage; 82 cause = page.forwardDirection ? ModelImporterPage.CAUSE_NEXT : ModelImporterPage.CAUSE_BACK; 83 page.pageDeactivated(cause); 84 } 85 86 oldPage = currentPage; 87 if (currentPage instanceof ModelImporterPage) 88 { 89 ModelImporterPage page = (ModelImporterPage)currentPage; 90 page.pageActivated(page.neverVisible, cause); 91 page.neverVisible = false; 92 } 93 } 94 } 95 96 public void firePageDeactivated(int cause) 97 { 98 if (pageChangeProvider != null && pageChangeProvider.getSelectedPage() instanceof ModelImporterPage) 99 { 100 ((ModelImporterPage)pageChangeProvider.getSelectedPage()).pageDeactivated(cause); 101 } 102 } 103 } 104 105 protected IStructuredSelection selection; 106 protected IWorkbench workbench; 107 protected PageHelper pageHelper; 108 109 protected ModelImporter modelImporter; 110 111 protected IFile originalGenModelFile; 112 protected IFile modelFile; 113 protected String fileName; 114 protected IPath genModelContainerPath; 115 protected IPath projectLocation; 116 protected IPath projectPath; 117 118 public ModelImporterWizard() 119 { 120 super(); 121 setNeedsProgressMonitor(true); 122 } 123 124 public void dispose() 125 { 126 selection = null; 127 workbench = null; 128 129 if (modelImporter != null) 130 { 131 modelImporter.dispose(); 132 modelImporter = null; 133 } 134 135 if (pageHelper != null) 136 { 137 if (getContainer() instanceof WizardDialog) 138 { 139 ((WizardDialog)getContainer()).removePageChangedListener(pageHelper); 140 } 141 pageHelper.dispose(); 142 pageHelper = null; 143 } 144 145 super.dispose(); 146 } 147 148 protected abstract ModelImporter createModelImporter(); 149 150 public ModelImporter getModelImporter() 151 { 152 if (modelImporter == null) 153 { 154 modelImporter = createModelImporter(); 155 } 156 return modelImporter; 157 } 158 159 public void init(IWorkbench workbench, IStructuredSelection selection) 160 { 161 this.workbench = workbench; 162 this.selection = selection; 163 } 164 165 public IWorkbench getWorkbench() 166 { 167 return workbench; 168 } 169 170 public IStructuredSelection getSelection() 171 { 172 return selection; 173 } 174 175 public List getFileExtensions() 176 { 177 return getModelImporter().getFileExtensions(); 178 } 179 180 public void setContainer(IWizardContainer wizardContainer) 181 { 182 super.setContainer(wizardContainer); 183 if (wizardContainer instanceof WizardDialog) 184 { 185 pageHelper = new PageHelper(); 186 ((WizardDialog)wizardContainer).addPageChangedListener(pageHelper); 187 } 188 } 189 190 public void setOriginalGenModelFile(IFile originalGenModelFile) 191 { 192 this.originalGenModelFile = originalGenModelFile; 193 if (originalGenModelFile != null) 194 { 195 setWindowTitle(ImporterPlugin.INSTANCE.getString("_UI_ReloadWizard_title")); 196 getModelImporter().defineOriginalGenModelPath(originalGenModelFile.getFullPath()); 197 } 198 } 199 200 public IFile getOriginalGenModelFile() 201 { 202 return originalGenModelFile; 203 } 204 205 public void setModelFile(IFile modelFile) 206 { 207 this.modelFile = modelFile; 208 getModelImporter().setModelFile(modelFile); 209 } 210 211 public IFile getModelFile() 212 { 213 return modelFile; 214 } 215 216 public void setGenModelFileName(String fileName) 217 { 218 this.fileName = fileName; 219 getModelImporter().setGenModelFileName(fileName); 220 } 221 222 public String getGenModelFileName() 223 { 224 return fileName; 225 } 226 227 public void setGenModelContainerPath(IPath genModelContainerPath) 228 { 229 this.genModelContainerPath = genModelContainerPath; 230 getModelImporter().setGenModelContainerPath(genModelContainerPath); 231 } 232 233 public IPath getGenModelContainerPath() 234 { 235 return genModelContainerPath; 236 } 237 238 public void setGenModelProjectLocation(IPath projectLocation) 239 { 240 this.projectLocation = projectLocation; 241 getModelImporter().setGenModelProjectLocation(projectLocation); 242 } 243 244 public IPath getGenModelProjectLocation() 245 { 246 return projectLocation; 247 } 248 249 public void setGenModelProjectPath(IPath projectPath) 250 { 251 this.projectPath = projectPath; 252 getModelImporter().setGenModelContainerPath(getModelImporter().computeGenModelContainerPath(projectPath)); 253 } 254 255 public IPath getGenModelProjectPath() 256 { 257 return projectPath; 258 } 259 260 public boolean performCancel() 261 { 262 pageHelper.firePageDeactivated(ModelImporterPage.CAUSE_CANCEL); 263 return true; 264 } 265 266 public boolean performFinish() 267 { 268 pageHelper.firePageDeactivated(ModelImporterPage.CAUSE_FINISH); 269 270 try 271 { 272 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() 273 { 274 protected void execute(IProgressMonitor progressMonitor) throws CoreException 275 { 276 try 277 { 278 getModelImporter().prepareGenModelAndEPackages(progressMonitor); 279 getModelImporter().saveGenModelAndEPackages(progressMonitor); 280 } 281 catch (Exception exception) 282 { 283 throw new CoreException(ImporterUtil.createErrorStatus(exception, true)); 284 } 285 finally 286 { 287 progressMonitor.done(); 288 } 289 } 290 }; 291 292 try 293 { 294 getContainer().run(false, false, operation); 295 } 296 catch (Exception exception) 297 { 298 ImporterPlugin.INSTANCE.log(exception); 299 ErrorDialog.openError(getShell(), ImporterPlugin.INSTANCE.getString("_UI_SaveError_title"), null, ImporterUtil.createErrorStatus(exception, true)); 300 return false; 301 } 302 303 IFile genModelFile = getFile(getModelImporter().getGenModelPath()); 304 if (getModelImporter().getOriginalGenModelPath() == null) 305 { 306 selectFile(genModelFile); 307 } 308 309 try 310 { 311 openEditor(genModelFile); 312 } 313 catch (PartInitException partInitException) 314 { 315 MessageDialog.openError(getShell(), ImporterPlugin.INSTANCE.getString("_UI_OpenEditor_title"), partInitException.getMessage()); 316 return false; 317 } 318 319 return true; 320 } 321 catch (Exception exception) 322 { 323 ImporterPlugin.INSTANCE.log(exception); 324 return false; 325 } 326 } 327 328 protected IFile getFile(IPath path) 329 { 330 return ResourcesPlugin.getWorkspace().getRoot().getFile(path); 331 } 332 333 protected void selectFile(IFile file) 334 { 335 IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage(); 336 final IWorkbenchPart activePart = page.getActivePart(); 337 if (activePart instanceof ISetSelectionTarget) 338 { 339 final ISelection targetSelection = new StructuredSelection(file); 340 getShell().getDisplay().asyncExec(new Runnable () 341 { 342 public void run() 343 { 344 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 345 } 346 }); 347 } 348 } 349 350 protected void openEditor(IFile file) throws PartInitException 351 { 352 IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage(); 353 page.openEditor(new FileEditorInput(file), getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId()); 354 } 355 } 356 | Popular Tags |