Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Prevent moving a figure outside parent's borders
Prevent moving a figure outside parent's borders [message #539061] Wed, 09 June 2010 14:19 Go to next message
emil salageanu is currently offline emil salageanuFriend
Messages: 94
Registered: June 2010
Location: Nice, France
Member
Hello,

I'm new to GMF.
I'mplementing a workflow editor. I noticed that, for some figures, when trying to move a child outside the parent borders it resizes the parent which seems correct.
For other cases, it just moves the child at a position which is no longer visible as its outside the parent client area (for example this happens, for some figures, in the Taipan example as well as the BPMN implementation ).

In order to prevent moving a child figure outside the parent, I added an edit policy to the EditPart of the child (installed for the key EditPolicy.PRIMARY_DRAG_ROLE).

My edit policy extends ResizableEditPolicyEx and implements the method getMoveCommand like this:

	protected Command getMoveCommand(ChangeBoundsRequest request) {

		Point moveDelta = request.getMoveDelta(); 

		Rectangle myRect = getHostFigure().getClientArea();
		myRect.translate(moveDelta);
		
		Rectangle parentRectangle = ((GraphicalEditPart)this.getHost().getParent()).getFigure().getClientArea();
		
		//cannot move outside parent left and  up side
		if (myRect.x<0 || myRect.y<0)
			return null;
		
		//cannot move outside parent bottom side
		if (myRect.y+myRect.height>parentRectangle.height)
			return null;
		
		//cannot move outside parent right side		
		if (myRect.x+myRect.width>parentRectangle.width)
			return null;
		
		return super.getMoveCommand(request);

	}



Well, just wanted to share this, anny comments wellcomed.

Emil.

Re: Prevent moving a figure outside parent's borders [message #556364 is a reply to message #539061] Wed, 01 September 2010 00:55 Go to previous messageGo to next message
Fernando Herrera is currently offline Fernando HerreraFriend
Messages: 49
Registered: January 2010
Member
Excellent Emil. I'm trying to do something similar but I haven't got succesfull. I have an editpart that need to move anothers editparts when a moveRequest arrives. I have already installed the edit policy like you in that editpart and defined the editPolicy class. Here is the code of the editpart (is correct?) and after the editPolicy code (nothing important for the moment). The problem is when I move an instance of the editPart, the getMoveCommand is not caught. The steps that i did are the following. Any idea about waht is wrong?. Thanks in advance Emil!

1. I have added the installEditPolicy in the editpart
/**
* @generated NOT
*/
protected void createDefaultEditPolicies() {
installEditPolicy(EditPolicyRoles.CREATION_ROLE,
new CreationEditPolicy());
super.createDefaultEditPolicies();
installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
new ControlFlowSegment2ItemSemanticEditPolicy());
installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new ControlFlowSegmentCustomEditPolicy());
installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
}

2. I have defined the following class:

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPa rt;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DragDropEdit Policy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableEdi tPolicyEx;
import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;

import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit. parts.ControlFlowSegmentEditPart;

public class ControlFlowSegmentCustomEditPolicy extends
ResizableEditPolicyEx {

public ControlFlowSegmentCustomEditPolicy()
{
}
protected Command getMoveCommand(ChangeBoundsRequest request) {

Point moveDelta = request.getMoveDelta();

ControlFlowSegmentEditPart controlFlowSegmentEditPart =
(ControlFlowSegmentEditPart) request.getEditParts().get(0);

EditPart controlFlowSegmentEditPart2 =
this.getHost();

//Rectangle myRect = controlFlowSegmentEditPart.getFigure().getClientArea();
//myRect.translate(moveDelta);

//Rectangle parentRectangle = ((GraphicalEditPart)this.getHost().getParent()).getFigure(). getClientArea();

//cannot move outside parent left and up side
//if (myRect.x<0 || myRect.y<0)
// return null;

//cannot move outside parent bottom side
//if (myRect.y myRect.height>parentRectangle.height)
// return null;

//cannot move outside parent right side
//if (myRect.x myRect.width>parentRectangle.width)
// return null;
//MoveRequest moveRequest = new MoveRequest();
//return super.getMoveCommand(request);
return null;

}
}
Re: Prevent moving a figure outside parent's borders [message #556397 is a reply to message #556364] Wed, 01 September 2010 07:30 Go to previous messageGo to next message
emil salageanu is currently offline emil salageanuFriend
Messages: 94
Registered: June 2010
Location: Nice, France
Member
Are you sure the getMoveCommand() is not called? (I see that you implementation always returns null wich would mean move is not allowed at all). If that's correct (mmethod is never called) take a look in the super classes of your edit part and see which edit policies are installed (each super class defines its own createDefaultEditPolicies() ), and which one provides the getMoveCommand (...)
I have no other ideas ...


Good luck.

Emil.
Re: Prevent moving a figure outside parent's borders [message #556400 is a reply to message #556397] Wed, 01 September 2010 07:39 Go to previous messageGo to next message
emil salageanu is currently offline emil salageanuFriend
Messages: 94
Registered: June 2010
Location: Nice, France
Member
If nothing works, just put a brackpoint in the getCommand() method of your edit part and debug to see what happens when you try to move your element.


Cheers,
Emil.
Re: Prevent moving a figure outside parent's borders [message #556584 is a reply to message #556400] Wed, 01 September 2010 21:59 Go to previous messageGo to next message
Fernando Herrera is currently offline Fernando HerreraFriend
Messages: 49
Registered: January 2010
Member
Thanks Emil. I fix the issue. i have replaced the following line installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new ControlFlowSegmentCustomEditPolicy()); for it: installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE, new ControlFlowSegmentCustomEditPolicy()); and now the changeboundsrequest is firing. I need drag and drog support in this editpolicy. Is correct for me extends from ResizableEditPolicyEx or from the DragDropEditPolicy class?. Thanks for your time!

[Updated on: Wed, 01 September 2010 22:00]

Report message to a moderator

Re: Prevent moving a figure outside parent's borders [message #556587 is a reply to message #556584] Wed, 01 September 2010 22:13 Go to previous message
Fernando Herrera is currently offline Fernando HerreraFriend
Messages: 49
Registered: January 2010
Member
No Message Body

[Updated on: Wed, 01 September 2010 22:15]

Report message to a moderator

Previous Topic:How to hide attributes in the standard property tab?
Next Topic:How to make a traingle in graphical definition
Goto Forum:
  


Current Time: Fri Apr 26 23:27:24 GMT 2024

Powered by FUDForum. Page generated in 0.03742 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top