Просмотр исходного кода

Fixed GroupStateIntoComposite refactoring.

tomqc86@googlemail.com 13 лет назад
Родитель
Сommit
c3e9675037

+ 1 - 2
plugins/org.yakindu.sct.refactoring/src/org/yakindu/sct/refactoring/handlers/AbstractRefactoringHandler.java

@@ -13,7 +13,6 @@ package org.yakindu.sct.refactoring.handlers;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IWorkbenchWindow;
@@ -29,7 +28,7 @@ import org.yakindu.sct.refactoring.refactor.AbstractRefactoring;
  * @author andreas muelder - Initial contribution and API
  * 
  */
-public abstract class AbstractRefactoringHandler<T extends EObject> extends AbstractHandler {
+public abstract class AbstractRefactoringHandler<T extends Object> extends AbstractHandler {
 
 	private AbstractRefactoring<T> refactoring;
 

+ 6 - 13
plugins/org.yakindu.sct.refactoring/src/org/yakindu/sct/refactoring/handlers/impl/GroupStatesIntoCompositeHandler.java

@@ -12,15 +12,12 @@ package org.yakindu.sct.refactoring.handlers.impl;
 
 import java.util.List;
 
-import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
-import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.yakindu.sct.refactoring.handlers.AbstractRefactoringHandler;
 import org.yakindu.sct.refactoring.refactor.AbstractRefactoring;
 import org.yakindu.sct.refactoring.refactor.impl.GroupStatesIntoCompositeRefactoring;
-
-import com.google.common.collect.Lists;
 /**
  * Handler for {@link GroupStatesIntoCompositeRefactoring}.
  * 
@@ -28,24 +25,20 @@ import com.google.common.collect.Lists;
  * 
  */
 public class GroupStatesIntoCompositeHandler extends
-		AbstractRefactoringHandler<View> {
+		AbstractRefactoringHandler<GraphicalEditPart> {
 
 	@Override
-	public AbstractRefactoring<View> createRefactoring() {
+	public AbstractRefactoring<GraphicalEditPart> createRefactoring() {
 		return new GroupStatesIntoCompositeRefactoring();
 	}
 
 	@SuppressWarnings("unchecked")
 	@Override
-	public void setContext(AbstractRefactoring<View> refactoring,
+	public void setContext(AbstractRefactoring<GraphicalEditPart> refactoring,
 			ISelection selection) {
 		IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-		List<IGraphicalEditPart> editParts = structuredSelection.toList();
-		List<View> result = Lists.newArrayList();
-		for (IGraphicalEditPart editPart : editParts) {
-			result.add(editPart.getNotationView());
-		}
-		refactoring.setContextObjects(result);
+		List<GraphicalEditPart> editParts = structuredSelection.toList();
+		refactoring.setContextObjects(editParts);
 	}
 
 }

+ 13 - 6
plugins/org.yakindu.sct.refactoring/src/org/yakindu/sct/refactoring/refactor/AbstractRefactoring.java

@@ -37,7 +37,8 @@ import org.yakindu.sct.refactoring.utils.RefactoringHelper;
  * @author thomas kutz - Initial contribution and API
  * 
  */
-public abstract class AbstractRefactoring<T extends EObject> implements IRefactoring<T> {
+public abstract class AbstractRefactoring<T extends Object> implements
+		IRefactoring<T> {
 
 	private List<T> contextObjects;
 
@@ -71,7 +72,11 @@ public abstract class AbstractRefactoring<T extends EObject> implements IRefacto
 	 * @return resource
 	 */
 	protected Resource getResource() {
-		return getContextObjects().iterator().next().eResource();
+		T firstContextObject = getContextObject();
+		if (firstContextObject instanceof EObject) {
+			return ((EObject) firstContextObject).eResource();
+		}
+		return null;
 	}
 
 	/**
@@ -84,11 +89,12 @@ public abstract class AbstractRefactoring<T extends EObject> implements IRefacto
 			return;
 		}
 
-		AbstractTransactionalCommand refactoringCommand = new AbstractTransactionalCommand(getEditingDomain(),
-				getCommandLabel(), getAffectedFiles()) {
+		AbstractTransactionalCommand refactoringCommand = new AbstractTransactionalCommand(
+				getEditingDomain(), getCommandLabel(), getAffectedFiles()) {
 
 			@Override
-			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
+			protected CommandResult doExecuteWithResult(
+					IProgressMonitor monitor, IAdaptable info)
 					throws ExecutionException {
 				try {
 					internalExecute();
@@ -144,7 +150,8 @@ public abstract class AbstractRefactoring<T extends EObject> implements IRefacto
 	 *            the resource used for enabling/disabling its serializer
 	 */
 	protected void executeCommand(IUndoableOperation command, Resource resource) {
-		IOperationHistory history = OperationHistoryFactory.getOperationHistory();
+		IOperationHistory history = OperationHistoryFactory
+				.getOperationHistory();
 
 		if (resource instanceof StextResource) {
 			// enable serializer

+ 66 - 64
plugins/org.yakindu.sct.refactoring/src/org/yakindu/sct/refactoring/refactor/impl/GroupStatesIntoCompositeRefactoring.java

@@ -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();
+	}
 
 }