eclipseswteclipse-gefdraw2d

Adding label to PolylineConnection in Draw2D


I'm trying to add a label to a PolylineConnection in Draw2d. I'm using the example in java2s as a basis. The problem is that even if I can create the text by using graphics.drawText() on the paintFigure method from the PathFigure object (that extends PolylineConnection), the label is cut out most of the time, as shown in these captures:

see 'finish' label cut

both labels are cut

To me, it looks like the bounds of the figure are leaving part of the text outside from the paint area, as it does indeed paint correctly in diagonal arrows, which have bigger bounds.

I have tried to set explicitly the bounds of the object, both in constructor and paint methods, but it seems like the PolylineConnection is ignoring them. Any idea of how to solve this or if there is another way of achieving this kind of label?


Solution

  • Please use below figure for your connection figure.

    import org.eclipse.draw2d.Label;
    import org.eclipse.draw2d.MidpointLocator;
    import org.eclipse.draw2d.PolygonDecoration;
    import org.eclipse.draw2d.PolylineConnection;
    
    public class LabelConnectionFigure extends PolylineConnection {
        protected Label label;
    
        public LabelConnectionFigure() {
            setTargetDecoration(new PolygonDecoration());
            MidpointLocator labelLocator = new MidpointLocator(this, 0);
            label = new Label("1");
            label.setOpaque(true);
            add(label, labelLocator);
        }
    
        public void setLabelText(String labelText) {
            label.setText(labelText);
        }
    
        public String getLabelText() {
            return label.getText();
        }
    }