|
|
Re: Child EditPart with Sliders [message #195913 is a reply to message #195831] |
Wed, 14 September 2005 09:51 |
J F Messages: 256 Registered: July 2009 |
Senior Member |
|
|
The following example class shows an edit policy that works with a Layout
container to constrain children to move, resize in the vertical direction
with appropriate feedback.
package regionTestEditor.parts;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.handles.NonResizableHandleKit;
import org.eclipse.gef.handles.ResizableHandleKit;
import org.eclipse.gef.requests.ChangeBoundsRequest;
/**
* A Test example that shows an edit policy that constrains a figure to
move or resize
* in the vertical dimension only.
* The source is mostly copied from ResizeableEditPolicy, but only
* createSelectionHandles
* showChangeBoundsFeedback
* are changed
*
* A Better implementation would override ResizeableEditPolicy directly,
this was just a self learning exercise
*
* To use merely install on the layout container which wishes children to
be moved, resized vertically only.
*/
public class VerticalOnlyResizableEditPolicy extends NonResizableEditPolicy
{
private int directions = -1;
/**
* @see
org.eclipse.gef.editpolicies.SelectionHandlesEditPolicy#crea teSelectionHandles()
*/
protected List createSelectionHandles()
{
List list = new ArrayList();
if(directions == 0)
NonResizableHandleKit.addHandles((GraphicalEditPart) getHost(), list);
else if(directions != -1)
{
ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(), list);
if((directions & PositionConstants.SOUTH) != 0)
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list,
PositionConstants.SOUTH);
else
NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list,
PositionConstants.SOUTH);
if((directions & PositionConstants.NORTH) != 0)
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list,
PositionConstants.NORTH);
else
NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list,
PositionConstants.NORTH);
}
else
ResizableHandleKit.addHandles((GraphicalEditPart) getHost(), list);
return list;
}
/**
* Dispatches erase requests to more specific methods.
*
* @see
org.eclipse.gef.EditPolicy#eraseSourceFeedback(org.eclipse.g ef.Request)
*/
public void eraseSourceFeedback(Request request)
{
if(REQ_RESIZE.equals(request.getType()))
{
System.out.println("Change bounds feed back y delta is " +
((ChangeBoundsRequest) request).getResizeDirection());
eraseChangeBoundsFeedback((ChangeBoundsRequest) request);
}
else
{
if(REQ_MOVE.equals(request.getType()))
{
System.out.println("Move feed back y delta is " +
((ChangeBoundsRequest) request).getMoveDelta().y);
}
super.eraseSourceFeedback(request);
}
}
/**
* @see org.eclipse.gef.EditPolicy#getCommand(org.eclipse.gef.Reques t)
*/
public Command getCommand(Request request)
{
if(REQ_RESIZE.equals(request.getType()))
return getResizeCommand((ChangeBoundsRequest) request);
return super.getCommand(request);
}
/**
* Returns the current resize directions integer that depicts which
handles
* can be resized on this object.
*
* @return handle directions that can be resized
*/
public int getResizeDirections()
{
return directions;
}
/**
* Returns the command contribution for the given resize request. By
* default, the request is redispatched to the host's parent as a {@link
* org.eclipse.gef.RequestConstants#REQ_RESIZE_CHILDREN}. The parent's
* editpolicies determine how to perform the resize based on the layout
* manager in use.
*
* @param request
* the resize request
* @return the command contribution obtained from the parent
*/
protected Command getResizeCommand(ChangeBoundsRequest request)
{
ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_RESIZE_CHILDREN);
req.setEditParts(getHost());
req.setMoveDelta(request.getMoveDelta());
req.setSizeDelta(request.getSizeDelta());
req.setLocation(request.getLocation());
req.setExtendedData(request.getExtendedData());
req.setResizeDirection(request.getResizeDirection());
return getHost().getParent().getCommand(req);
}
/**
* Sets the directions in which handles should allow resizing. Valid
values
* are bit-wise combinations of:
* <UL>
* <LI>{@link PositionConstants#NORTH}
* <LI>{@link PositionConstants#SOUTH}
* <LI>{@link PositionConstants#EAST}
* <LI>{@link PositionConstants#WEST}
* </UL>
*
* @param newDirections
* the direction in which resizing is allowed
*/
public void setResizeDirections(int newDirections)
{
directions = newDirections;
}
/**
* @see
org.eclipse.gef.EditPolicy#showSourceFeedback(org.eclipse.ge f.Request)
*/
public void showSourceFeedback(Request request)
{
if(REQ_RESIZE.equals(request.getType()))
showChangeBoundsFeedback((ChangeBoundsRequest) request);
else
super.showSourceFeedback(request);
}
/**
* @see
org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.ge f.Request)
*/
public boolean understandsRequest(Request request)
{
if(REQ_RESIZE.equals(request.getType()))
return true;
return super.understandsRequest(request);
}
protected void showChangeBoundsFeedback(ChangeBoundsRequest request)
{
IFigure feedback = getDragSourceFeedbackFigure();
PrecisionRectangle rect = new
PrecisionRectangle(getInitialFeedbackBounds().getCopy());
getHostFigure().translateToAbsolute(rect);
System.out.println("Feedback rectangle x=" + request.getMoveDelta().x +
", y=" + request.getMoveDelta().y);
Point verticallyConstrainedPoint = request.getMoveDelta();
verticallyConstrainedPoint.x = 0;
rect.translate(verticallyConstrainedPoint);
rect.resize(request.getSizeDelta());
feedback.translateToRelative(rect);
feedback.setBounds(rect);
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.02924 seconds