|
|
@@ -15,13 +15,15 @@ import java.util.List;
|
|
|
import org.eclipse.draw2d.geometry.Insets;
|
|
|
import org.eclipse.draw2d.geometry.Rectangle;
|
|
|
import org.eclipse.emf.ecore.EObject;
|
|
|
+import org.eclipse.emf.ecore.resource.Resource;
|
|
|
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
|
|
|
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
|
|
|
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
|
|
|
+import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
|
|
|
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
|
|
|
import org.eclipse.gmf.runtime.notation.Bounds;
|
|
|
import org.eclipse.gmf.runtime.notation.Node;
|
|
|
import org.eclipse.gmf.runtime.notation.NotationFactory;
|
|
|
-import org.eclipse.gmf.runtime.notation.Shape;
|
|
|
import org.eclipse.gmf.runtime.notation.View;
|
|
|
import org.yakindu.sct.model.sgraph.Region;
|
|
|
import org.yakindu.sct.model.sgraph.SGraphFactory;
|
|
|
@@ -48,12 +50,12 @@ import com.google.common.collect.Lists;
|
|
|
*
|
|
|
*/
|
|
|
public class GroupStatesIntoCompositeRefactoring extends
|
|
|
- AbstractRefactoring<View> {
|
|
|
+ AbstractRefactoring<GraphicalEditPart> {
|
|
|
|
|
|
- private Region parentRegion;
|
|
|
+ private View parentRegion;
|
|
|
private PreferencesHint preferencesHint = DiagramActivator.DIAGRAM_PREFERENCES_HINT;
|
|
|
|
|
|
- private final int PADDING = 40;
|
|
|
+ private final int PADDING = 55;
|
|
|
|
|
|
@Override
|
|
|
protected void internalExecute() {
|
|
|
@@ -65,70 +67,65 @@ public class GroupStatesIntoCompositeRefactoring extends
|
|
|
return super.isExecutable() && allStatesHaveSameParentRegion();
|
|
|
}
|
|
|
|
|
|
- private void groupSelectedStatesIntoNewCompositeState() {
|
|
|
+ protected void groupSelectedStatesIntoNewCompositeState() {
|
|
|
|
|
|
State compositeState = createCompositeState();
|
|
|
+
|
|
|
+ Region innerRegion = SGraphFactory.eINSTANCE.createRegion();
|
|
|
+ innerRegion.setName("r1"); // TODO check for uniqueness
|
|
|
+ compositeState.getRegions().add(innerRegion);
|
|
|
+
|
|
|
+ for (State state : getSelectedStates()) {
|
|
|
+ innerRegion.getVertices().add(state);
|
|
|
+ }
|
|
|
+
|
|
|
Node compositeStateView = createNodeForCompositeState(compositeState);
|
|
|
- Region innerRegion = createInnerRegionInCompositeState(compositeState);
|
|
|
- createNodeForInnerRegion(compositeStateView, innerRegion);
|
|
|
- moveSelectedStatesToCompositeState(compositeState);
|
|
|
+ Node innerRegionNode = ViewService.createNode(
|
|
|
+ getStateFigureCompartmentView(compositeStateView), innerRegion,
|
|
|
+ SemanticHints.REGION, preferencesHint);
|
|
|
+
|
|
|
+ View regionCompartment = ViewUtil.getChildBySemanticHint(innerRegionNode, SemanticHints.REGION_COMPARTMENT);
|
|
|
+ moveSelectedStateNodesTo(regionCompartment, (Bounds)compositeStateView.getLayoutConstraint());
|
|
|
}
|
|
|
|
|
|
- private State createCompositeState() {
|
|
|
+ protected State createCompositeState() {
|
|
|
State compositeState = SGraphFactory.eINSTANCE.createState();
|
|
|
compositeState.setName(getNameForCompositeState());
|
|
|
- // add it to parent region
|
|
|
- parentRegion.getVertices().add(compositeState);
|
|
|
+ ((Region)parentRegion.getElement()).getVertices().add(compositeState);
|
|
|
return compositeState;
|
|
|
}
|
|
|
|
|
|
- private Node createNodeForCompositeState(State compositeState) {
|
|
|
- View parentRegionView = getViewOfParentRegion();
|
|
|
- Node compositeStateView = ViewService.createNode(parentRegionView,
|
|
|
+ protected Node createNodeForCompositeState(State compositeState) {
|
|
|
+ Node compositeStateNode = ViewService.createNode(parentRegion,
|
|
|
compositeState, SemanticHints.STATE, preferencesHint);
|
|
|
- setCompositeStateLayoutConstraint(compositeStateView);
|
|
|
- return compositeStateView;
|
|
|
- }
|
|
|
-
|
|
|
- private View getViewOfParentRegion() {
|
|
|
- // since all states are in same region, only the first state's edit part
|
|
|
- // is taken here
|
|
|
- View stateView = getContextObjects().iterator().next();
|
|
|
- View viewContainerForState = ViewUtil.getViewContainer(stateView);
|
|
|
- return viewContainerForState;
|
|
|
+ setCompositeStateLayoutConstraint(compositeStateNode);
|
|
|
+ return compositeStateNode;
|
|
|
}
|
|
|
|
|
|
protected List<State> getSelectedStates() {
|
|
|
- List<View> contextObjects = getContextObjects();
|
|
|
+ List<GraphicalEditPart> contextObjects = getContextObjects();
|
|
|
List<State> result = Lists.newArrayList();
|
|
|
- for (View view : contextObjects) {
|
|
|
- EObject element = view.getElement();
|
|
|
+ for (GraphicalEditPart editPart : contextObjects) {
|
|
|
+ EObject element = editPart.resolveSemanticElement();
|
|
|
result.add((State) element);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
- private void moveSelectedStatesToCompositeState(State compositeState) {
|
|
|
- for (State state : getSelectedStates()) {
|
|
|
- compositeState.getRegions().get(0).getVertices().add(state);
|
|
|
+
|
|
|
+ protected void moveSelectedStateNodesTo(View containerView, Bounds compositeBounds) {
|
|
|
+
|
|
|
+ for (GraphicalEditPart editPart : getContextObjects()) {
|
|
|
+ Node stateNode = (Node)editPart.getNotationView();
|
|
|
+ ViewUtil.insertChildView(containerView, stateNode, ViewUtil.APPEND, true);
|
|
|
+ Bounds newBounds = NotationFactory.eINSTANCE.createBounds();
|
|
|
+ Bounds oldBounds = (Bounds)stateNode.getLayoutConstraint();
|
|
|
+ newBounds.setX(oldBounds.getX() - compositeBounds.getX() - 7); //FIXME use bounds of region view
|
|
|
+ newBounds.setY(oldBounds.getY() - compositeBounds.getY() - 34); //FIXME use bounds of region view
|
|
|
+
|
|
|
+ ((Node)editPart.getNotationView()).setLayoutConstraint(newBounds);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private void createNodeForInnerRegion(Node compositeStateView,
|
|
|
- Region innerRegion) {
|
|
|
- ViewService.createNode(
|
|
|
- getStateFigureCompartmentView(compositeStateView), innerRegion,
|
|
|
- SemanticHints.REGION, preferencesHint);
|
|
|
- }
|
|
|
-
|
|
|
- private Region createInnerRegionInCompositeState(State compositeState) {
|
|
|
- Region innerRegion = SGraphFactory.eINSTANCE.createRegion();
|
|
|
- innerRegion.setName("r1");
|
|
|
- // add it to composite state
|
|
|
- compositeState.getRegions().add(innerRegion);
|
|
|
- return innerRegion;
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Iterates through all {@link StateEditPart}s of the current selection and
|
|
|
* computes layout constraints for the composite node.
|
|
|
@@ -136,17 +133,15 @@ public class GroupStatesIntoCompositeRefactoring extends
|
|
|
* @param compositeStateNode
|
|
|
* node of the composite state
|
|
|
*/
|
|
|
- private void setCompositeStateLayoutConstraint(Node compositeStateNode) {
|
|
|
-
|
|
|
- Rectangle newbounds = new Rectangle();
|
|
|
- for (View view : getContextObjects()) {
|
|
|
- if (!(view instanceof Shape))
|
|
|
- continue;
|
|
|
-
|
|
|
- Shape shape = (Shape) view;
|
|
|
- Bounds bounds = (Bounds) shape.getLayoutConstraint();
|
|
|
- Rectangle childBounds = new Rectangle(bounds.getX(), bounds.getY(),
|
|
|
- bounds.getWidth(), bounds.getHeight());
|
|
|
+ protected void setCompositeStateLayoutConstraint(Node compositeStateNode) {
|
|
|
+
|
|
|
+ Rectangle newbounds = null;
|
|
|
+
|
|
|
+ for (GraphicalEditPart editPart : getContextObjects()) {
|
|
|
+ Rectangle childBounds = editPart.getFigure().getBounds();
|
|
|
+ if (newbounds == null)
|
|
|
+ newbounds = childBounds.getCopy();
|
|
|
+
|
|
|
newbounds.union(childBounds);
|
|
|
}
|
|
|
newbounds.expand(new Insets(PADDING, PADDING, PADDING, PADDING));
|
|
|
@@ -159,12 +154,12 @@ public class GroupStatesIntoCompositeRefactoring extends
|
|
|
compositeStateNode.setLayoutConstraint(bounds);
|
|
|
}
|
|
|
|
|
|
- private View getStateFigureCompartmentView(Node compositeStateView) {
|
|
|
+ protected View getStateFigureCompartmentView(Node compositeStateView) {
|
|
|
return ViewUtil.getChildBySemanticHint(compositeStateView,
|
|
|
SemanticHints.STATE_FIGURE_COMPARTMENT);
|
|
|
}
|
|
|
|
|
|
- private String getNameForCompositeState() {
|
|
|
+ protected String getNameForCompositeState() {
|
|
|
StringBuilder nameBuilder = new StringBuilder("Composite");
|
|
|
for (State state : getSelectedStates()) {
|
|
|
nameBuilder.append("_");
|
|
|
@@ -173,12 +168,14 @@ public class GroupStatesIntoCompositeRefactoring extends
|
|
|
return nameBuilder.toString();
|
|
|
}
|
|
|
|
|
|
- private boolean allStatesHaveSameParentRegion() {
|
|
|
- for (State state : getSelectedStates()) {
|
|
|
+ protected boolean allStatesHaveSameParentRegion() {
|
|
|
+ parentRegion = null;
|
|
|
+ for (IGraphicalEditPart editPart : getContextObjects()) {
|
|
|
if (parentRegion == null) {
|
|
|
- parentRegion = state.getParentRegion();
|
|
|
+ parentRegion = (Node) ((Node)editPart.getNotationView()).eContainer();
|
|
|
} else {
|
|
|
- if (!state.getParentRegion().equals(parentRegion)) {
|
|
|
+ Node nextParentRegion = (Node) ((Node)editPart.getNotationView()).eContainer();
|
|
|
+ if (!nextParentRegion.equals(parentRegion)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -190,5 +187,10 @@ public class GroupStatesIntoCompositeRefactoring extends
|
|
|
protected String getCommandLabel() {
|
|
|
return "Group states into composite state";
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Resource getResource() {
|
|
|
+ return getContextObject().resolveSemanticElement().eResource();
|
|
|
+ }
|
|
|
|
|
|
}
|