1 17 package org.eclipse.emf.ecore.presentation; 18 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IFolder; 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.IResource; 28 import org.eclipse.core.resources.ResourcesPlugin; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.jface.viewers.ISelection; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.StructuredSelection; 35 import org.eclipse.jface.wizard.Wizard; 36 import org.eclipse.ui.IEditorDescriptor; 37 import org.eclipse.ui.INewWizard; 38 import org.eclipse.ui.IWorkbench; 39 import org.eclipse.ui.IWorkbenchPage; 40 import org.eclipse.ui.IWorkbenchPart; 41 import org.eclipse.ui.IWorkbenchWindow; 42 import org.eclipse.ui.PartInitException; 43 import org.eclipse.ui.actions.WorkspaceModifyOperation; 44 import org.eclipse.ui.dialogs.WizardNewFileCreationPage; 45 import org.eclipse.ui.part.FileEditorInput; 46 import org.eclipse.ui.part.ISetSelectionTarget; 47 48 import org.eclipse.emf.common.util.URI; 49 import org.eclipse.emf.ecore.EClass; 50 import org.eclipse.emf.ecore.EObject; 51 import org.eclipse.emf.ecore.plugin.EcorePlugin; 52 import org.eclipse.emf.ecore.resource.Resource; 53 import org.eclipse.emf.ecore.resource.ResourceSet; 54 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 55 import org.eclipse.emf.ecore.util.EcoreUtil; 56 import org.eclipse.emf.edit.EMFEditPlugin; 57 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; 58 59 60 63 public class DynamicModelWizard extends Wizard implements INewWizard 64 { 65 68 protected EClass eClass; 69 70 73 protected DynamicModelWizardNewFileCreationPage newFileCreationPage; 74 75 78 protected IStructuredSelection selection; 79 80 83 protected IWorkbench workbench; 84 85 88 public DynamicModelWizard(EClass eClass) 89 { 90 this.eClass = eClass; 91 } 92 93 96 public void init(IWorkbench workbench, IStructuredSelection selection) 97 { 98 this.workbench = workbench; 99 this.selection = selection; 100 setDefaultPageImageDescriptor 101 (ExtendedImageRegistry.INSTANCE.getImageDescriptor(EMFEditPlugin.INSTANCE.getImage("full/wizban/NewModel"))); 102 } 103 104 107 EObject createInitialModel() 108 { 109 return EcoreUtil.create(eClass); 110 } 111 112 115 public boolean performFinish() 116 { 117 try 118 { 119 final IFile modelFile = getModelFile(); 122 123 WorkspaceModifyOperation operation = 126 new WorkspaceModifyOperation() 127 { 128 protected void execute(IProgressMonitor progressMonitor) 129 { 130 try 131 { 132 ResourceSet resourceSet = new ResourceSetImpl(); 135 resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap()); 136 137 URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString()); 140 141 Resource resource = resourceSet.createResource(fileURI); 144 145 EObject rootObject = createInitialModel(); 148 if (rootObject != null) 149 { 150 resource.getContents().add(rootObject); 151 } 152 153 Map options = new HashMap (); 156 options.put("SCHEMA_LOCATION", Boolean.TRUE); 157 options.put("LINE_WIDTH", new Integer (80)); 158 resource.save(options); 159 } 160 catch (Exception exception) 161 { 162 exception.printStackTrace(); 163 } 164 finally 165 { 166 progressMonitor.done(); 167 } 168 } 169 }; 170 171 getContainer().run(false, false, operation); 172 173 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); 176 IWorkbenchPage page = workbenchWindow.getActivePage(); 177 final IWorkbenchPart activePart = page.getActivePart(); 178 if (activePart instanceof ISetSelectionTarget) 179 { 180 final ISelection targetSelection = new StructuredSelection(modelFile); 181 getShell().getDisplay().asyncExec 182 (new Runnable () 183 { 184 public void run() 185 { 186 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 187 } 188 }); 189 } 190 191 try 194 { 195 IEditorDescriptor editorDescriptor = workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()); 196 if (editorDescriptor != null) 197 { 198 page.openEditor(new FileEditorInput(modelFile), editorDescriptor.getId()); 199 } 200 } 201 catch (PartInitException exception) 202 { 203 MessageDialog.openError 204 (workbenchWindow.getShell(), EcoreEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); 205 return false; 206 } 207 208 return true; 209 } 210 catch (Exception exception) 211 { 212 exception.printStackTrace(); 213 return false; 214 } 215 } 216 217 220 public class DynamicModelWizardNewFileCreationPage extends WizardNewFileCreationPage 221 { 222 225 protected IFile modelFile; 226 227 230 public DynamicModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) 231 { 232 super(pageId, selection); 233 } 234 235 238 protected boolean validatePage() 239 { 240 if (super.validatePage()) 241 { 242 String requiredExt = EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicInstanceFilenameExtension"); 245 String enteredExt = new Path(getFileName()).getFileExtension(); 246 if (enteredExt == null || !enteredExt.equals(requiredExt)) 247 { 248 setErrorMessage(EcoreEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object [] { requiredExt })); 249 return false; 250 } 251 else 252 { 253 return true; 254 } 255 } 256 else 257 { 258 return false; 259 } 260 } 261 262 265 public boolean performFinish() 266 { 267 modelFile = getModelFile(); 268 return true; 269 } 270 271 273 public IFile getModelFile() 274 { 275 return 276 modelFile == null ? 277 ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())) : 278 modelFile; 279 } 280 } 281 282 285 public void addPages() 286 { 287 newFileCreationPage = new DynamicModelWizardNewFileCreationPage("Whatever", selection); 290 newFileCreationPage.setTitle(EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicModelWizard_label")); 291 newFileCreationPage.setDescription 292 (EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicModelWizard_description", new Object [] { eClass.getName() })); 293 newFileCreationPage.setFileName 294 (eClass.getName() + "." + EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicInstanceFilenameExtension")); 295 addPage(newFileCreationPage); 296 297 if (selection != null && !selection.isEmpty()) 300 { 301 Object selectedElement = selection.iterator().next(); 304 if (selectedElement instanceof IResource) 305 { 306 IResource selectedResource = (IResource)selectedElement; 309 if (selectedResource.getType() == IResource.FILE) 310 { 311 selectedResource = selectedResource.getParent(); 312 } 313 314 if (selectedResource instanceof IFolder || selectedResource instanceof IProject) 317 { 318 newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); 321 322 String defaultModelBaseFilename = eClass.getName(); 325 String defaultModelFilenameExtension = EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicInstanceFilenameExtension"); 326 String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; 327 for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) 328 { 329 modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; 330 } 331 newFileCreationPage.setFileName(modelFilename); 332 } 333 } 334 } 335 } 336 337 340 public IFile getModelFile() 341 { 342 return newFileCreationPage.getModelFile(); 343 } 344 } 345 | Popular Tags |