/* * License (BSD Style License): * Copyright (c) 2011 * Software Engineering * Department of Computer Science * Technische Universität Darmstadt * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Software Engineering Group or Technische * Universität Darmstadt nor the names of its contributors may be used to * endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.tud.cs.st.vespucci.vespucci_model.diagram.part; import java.io.IOException; import java.util.LinkedList; import java.util.List; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.commands.operations.OperationHistoryFactory; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.gmf.runtime.common.core.command.CommandResult; import org.eclipse.gmf.runtime.diagram.core.services.ViewService; import org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation; import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.osgi.util.NLS; import org.eclipse.ui.PartInitException; import org.eclipse.ui.dialogs.WizardNewFileCreationPage; /** * @generated */ public class VespucciNewDiagramFileWizard extends Wizard { /** * @generated */ private WizardNewFileCreationPage myFileCreationPage; /** * @generated */ private de.tud.cs.st.vespucci.vespucci_model.diagram.part.ModelElementSelectionPage diagramRootElementSelectionPage; /** * @generated */ private TransactionalEditingDomain myEditingDomain; /** * @generated */ public VespucciNewDiagramFileWizard(URI domainModelURI, EObject diagramRoot, TransactionalEditingDomain editingDomain) { assert domainModelURI != null : "Domain model uri must be specified"; //$NON-NLS-1$ assert diagramRoot != null : "Doagram root element must be specified"; //$NON-NLS-1$ assert editingDomain != null : "Editing domain must be specified"; //$NON-NLS-1$ myFileCreationPage = new WizardNewFileCreationPage( de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_CreationPageName, StructuredSelection.EMPTY); myFileCreationPage .setTitle(de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_CreationPageTitle); myFileCreationPage.setDescription(NLS.bind( de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_CreationPageDescription, de.tud.cs.st.vespucci.vespucci_model.diagram.edit.parts.ShapesDiagramEditPart.MODEL_ID)); IPath filePath; String fileName = URI.decode(domainModelURI.trimFileExtension().lastSegment()); if (domainModelURI.isPlatformResource()) { filePath = new Path(domainModelURI.trimSegments(1).toPlatformString(true)); } else if (domainModelURI.isFile()) { filePath = new Path(domainModelURI.trimSegments(1).toFileString()); } else { // TODO : use some default path throw new IllegalArgumentException("Unsupported URI: " + domainModelURI); //$NON-NLS-1$ } myFileCreationPage.setContainerFullPath(filePath); myFileCreationPage.setFileName(de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorUtil .getUniqueFileName(filePath, fileName, "sad")); //$NON-NLS-1$ diagramRootElementSelectionPage = new DiagramRootElementSelectionPage( de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_RootSelectionPageName); diagramRootElementSelectionPage .setTitle(de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_RootSelectionPageTitle); diagramRootElementSelectionPage .setDescription(de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_RootSelectionPageDescription); diagramRootElementSelectionPage.setModelElement(diagramRoot); myEditingDomain = editingDomain; } /** * @generated */ public void addPages() { addPage(myFileCreationPage); addPage(diagramRootElementSelectionPage); } /** * @generated */ public boolean performFinish() { LinkedList<IFile> affectedFiles = new LinkedList<IFile>(); IFile diagramFile = myFileCreationPage.createNewFile(); de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorUtil.setCharset(diagramFile); affectedFiles.add(diagramFile); URI diagramModelURI = URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true); ResourceSet resourceSet = myEditingDomain.getResourceSet(); final Resource diagramResource = resourceSet.createResource(diagramModelURI); AbstractTransactionalCommand command = new AbstractTransactionalCommand(myEditingDomain, de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_InitDiagramCommand, affectedFiles) { protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { int diagramVID = de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciVisualIDRegistry .getDiagramVisualID(diagramRootElementSelectionPage.getModelElement()); if (diagramVID != de.tud.cs.st.vespucci.vespucci_model.diagram.edit.parts.ShapesDiagramEditPart.VISUAL_ID) { return CommandResult .newErrorCommandResult(de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_IncorrectRootError); } Diagram diagram = ViewService.createDiagram(diagramRootElementSelectionPage.getModelElement(), de.tud.cs.st.vespucci.vespucci_model.diagram.edit.parts.ShapesDiagramEditPart.MODEL_ID, de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); diagramResource.getContents().add(diagram); diagramResource.getContents().add(diagram.getElement()); return CommandResult.newOKCommandResult(); } }; try { OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null); diagramResource.save(de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorUtil.getSaveOptions()); de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorUtil.openDiagram(diagramResource); } catch (ExecutionException e) { de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorPlugin.getInstance().logError( "Unable to create model and diagram", e); //$NON-NLS-1$ } catch (IOException ex) { de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorPlugin.getInstance().logError( "Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$ } catch (PartInitException ex) { de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorPlugin.getInstance().logError( "Unable to open editor", ex); //$NON-NLS-1$ } return true; } /** * @generated */ private static class DiagramRootElementSelectionPage extends de.tud.cs.st.vespucci.vespucci_model.diagram.part.ModelElementSelectionPage { /** * @generated */ protected DiagramRootElementSelectionPage(String pageName) { super(pageName); } /** * @generated */ protected String getSelectionTitle() { return de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_RootSelectionPageSelectionTitle; } /** * @generated */ protected boolean validatePage() { if (selectedModelElement == null) { setErrorMessage(de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_RootSelectionPageNoSelectionMessage); return false; } boolean result = ViewService .getInstance() .provides( new CreateDiagramViewOperation( new EObjectAdapter(selectedModelElement), de.tud.cs.st.vespucci.vespucci_model.diagram.edit.parts.ShapesDiagramEditPart.MODEL_ID, de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT)); setErrorMessage(result ? null : de.tud.cs.st.vespucci.vespucci_model.diagram.part.Messages.VespucciNewDiagramFileWizard_RootSelectionPageInvalidSelectionMessage); return result; } } }