/*
* StsToolNewDiagramFileWizard.java
*
* This file is part of the STS-Tool project.
* Copyright (c) 2011-2012 "University of Trento - DISI" All rights reserved.
*
* Is strictly forbidden to remove this copyright notice from this source code.
*
* Disclaimer of Warranty:
* STS-Tool (this software) is provided "as-is" and without warranty of any kind,
* express, implied or otherwise, including without limitation, any warranty of
* merchantability or fitness for a particular purpose.
* In no event shall the copyright holder 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.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation with the addition of the
* following permission added to Section 15 as permitted in Section 7(a):
* FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
* "University of Trento - DISI","University of Trento - DISI" DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA, 02110-1301 USA, or download the license from the following URL:
* http://www.sts-tool.eu/License.php
*
* For more information, please contact STS-Tool group at this
* address: ststool@disi.unitn.it
*
*/
package eu.aniketos.wp1.ststool.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.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 eu.aniketos.wp1.ststool.diagram.application.WizardNewFileCreationPage;
import eu.aniketos.wp1.ststool.diagram.edit.parts.StsToolDiagramEditPart;
/**
* @generated
*/
public class StsToolNewDiagramFileWizard extends Wizard {
/**
* @generated
*/
private WizardNewFileCreationPage myFileCreationPage;
/**
* @generated
*/
private ModelElementSelectionPage diagramRootElementSelectionPage;
/**
* @generated
*/
private TransactionalEditingDomain myEditingDomain;
/**
* @generated
*/
public StsToolNewDiagramFileWizard(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(Messages.StsToolNewDiagramFileWizard_CreationPageName, StructuredSelection.EMPTY);
myFileCreationPage.setTitle(Messages.StsToolNewDiagramFileWizard_CreationPageTitle);
myFileCreationPage.setDescription(NLS.bind(Messages.StsToolNewDiagramFileWizard_CreationPageDescription, StsToolDiagramEditPart.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(StsToolDiagramEditorUtil.getUniqueFileName(filePath, fileName, "ststool_diagram")); //$NON-NLS-1$
diagramRootElementSelectionPage = new DiagramRootElementSelectionPage(Messages.StsToolNewDiagramFileWizard_RootSelectionPageName);
diagramRootElementSelectionPage.setTitle(Messages.StsToolNewDiagramFileWizard_RootSelectionPageTitle);
diagramRootElementSelectionPage.setDescription(Messages.StsToolNewDiagramFileWizard_RootSelectionPageDescription);
diagramRootElementSelectionPage.setModelElement(diagramRoot);
myEditingDomain = editingDomain;
}
/**
* @generated
*/
@Override
public void addPages(){
addPage(myFileCreationPage);
addPage(diagramRootElementSelectionPage);
}
/**
* @generated
*/
@Override
public boolean performFinish(){
List affectedFiles = new LinkedList();
IPath diagramModelPath = myFileCreationPage.getContainerFullPath().append(myFileCreationPage.getFileName());
URI diagramModelURI = URI.createFileURI(diagramModelPath.toString());
ResourceSet resourceSet = myEditingDomain.getResourceSet();
final Resource diagramResource = resourceSet.createResource(diagramModelURI);
AbstractTransactionalCommand command = new AbstractTransactionalCommand(myEditingDomain, Messages.StsToolNewDiagramFileWizard_InitDiagramCommand, affectedFiles) {
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,IAdaptable info)
throws ExecutionException{
int diagramVID = StsToolVisualIDRegistry.getDiagramVisualID(diagramRootElementSelectionPage.getModelElement());
if (diagramVID != StsToolDiagramEditPart.VISUAL_ID) { return CommandResult.newErrorCommandResult(Messages.StsToolNewDiagramFileWizard_IncorrectRootError); }
Diagram diagram = ViewService.createDiagram(diagramRootElementSelectionPage.getModelElement(), StsToolDiagramEditPart.MODEL_ID, StsToolDiagramEditorPlugin.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(StsToolDiagramEditorUtil.getSaveOptions());
StsToolDiagramEditorUtil.openDiagram(diagramResource);
} catch (ExecutionException e) {
StsToolDiagramEditorPlugin.getInstance().logError("Unable to create model and diagram", e); //$NON-NLS-1$
} catch (IOException ex) {
StsToolDiagramEditorPlugin.getInstance().logError("Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$
} catch (PartInitException ex) {
StsToolDiagramEditorPlugin.getInstance().logError("Unable to open editor", ex); //$NON-NLS-1$
}
return true;
}
/**
* @generated
*/
private static class DiagramRootElementSelectionPage extends ModelElementSelectionPage {
/**
* @generated
*/
protected DiagramRootElementSelectionPage(String pageName) {
super(pageName);
}
/**
* @generated
*/
@Override
protected String getSelectionTitle(){
return Messages.StsToolNewDiagramFileWizard_RootSelectionPageSelectionTitle;
}
/**
* @generated
*/
@Override
protected boolean validatePage(){
if (selectedModelElement == null) {
setErrorMessage(Messages.StsToolNewDiagramFileWizard_RootSelectionPageNoSelectionMessage);
return false;
}
boolean result = ViewService.getInstance().provides(new CreateDiagramViewOperation(new EObjectAdapter(selectedModelElement), StsToolDiagramEditPart.MODEL_ID, StsToolDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT));
setErrorMessage(result ? null : Messages.StsToolNewDiagramFileWizard_RootSelectionPageInvalidSelectionMessage);
return result;
}
}
}