package org.eclipse.papyrus.uml.diagram.statemachine.custom.policies;
import java.util.Iterator;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.ICompositeCommand;
import org.eclipse.gmf.runtime.diagram.core.commands.SetPropertyCommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyRequest;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.commands.SemanticAdapter;
import org.eclipse.papyrus.uml.diagram.statemachine.custom.commands.CustomRegionDeleteCommand;
import org.eclipse.papyrus.uml.diagram.statemachine.custom.commands.CustomRegionDestroyElementCommand;
import org.eclipse.papyrus.uml.diagram.statemachine.edit.parts.RegionCompartmentEditPart;
import org.eclipse.papyrus.uml.diagram.statemachine.edit.policies.RegionItemSemanticEditPolicy;
import org.eclipse.papyrus.uml.diagram.statemachine.part.UMLVisualIDRegistry;
public class CustomRegionItemSemanticEditPolicy extends RegionItemSemanticEditPolicy {
@Override
protected Command addDeleteViewCommand(Command mainCommand, DestroyRequest completedRequest) {
Command deleteViewCommand = getGEFWrapper(new CustomRegionDeleteCommand(getEditingDomain(), (View)getHost().getModel()));
return mainCommand == null ? deleteViewCommand : mainCommand.chain(deleteViewCommand);
}
protected void addDestroyChildNodesCommand(ICompositeCommand cmd) {
View view = (View)getHost().getModel();
for(Iterator nit = view.getChildren().iterator(); nit.hasNext();) {
Node node = (Node)nit.next();
switch(UMLVisualIDRegistry.getVisualID(node)) {
case RegionCompartmentEditPart.VISUAL_ID:
for(Iterator cit = node.getChildren().iterator(); cit.hasNext();) {
Node cnode = (Node)cit.next();
switch(UMLVisualIDRegistry.getVisualID(cnode)) {
}
}
break;
}
}
}
@Override
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View)getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if(annotation == null) {
// there are indirectly referenced children, need extra commands:
// false
addDestroyChildNodesCommand(cmd);
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new CustomRegionDestroyElementCommand(req));
} else {
cmd.add(new CustomRegionDeleteCommand(getEditingDomain(), view));
}
View compartmentView = (View)view.eContainer();
// get and adaptable for it, to pass on to commands
IAdaptable adaptableForCompartmentView = (IAdaptable)new SemanticAdapter(null, compartmentView);
if(compartmentView.getChildren().size() == 1) {
SetPropertyCommand showCompartment = new SetPropertyCommand(getEditingDomain(), adaptableForCompartmentView, "notation.View.visible", "Visibility", false);
cmd.compose(showCompartment);
}
return getGEFWrapper(cmd.reduce());
}
}