如何构建自定义的draw2d布局管理器?

发布于 2024-09-09 06:25:58 字数 270 浏览 5 评论 0原文

我想要一个布局管理器,可以按如下方式排列两个元素:

  • 一个主元素 ABCDEF 居中

  • 一个“postscript”元素 XYZ,位于右上角封装图

例如:

***********XYZ*
*   ABCDEF    *
***************

我可以为此使用现有的布局管理器吗?如何构建支持它的自定义布局管理器?

非常感谢您的建议。

I would like to have a layout manager that can arrange two elements as follows:

  • one main element ABCDEF centered

  • one "postscript" element XYZ, positioned on the top right corner of the encapsulating figure

For example:

***********XYZ*
*   ABCDEF    *
***************

Can I use an existing layoutmanager for this? How do I build a custom layout manager that supports it?

Many thanks for your advice.

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

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

发布评论

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

评论(1

去了角落 2024-09-16 06:25:58

您可以使用 XYLayout 来做到这一点。

有一个示例可供您构建:

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class HHH {

    public static void main(String[] args) {
        Display d = new Display();
        Shell s = new Shell();
        s.setLayout(new FillLayout());

        FigureCanvas canvas = new FigureCanvas(s);

        Figure content = new Figure();
        content.setLayoutManager(new XYLayout());

        PostScriptedFigure figure = new PostScriptedFigure();
        content.add(figure, new Rectangle(100, 100, -1, -1));

        figure.setMainFigure(new Label("The Main figure"));
        figure.setPostScriptFigure(new Label("ps"));
        figure.setBorder(new LineBorder());

        canvas.setContents(content);

        s.setSize(600, 500);
        s.open();

        while (!s.isDisposed()) {
            if (!d.readAndDispatch()) {
                d.sleep();
            }
        }
    }

}

class PostScriptedFigure extends Panel {

    private IFigure mainFigure, postScriptFigure;

    private class PostScriptedLayoutManager extends AbstractLayout {

    @Override
    public void layout(IFigure container) {
        final Rectangle clientArea = container.getClientArea();

        final IFigure mainFigure = getMainFigure();
        final IFigure postScriptFigure = getPostScriptFigure();

        if (mainFigure != null) {
            final Rectangle bounds = new PrecisionRectangle();
            bounds.setSize(mainFigure.getPreferredSize(SWT.DEFAULT, SWT.DEFAULT));

            final Rectangle mainClientArea = clientArea.getCopy();
            if (postScriptFigure != null) {
                mainClientArea.shrink(new Insets(postScriptFigure.getPreferredSize().height(), 0, 0, 0));
            }
            bounds.translate(mainClientArea.getCenter().getTranslated(bounds.getSize().getScaled(0.5f).getNegated()));
            mainFigure.setBounds(bounds);
        }
        if (postScriptFigure != null) {
            final Rectangle bounds = new PrecisionRectangle();
            bounds.setSize(postScriptFigure.getPreferredSize(SWT.DEFAULT, SWT.DEFAULT));
            bounds.translate(clientArea.getTopRight().getTranslated(bounds.getSize().getNegated().width(), 0));
            postScriptFigure.setBounds(bounds);
        }
        // note that other potentionally added figures are ignored
    }

    @Override
    protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHint) {
        final Rectangle rect = new PrecisionRectangle();

        final IFigure mainFigure = getMainFigure();
        if (mainFigure != null) {
            rect.setSize(mainFigure.getPreferredSize());
        }
        final IFigure postScriptFigure = getPostScriptFigure();
        if (postScriptFigure != null) {
            rect.resize(mainFigure != null ? 0 : postScriptFigure.getPreferredSize().width() , postScriptFigure.getPreferredSize().height());
        }

        // note that other potentionally added figures are ignored

        final Dimension d = rect.getSize();
        final Insets insets = container.getInsets();
        return new Dimension(d.width + insets.getWidth(), d.height + insets.getHeight()).union(getBorderPreferredSize(container));
    }

}

public PostScriptedFigure() {
    super.setLayoutManager(new PostScriptedLayoutManager());
}

@Override
public void setLayoutManager(LayoutManager manager) {
    // prevent from setting wrong layout manager
}

public IFigure getMainFigure() {
    return mainFigure;
}

public void setMainFigure(IFigure mainFigure) {
    if (getMainFigure() != null) {
        remove(getMainFigure());
    }
    this.mainFigure = mainFigure;
    add(mainFigure);
}

public IFigure getPostScriptFigure() {
    return postScriptFigure;
}

public void setPostScriptFigure(IFigure postScriptFigure) {
    if (getPostScriptFigure() != null) {
        remove(getPostScriptFigure());
    }
    this.postScriptFigure = postScriptFigure;
    add(postScriptFigure);
}

}

You can do that by using XYLayout.

There is a sample you can build on:

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class HHH {

    public static void main(String[] args) {
        Display d = new Display();
        Shell s = new Shell();
        s.setLayout(new FillLayout());

        FigureCanvas canvas = new FigureCanvas(s);

        Figure content = new Figure();
        content.setLayoutManager(new XYLayout());

        PostScriptedFigure figure = new PostScriptedFigure();
        content.add(figure, new Rectangle(100, 100, -1, -1));

        figure.setMainFigure(new Label("The Main figure"));
        figure.setPostScriptFigure(new Label("ps"));
        figure.setBorder(new LineBorder());

        canvas.setContents(content);

        s.setSize(600, 500);
        s.open();

        while (!s.isDisposed()) {
            if (!d.readAndDispatch()) {
                d.sleep();
            }
        }
    }

}

class PostScriptedFigure extends Panel {

    private IFigure mainFigure, postScriptFigure;

    private class PostScriptedLayoutManager extends AbstractLayout {

    @Override
    public void layout(IFigure container) {
        final Rectangle clientArea = container.getClientArea();

        final IFigure mainFigure = getMainFigure();
        final IFigure postScriptFigure = getPostScriptFigure();

        if (mainFigure != null) {
            final Rectangle bounds = new PrecisionRectangle();
            bounds.setSize(mainFigure.getPreferredSize(SWT.DEFAULT, SWT.DEFAULT));

            final Rectangle mainClientArea = clientArea.getCopy();
            if (postScriptFigure != null) {
                mainClientArea.shrink(new Insets(postScriptFigure.getPreferredSize().height(), 0, 0, 0));
            }
            bounds.translate(mainClientArea.getCenter().getTranslated(bounds.getSize().getScaled(0.5f).getNegated()));
            mainFigure.setBounds(bounds);
        }
        if (postScriptFigure != null) {
            final Rectangle bounds = new PrecisionRectangle();
            bounds.setSize(postScriptFigure.getPreferredSize(SWT.DEFAULT, SWT.DEFAULT));
            bounds.translate(clientArea.getTopRight().getTranslated(bounds.getSize().getNegated().width(), 0));
            postScriptFigure.setBounds(bounds);
        }
        // note that other potentionally added figures are ignored
    }

    @Override
    protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHint) {
        final Rectangle rect = new PrecisionRectangle();

        final IFigure mainFigure = getMainFigure();
        if (mainFigure != null) {
            rect.setSize(mainFigure.getPreferredSize());
        }
        final IFigure postScriptFigure = getPostScriptFigure();
        if (postScriptFigure != null) {
            rect.resize(mainFigure != null ? 0 : postScriptFigure.getPreferredSize().width() , postScriptFigure.getPreferredSize().height());
        }

        // note that other potentionally added figures are ignored

        final Dimension d = rect.getSize();
        final Insets insets = container.getInsets();
        return new Dimension(d.width + insets.getWidth(), d.height + insets.getHeight()).union(getBorderPreferredSize(container));
    }

}

public PostScriptedFigure() {
    super.setLayoutManager(new PostScriptedLayoutManager());
}

@Override
public void setLayoutManager(LayoutManager manager) {
    // prevent from setting wrong layout manager
}

public IFigure getMainFigure() {
    return mainFigure;
}

public void setMainFigure(IFigure mainFigure) {
    if (getMainFigure() != null) {
        remove(getMainFigure());
    }
    this.mainFigure = mainFigure;
    add(mainFigure);
}

public IFigure getPostScriptFigure() {
    return postScriptFigure;
}

public void setPostScriptFigure(IFigure postScriptFigure) {
    if (getPostScriptFigure() != null) {
        remove(getPostScriptFigure());
    }
    this.postScriptFigure = postScriptFigure;
    add(postScriptFigure);
}

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