1 17 package org.eclipse.emf.mapping.ecore2xml.presentation; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 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 34 36 import org.eclipse.emf.ecore.resource.Resource; 37 import org.eclipse.emf.ecore.resource.ResourceSet; 38 39 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 40 41 import org.eclipse.emf.ecore.EObject; 42 43 import org.eclipse.emf.ecore.xmi.XMLResource; 44 45 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; 46 47 import org.eclipse.core.resources.IContainer; 48 import org.eclipse.core.resources.IFile; 49 import org.eclipse.core.resources.IFolder; 50 import org.eclipse.core.resources.IProject; 51 import org.eclipse.core.resources.IResource; 52 import org.eclipse.core.resources.ResourcesPlugin; 53 54 import org.eclipse.core.runtime.IProgressMonitor; 55 56 import org.eclipse.jface.dialogs.MessageDialog; 57 58 import org.eclipse.jface.viewers.IStructuredSelection; 59 60 import org.eclipse.jface.wizard.Wizard; 61 import org.eclipse.jface.wizard.WizardPage; 62 63 import org.eclipse.swt.SWT; 64 65 import org.eclipse.swt.events.ModifyListener; 66 import org.eclipse.swt.events.ModifyEvent; 67 68 import org.eclipse.swt.layout.GridData; 69 import org.eclipse.swt.layout.GridLayout; 70 71 import org.eclipse.swt.widgets.Combo; 72 import org.eclipse.swt.widgets.Composite; 73 import org.eclipse.swt.widgets.Label; 74 75 import org.eclipse.ui.INewWizard; 76 import org.eclipse.ui.IWorkbench; 77 78 import org.eclipse.ui.actions.WorkspaceModifyOperation; 79 80 import org.eclipse.ui.dialogs.WizardNewFileCreationPage; 81 82 import org.eclipse.ui.part.FileEditorInput; 83 import org.eclipse.ui.part.ISetSelectionTarget; 84 85 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLFactory; 86 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLPackage; 87 import org.eclipse.emf.mapping.ecore2xml.ui.Ecore2XMLUIPlugin; 88 89 90 import org.eclipse.core.runtime.Path; 91 92 import org.eclipse.jface.viewers.ISelection; 93 import org.eclipse.jface.viewers.StructuredSelection; 94 95 import org.eclipse.ui.IWorkbenchPage; 96 import org.eclipse.ui.IWorkbenchPart; 97 import org.eclipse.ui.IWorkbenchWindow; 98 import org.eclipse.ui.PartInitException; 99 100 101 107 public class Ecore2XMLModelWizard extends Wizard implements INewWizard 108 { 109 115 protected Ecore2XMLPackage ecore2XMLPackage = Ecore2XMLPackage.eINSTANCE; 116 117 123 protected Ecore2XMLFactory ecore2XMLFactory = ecore2XMLPackage.getEcore2XMLFactory(); 124 125 131 protected Ecore2XMLModelWizardNewFileCreationPage newFileCreationPage; 132 133 139 protected Ecore2XMLModelWizardInitialObjectCreationPage initialObjectCreationPage; 140 141 147 protected IStructuredSelection selection; 148 149 155 protected IWorkbench workbench; 156 157 163 protected List initialObjectNames; 164 165 171 public void init(IWorkbench workbench, IStructuredSelection selection) 172 { 173 this.workbench = workbench; 174 this.selection = selection; 175 setWindowTitle(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Wizard_label")); setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(Ecore2XMLUIPlugin.INSTANCE.getImage("full/wizban/NewEcore2XML"))); } 178 179 185 protected Collection getInitialObjectNames() 186 { 187 if (initialObjectNames == null) 188 { 189 initialObjectNames = new ArrayList (); 190 initialObjectNames.add(ecore2XMLPackage.getXMLMap().getName()); 191 } 192 return initialObjectNames; 193 } 194 195 201 protected EObject createInitialModel() 202 { 203 EClass eClass = (EClass)ecore2XMLPackage.getEClassifier(initialObjectCreationPage.getInitialObjectName()); 204 EObject rootObject = ecore2XMLFactory.create(eClass); 205 return rootObject; 206 } 207 208 214 public boolean performFinish() 215 { 216 try 217 { 218 final IFile modelFile = getModelFile(); 221 222 WorkspaceModifyOperation operation = 225 new WorkspaceModifyOperation() 226 { 227 protected void execute(IProgressMonitor progressMonitor) 228 { 229 try 230 { 231 ResourceSet resourceSet = new ResourceSetImpl(); 234 235 URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString()); 238 239 Resource resource = resourceSet.createResource(fileURI); 242 243 EObject rootObject = createInitialModel(); 246 if (rootObject != null) 247 { 248 resource.getContents().add(rootObject); 249 } 250 251 Map options = new HashMap (); 254 options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding()); 255 resource.save(options); 256 } 257 catch (Exception exception) 258 { 259 Ecore2XMLUIPlugin.INSTANCE.log(exception); 260 } 261 finally 262 { 263 progressMonitor.done(); 264 } 265 } 266 }; 267 268 getContainer().run(false, false, operation); 269 270 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); 273 IWorkbenchPage page = workbenchWindow.getActivePage(); 274 final IWorkbenchPart activePart = page.getActivePart(); 275 if (activePart instanceof ISetSelectionTarget) 276 { 277 final ISelection targetSelection = new StructuredSelection(modelFile); 278 getShell().getDisplay().asyncExec 279 (new Runnable () 280 { 281 public void run() 282 { 283 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 284 } 285 }); 286 } 287 288 try 291 { 292 page.openEditor 293 (new FileEditorInput(modelFile), 294 workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); 295 } 296 catch (PartInitException exception) 297 { 298 MessageDialog.openError(workbenchWindow.getShell(), Ecore2XMLUIPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); return false; 300 } 301 302 return true; 303 } 304 catch (Exception exception) 305 { 306 Ecore2XMLUIPlugin.INSTANCE.log(exception); 307 return false; 308 } 309 } 310 311 317 public class Ecore2XMLModelWizardNewFileCreationPage extends WizardNewFileCreationPage 318 { 319 325 public Ecore2XMLModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) 326 { 327 super(pageId, selection); 328 } 329 330 336 protected boolean validatePage() 337 { 338 if (super.validatePage()) 339 { 340 String requiredExt = Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameExtension"); String enteredExt = new Path(getFileName()).getFileExtension(); 344 if (enteredExt == null || !enteredExt.equals(requiredExt)) 345 { 346 setErrorMessage(Ecore2XMLUIPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object [] { requiredExt })); return false; 348 } 349 else 350 { 351 return true; 352 } 353 } 354 else 355 { 356 return false; 357 } 358 } 359 360 365 public IFile getModelFile() 366 { 367 return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); 368 } 369 } 370 371 377 public class Ecore2XMLModelWizardInitialObjectCreationPage extends WizardPage 378 { 379 384 protected Combo initialObjectField; 385 386 391 protected List encodings; 392 393 398 protected Combo encodingField; 399 400 406 public Ecore2XMLModelWizardInitialObjectCreationPage(String pageId) 407 { 408 super(pageId); 409 } 410 411 416 public void createControl(Composite parent) 417 { 418 Composite composite = new Composite(parent, SWT.NONE); 419 { 420 GridLayout layout = new GridLayout(); 421 layout.numColumns = 1; 422 layout.verticalSpacing = 12; 423 composite.setLayout(layout); 424 425 GridData data = new GridData(); 426 data.verticalAlignment = GridData.FILL; 427 data.grabExcessVerticalSpace = true; 428 data.horizontalAlignment = GridData.FILL; 429 composite.setLayoutData(data); 430 } 431 432 Label containerLabel = new Label(composite, SWT.LEFT); 433 { 434 containerLabel.setText(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_ModelObject")); 436 GridData data = new GridData(); 437 data.horizontalAlignment = GridData.FILL; 438 containerLabel.setLayoutData(data); 439 } 440 441 initialObjectField = new Combo(composite, SWT.BORDER); 442 { 443 GridData data = new GridData(); 444 data.horizontalAlignment = GridData.FILL; 445 data.grabExcessHorizontalSpace = true; 446 initialObjectField.setLayoutData(data); 447 } 448 449 for (Iterator i = getInitialObjectNames().iterator(); i.hasNext(); ) 450 { 451 initialObjectField.add(getLabel((String )i.next())); 452 } 453 454 if (initialObjectField.getItemCount() == 1) 455 { 456 initialObjectField.select(0); 457 } 458 initialObjectField.addModifyListener(validator); 459 460 Label encodingLabel = new Label(composite, SWT.LEFT); 461 { 462 encodingLabel.setText(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_XMLEncoding")); 464 GridData data = new GridData(); 465 data.horizontalAlignment = GridData.FILL; 466 encodingLabel.setLayoutData(data); 467 } 468 encodingField = new Combo(composite, SWT.BORDER); 469 { 470 GridData data = new GridData(); 471 data.horizontalAlignment = GridData.FILL; 472 data.grabExcessHorizontalSpace = true; 473 encodingField.setLayoutData(data); 474 } 475 476 for (Iterator i = getEncodings().iterator(); i.hasNext(); ) 477 { 478 encodingField.add((String )i.next()); 479 } 480 481 encodingField.select(0); 482 encodingField.addModifyListener(validator); 483 484 setPageComplete(validatePage()); 485 setControl(composite); 486 } 487 488 493 protected ModifyListener validator = 494 new ModifyListener() 495 { 496 public void modifyText(ModifyEvent e) 497 { 498 setPageComplete(validatePage()); 499 } 500 }; 501 502 507 protected boolean validatePage() 508 { 509 return getInitialObjectName() != null && getEncodings().contains(encodingField.getText()); 510 } 511 512 517 public void setVisible(boolean visible) 518 { 519 super.setVisible(visible); 520 if (visible) 521 { 522 if (initialObjectField.getItemCount() == 1) 523 { 524 initialObjectField.clearSelection(); 525 encodingField.setFocus(); 526 } 527 else 528 { 529 encodingField.clearSelection(); 530 initialObjectField.setFocus(); 531 } 532 } 533 } 534 535 540 public String getInitialObjectName() 541 { 542 String label = initialObjectField.getText(); 543 544 for (Iterator i = getInitialObjectNames().iterator(); i.hasNext(); ) 545 { 546 String name = (String )i.next(); 547 if (getLabel(name).equals(label)) 548 { 549 return name; 550 } 551 } 552 return null; 553 } 554 555 560 public String getEncoding() 561 { 562 return encodingField.getText(); 563 } 564 565 571 protected String getLabel(String typeName) 572 { 573 try 574 { 575 return Ecore2XMLUIPlugin.INSTANCE.getString("_UI_" + typeName + "_type"); } 577 catch(MissingResourceException mre) 578 { 579 Ecore2XMLUIPlugin.INSTANCE.log(mre); 580 } 581 return typeName; 582 } 583 584 589 protected Collection getEncodings() 590 { 591 if (encodings == null) 592 { 593 encodings = new ArrayList (); 594 for (StringTokenizer stringTokenizer = new StringTokenizer (Ecore2XMLUIPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) { 596 encodings.add(stringTokenizer.nextToken()); 597 } 598 } 599 return encodings; 600 } 601 } 602 603 609 public void addPages() 610 { 611 newFileCreationPage = new Ecore2XMLModelWizardNewFileCreationPage("Whatever", selection); newFileCreationPage.setTitle(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLModelWizard_label")); newFileCreationPage.setDescription(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLModelWizard_description")); newFileCreationPage.setFileName(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameDefaultBase") + "." + Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameExtension")); addPage(newFileCreationPage); 618 619 if (selection != null && !selection.isEmpty()) 622 { 623 Object selectedElement = selection.iterator().next(); 626 if (selectedElement instanceof IResource) 627 { 628 IResource selectedResource = (IResource)selectedElement; 631 if (selectedResource.getType() == IResource.FILE) 632 { 633 selectedResource = selectedResource.getParent(); 634 } 635 636 if (selectedResource instanceof IFolder || selectedResource instanceof IProject) 639 { 640 newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); 643 644 String defaultModelBaseFilename = Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameDefaultBase"); String defaultModelFilenameExtension = Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameExtension"); String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) 650 { 651 modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; } 653 newFileCreationPage.setFileName(modelFilename); 654 } 655 } 656 } 657 initialObjectCreationPage = new Ecore2XMLModelWizardInitialObjectCreationPage("Whatever2"); initialObjectCreationPage.setTitle(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLModelWizard_label")); initialObjectCreationPage.setDescription(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); addPage(initialObjectCreationPage); 661 } 662 663 669 public IFile getModelFile() 670 { 671 return newFileCreationPage.getModelFile(); 672 } 673 674 } 675 | Popular Tags |