Michael Schmidt
Michael Schmidt

Reputation: 391

Avoid Tree Resize after Section expand

I've got a question I couldn't resolve myself for quite a while now.

I have a RCP ViewPage containing two sections. The sections are inside a SashForm so that the user is able to resize the expanded sections. In the bottom section there is a Tree which is empty after initialization. Through user interaction (i.e. removing a filter) the tree gets filled and has a lot of data in it. If the user now collapses the bottom view and expands it again the tree gets resized which causes ScrollBars in my form. What I want is scrollbars in the tree view.

Here is how the view is build:

- ScrolledForm
  - Form Body
    - Sash
      - Section 1
        - Composite
          - Some View
      - Section 2
        - Composite
          - Tree

I hope you understand what I'm trying to achieve.

UPDATE: Here is some source code to play with. It uses a Table instead of a tree but produces the same issue.

public class MyPersonPageEditor extends FormPage {

    public static final String ID = "some.ID"; 

    TableViewer tableViewer;

    public MyPersonPageEditor(FormEditor editor) {
        super(editor, ID, "Some Title");
    }

    @Override
    protected void createFormContent(IManagedForm managedForm) {
        FormToolkit toolkit = managedForm.getToolkit();
        ScrolledForm form = managedForm.getForm();
        Composite formBody = form.getBody();
        formBody.setLayout(new GridLayout());

        form.setText("Some Title");
        toolkit.decorateFormHeading(form.getForm());

        SashForm sfForm = new SashForm(formBody, SWT.VERTICAL);
        sfForm.setLayout(new GridLayout());
        sfForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Section topSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
        topSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        topSection.setText("Section 1 Title");
        Composite topSectionComposite = toolkit.createComposite(topSection);
        topSectionComposite.setLayout(new GridLayout());
        toolkit.createLabel(topSectionComposite, "Just some content. Doesn't need to be much");
        Button btn = toolkit.createButton(topSectionComposite, "Create Table Content", SWT.PUSH);
        btn.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                for (int i = 0 ; i < 10 ; i++) {
                    tableViewer.add("Element " + i);
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {

            }
        });
        topSection.setClient(topSectionComposite);

        Section bottomSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
        bottomSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        bottomSection.setText("Section 2 Title");
        Composite bottomSectionComposite = toolkit.createComposite(bottomSection);
        bottomSectionComposite.setLayout(new GridLayout());
        bottomSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        bottomSection.setClient(bottomSectionComposite);
        Table table = toolkit.createTable(bottomSectionComposite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | 
                 SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        table.setHeaderVisible(true);

        tableViewer = new TableViewer(table);
        tableViewer.add("New Element");
        new TableColumn(table, SWT.LEFT).setText("Spalte 1");

        TableLayout layoutDefault = new TableLayout();
        layoutDefault.addColumnData(new ColumnWeightData(1));
        table.setLayout(layoutDefault);

        form.reflow(true);
    }
}

If you click the button after start the table looks like the left picture. After you collapse and expand the second section it looks like the right one.

enter image description here

Upvotes: 1

Views: 1084

Answers (2)

viorel hojda
viorel hojda

Reputation: 21

Here is the code that works: you just have to tweak the size of the Tree/Table and make sure it will not span across the vertical space:

public void createPartControl(Composite parent) {

    FormToolkit toolkit = new FormToolkit(parent.getDisplay());
    final ScrolledForm form = toolkit.createScrolledForm(parent);
    Composite formBody = form.getBody();
    formBody.setLayout(new GridLayout());
    form.setText("Some Title");
    toolkit.decorateFormHeading(form.getForm());

    SashForm sfForm = new SashForm(formBody, SWT.VERTICAL);
    sfForm.setLayout(new GridLayout());
    sfForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    //top section
    Section topSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
    topSection.setLayout(new GridLayout());
    topSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    topSection.setText("Section 1 Title");
    Composite topSectionComposite = toolkit.createComposite(topSection);
    topSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    topSectionComposite.setLayout(new TableWrapLayout());

    toolkit.createLabel(topSectionComposite, "Just some content. Doesn't need to be much");
    Button btn = toolkit.createButton(topSectionComposite, "Create Table Content", SWT.PUSH);
    btn.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            for (int i = 0 ; i < 10 ; i++) {
                tableViewer.add("Element " + i);
            }
            form.reflow(true);
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {

        }
    });
    topSection.setClient(topSectionComposite);

    //bottom section
    Section bottomSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
    bottomSection.setLayout(new GridLayout());
    bottomSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    bottomSection.setText("Section 2 Title");
    Composite bottomSectionComposite = toolkit.createComposite(bottomSection);
    bottomSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    bottomSectionComposite.setLayout(new TableWrapLayout());

    Table table = toolkit.createTable(bottomSectionComposite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | 
            SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);

    TableWrapData ttd222 = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.FILL_GRAB);
    ttd222.maxHeight =200;
    table.setLayoutData(ttd222);
    table.setHeaderVisible(true);

    tableViewer = new TableViewer(table);
    tableViewer.add("New Element");
    new TableColumn(table, SWT.LEFT).setText("Spalte 1");

    TableLayout layoutDefault = new TableLayout();
    layoutDefault.addColumnData(new ColumnWeightData(1));
    table.setLayout(layoutDefault);

    bottomSection.setClient(bottomSectionComposite);
    form.reflow(true);
}

Upvotes: 1

viorel hojda
viorel hojda

Reputation: 21

-make sure the Tree has the SWT.H_SCROLL style (if I remember correctly), that is has a minimum size (for example with GridLayout and GridData set the minHeight to X or with TableWrapLayout and TableData heightHint to X) -if you don't manage to make it work just tell me and I'll try to make the code; also, a picture with the layout would be great

Upvotes: 0

Related Questions