Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have to create a container figure as you mentioned with an XYLayout and "manually" place and "size" the 2 (the shape and the label) children figure inside this layout using the <code>IFigure.add(IFigure child, Object constraint)</code> method with a Constraint of type Rectangle (Draw2d)</p> <p>Edit with code sample</p> <p>Here is an example of what your figure class could look like:</p> <pre><code>package draw2dtest.views; import org.eclipse.draw2d.ColorConstants; import org.eclipse.draw2d.Ellipse; import org.eclipse.draw2d.Figure; import org.eclipse.draw2d.FigureListener; import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.Label; import org.eclipse.draw2d.MouseEvent; import org.eclipse.draw2d.MouseListener; import org.eclipse.draw2d.XYLayout; import org.eclipse.draw2d.geometry.Rectangle; public class LabeledFigure extends Figure { private final Figure shapeFigure; private final Label labelFigure; private Rectangle customShapeConstraint; public LabeledFigure(String label) { setLayoutManager(new XYLayout()); setBackgroundColor(ColorConstants.lightGray); setOpaque(true); shapeFigure = new Ellipse(); this.add(shapeFigure); shapeFigure.setBackgroundColor(ColorConstants.yellow); shapeFigure.addMouseListener(new MouseListener.Stub() { @Override public void mousePressed(MouseEvent me) { customShapeConstraint = new Rectangle( (Rectangle) LabeledFigure.this.getLayoutManager() .getConstraint(shapeFigure)); customShapeConstraint.width -= 6; customShapeConstraint.x += 3; LabeledFigure.this.getLayoutManager().setConstraint( shapeFigure, customShapeConstraint); LabeledFigure.this.revalidate(); } }); labelFigure = new Label(label); labelFigure.setOpaque(true); labelFigure.setBackgroundColor(ColorConstants.green); labelFigure.addMouseListener(new MouseListener.Stub() { @Override public void mousePressed(MouseEvent me) { Rectangle shapeFigureConstraint = new Rectangle(0, 0, bounds.width, bounds.height - 15); LabeledFigure.this.getLayoutManager().setConstraint( shapeFigure, shapeFigureConstraint); LabeledFigure.this.revalidate(); } }); this.add(labelFigure); this.addFigureListener(new FigureListener() { @Override public void figureMoved(IFigure source) { Rectangle bounds = LabeledFigure.this.getBounds(); Rectangle shapeFigureConstraint = new Rectangle(0, 0, bounds.width, bounds.height - 15); LabeledFigure.this.getLayoutManager().setConstraint( shapeFigure, shapeFigureConstraint); Rectangle labelFigureConstraint = new Rectangle(0, bounds.height - 15, bounds.width, 15); if (customShapeConstraint != null) { labelFigureConstraint = customShapeConstraint; } LabeledFigure.this.getLayoutManager().setConstraint( labelFigure, labelFigureConstraint); } }); } } </code></pre> <p>This is not a clean class but it should be a good entry to show you how to achieve your goal. This is an example based on pure Draw2d without any Gef code, thus the resizing of the shape is done by clicking in the yellow Ellipse (the size is decreased) and on the green label (the initial size is restored)</p> <p>To test this class I created a simple Eclipse view as following:</p> <pre><code>@Override public void createPartControl(Composite parent) { FigureCanvas fc = new FigureCanvas(parent, SWT.DOUBLE_BUFFERED); fc.setBackground(ColorConstants.red); Panel panel = new Panel(); panel.setLayoutManager(new XYLayout()); LabeledFigure labeledFigure = new LabeledFigure("This is the label"); fc.setContents(panel); panel.add(labeledFigure, new Rectangle(10,10, 200,100)); } </code></pre> <p>Hope this can help, Manu</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload