确定 Eclipse GEF 中 ScrollingGraphicalViewer 可见区域的大小

发布于 2024-08-31 05:02:01 字数 1925 浏览 7 评论 0原文

我正在尝试使用 Eclipse GEF 创建一个简单的应用程序,该应用程序在 ScrollingGraphicalViewer 中显示图表。启动时,我希望图表在查看器内居中(想象一个星形布局,其中星形的中心位于视图的中心)。

以下是我认为相关的代码部分:

我的观点:

public class View extends ViewPart {
    public static final String ID = "GEFProofOfConcept.view";

...         

    public void createPartControl(Composite parent) {
        viewer.createControl(parent);

        viewer.setRootEditPart(rootEditPart);
        viewer.setEditPartFactory(editPartFactory);

        viewer.setContents(DummyModelCreator.generateModel());          
    }

编辑部分代码:

@Override
protected void refreshVisuals() {
    Project project = (Project) getModel();

    // This is where the actual drawing is done,
    // Simply a rectangle with text
    Rectangle bounds = new Rectangle(50, 50, 75, 50);
    getFigure().setBounds(bounds);
    Label label = new Label(project.getName());
    projectFont = new Font(null, "Arial", 6, SWT.NORMAL);
    label.setFont(projectFont);
    label.setTextAlignment(PositionConstants.CENTER);
    label.setBounds(bounds.crop(IFigure.NO_INSETS));

    getFigure().add(label);

    setLocation();
}

private void setLocation() {
    Project project = (Project) getModel();

    if (project.isRoot()) {
        // Place in centre of the layout
        Point centrePoint = new Point(0, 0); // This is where I need the center of the view
        getFigure().setLocation(centrePoint);
    } else {
        ...
    }       
}

以及上述编辑部分的父级:

public class ProjectDependencyModelEditPart extends AbstractGraphicalEditPart {

@Override
protected IFigure createFigure() {
    Figure f = new FreeformLayer();
    f.setLayoutManager(new XYLayout());

    return f;
}
...

也欢迎问题的替代解决方案,我肯定是 GEF(以及一般的 Eclipse)新手。

I am attempting to create a simple application using Eclipse GEF that displays a diagram inside a ScrollingGraphicalViewer. On start-up I want the diagram to be centered inside the viewer (imagine a star layout where the center of the star is in the center of the view).

Here are what I think are the relevant code sections:

My view:

public class View extends ViewPart {
    public static final String ID = "GEFProofOfConcept.view";

...         

    public void createPartControl(Composite parent) {
        viewer.createControl(parent);

        viewer.setRootEditPart(rootEditPart);
        viewer.setEditPartFactory(editPartFactory);

        viewer.setContents(DummyModelCreator.generateModel());          
    }

The edit part code:

@Override
protected void refreshVisuals() {
    Project project = (Project) getModel();

    // This is where the actual drawing is done,
    // Simply a rectangle with text
    Rectangle bounds = new Rectangle(50, 50, 75, 50);
    getFigure().setBounds(bounds);
    Label label = new Label(project.getName());
    projectFont = new Font(null, "Arial", 6, SWT.NORMAL);
    label.setFont(projectFont);
    label.setTextAlignment(PositionConstants.CENTER);
    label.setBounds(bounds.crop(IFigure.NO_INSETS));

    getFigure().add(label);

    setLocation();
}

private void setLocation() {
    Project project = (Project) getModel();

    if (project.isRoot()) {
        // Place in centre of the layout
        Point centrePoint = new Point(0, 0); // This is where I need the center of the view
        getFigure().setLocation(centrePoint);
    } else {
        ...
    }       
}

And the parent of the above edit part:

public class ProjectDependencyModelEditPart extends AbstractGraphicalEditPart {

@Override
protected IFigure createFigure() {
    Figure f = new FreeformLayer();
    f.setLayoutManager(new XYLayout());

    return f;
}
...

Alternative solutions to the problem also welcome, I am most certainly a GEF (and Eclipse in general) newbie.

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

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

发布评论

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

评论(1

扶醉桌前 2024-09-07 05:02:01

为有兴趣的人解决了这个问题:

        Dimension viewSize = (((FigureCanvas) getViewer().getControl()).getViewport().getSize());

Worked it out, for anyone who's interested:

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