开发者

How to create a GEF figure with separate label?

I've been trying to create a Draw2D Figure that consists of two parts - a central resizeable shape, such as a circle or rectangle, and an editable label for the bottom part. An example of this type of figure is the icon/label you see on a computer's Desktop.

The first attempt was to create a parent container figure with two child sub-figures - a shape figure placed centrally and a label p开发者_如何学编程laced at the bottom. It also implemented HandleBounds so that selection and resizing occurs only on the upper shape sub-figure. This turned out not to be a working solution because as the label gets wider with more text so does the main parent figure and consequently the central shape figure. In other words the overall parent figure is as wide as the child label figure.

What I'm seeking is a Figure that maintains the size of the shape figure but allows the width of the label figure to grow independently. Exactly the same behaviour as a desktop icon.


Ok I get your question right now. It's impossible to do what you want:

The parent figure can't be smaller than one of its children or this child will not be visible !!!


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 IFigure.add(IFigure child, Object constraint) method with a Constraint of type Rectangle (Draw2d)

Edit with code sample

Here is an example of what your figure class could look like:

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);
            }
        });
    }
}

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)

To test this class I created a simple Eclipse view as following:

@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));
}

Hope this can help, Manu

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜