如何创建带有单独标签的 GEF 图?

发布于 2024-10-21 22:29:57 字数 326 浏览 7 评论 0原文

我一直在尝试创建一个由两部分组成的 Draw2D 图 - 一个中心可调整大小的形状,例如圆形或矩形,以及底部的可编辑标签。此类图形的一个示例是您在计算机桌面上看到的图标/标签。

第一次尝试是创建一个带有两个子子图形的父容器图形 - 一个形状图形放置在中央,一个标签放置在底部。它还实现了 HandleBounds,以便仅在上部形状子图上进行选择和调整大小。事实证明这不是一个有效的解决方案,因为随着标签随着文本的增多而变宽,主要父图形也会变宽,因此中心形状图形也会变宽。换句话说,整个父图形与子标签图形一样宽。

我正在寻找的是一个保持形状图形大小但允许标签图形宽度独立增长的图形。与桌面图标完全相同的行为。

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 placed 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-10-28 22:29:57

好的,我现在明白你的问题了。不可能做你想做的事:

父图形不能小于其子图形之一,否则该子图形将不可见!

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 !!!

蝶…霜飞 2024-10-28 22:29:57

您必须使用 XYLayout 创建一个容器图形,并使用 IFigure.add(IFigure child, Object constraint 在此布局内“手动”放置和“调整”2 个(形状和标签)子图形的大小) 方法,具有类型为 Rectangle (Draw2d) 的约束

使用代码示例进行编辑

以下是您的图形类的示例:

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

这不是一个干净的类,但它应该是一个很好的入口,向您展示如何实现你的目标。这是一个基于纯 Draw2d 的示例,没有任何 Gef 代码,因此形状的大小调整是通过单击黄色椭圆(尺寸减小)和绿色标签(恢复初始尺寸)来完成的

。创建了一个简单的 Eclipse 视图,如下所示:

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

希望这可以有所帮助,
马努

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文