blob: 0b526292c246c2193c8b0dd614a0d3cdc0335b0b [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2011 CEA LIST.
*
* 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:
*
* CEA LIST - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.sysml.diagram.internalblock.helper.advice;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.DecorationNode;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.sysml.diagram.internalblock.provider.ElementTypes;
import org.eclipse.papyrus.uml.diagram.common.util.CrossReferencerUtil;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
/**
*
* This advice is used to remove the view of the parts which become inconsistent when we change the type of the property
*
*/
public class PropertyHelperAdvice extends AbstractEditHelperAdvice {
/**
* Returns the command to destroy the views of the parts which are not owned by the new type
*
* @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getBeforeSetCommand(org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest)
*
* @param request
* the request to modify the model
* @return
* the command to destroy the views of the parts which are not owned by the new type
*
*/
@Override
protected ICommand getBeforeSetCommand(SetRequest request) {
Type oldType = null;
Type newType = null;
EObject elementToEdit = request.getElementToEdit();
Set<View> viewsToDelete = new HashSet<View>();
if ((elementToEdit instanceof Property) && (request.getFeature() == UMLPackage.eINSTANCE.getTypedElement_Type()) && ((request.getValue() == null) || (request.getValue() instanceof Type))) {
Property propertyToEdit = (Property) elementToEdit;
oldType = propertyToEdit.getType();
newType = (Type) request.getValue();
if ((oldType != null) && (oldType instanceof Classifier) && ((request.getValue() == null) || (newType instanceof Classifier))) {
EList<NamedElement> newTypeMembers = (newType != null) ? ((Classifier) newType).getMembers() : new BasicEList<NamedElement>();
EList<NamedElement> oldTypeMembers = ((Classifier) oldType).getMembers();
// Remove members of the new type from the list.
// oldTypeMembers now contains the list of members for which views will become
// inconsistent (if shown in the propertyToEdit) after setting the new type.
Set<NamedElement> possiblyInconsistentMembers = new HashSet<NamedElement>();
possiblyInconsistentMembers.addAll(oldTypeMembers);
possiblyInconsistentMembers.removeAll(newTypeMembers);
if (!possiblyInconsistentMembers.isEmpty()) {
Set<View> propertyToEditViews = null;
// Parse the list of possibly inconsistent members
for (NamedElement possiblyInconsistentMember : possiblyInconsistentMembers) {
// Retrieve views of the current possiblyInconsistentMember
Iterator<View> viewIt = CrossReferencerUtil.getCrossReferencingViews(possiblyInconsistentMember, ElementTypes.DIAGRAM_ID).iterator();
while (viewIt.hasNext()) {
if (propertyToEditViews == null) {
propertyToEditViews = CrossReferencerUtil.getCrossReferencingViews(propertyToEdit, ElementTypes.DIAGRAM_ID);
}
View possiblyInconsistentMemberView = viewIt.next();
if (isConcerned(possiblyInconsistentMemberView, propertyToEditViews)) {
viewsToDelete.add(possiblyInconsistentMemberView);
}
}
}
}
}
}
if ((viewsToDelete != null) && !(viewsToDelete.isEmpty())) {
DestroyDependentsRequest req = new DestroyDependentsRequest(request.getEditingDomain(), elementToEdit, false);
req.setClientContext(request.getClientContext());
req.addParameters(request.getParameters());
return req.getDestroyDependentsCommand(viewsToDelete);
}
return null;
}
/**
* Tests if the view must be deleted
*
* @param view
* the view to test
* @param list
* the list of the property view
* @return
* <code>true</code> if the view need to be removed <code>false</code> if not
*/
protected boolean isConcerned(View view, Set<View> propertyViews) {
EObject parentView = view.eContainer();
if (parentView instanceof DecorationNode) {
parentView = parentView.eContainer();
}
return propertyViews.contains(parentView);
}
}