/*******************************************************************************
* Copyright (c) 2012 University of Mannheim: Chair for Software Engineering
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ralph Gerbig - initial API and implementation and initial documentation
*******************************************************************************/
package de.uni_mannheim.informatik.swt.mlm.emendation.service.commands;
import java.util.Set;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.PlatformUI;
import de.uni_mannheim.informatik.swt.mlm.emendation.service.ImpactAnalyzer;
import de.uni_mannheim.informatik.swt.mlm.emendation.service.dialogs.EmendAddAttributeDialog;
import de.uni_mannheim.informatik.swt.models.plm.PLM.Attribute;
import de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject;
import de.uni_mannheim.informatik.swt.models.plm.PLM.Feature;
import de.uni_mannheim.informatik.swt.models.plm.PLM.PLMFactory;
import de.uni_mannheim.informatik.swt.models.plm.PLM.PLMPackage;
public class EmendAddAttributeCommand{
private Set<Clabject> emendedElements;
public Set<Clabject> getEmendedObjects(){
return emendedElements;
}
public CompoundCommand run(Clabject changeOrigin, Feature addedFeature){
try {
EmendAddAttributeDialog dialog = showAddModelElementDialog(addedFeature);
if (dialog == null)
return null;
return runEmendation(changeOrigin, addedFeature, dialog.getNewName(), dialog.getNewDurability(), dialog.getNewMutability(), dialog.getPreventDuplicates(), dialog.getAddToSupertypesOnly(), dialog.getAddToOntologicalTypes(), dialog.getAddToOntologicalInstances());
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
protected EmendAddAttributeDialog showAddModelElementDialog(Feature addedFeature) throws ExecutionException{
EmendAddAttributeDialog dialog = new EmendAddAttributeDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
dialog.setNewName(addedFeature.getName());
dialog.setNewDurability(addedFeature.getClabject().getPotencyAsString());
dialog.setNewMutability(addedFeature.getClabject().getPotencyAsString());
if (dialog.open() == Window.OK)
return dialog;
else
return null;
}
/**
*
* @param changeOrigin The element on which the original change operation has been performed
* @param traitToChange The EAttribut in the meta-model to change
* @param addedFeature can bee needed because if emendation request comes from UI it can be already changed in
* the feature
* @param addedFeature the new value to set
* @param changeOntologicalTypes change ontological types?
* @param changeSubtypes change subtypes?
* @param changeSuperTypes change supertypes?
* @return
*/
protected CompoundCommand runEmendation(Clabject changeOrigin, Feature addedFeature, String name, int durability, int mutability, boolean preventDuplicates, boolean addToSuperTypesOnly, boolean includeOntologicalTypes, boolean includeOntologicalInstances){
emendedElements = new ImpactAnalyzer().getEffectedModelElementsForAddFeatureOperation(changeOrigin, addedFeature, name, !preventDuplicates, addToSuperTypesOnly, includeOntologicalTypes, includeOntologicalInstances);
CompoundCommand emendationCommand = new CompoundCommand("Emendation - Add Feature" + addedFeature.getName());
TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(changeOrigin);
//***********************************
//Setup the via UI added element
//***********************************
Command setOriginName = SetCommand.create(domain, addedFeature, PLMPackage.eINSTANCE.getElement_Name(), name);
emendationCommand.append(setOriginName);
Command setOriginDurability = SetCommand.create(domain, addedFeature, PLMPackage.eINSTANCE.getFeature_Durability(), durability);
emendationCommand.append(setOriginDurability);
Command setOriginMutabilityCommand = SetCommand.create(domain, addedFeature, PLMPackage.eINSTANCE.getAttribute_Mutability(), mutability);
emendationCommand.append(setOriginMutabilityCommand);
//This is only used as template for the configure method of PLMFactory
Feature template = addedFeature instanceof Attribute ? PLMFactory.eINSTANCE.createAttribute() : PLMFactory.eINSTANCE.createMethod();
template.setName(name);
template.setDurability(durability);
if (template instanceof Attribute)
((Attribute)template).setMutability(mutability);
//***************************************************************
//Execute change operation
//***************************************************************
for (Clabject element : emendedElements)
if (element != changeOrigin)
{
Feature newFeature = PLMFactory.eINSTANCE.createFeature(template, addedFeature.getClabject(), element, false);
emendationCommand.append(AddCommand.create(domain, element, PLMPackage.eINSTANCE.getClabject_Feature(), newFeature));
}
return emendationCommand;
}
}