statecharts icon indicating copy to clipboard operation
statecharts copied to clipboard

Preferred size action does not take transition bend-points into account

Open tkutz opened this issue 6 years ago • 1 comments

When I set preferred size on the following region, it gets too small because transitions bend-points are not taken into account:

bildschirmfoto 2018-07-30 um 16 04 58

tkutz avatar Jul 30 '18 14:07 tkutz

I spent some time in fixing this now, but you can always construct examples where it does not work. The effort is currently too high for the priority of that issue.

The best solution so far I had is the one below. It is still a bit hacky, but whoever wants to continue to work on that can take this as a starting point:

public class SetPreferredSizeRequest extends ChangeBoundsRequest {

	public SetPreferredSizeRequest(IGraphicalEditPart host) {
		super(RequestConstants.REQ_RESIZE);
		setEditParts(host);
		IFigure figure = host.getFigure();
		Dimension prefSize = figure.getLayoutManager().getPreferredSize(figure, -1, -1);

		Point absoluteHostPos = getAbsolutePos(host);
		Set<Edge> allInnerEdges = ViewUtil.getAllInnerEdges((View) host.getModel());
		for (Edge edge : allInnerEdges) {
			IGraphicalEditPart ep = (IGraphicalEditPart) host.getViewer().getEditPartRegistry().get(edge);
			Rectangle edgeBounds = ep.getFigure().getBounds().getCopy();
			Dimension edgeSize = new Dimension(
					edgeBounds.x - absoluteHostPos.x + edgeBounds.width,
					edgeBounds.y - absoluteHostPos.y + edgeBounds.height);
			
			prefSize.union(edgeSize);
		}
		Dimension currentSize = figure.getSize();
		setSizeDelta(new Dimension(prefSize.width - currentSize.width, prefSize.height - currentSize.height));
	}
	
	protected Point getAbsolutePos(IGraphicalEditPart context) {
		RegionEditPart topRegion = null;
		Point p = context.getFigure().getBounds().getCopy().getLocation();
		if (context instanceof RegionEditPart) {
			topRegion = (RegionEditPart) context;
			p = new Point(0,0);
		}
		EditPart parent = context;
		while (parent.getParent() != null) {
			parent = parent.getParent();
			if (parent instanceof StateEditPart) {
				IGraphicalEditPart parentEditPart = (IGraphicalEditPart) parent;
				p = parentEditPart.getFigure().getBounds().getCopy().getLocation().translate(p);
			}
			if (parent instanceof RegionEditPart) {
				topRegion = (RegionEditPart) parent;
			}
		}
		p = topRegion.getFigure().getBounds().getCopy().getLocation().translate(p);
		return p;
	}

}

tkutz avatar Aug 28 '18 13:08 tkutz