1 7 package org.enhydra.dods.editor.Doml.presentation; 8 9 10 import java.util.ArrayList ; 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.MissingResourceException ; 17 import java.util.StringTokenizer ; 18 19 import org.eclipse.emf.common.util.URI; 20 21 import org.eclipse.emf.ecore.EClass; 22 import org.eclipse.emf.ecore.EClassifier; 23 24 import org.eclipse.emf.ecore.resource.Resource; 25 import org.eclipse.emf.ecore.resource.ResourceSet; 26 27 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 28 29 import org.eclipse.emf.ecore.EObject; 30 31 import org.eclipse.emf.ecore.xmi.XMLResource; 32 33 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; 34 35 import org.eclipse.core.resources.IContainer; 36 import org.eclipse.core.resources.IFile; 37 import org.eclipse.core.resources.IFolder; 38 import org.eclipse.core.resources.IProject; 39 import org.eclipse.core.resources.IResource; 40 import org.eclipse.core.resources.ResourcesPlugin; 41 42 import org.eclipse.core.runtime.IProgressMonitor; 43 44 import org.eclipse.jface.dialogs.MessageDialog; 45 46 import org.eclipse.jface.viewers.IStructuredSelection; 47 48 import org.eclipse.jface.wizard.Wizard; 49 import org.eclipse.jface.wizard.WizardPage; 50 51 import org.eclipse.swt.SWT; 52 53 import org.eclipse.swt.events.SelectionAdapter; 54 import org.eclipse.swt.events.SelectionEvent; 55 56 import org.eclipse.swt.layout.GridData; 57 import org.eclipse.swt.layout.GridLayout; 58 59 import org.eclipse.swt.widgets.Combo; 60 import org.eclipse.swt.widgets.Composite; 61 import org.eclipse.swt.widgets.Label; 62 63 import org.eclipse.ui.INewWizard; 64 import org.eclipse.ui.IWorkbench; 65 66 import org.eclipse.ui.actions.WorkspaceModifyOperation; 67 68 import org.eclipse.ui.dialogs.WizardNewFileCreationPage; 69 70 import org.eclipse.ui.part.FileEditorInput; 71 import org.eclipse.ui.part.ISetSelectionTarget; 72 73 import org.enhydra.dods.editor.Doml.DomlFactory; 74 import org.enhydra.dods.editor.Doml.DomlPackage; 75 import org.enhydra.dods.editor.Doml.provider.DomlEditPlugin; 76 77 78 import org.eclipse.core.runtime.Path; 79 80 import org.eclipse.emf.ecore.EStructuralFeature; 81 82 import org.eclipse.emf.ecore.util.EcoreUtil; 83 import org.eclipse.emf.ecore.util.ExtendedMetaData; 84 85 import org.eclipse.jface.viewers.ISelection; 86 import org.eclipse.jface.viewers.StructuredSelection; 87 88 import org.eclipse.ui.IWorkbenchPage; 89 import org.eclipse.ui.IWorkbenchPart; 90 import org.eclipse.ui.IWorkbenchWindow; 91 import org.eclipse.ui.PartInitException; 92 93 94 100 public class DomlModelWizard extends Wizard implements INewWizard { 101 107 protected DomlPackage domlPackage = DomlPackage.eINSTANCE; 108 109 115 protected DomlFactory domlFactory = domlPackage.getDomlFactory(); 116 117 123 protected DomlModelWizardNewFileCreationPage newFileCreationPage; 124 125 131 protected DomlModelWizardInitialObjectCreationPage initialObjectCreationPage; 132 133 139 protected IStructuredSelection selection; 140 141 147 protected IWorkbench workbench; 148 149 155 public void init(IWorkbench workbench, IStructuredSelection selection) { 156 this.workbench = workbench; 157 this.selection = selection; 158 setWindowTitle(DomlEditPlugin.INSTANCE.getString("_UI_Wizard_label")); 159 setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(DomlEditPlugin.INSTANCE.getImage("full/wizban/NewDoml"))); 160 } 161 162 168 EObject createInitialModel() { 169 EClass eClass = ExtendedMetaData.INSTANCE.getDocumentRoot(domlPackage); 170 EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(initialObjectCreationPage.getInitialObjectName()); 171 EObject rootObject = domlFactory.create(eClass); 172 rootObject.eSet(eStructuralFeature, EcoreUtil.create((EClass)eStructuralFeature.getEType())); 173 return rootObject; 174 } 175 176 182 public boolean performFinish() { 183 try { 184 final IFile modelFile = getModelFile(); 187 188 WorkspaceModifyOperation operation = 191 new WorkspaceModifyOperation() { 192 protected void execute(IProgressMonitor progressMonitor) { 193 try { 194 ResourceSet resourceSet = new ResourceSetImpl(); 197 198 URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString()); 201 202 Resource resource = resourceSet.createResource(fileURI); 205 206 EObject rootObject = createInitialModel(); 209 if (rootObject != null) { 210 resource.getContents().add(rootObject); 211 } 212 213 Map options = new HashMap (); 216 options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding()); 217 resource.save(options); 218 } 219 catch (Exception exception) { 220 DomlEditPlugin.INSTANCE.log(exception); 221 } 222 finally { 223 progressMonitor.done(); 224 } 225 } 226 }; 227 228 getContainer().run(false, false, operation); 229 230 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); 233 IWorkbenchPage page = workbenchWindow.getActivePage(); 234 final IWorkbenchPart activePart = page.getActivePart(); 235 if (activePart instanceof ISetSelectionTarget) { 236 final ISelection targetSelection = new StructuredSelection(modelFile); 237 getShell().getDisplay().asyncExec 238 (new Runnable () { 239 public void run() { 240 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 241 } 242 }); 243 } 244 245 try { 248 page.openEditor 249 (new FileEditorInput(modelFile), 250 workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); 251 } 252 catch (PartInitException exception) { 253 MessageDialog.openError(workbenchWindow.getShell(), DomlEditPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); 254 return false; 255 } 256 257 return true; 258 } 259 catch (Exception exception) { 260 DomlEditPlugin.INSTANCE.log(exception); 261 return false; 262 } 263 } 264 265 271 public class DomlModelWizardNewFileCreationPage extends WizardNewFileCreationPage { 272 278 protected IFile modelFile; 279 280 286 public DomlModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) { 287 super(pageId, selection); 288 } 289 290 296 protected boolean validatePage() { 297 if (super.validatePage()) { 298 String requiredExt = DomlEditPlugin.INSTANCE.getString("_UI_DomlEditorFilenameExtension"); 301 String enteredExt = new Path(getFileName()).getFileExtension(); 302 if (enteredExt == null || !enteredExt.equals(requiredExt)) { 303 setErrorMessage(DomlEditPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object [] { requiredExt })); 304 return false; 305 } 306 else { 307 return true; 308 } 309 } 310 else { 311 return false; 312 } 313 } 314 315 321 public boolean performFinish() { 322 modelFile = getModelFile(); 323 return true; 324 } 325 326 331 public IFile getModelFile() { 332 return 333 modelFile == null ? 334 ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())) : 335 modelFile; 336 } 337 } 338 339 345 public class DomlModelWizardInitialObjectCreationPage extends WizardPage { 346 351 protected String initialObjectName; 352 353 358 protected Combo initialObjectField; 359 360 365 protected String encoding; 366 367 372 protected Combo encodingField; 373 374 380 public DomlModelWizardInitialObjectCreationPage(String pageId) { 381 super(pageId); 382 } 383 384 389 public void createControl(Composite parent) { 390 Composite composite = new Composite(parent, SWT.NONE); 391 { 392 GridLayout layout = new GridLayout(); 393 layout.numColumns = 1; 394 layout.verticalSpacing = 12; 395 composite.setLayout(layout); 396 397 GridData data = new GridData(); 398 data.verticalAlignment = GridData.FILL; 399 data.grabExcessVerticalSpace = true; 400 data.horizontalAlignment = GridData.FILL; 401 composite.setLayoutData(data); 402 } 403 404 Label containerLabel = new Label(composite, SWT.LEFT); 405 { 406 containerLabel.setText(DomlEditPlugin.INSTANCE.getString("_UI_ModelObject")); 407 408 GridData data = new GridData(); 409 data.horizontalAlignment = GridData.FILL; 410 containerLabel.setLayoutData(data); 411 } 412 413 initialObjectField = new Combo(composite, SWT.BORDER); 414 { 415 GridData data = new GridData(); 416 data.horizontalAlignment = GridData.FILL; 417 data.grabExcessHorizontalSpace = true; 418 initialObjectField.setLayoutData(data); 419 } 420 421 List objectNames = new ArrayList (); 422 for (Iterator elements = ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE.getDocumentRoot(domlPackage)).iterator(); elements.hasNext(); ) { 423 EStructuralFeature eStructuralFeature = (EStructuralFeature)elements.next(); 424 EClassifier eClassifier = eStructuralFeature.getEType(); 425 if (eClassifier instanceof EClass) { 426 EClass eClass = (EClass)eClassifier; 427 if (!eClass.isAbstract()) { 428 objectNames.add(getLabel(eStructuralFeature)); 429 } 430 } 431 } 432 433 Collections.sort(objectNames, java.text.Collator.getInstance()); 434 for (Iterator i = objectNames.iterator(); i.hasNext(); ) { 435 String objectName = (String )i.next(); 436 initialObjectField.add(objectName); 437 } 438 439 initialObjectField.addSelectionListener 440 (new SelectionAdapter() { 441 public void widgetSelected(SelectionEvent e) { 442 setPageComplete(isPageComplete()); 443 } 444 }); 445 446 Label encodingLabel = new Label(composite, SWT.LEFT); 447 { 448 encodingLabel.setText(DomlEditPlugin.INSTANCE.getString("_UI_XMLEncoding")); 449 450 GridData data = new GridData(); 451 data.horizontalAlignment = GridData.FILL; 452 encodingLabel.setLayoutData(data); 453 } 454 encodingField = new Combo(composite, SWT.BORDER); 455 { 456 GridData data = new GridData(); 457 data.horizontalAlignment = GridData.FILL; 458 data.grabExcessHorizontalSpace = true; 459 encodingField.setLayoutData(data); 460 } 461 462 for (StringTokenizer stringTokenizer = new StringTokenizer (DomlEditPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) { 463 encodingField.add(stringTokenizer.nextToken()); 464 } 465 encodingField.select(0); 466 467 setControl(composite); 468 } 469 470 476 public boolean isPageComplete() { 477 if (super.isPageComplete()) { 478 return initialObjectField.getSelectionIndex() != -1; 479 } 480 else { 481 return false; 482 } 483 } 484 485 491 public boolean performFinish() { 492 initialObjectName = getInitialObjectName(); 493 encoding = getEncoding(); 494 return true; 495 } 496 497 502 public String getInitialObjectName() { 503 if (initialObjectName != null) { 504 return initialObjectName; 505 } 506 else { 507 String label = initialObjectField.getText(); 508 for (Iterator elements = ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE.getDocumentRoot(domlPackage)).iterator(); elements.hasNext(); ) { 509 EStructuralFeature eStructuralFeature = (EStructuralFeature)elements.next(); 510 EClassifier eClassifier = eStructuralFeature.getEType(); 511 if (eClassifier instanceof EClass) { 512 EClass eClass = (EClass)eClassifier; 513 if (!eClass.isAbstract() && getLabel(eStructuralFeature).equals(label)) { 514 return eStructuralFeature.getName(); 515 } 516 } 517 } 518 return label; 519 } 520 } 521 522 527 public String getEncoding() { 528 return 529 encoding == null ? 530 encodingField.getText() : 531 encoding; 532 } 533 539 protected String getLabel(EStructuralFeature eStructuralFeature) { 540 String name = eStructuralFeature.getName(); 541 try { 542 return DomlEditPlugin.INSTANCE.getString("_UI_" + name + "_feature"); 543 } 544 catch(MissingResourceException mre) { 545 } 546 return name; 547 } 548 } 549 550 556 public void addPages() { 557 newFileCreationPage = new DomlModelWizardNewFileCreationPage("Whatever", selection); 560 newFileCreationPage.setTitle(DomlEditPlugin.INSTANCE.getString("_UI_DomlModelWizard_label")); 561 newFileCreationPage.setDescription(DomlEditPlugin.INSTANCE.getString("_UI_DomlModelWizard_description")); 562 newFileCreationPage.setFileName(DomlEditPlugin.INSTANCE.getString("_UI_DomlEditorFilenameDefaultBase") + "." + DomlEditPlugin.INSTANCE.getString("_UI_DomlEditorFilenameExtension")); 563 addPage(newFileCreationPage); 564 565 if (selection != null && !selection.isEmpty()) { 568 Object selectedElement = selection.iterator().next(); 571 if (selectedElement instanceof IResource) { 572 IResource selectedResource = (IResource)selectedElement; 575 if (selectedResource.getType() == IResource.FILE) { 576 selectedResource = selectedResource.getParent(); 577 } 578 579 if (selectedResource instanceof IFolder || selectedResource instanceof IProject) { 582 String currentDirectory = selectedResource.getLocation().toOSString(); 585 newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); 586 587 String defaultModelBaseFilename = DomlEditPlugin.INSTANCE.getString("_UI_DomlEditorFilenameDefaultBase"); 590 String defaultModelFilenameExtension = DomlEditPlugin.INSTANCE.getString("_UI_DomlEditorFilenameExtension"); 591 String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; 592 for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) { 593 modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; 594 } 595 newFileCreationPage.setFileName(modelFilename); 596 } 597 } 598 } 599 initialObjectCreationPage = new DomlModelWizardInitialObjectCreationPage("Whatever2"); 600 initialObjectCreationPage.setTitle(DomlEditPlugin.INSTANCE.getString("_UI_DomlModelWizard_label")); 601 initialObjectCreationPage.setDescription(DomlEditPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); 602 addPage(initialObjectCreationPage); 603 } 604 605 611 public IFile getModelFile() { 612 return newFileCreationPage.getModelFile(); 613 } 614 615 } 616 | Popular Tags |