GEF - 在Parent的其他数字下绘制儿童EditParts?

时间:2013-09-26 18:26:46

标签: eclipse-gef draw2d

我试图用eclipse中的GEF驱动的图形编辑器来说明(和编辑)一个xml模型。我的xml模型在其父子层次结构中最多可以有五个级别。层次结构中的每个元素都是它自己的EditPart(看起来像一个盒子)。子元素将表示为父盒子中包含的“box”EditParts,依此类推......

我的每个EditPart都有一个draw2d图,它本身至少会有两到三个(装饰性)draw2d数字。装饰图是像Header Rectangle,内容Rectangles,label等等。我看到这些装饰图形被绘制在EditPart的子EditParts上方 - 这意味着我看不到任何子EditParts。

我有一个解决方法,我会手动强制孩子将EditPart的数字移动到其父级EditPart的数字堆栈的顶部:

@Override
protected void refreshVisuals() {

    super.refreshVisuals();

    IFigure figure = getFigure();

    if(figure instanceof BaseElementFigure){
        //Refresh the figure...
        ((BaseElementFigure) figure).refresh(this);
    }
    if (figure.getParent() != null) {
        //This moves the figure to the top of its parent's stack so it is not drawn behind the parent's other (decorative) figures
        figure.getParent().add(figure);

        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, figure, getBounds());
    }

    refreshChildrenVisuals();
}

但是,这只是部分有效。孩子EditPart现在被渲染到Parent EditPart之上,但就Gef而言,它已经不足了 - 一些Gef事件(如drop listeners和tooltip)的行为就好像子EditPart不存在一样。

修改

EditPart的图形如下创建

@Override
protected IFigure createFigure() {
    return new PageFigure(this);
}

其中PageFigure是图的子类,它构造了它自己的装饰子图。

public class PageFigure extends Figure {

    protected Label headerLabel;
    protected RectangleFigure contentRectangle;
    protected RectangleFigure headerRectangle;

    private UiElementEditPart context;


    public PageFigure(UiElementEditPart context) {
        this.context = context;
        setLayoutManager(new XYLayout());

        this.contentRectangle = new RectangleFigure();
        contentRectangle.setFill(false);
        contentRectangle.setOpaque(false);

        this.headerRectangle = new RectangleFigure();
        headerRectangle.setFill(false);
        headerRectangle.setOpaque(false);

        this.headerLabel = new Label();
        headerLabel.setForegroundColor(ColorConstants.black);
        headerLabel.setBackgroundColor(ColorConstants.lightGray);
        headerLabel.setOpaque(true);
        headerLabel.setLabelAlignment(Label.LEFT);
        headerLabel.setBorder(new MarginBorder(0, 5, 0, 0));

        headerRectangle.add(headerLabel);

        add(contentRectangle);
        add(headerRectangle);

        //Initializing the bounds for these figures (including this one)
        setBounds(context.getBounds());

        contentRectangle.setBounds(new Rectangle(this.getBounds().x, this.getBounds().y + 20, this.getBounds().width, this.getBounds().height - 20));


        Rectangle headerBounds = new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, 20);
        headerRectangle.setBounds(headerBounds);

        headerLabel.setBounds(new Rectangle(headerBounds.x + 30, headerBounds.y, headerBounds.width - 30, 20));
    }
}

1 个答案:

答案 0 :(得分:2)

正在发生的事情是,全球环境基金正在将EditPart个子数字添加到PageFigure,并且从上一个数字开始添加GEF中的绘图顺序,并在之前添加的数字之上,如堆。这就是为什么您的孩子EditPart的数字低于父母EditPart的子女数字。

您可以做的是添加一个内容窗格图,其中包含子图,并使其成为添加到EditPart图中的第一个图。然后覆盖图的contentPane,以便在此图上添加所有子图。

如果这没有帮助,请在前一段时间查看有关此主题的教程。我可能忘记了一些细节:http://www.vainolo.com/2011/09/01/creating-an-opm-gef-editor-%E2%80%93-part-17-how-to-define-container-edit-parts/

相关问题