JFace ColumnWeigthData заставляет родительский элемент расти

Я получил приложение Eclipse RCP и хочу использовать динамический размер столбца в TableViewer, используя ColumnWeigthData как ColumnLayoutData. Проблема в том, что родительская форма (ScrolledForm в примере кода) увеличивается на несколько пикселей всякий раз, когда я компоную таблицу.

Для воспроизведения вы можете запустить пример и открыть/закрыть раздел несколько раз. При каждом закрытии секция становится шире.

Почему это происходит и как я могу остановить это?

package com.test;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;

public class TestShell extends Shell {
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
    private final Table table;

    public static void main(final String args[]) {
        try {
            final Display display = Display.getDefault();
            final TestShell shell = new TestShell(display);
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

    public TestShell(final Display display) {
        super(display, SWT.SHELL_TRIM);
        setLayout(new FillLayout(SWT.HORIZONTAL));

        final ScrolledForm scrldfrmNewScrolledform = formToolkit.createScrolledForm(this);
        formToolkit.paintBordersFor(scrldfrmNewScrolledform);
        scrldfrmNewScrolledform.setText("New ScrolledForm");
        scrldfrmNewScrolledform.getBody().setLayout(new GridLayout(1, false));

        final Section sctnSection =
                formToolkit.createSection(scrldfrmNewScrolledform.getBody(), Section.TWISTIE | Section.TITLE_BAR);
        sctnSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        formToolkit.paintBordersFor(sctnSection);
        sctnSection.setText("Section");
        sctnSection.setExpanded(true);

        final Composite composite = new Composite(sctnSection, SWT.NONE);
        formToolkit.adapt(composite);
        formToolkit.paintBordersFor(composite);
        sctnSection.setClient(composite);
        final TableColumnLayout tcl_composite = new TableColumnLayout();
        composite.setLayout(tcl_composite);

        final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
        table = tableViewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        formToolkit.paintBordersFor(table);

        final TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        final TableColumn tblclmnColumn = tableViewerColumn.getColumn();
        tcl_composite.setColumnData(tblclmnColumn, new ColumnWeightData(150, 100));
        tblclmnColumn.setText("Column");
        createContents();
    }

    protected void createContents() {
        setText("SWT Application");
        setSize(450, 300);

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}

Спасибо за помощь.


person KlausS    schedule 12.04.2013    source источник
comment
Это ошибка, решение которой показано здесь: bugs.eclipse.org/bugs /show_bug.cgi?id=215997#c4 Хитрость заключается в том, чтобы добавить weigthHint=1 к GridData из Composite, содержащего TableViewer.   -  person KlausS    schedule 12.04.2013
comment
Тогда продолжайте и опубликуйте это как ответ.   -  person Baz    schedule 12.04.2013
comment
Недостаточно респ. Придется ждать 6 часов. Если хочешь, можешь идти вперед. Я бы проголосовал за вас благодаря вашим изменениям в исходном вопросе.   -  person KlausS    schedule 12.04.2013
comment
Нет, просто подождите 6 часов. Я проголосую, как только ответ будет здесь, чтобы вы получили репутацию ...   -  person Baz    schedule 12.04.2013


Ответы (2)


Это ошибка, связанная с макетом ColumnWeightData. Обходной путь показан здесь: bugs.eclipse. org/bugs/show_bug.cgi?id=215997#c4

Хитрость заключается в том, чтобы установить weigthHint=1 в GridData из Composite, который содержит TableViewer.

person KlausS    schedule 12.04.2013
comment
Спасибо, эта подсказка мне очень помогла. Печально, хотя они не могут этого бага с 2008 года... - person Danny Lo; 24.11.2014

Если вы используете макет сверху вниз (размеры родителей четко определены), вы можете переопределить TableColumnLayout, чтобы всегда учитывать размер родителя:

/** Forces table never to take more space than parent has*/
public class TableColumnLayout extends org.eclipse.jface.layout.TableColumnLayout {

    @Override
    protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
        return new Point(wHint, hHint);
    }
}
person Basilevs    schedule 04.09.2014