获取 SWT 视图的大小

Get size of an SWT view

我正在尝试确定 SWT 视图的大小,以便我可以在插件中正确布置小部件。我 运行 在 Eclipse Neon 上 Java 8.

我使用的代码如下:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;


public class ClockView extends ViewPart {

/**
     * The constructor.
     */
    public ClockView() {
    }

    /**
     * This is a callback that will allow us
     * to create the viewer and initialize it.
     */
    @Override
    public void createPartControl(Composite parent) {
        final RowLayout layout = new RowLayout(SWT.HORIZONTAL);
        layout.center = true;
        layout.justify = true;
        final Point area = parent.getSize();
        final ImmutableList<Integer> clockWidths = ImmutableList.<Integer>of(100, 200, 400);
        layout.marginWidth = (area.x - Iterables.getLast(clockWidths, null)) / 2;
        layout.marginHeight = (area.y - Iterables.getLast(clockWidths, null)) / 2;
        parent.setLayout(layout);
        clockWidths.forEach(width -> {
            ClockWidget c = new ClockWidget(parent, SWT.NONE);
            c.setLayoutData(new RowData(width, width));
        });
    }
    /**
     * Passing the focus request to the viewer's control.
     */
    @Override
    public void setFocus() {
    }

}

我的问题是我尝试过的每个方法 returns 0, 0 用于视图的大小。我试过 scrollable.getClientArea 和 Composite.getSize.

如何确定可用的区域?

调用 createPartControl 时尚未确定尺寸。

设置大小时会有Resize事件。

在主Composite上使用getClientArea()获取视图区域的大小。