Movable Labels [message #1009145] |
Thu, 14 February 2013 14:48 |
Tim E. Messages: 56 Registered: November 2012 |
Member |
|
|
Hi,
I'm currently trying to implement movable labels for my connections.
What I did is the following (for more details, don't hesitate to ask):
- implemented model elements for connection labels, namely ConnectionLabel
- ConnectionsEditPart returns in getModelChildren() all its ConnectionLabels
- implementeted ConnectionLabelEditPart for ConnectionLabels
-- refreshVisuals: update values of labels (position) according to model
-- createEditPolicies:installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new ConnectionLabelMoveEditPolicy());
- implemented ConnectionLabelMoveEditPolicy (see below)
- implemented ConnectionLabelMoveCommand (see below)
ConnectionLabelMoveEditPolicy:
public class ConnectionLabelMoveEditPolicy extends NonResizableEditPolicy {
@Override
public Command getCommand(Request request) {
// System.err.println("get COMMAND: " + request + " return: " +
// super.getCommand(request));
return super.getCommand(request);
}
@Override
public Command getMoveCommand(ChangeBoundsRequest request) {
System.err.println("GET MOVE COMMAND");
ConnectionLabelEditPart host = (ConnectionLabelEditPart) getHost();
IShapeViewElement sve = (IShapeViewElement) host.getViewElement();
IFigure parentFig = ((GraphicalEditPart) host.getParent()).getFigure();
Point delta = request.getMoveDelta();
return new ConnectionLabelMoveCommand(sve, parentFig, delta);
}
}
where ViewElement are simple data structures to store positions etc...
ConnectionLabelMoveCommand:
public class ConnectionLabelMoveCommand extends Command {
private IShapeViewElement sve;
private IFigure parent;
private Point offset;
private Point oldOffset;
/**
* Constructor
*
* @param sve The {@link IShapeViewElement} to change
* @param parent The parent of the connection label
* @param offset The new offset as {@link Point}
*/
public ConnectionLabelMoveCommand(IShapeViewElement sve, IFigure parent, Point offset) {
System.err.println("IShapeViewElement: " + sve + " IFigure: " + parent + " Point: " + offset);
this.sve = sve;
this.parent = parent;
this.offset = offset;
}
@Override
public void execute() {
System.err.println("IShapeViewElement: " + sve + " IFigure: " + parent + " Point: " + offset);
oldOffset = new Point(sve.getX(), sve.getY());
Point newOffset = oldOffset.getCopy();
parent.translateToAbsolute(newOffset);
newOffset.translate(offset);
parent.translateToRelative(newOffset);
sve.setX(newOffset.x);
sve.setY(newOffset.y);
sve.notifyObservers();
}
@Override
public void undo() {
sve.setX(oldOffset.x);
sve.setY(oldOffset.y);
sve.notifyObservers();
}
}
Now, the first thing that surprises me is:
getCommand(...) is called with a ChangeBoundsRequest, but the getMoveCommand(...) method is never called.
I narrowed it down to the fact, that a ChangeBoundsRequests' type is "orphan".
So I tried to implement the following method in the EditPolicy as well:
@Override
protected Command getOrphanCommand(Request req) {
if (!(req instanceof ChangeBoundsRequest)) {
return super.getOrphanCommand(req);
}
ChangeBoundsRequest request = (ChangeBoundsRequest) req;
ConnectionLabelEditPart host = (ConnectionLabelEditPart) getHost();
IShapeViewElement sve = (IShapeViewElement) host.getViewElement();
IFigure parentFig = ((GraphicalEditPart) host.getParent()).getFigure();
Point delta = request.getMoveDelta();
return new ConnectionLabelMoveCommand(sve, parentFig, delta);
}
}
which is called, and a command is returned, but never executed...
So I don't really know what to do at this point.
Perhaps my idea of implementing movable labels like this is just wrong, or I miss something.
I'd appreciate any help!
Thanks in advance,
Tim
PS: is there any mailing list I should/could use instead of this forum?
[Updated on: Thu, 14 February 2013 14:49] Report message to a moderator
|
|
|
Re: Movable Labels [message #1021815 is a reply to message #1009145] |
Wed, 20 March 2013 18:20 |
Tim E. Messages: 56 Registered: November 2012 |
Member |
|
|
Hi,
I tried again to sort out my problem.
I narrowed it down to a new problem
So I installed the ConnectionLabelMoveEditPolicy from my post above into my ConnectionLabelEditPart implementation and I tried to resize
or move my ConnectionLabel in the editor.
The getCommand method of the EditPolicy is called with a ChangeBoundsRequest.
The EditPolicy then calls this method:
/**
* @see org.eclipse.gef.EditPolicy#getCommand(org.eclipse.gef.Request)
*/
public Command getCommand(Request request) {
if (REQ_RESIZE.equals(request.getType())) {
return getResizeCommand((ChangeBoundsRequest) request);
}
return super.getCommand(request);
}
to find the command, which calls
/**
* Returns the command contribution for the given resize request. By
* default, the request is re-dispatched to the host's parent as a
* {@link org.eclipse.gef.RequestConstants#REQ_RESIZE_CHILDREN}. The
* parent's edit policies 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);
}
So the getHost().getParent() is called to find a command.
However, the parent of the host (the ConnectionLabel) is a ScalableRootEditPart
and I don't know how to install an EditPart in this.
I even don't know, why this is the parent, and not the connection.
I also tried to use a XYLayout policy, which does not return a command
at all, since the type of the ChangeBoundsRequest is "resize" which
is not matched by a XYLayout EditPolicy.
So at the moment I absolutely don't understand what GEF is doing and
if I'm doing something wrong, because I don't understand it.
I'd appreciate any help or pointers!
Thanks in advance!
[Updated on: Wed, 20 March 2013 18:21] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03068 seconds