SWT:删除复合中不可见的组件空间

时间:2012-07-06 01:34:41

标签: eclipse eclipse-plugin swt eclipse-rcp composite

我在一个composite0中放了三个复合(1,2,3),所以他们都根据composite0.setLayout(new FormLayout())设置了它们的布局。我现在遇到的问题是,我使composite3不可见:composite3.setVisible(false);(我不想删除数据,仍然需要该组件,但只是不希望它在UI上显示),并且存在很大的差距在componite2之后。如何消除大差距(用于放置复合材料2)?先谢谢你!

3 个答案:

答案 0 :(得分:1)

SWT官方示例中的Snippet 313应该可以帮助您。您基本上在composite3上设置了不同的FormData,具体取决于您是要显示还是隐藏它。

答案 1 :(得分:0)

我有一个数据绑定observable,但它假设控件在GridLayout内。我使用它来显示和隐藏向导页面中的复合和小部件,具体取决于复选框的选择状态。

要使用它,请使用以下内容进行设置:

DataBindingContext dbc = new DataBindingContext();
Button button = new Button(composite, SWT.CHECK);
IObservableValue target = SWTObservables.observeSelection(button);
dbc.bindValue(target, new HideControlObservable(someControl));

这是可观察的:

/**
 * Observable to control the presence (visibility and size) of any control
 * within a grid layout.
 * <p>
 * Changing the value of this observable will do two things:
 * <ol>
 * <li>Set the visibility of the control</li>
 * <li>Set the size of the control to zero when the control is invisible.</li>
 * </ol>
 * So, when using this observable, the control will not only be invisible,
 * but it will be gone, completely. Normally, when setting the visibility of
 * a control to <code>false</code>, the control will not be displayed but
 * will still take all the space on the screen.
 * </p>
 * <p>
 * <strong>Note:</strong> this observable works for controls within a
 * <strong>GridLayout only</strong>.
 * </p>
 */
public class HideControlObservable extends WritableValue implements IValueChangeListener {
    private final DataBindingContext dbc = new DataBindingContext();
    private final ISWTObservableValue sizeObservable;
    private final Point size = new Point(0, 0);
    private final Control control;

    public HideControlObservable(Control control) {
        super(control.getVisible(), Boolean.class);
        this.control = control;

        UpdateValueStrategy never = new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER);
        dbc.bindValue(SWTObservables.observeVisible(control), this, never, null);

        sizeObservable = SWTObservables.observeSize(control);
        sizeObservable.addValueChangeListener(this);

        if (!control.isVisible()) {
            GridData gd = (GridData) control.getLayoutData();
            if (gd == null) {
                gd = new GridData();
            }
            gd.exclude = true;
            control.setLayoutData(gd);
            control.setSize(new Point(0, 0));
        }
    }

    @Override
    public void doSetValue(Object value) {
        super.doSetValue(value);
        Boolean bool = (Boolean) value;
        if (bool) {
            GridData gd = (GridData) control.getLayoutData();
            if (gd == null) {
                gd = new GridData();
            }
            gd.exclude = false;
            control.setLayoutData(gd);
            control.setSize(size);
            control.getParent().layout();
        } else {
            GridData gd = (GridData) control.getLayoutData();
            if (gd == null) {
                gd = new GridData();
            }
            gd.exclude = true;
            control.setLayoutData(gd);
            control.setSize(new Point(0, 0));
            control.getParent().layout();
        }
    }

    @Override
    public synchronized void dispose() {
        sizeObservable.dispose();
        super.dispose();
    }

    @Override
    public void handleValueChange(ValueChangeEvent event) {
        Point newSize = (Point) event.getObservableValue().getValue();
        if (newSize.x > size.x) {
            size.x = newSize.x;
        }
        if (newSize.y > size.y) {
            size.y = newSize.y;
        }
    }
}

答案 2 :(得分:-2)

        FormData data = new FormData();
        data.left = new FormAttachment(0, 100, EditorPanel.SPACING);
        data.top = new FormAttachment(section1, EditorPanel.SPACING);
        data.height = 0;
        data.width = 0;
        addSectionFormData(a, b, c);
        a.setLayoutData(data);
        b.setLayoutData(data);
这解决了这个问题! :)