为什么在删除容器的绝对大小时没有绘制 GEF 子项?

发布于 2024-11-11 18:14:49 字数 1626 浏览 2 评论 0原文

我尝试获得一个简单的 GEF 编辑器。我有一个 GraphicalEditorWithPalette 来创建我的示例模型。我有一个 RootEditPart ,它覆盖 createFiguregetModelChildren ,如下所示:

public class RootEditPart extends AbstractGraphicalEditPart {
    @Override
    protected IFigure createFigure() {
        Figure figure = new Figure();
        figure.setOpaque(true);
        figure.setLayoutManager(new XYLayout());
        figure.setSize(1000, 1000);
        return figure;
    }
    @Override
    protected List<Positionable> getModelChildren() {
        return getModel().getChildren();
    }
    // More methods ...
}

我有一个 ChildEditPart,它创建标签并覆盖 refreshVisuals > 像这样:

public class ChildEditPart extends AbstractGraphicalEditPart {
    @Override
    protected void refreshVisuals() {
        RDC rdc = getModel();
        Label nameLabel = (Label) getFigure();
        nameLabel.setText(rdc.getName());
        Point position = rdc.getPosition();

        Rectangle r = new Rectangle(position.x, position.y, -1,-1);
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, nameLabel, r);
    }
    // More methods ...
}

这按预期工作。但我不想让容器有固定的大小。相反,我尝试让容器的大小适应孩子们的需要。如果我删除 figure.setSize(1000, 1000); 则不再绘制子项。我的错误是什么?

编辑: 也许重要的是要知道我的编辑器像这样配置根编辑部分:viewer.setRootEditPart(new ScalableFreeformRootEditPart());

Edit2: 看起来实际上调用 viewer.setRootEditPart(new ScalableFreeformRootEditPart()); 是问题所在。如果我删除它,它将按预期工作。目前我不完全明白为什么......如果有人能解释一下那就太好了。

I try to get a simple working GEF editor. I have a GraphicalEditorWithPalette that creates my example model. I have a RootEditPart that overrides createFigure and getModelChildren like this:

public class RootEditPart extends AbstractGraphicalEditPart {
    @Override
    protected IFigure createFigure() {
        Figure figure = new Figure();
        figure.setOpaque(true);
        figure.setLayoutManager(new XYLayout());
        figure.setSize(1000, 1000);
        return figure;
    }
    @Override
    protected List<Positionable> getModelChildren() {
        return getModel().getChildren();
    }
    // More methods ...
}

I have a ChildEditPart that creates a label and override refreshVisuals like this:

public class ChildEditPart extends AbstractGraphicalEditPart {
    @Override
    protected void refreshVisuals() {
        RDC rdc = getModel();
        Label nameLabel = (Label) getFigure();
        nameLabel.setText(rdc.getName());
        Point position = rdc.getPosition();

        Rectangle r = new Rectangle(position.x, position.y, -1,-1);
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, nameLabel, r);
    }
    // More methods ...
}

This works as expected. But I don't want to have a fixed size for the container. Instead I try to achieve that the containers size adapts to the children. If I remove the figure.setSize(1000, 1000); the children are not drawn any more. What is my mistake?

Edit:
Maybe it is important to know that my editor configures the root edit part like this: viewer.setRootEditPart(new ScalableFreeformRootEditPart());

Edit2:
Seems like in fact the call viewer.setRootEditPart(new ScalableFreeformRootEditPart()); is the problem. If I remove this it works as expected. Currently I don't fully understand why ... would be great if someone could explain.

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

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

发布评论

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

评论(1

幼儿园老大 2024-11-18 18:14:49

您必须在需要时手动设置父母身材的大小。

figure.setSize(figure.getPreferredSize());

图形代表计算其首选大小而不是其布局(如果存在)。
XYLayout 将计算表示内容的最小尺寸。

我认为插入此代码的最佳位置是父 EditPart 的 setLayoutConstraint。

You have to set size of parent's figure manually when it needed.

figure.setSize(figure.getPreferredSize());

figure delegates computing it's prefer size to it's layout(if exist).
XYLayout will compute minimal size to represent contents.

I think best place to insert this code is setLayoutConstraint of parent EditPart.

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