1 7 package org.eclipse.emf.ecore.presentation; 8 9 10 import java.util.ArrayList ; 11 import java.util.Collection ; 12 import java.util.Collections ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.MissingResourceException ; 18 import java.util.StringTokenizer ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IFolder; 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.jface.viewers.ISelection; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.wizard.Wizard; 33 import org.eclipse.jface.wizard.WizardPage; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.events.ModifyListener; 36 import org.eclipse.swt.events.ModifyEvent; 37 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Composite; 41 import org.eclipse.swt.widgets.Combo; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.ui.INewWizard; 44 import org.eclipse.ui.IWorkbench; 45 import org.eclipse.ui.IWorkbenchPage; 46 import org.eclipse.ui.IWorkbenchPart; 47 import org.eclipse.ui.IWorkbenchWindow; 48 import org.eclipse.ui.PartInitException; 49 import org.eclipse.ui.actions.WorkspaceModifyOperation; 50 import org.eclipse.ui.dialogs.WizardNewFileCreationPage; 51 import org.eclipse.ui.part.FileEditorInput; 52 import org.eclipse.ui.part.ISetSelectionTarget; 53 54 import org.eclipse.emf.common.util.URI; 55 import org.eclipse.emf.ecore.EClass; 56 import org.eclipse.emf.ecore.EClassifier; 57 import org.eclipse.emf.ecore.EObject; 58 import org.eclipse.emf.ecore.EcoreFactory; 59 import org.eclipse.emf.ecore.EcorePackage; 60 import org.eclipse.emf.ecore.plugin.EcorePlugin; 61 import org.eclipse.emf.ecore.provider.EcoreEditPlugin; 62 63 64 import org.eclipse.emf.ecore.resource.Resource; 65 import org.eclipse.emf.ecore.resource.ResourceSet; 66 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 67 import org.eclipse.emf.ecore.xmi.XMLResource; 68 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; 69 70 71 77 public class EcoreModelWizard extends Wizard implements INewWizard 78 { 79 85 protected EcorePackage ecorePackage = EcorePackage.eINSTANCE; 86 87 93 protected EcoreFactory ecoreFactory = ecorePackage.getEcoreFactory(); 94 95 101 protected EcoreModelWizardNewFileCreationPage newFileCreationPage; 102 103 109 protected EcoreModelWizardInitialObjectCreationPage initialObjectCreationPage; 110 111 117 protected IStructuredSelection selection; 118 119 125 protected IWorkbench workbench; 126 127 133 protected List initialObjectNames; 134 135 141 public void init(IWorkbench workbench, IStructuredSelection selection) 142 { 143 this.workbench = workbench; 144 this.selection = selection; 145 setWindowTitle(EcoreEditorPlugin.INSTANCE.getString("_UI_Wizard_label")); 146 setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(EcoreEditorPlugin.INSTANCE.getImage("full/wizban/NewEcore"))); 147 } 148 149 155 protected Collection getInitialObjectNames() 156 { 157 if (initialObjectNames == null) 158 { 159 initialObjectNames = new ArrayList (); 160 for (Iterator classifiers = ecorePackage.getEClassifiers().iterator(); classifiers.hasNext(); ) 161 { 162 EClassifier eClassifier = (EClassifier)classifiers.next(); 163 if (eClassifier instanceof EClass) 164 { 165 EClass eClass = (EClass)eClassifier; 166 if (!eClass.isAbstract()) 167 { 168 initialObjectNames.add(eClass.getName()); 169 } 170 } 171 } 172 Collections.sort(initialObjectNames, java.text.Collator.getInstance()); 173 } 174 return initialObjectNames; 175 } 176 177 183 protected EObject createInitialModel() 184 { 185 EClass eClass = (EClass)ecorePackage.getEClassifier(initialObjectCreationPage.getInitialObjectName()); 186 EObject rootObject = ecoreFactory.create(eClass); 187 return rootObject; 188 } 189 190 196 public boolean performFinish() 197 { 198 try 199 { 200 final IFile modelFile = getModelFile(); 203 204 WorkspaceModifyOperation operation = 207 new WorkspaceModifyOperation() 208 { 209 protected void execute(IProgressMonitor progressMonitor) 210 { 211 try 212 { 213 ResourceSet resourceSet = new ResourceSetImpl(); 216 resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap()); 217 218 URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString()); 221 222 Resource resource = resourceSet.createResource(fileURI); 225 226 EObject rootObject = createInitialModel(); 229 if (rootObject != null) 230 { 231 resource.getContents().add(rootObject); 232 } 233 234 Map options = new HashMap (); 237 options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding()); 238 resource.save(options); 239 } 240 catch (Exception exception) 241 { 242 EcoreEditorPlugin.INSTANCE.log(exception); 243 } 244 finally 245 { 246 progressMonitor.done(); 247 } 248 } 249 }; 250 251 getContainer().run(false, false, operation); 252 253 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); 256 IWorkbenchPage page = workbenchWindow.getActivePage(); 257 final IWorkbenchPart activePart = page.getActivePart(); 258 if (activePart instanceof ISetSelectionTarget) 259 { 260 final ISelection targetSelection = new StructuredSelection(modelFile); 261 getShell().getDisplay().asyncExec 262 (new Runnable () 263 { 264 public void run() 265 { 266 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 267 } 268 }); 269 } 270 271 try 274 { 275 page.openEditor 276 (new FileEditorInput(modelFile), 277 workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); 278 } 279 catch (PartInitException exception) 280 { 281 MessageDialog.openError(workbenchWindow.getShell(), EcoreEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); 282 return false; 283 } 284 285 return true; 286 } 287 catch (Exception exception) 288 { 289 EcoreEditorPlugin.INSTANCE.log(exception); 290 return false; 291 } 292 } 293 294 300 public class EcoreModelWizardNewFileCreationPage extends WizardNewFileCreationPage 301 { 302 308 public EcoreModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) 309 { 310 super(pageId, selection); 311 } 312 313 319 protected boolean validatePage() 320 { 321 if (super.validatePage()) 322 { 323 String requiredExt = EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreEditorFilenameExtension"); 326 String enteredExt = new Path(getFileName()).getFileExtension(); 327 if (enteredExt == null || !enteredExt.equals(requiredExt)) 328 { 329 setErrorMessage(EcoreEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object [] { requiredExt })); 330 return false; 331 } 332 else 333 { 334 return true; 335 } 336 } 337 else 338 { 339 return false; 340 } 341 } 342 343 348 public IFile getModelFile() 349 { 350 return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); 351 } 352 } 353 354 360 public class EcoreModelWizardInitialObjectCreationPage extends WizardPage 361 { 362 367 protected Combo initialObjectField; 368 369 374 protected List encodings; 375 376 381 protected Combo encodingField; 382 383 389 public EcoreModelWizardInitialObjectCreationPage(String pageId) 390 { 391 super(pageId); 392 } 393 394 399 public void createControl(Composite parent) 400 { 401 Composite composite = new Composite(parent, SWT.NONE); 402 { 403 GridLayout layout = new GridLayout(); 404 layout.numColumns = 1; 405 layout.verticalSpacing = 12; 406 composite.setLayout(layout); 407 408 GridData data = new GridData(); 409 data.verticalAlignment = GridData.FILL; 410 data.grabExcessVerticalSpace = true; 411 data.horizontalAlignment = GridData.FILL; 412 composite.setLayoutData(data); 413 } 414 415 Label containerLabel = new Label(composite, SWT.LEFT); 416 { 417 containerLabel.setText(EcoreEditorPlugin.INSTANCE.getString("_UI_ModelObject")); 418 419 GridData data = new GridData(); 420 data.horizontalAlignment = GridData.FILL; 421 containerLabel.setLayoutData(data); 422 } 423 424 initialObjectField = new Combo(composite, SWT.BORDER); 425 { 426 GridData data = new GridData(); 427 data.horizontalAlignment = GridData.FILL; 428 data.grabExcessHorizontalSpace = true; 429 initialObjectField.setLayoutData(data); 430 } 431 432 for (Iterator i = getInitialObjectNames().iterator(); i.hasNext(); ) 433 { 434 initialObjectField.add(getLabel((String )i.next())); 435 } 436 437 if (initialObjectField.getItemCount() == 1) 438 { 439 initialObjectField.select(0); 440 } 441 initialObjectField.addModifyListener(validator); 442 443 Label encodingLabel = new Label(composite, SWT.LEFT); 444 { 445 encodingLabel.setText(EcoreEditorPlugin.INSTANCE.getString("_UI_XMLEncoding")); 446 447 GridData data = new GridData(); 448 data.horizontalAlignment = GridData.FILL; 449 encodingLabel.setLayoutData(data); 450 } 451 encodingField = new Combo(composite, SWT.BORDER); 452 { 453 GridData data = new GridData(); 454 data.horizontalAlignment = GridData.FILL; 455 data.grabExcessHorizontalSpace = true; 456 encodingField.setLayoutData(data); 457 } 458 459 for (Iterator i = getEncodings().iterator(); i.hasNext(); ) 460 { 461 encodingField.add((String )i.next()); 462 } 463 464 encodingField.select(0); 465 encodingField.addModifyListener(validator); 466 467 setPageComplete(validatePage()); 468 setControl(composite); 469 } 470 471 476 protected ModifyListener validator = 477 new ModifyListener() 478 { 479 public void modifyText(ModifyEvent e) 480 { 481 setPageComplete(validatePage()); 482 } 483 }; 484 485 490 protected boolean validatePage() 491 { 492 return getInitialObjectName() != null && getEncodings().contains(encodingField.getText()); 493 } 494 495 500 public void setVisible(boolean visible) 501 { 502 super.setVisible(visible); 503 if (visible) 504 { 505 if (initialObjectField.getItemCount() == 1) 506 { 507 initialObjectField.clearSelection(); 508 encodingField.setFocus(); 509 } 510 else 511 { 512 encodingField.clearSelection(); 513 initialObjectField.setFocus(); 514 } 515 } 516 } 517 518 523 public String getInitialObjectName() 524 { 525 String label = initialObjectField.getText(); 526 527 for (Iterator i = getInitialObjectNames().iterator(); i.hasNext(); ) 528 { 529 String name = (String )i.next(); 530 if (getLabel(name).equals(label)) 531 { 532 return name; 533 } 534 } 535 return null; 536 } 537 538 543 public String getEncoding() 544 { 545 return encodingField.getText(); 546 } 547 548 554 protected String getLabel(String typeName) 555 { 556 try 557 { 558 return EcoreEditPlugin.INSTANCE.getString("_UI_" + typeName + "_type"); 559 } 560 catch(MissingResourceException mre) 561 { 562 EcoreEditorPlugin.INSTANCE.log(mre); 563 } 564 return typeName; 565 } 566 567 572 protected Collection getEncodings() 573 { 574 if (encodings == null) 575 { 576 encodings = new ArrayList (); 577 for (StringTokenizer stringTokenizer = new StringTokenizer (EcoreEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) 578 { 579 encodings.add(stringTokenizer.nextToken()); 580 } 581 } 582 return encodings; 583 } 584 } 585 586 592 public void addPages() 593 { 594 newFileCreationPage = new EcoreModelWizardNewFileCreationPage("Whatever", selection); 597 newFileCreationPage.setTitle(EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreModelWizard_label")); 598 newFileCreationPage.setDescription(EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreModelWizard_description")); 599 newFileCreationPage.setFileName(EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreEditorFilenameDefaultBase") + "." + EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreEditorFilenameExtension")); 600 addPage(newFileCreationPage); 601 602 if (selection != null && !selection.isEmpty()) 605 { 606 Object selectedElement = selection.iterator().next(); 609 if (selectedElement instanceof IResource) 610 { 611 IResource selectedResource = (IResource)selectedElement; 614 if (selectedResource.getType() == IResource.FILE) 615 { 616 selectedResource = selectedResource.getParent(); 617 } 618 619 if (selectedResource instanceof IFolder || selectedResource instanceof IProject) 622 { 623 newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); 626 627 String defaultModelBaseFilename = EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreEditorFilenameDefaultBase"); 630 String defaultModelFilenameExtension = EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreEditorFilenameExtension"); 631 String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; 632 for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) 633 { 634 modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; 635 } 636 newFileCreationPage.setFileName(modelFilename); 637 } 638 } 639 } 640 initialObjectCreationPage = new EcoreModelWizardInitialObjectCreationPage("Whatever2"); 641 initialObjectCreationPage.setTitle(EcoreEditorPlugin.INSTANCE.getString("_UI_EcoreModelWizard_label")); 642 initialObjectCreationPage.setDescription(EcoreEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); 643 addPage(initialObjectCreationPage); 644 } 645 646 652 public IFile getModelFile() 653 { 654 return newFileCreationPage.getModelFile(); 655 } 656 657 } 658 | Popular Tags |