1 17 package org.eclipse.emf.mapping.ecore2ecore.presentation; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.MissingResourceException ; 28 import java.util.StringTokenizer ; 29 30 import org.eclipse.emf.common.util.URI; 31 32 import org.eclipse.emf.ecore.EClass; 33 import org.eclipse.emf.ecore.EClassifier; 34 35 import org.eclipse.emf.ecore.resource.Resource; 36 import org.eclipse.emf.ecore.resource.ResourceSet; 37 38 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 39 40 import org.eclipse.emf.ecore.EObject; 41 42 import org.eclipse.emf.ecore.xmi.XMLResource; 43 44 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; 45 46 import org.eclipse.core.resources.IContainer; 47 import org.eclipse.core.resources.IFile; 48 import org.eclipse.core.resources.IFolder; 49 import org.eclipse.core.resources.IProject; 50 import org.eclipse.core.resources.IResource; 51 import org.eclipse.core.resources.ResourcesPlugin; 52 53 import org.eclipse.core.runtime.IProgressMonitor; 54 import org.eclipse.core.runtime.Path; 55 56 import org.eclipse.jface.dialogs.MessageDialog; 57 58 import org.eclipse.jface.viewers.ISelection; 59 import org.eclipse.jface.viewers.IStructuredSelection; 60 import org.eclipse.jface.viewers.StructuredSelection; 61 62 import org.eclipse.jface.wizard.Wizard; 63 import org.eclipse.jface.wizard.WizardPage; 64 65 import org.eclipse.swt.SWT; 66 67 import org.eclipse.swt.events.ModifyListener; 68 import org.eclipse.swt.events.ModifyEvent; 69 70 import org.eclipse.swt.layout.GridData; 71 import org.eclipse.swt.layout.GridLayout; 72 73 import org.eclipse.swt.widgets.Composite; 74 import org.eclipse.swt.widgets.Combo; 75 import org.eclipse.swt.widgets.Label; 76 77 import org.eclipse.ui.INewWizard; 78 import org.eclipse.ui.IWorkbench; 79 import org.eclipse.ui.IWorkbenchPage; 80 import org.eclipse.ui.IWorkbenchPart; 81 import org.eclipse.ui.IWorkbenchWindow; 82 import org.eclipse.ui.PartInitException; 83 84 import org.eclipse.ui.actions.WorkspaceModifyOperation; 85 86 import org.eclipse.ui.dialogs.WizardNewFileCreationPage; 87 88 import org.eclipse.ui.part.FileEditorInput; 89 import org.eclipse.ui.part.ISetSelectionTarget; 90 91 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreFactory; 92 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreMappingRoot; 93 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcorePackage; 94 95 96 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcorePlugin; 97 98 99 105 public class Ecore2EcoreModelWizard extends Wizard implements INewWizard 106 { 107 113 protected Ecore2EcorePackage ecore2EcorePackage = Ecore2EcorePackage.eINSTANCE; 114 115 121 protected Ecore2EcoreFactory ecore2EcoreFactory = ecore2EcorePackage.getEcore2EcoreFactory(); 122 123 129 protected Ecore2EcoreModelWizardNewFileCreationPage newFileCreationPage; 130 131 137 protected Ecore2EcoreModelWizardInitialObjectCreationPage initialObjectCreationPage; 138 139 145 protected IStructuredSelection selection; 146 147 153 protected IWorkbench workbench; 154 155 161 protected List initialObjectNames; 162 163 169 public void init(IWorkbench workbench, IStructuredSelection selection) 170 { 171 this.workbench = workbench; 172 this.selection = selection; 173 setWindowTitle(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Wizard_label")); 174 setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(Ecore2EcoreEditorPlugin.INSTANCE.getImage("full/wizban/NewEcore2Ecore"))); 175 } 176 177 183 protected Collection getInitialObjectNames() 184 { 185 if (initialObjectNames == null) 186 { 187 initialObjectNames = new ArrayList (); 188 for (Iterator classifiers = ecore2EcorePackage.getEClassifiers().iterator(); classifiers.hasNext(); ) 189 { 190 EClassifier eClassifier = (EClassifier)classifiers.next(); 191 if (eClassifier instanceof EClass) 192 { 193 EClass eClass = (EClass)eClassifier; 194 if (!eClass.isAbstract()) 195 { 196 initialObjectNames.add(eClass.getName()); 197 } 198 } 199 } 200 Collections.sort(initialObjectNames, java.text.Collator.getInstance()); 201 } 202 return initialObjectNames; 203 } 204 205 211 protected EObject createInitialModelGen() 212 { 213 EClass eClass = (EClass)ecore2EcorePackage.getEClassifier(initialObjectCreationPage.getInitialObjectName()); 214 EObject rootObject = ecore2EcoreFactory.create(eClass); 215 return rootObject; 216 } 217 218 EObject createInitialModel() 219 { 220 EObject rootObject = createInitialModelGen(); 221 222 if (Ecore2EcoreMappingRoot.class.isInstance(rootObject)) 223 { 224 ((Ecore2EcoreMappingRoot)rootObject).setTopToBottom(true); 225 } 226 227 return rootObject; 228 } 229 230 236 public boolean performFinish() 237 { 238 try 239 { 240 final IFile modelFile = getModelFile(); 243 244 WorkspaceModifyOperation operation = 247 new WorkspaceModifyOperation() 248 { 249 protected void execute(IProgressMonitor progressMonitor) 250 { 251 try 252 { 253 ResourceSet resourceSet = new ResourceSetImpl(); 256 257 URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString()); 260 261 Resource resource = resourceSet.createResource(fileURI); 264 265 EObject rootObject = createInitialModel(); 268 if (rootObject != null) 269 { 270 resource.getContents().add(rootObject); 271 } 272 273 Map options = new HashMap (); 276 options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding()); 277 resource.save(options); 278 } 279 catch (Exception exception) 280 { 281 Ecore2EcoreEditorPlugin.INSTANCE.log(exception); 282 } 283 finally 284 { 285 progressMonitor.done(); 286 } 287 } 288 }; 289 290 getContainer().run(false, false, operation); 291 292 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); 295 IWorkbenchPage page = workbenchWindow.getActivePage(); 296 final IWorkbenchPart activePart = page.getActivePart(); 297 if (activePart instanceof ISetSelectionTarget) 298 { 299 final ISelection targetSelection = new StructuredSelection(modelFile); 300 getShell().getDisplay().asyncExec 301 (new Runnable () 302 { 303 public void run() 304 { 305 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 306 } 307 }); 308 } 309 310 try 313 { 314 page.openEditor 315 (new FileEditorInput(modelFile), 316 workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); 317 } 318 catch (PartInitException exception) 319 { 320 MessageDialog.openError(workbenchWindow.getShell(), Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); 321 return false; 322 } 323 324 return true; 325 } 326 catch (Exception exception) 327 { 328 Ecore2EcoreEditorPlugin.INSTANCE.log(exception); 329 return false; 330 } 331 } 332 333 339 public class Ecore2EcoreModelWizardNewFileCreationPage extends WizardNewFileCreationPage 340 { 341 347 public Ecore2EcoreModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) 348 { 349 super(pageId, selection); 350 } 351 352 358 protected boolean validatePage() 359 { 360 if (super.validatePage()) 361 { 362 String requiredExt = Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameExtension"); 365 String enteredExt = new Path(getFileName()).getFileExtension(); 366 if (enteredExt == null || !enteredExt.equals(requiredExt)) 367 { 368 setErrorMessage(Ecore2EcoreEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object [] { requiredExt })); 369 return false; 370 } 371 else 372 { 373 return true; 374 } 375 } 376 else 377 { 378 return false; 379 } 380 } 381 382 387 public IFile getModelFile() 388 { 389 return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); 390 } 391 } 392 393 399 public class Ecore2EcoreModelWizardInitialObjectCreationPage extends WizardPage 400 { 401 406 protected Combo initialObjectField; 407 408 413 protected List encodings; 414 415 420 protected Combo encodingField; 421 422 428 public Ecore2EcoreModelWizardInitialObjectCreationPage(String pageId) 429 { 430 super(pageId); 431 } 432 433 438 public void createControl(Composite parent) 439 { 440 Composite composite = new Composite(parent, SWT.NONE); 441 { 442 GridLayout layout = new GridLayout(); 443 layout.numColumns = 1; 444 layout.verticalSpacing = 12; 445 composite.setLayout(layout); 446 447 GridData data = new GridData(); 448 data.verticalAlignment = GridData.FILL; 449 data.grabExcessVerticalSpace = true; 450 data.horizontalAlignment = GridData.FILL; 451 composite.setLayoutData(data); 452 } 453 454 Label containerLabel = new Label(composite, SWT.LEFT); 455 { 456 containerLabel.setText(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_ModelObject")); 457 458 GridData data = new GridData(); 459 data.horizontalAlignment = GridData.FILL; 460 containerLabel.setLayoutData(data); 461 } 462 463 initialObjectField = new Combo(composite, SWT.BORDER); 464 { 465 GridData data = new GridData(); 466 data.horizontalAlignment = GridData.FILL; 467 data.grabExcessHorizontalSpace = true; 468 initialObjectField.setLayoutData(data); 469 } 470 471 for (Iterator i = getInitialObjectNames().iterator(); i.hasNext(); ) 472 { 473 initialObjectField.add(getLabel((String )i.next())); 474 } 475 476 if (initialObjectField.getItemCount() == 1) 477 { 478 initialObjectField.select(0); 479 } 480 initialObjectField.addModifyListener(validator); 481 482 Label encodingLabel = new Label(composite, SWT.LEFT); 483 { 484 encodingLabel.setText(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_XMLEncoding")); 485 486 GridData data = new GridData(); 487 data.horizontalAlignment = GridData.FILL; 488 encodingLabel.setLayoutData(data); 489 } 490 encodingField = new Combo(composite, SWT.BORDER); 491 { 492 GridData data = new GridData(); 493 data.horizontalAlignment = GridData.FILL; 494 data.grabExcessHorizontalSpace = true; 495 encodingField.setLayoutData(data); 496 } 497 498 for (Iterator i = getEncodings().iterator(); i.hasNext(); ) 499 { 500 encodingField.add((String )i.next()); 501 } 502 503 encodingField.select(0); 504 encodingField.addModifyListener(validator); 505 506 setPageComplete(validatePage()); 507 setControl(composite); 508 } 509 510 515 protected ModifyListener validator = 516 new ModifyListener() 517 { 518 public void modifyText(ModifyEvent e) 519 { 520 setPageComplete(validatePage()); 521 } 522 }; 523 524 529 protected boolean validatePage() 530 { 531 return getInitialObjectName() != null && getEncodings().contains(encodingField.getText()); 532 } 533 534 539 public void setVisible(boolean visible) 540 { 541 super.setVisible(visible); 542 if (visible) 543 { 544 if (initialObjectField.getItemCount() == 1) 545 { 546 initialObjectField.clearSelection(); 547 encodingField.setFocus(); 548 } 549 else 550 { 551 encodingField.clearSelection(); 552 initialObjectField.setFocus(); 553 } 554 } 555 } 556 557 562 public String getInitialObjectName() 563 { 564 String label = initialObjectField.getText(); 565 566 for (Iterator i = getInitialObjectNames().iterator(); i.hasNext(); ) 567 { 568 String name = (String )i.next(); 569 if (getLabel(name).equals(label)) 570 { 571 return name; 572 } 573 } 574 return null; 575 } 576 577 582 public String getEncoding() 583 { 584 return encodingField.getText(); 585 } 586 587 593 protected String getLabel(String typeName) 594 { 595 try 596 { 597 return Ecore2EcorePlugin.INSTANCE.getString("_UI_" + typeName + "_type"); 598 } 599 catch(MissingResourceException mre) 600 { 601 Ecore2EcoreEditorPlugin.INSTANCE.log(mre); 602 } 603 return typeName; 604 } 605 606 611 protected Collection getEncodings() 612 { 613 if (encodings == null) 614 { 615 encodings = new ArrayList (); 616 for (StringTokenizer stringTokenizer = new StringTokenizer (Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) 617 { 618 encodings.add(stringTokenizer.nextToken()); 619 } 620 } 621 return encodings; 622 } 623 } 624 625 631 public void addPages() 632 { 633 newFileCreationPage = new Ecore2EcoreModelWizardNewFileCreationPage("Whatever", selection); 636 newFileCreationPage.setTitle(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreModelWizard_label")); 637 newFileCreationPage.setDescription(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreModelWizard_description")); 638 newFileCreationPage.setFileName(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameDefaultBase") + "." + Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameExtension")); 639 addPage(newFileCreationPage); 640 641 if (selection != null && !selection.isEmpty()) 644 { 645 Object selectedElement = selection.iterator().next(); 648 if (selectedElement instanceof IResource) 649 { 650 IResource selectedResource = (IResource)selectedElement; 653 if (selectedResource.getType() == IResource.FILE) 654 { 655 selectedResource = selectedResource.getParent(); 656 } 657 658 if (selectedResource instanceof IFolder || selectedResource instanceof IProject) 661 { 662 newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); 665 666 String defaultModelBaseFilename = Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameDefaultBase"); 669 String defaultModelFilenameExtension = Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameExtension"); 670 String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; 671 for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) 672 { 673 modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; 674 } 675 newFileCreationPage.setFileName(modelFilename); 676 } 677 } 678 } 679 initialObjectCreationPage = new Ecore2EcoreModelWizardInitialObjectCreationPage("Whatever2"); 680 initialObjectCreationPage.setTitle(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreModelWizard_label")); 681 initialObjectCreationPage.setDescription(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); 682 addPage(initialObjectCreationPage); 683 } 684 685 691 public IFile getModelFile() 692 { 693 return newFileCreationPage.getModelFile(); 694 } 695 696 } | Popular Tags |