All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.mdl.test.TableLayoutManagerTest Maven / Gradle / Ivy

/*
 * @(#) $Id:  $
 *
 * Copyright 2009/2010 by sIT Solutions,
 * A-1110 Wien, Geiselbergstr.21-25.
 * All rights reserved.
 *
 */
package at.spardat.xma.mdl.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import at.spardat.xma.mdl.table.TableLayoutManager;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TableLayoutManagerTest extends TestCase {
    Shell shell;
    int border = 4;
    int scrollbar = 17;

    protected void setUp() throws Exception {
        shell = new Shell();
    }

    protected void tearDown() throws Exception {
        shell.dispose();
    }

    public void testSimplePercent() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize(200+border, 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setPercent(0, 50);
        man.setPercent(1, 50);
        doTestSimplePercent(table, man, false);
    }

    public void testSimplePercentScrollbar() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableItem(table, SWT.DEFAULT);
        new TableItem(table, SWT.DEFAULT);
        table.setSize(200+border+scrollbar, 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setPercent(0, 50);
        man.setPercent(1, 50);
        doTestSimplePercent(table, man, true);
    }

    public void testSimplePercentTake() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize(200+border, 10);
        table.getColumn(0).setWidth(100);
        table.getColumn(1).setWidth(100);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT);
        doTestSimplePercent(table, man, false);
    }

    public void testSimplePercentTakeS() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize(200+border, 10);
        table.getColumn(0).setWidth(10);
        table.getColumn(1).setWidth(10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT);
        doTestSimplePercent(table, man, false);
    }

    private void doTestSimplePercent(Table table, TableLayoutManager man,boolean scroll) {
        int border = this.border;
        if(scroll) border+=scrollbar;
        man.layout();
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());
        table.setSize(210+border,10);
        man.layout(false);
        Assert.assertEquals(105, table.getColumn(0).getWidth());
        Assert.assertEquals(105, table.getColumn(1).getWidth());
        table.getColumn(0).setWidth(100);
        man.columnResized(table.getColumn(0));
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(110, table.getColumn(1).getWidth());
        table.setSize(315+border,10);
        man.layout(false);
        Assert.assertEquals(150, table.getColumn(0).getWidth());
        Assert.assertEquals(165, table.getColumn(1).getWidth());
        table.setSize(105+border,10);
        man.layout(false);
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(55, table.getColumn(1).getWidth());
        table.dispose();
    }

    public void testAbsolut() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize(200+border, 100);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setAbsolutWidth(0, 100);
        man.setAbsolutWidth(1, 100);
        doTestAbsolut(table, man);
    }

    public void testAbsolutTake() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.getColumn(0).setWidth(100);
        table.getColumn(1).setWidth(100);
        table.setSize(200+border, 100);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.ABSOLUT);
        doTestAbsolut(table, man);
    }

    private void doTestAbsolut(Table table, TableLayoutManager man) {
        man.layout();
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());

        table.setSize(210+border,100);
        man.layout(false);
        // columns will not resize
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());

        table.getColumn(0).setWidth(50);
        man.columnResized(table.getColumn(0));
        // other column will stay fixed
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());

        table.setSize(315+border,100);
        man.layout(false);
        // columns will not resize
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());

        table.getColumn(1).setWidth(5);
        man.columnResized(table.getColumn(1));
        // column will have width of minLimit 10
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(10, table.getColumn(1).getWidth());

        table.getColumn(1).setWidth(300);
        man.columnResized(table.getColumn(1));
        // other column will stay fixed
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(300, table.getColumn(1).getWidth());
        table.dispose();
    }

    public void testDefaults() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize(200+border, 100);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.layout();
        // columns will have the default width 40
        Assert.assertEquals(40, table.getColumn(0).getWidth());
        Assert.assertEquals(40, table.getColumn(1).getWidth());

        table.setSize(210+border,100);
        man.layout(false);
        // columns will not resize
        Assert.assertEquals(40, table.getColumn(0).getWidth());
        Assert.assertEquals(40, table.getColumn(1).getWidth());

        table.getColumn(0).setWidth(100);
        man.columnResized(table.getColumn(0));
        // other column will stay fixed
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(40, table.getColumn(1).getWidth());

        table.setSize(315+border,100);
        man.layout(false);
        // columns will not resize
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(40, table.getColumn(1).getWidth());

        table.getColumn(1).setWidth(5);
        man.columnResized(table.getColumn(1));
        // column will have width of minLimit 10
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(10, table.getColumn(1).getWidth());
        table.dispose();
    }

    public void testPercentMinMax() {
        doTestPercentMinMax(false,1);
    }

    public void testPercentMinMaxScroll() {
        doTestPercentMinMax(true,1);
    }

    public void testPercentMinMaxUnder100() {
        doTestPercentMinMax(false,0.5f);
    }

    public void testPercentMinMaxOver100() {
        doTestPercentMinMax(false,2);
    }

    public void testPercentMinMaxScrollUnder100() {
        doTestPercentMinMax(true,0.5f);
    }

    public void testPercentMinMaxScrollOver100() {
        doTestPercentMinMax(true,2);
    }

    private void doTestPercentMinMax(boolean scroll,float factor) {
        int border = this.border;
        if(scroll) border+=scrollbar;
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        if(scroll) {
            new TableItem(table, SWT.DEFAULT);
            new TableItem(table, SWT.DEFAULT);
        }
        table.setSize((int)(200*factor+border), 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setPercent(0, 25/factor);
        man.setPercent(1, 25/factor);
        man.setPercent(2, 25/factor);
        man.setPercent(3, 25/factor);
        man.setMinWidth(0, 50);
        man.setMinWidth(1, 25);
        man.setMinWidth(2, 5);
        man.setMaxWidth(0, 50);
        man.setMaxWidth(1, 75);
        man.setMaxWidth(3, 100);
        man.layout();
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth());

        table.setSize((int)(210*factor+border),10);
        man.layout(false);
        // all scale up, except column 0
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(52, table.getColumn(1).getWidth());
        Assert.assertEquals(52, table.getColumn(2).getWidth());
        Assert.assertEquals(52, table.getColumn(3).getWidth());

        table.setSize((int)(320*factor+border),10);
        man.layout(false);
        // all scale up, except column 0,1
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(75, table.getColumn(1).getWidth());
        Assert.assertEquals(80, table.getColumn(2).getWidth());
        Assert.assertEquals(80, table.getColumn(3).getWidth());

        table.setSize((int)(180*factor+border),10);
        man.layout(false);
        // all scale down, except column 0
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(45, table.getColumn(1).getWidth());
        Assert.assertEquals(45, table.getColumn(2).getWidth());
        Assert.assertEquals(45, table.getColumn(3).getWidth());

        table.setSize((int)(80*factor+border),10);
        man.layout(false);
        // all scale down, except column 0,1
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(25, table.getColumn(1).getWidth());
        Assert.assertEquals(20, table.getColumn(2).getWidth());
        Assert.assertEquals(20, table.getColumn(3).getWidth());

        table.setSize((int)(20*factor+border),10);
        man.layout(false);
        // column 2 goes below minLimit because its minWidth is smaller
        // column 3 stops at minLimit because it has no minWidth
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(25, table.getColumn(1).getWidth());
        Assert.assertEquals(5, table.getColumn(2).getWidth());
        Assert.assertEquals(10, table.getColumn(3).getWidth());

        table.setSize((int)(200*factor+border),10); // back to original size
        man.layout(false);
        table.getColumn(0).setWidth((int)(75*factor));
        man.columnResized(table.getColumn(0));
        // all other columns are shrinked
        Assert.assertEquals((int)(75*factor), table.getColumn(0).getWidth());
        if(factor>=1) {
            Assert.assertEquals(Math.min((int)(41.666*factor),75), table.getColumn(1).getWidth());
            Assert.assertEquals((int)(41.666*factor), table.getColumn(2).getWidth());
            Assert.assertEquals(Math.min((int)(41.666*factor),100), table.getColumn(3).getWidth());
        } else {
            Assert.assertEquals(Math.max((int)(42*factor),25), table.getColumn(1).getWidth());
            Assert.assertEquals(Math.max((int)(42*factor),5), table.getColumn(2).getWidth());
            Assert.assertEquals(Math.max((int)(42*factor),10), table.getColumn(3).getWidth());
        }
        table.setSize((int)(320*factor+border),10);
        man.layout(false);
        if(factor>=1) {
            // column 0 dose not scale up because its maxWidth is still 75
            Assert.assertEquals((int)(75*factor), table.getColumn(0).getWidth());
            Assert.assertEquals(Math.min((int)(66.5*factor),75), table.getColumn(1).getWidth());
            Assert.assertEquals((int)(66.5*factor), table.getColumn(2).getWidth());
            Assert.assertEquals(Math.min((int)(66.5*factor),100), table.getColumn(3).getWidth());
        } else {
            // column 0 may or may not scale up depending on the factor
            if(factor<=0.5) {
                Assert.assertEquals(50, table.getColumn(0).getWidth());
            }
            Assert.assertEquals(Math.max((int)(66.5*factor),25), table.getColumn(1).getWidth());
            Assert.assertEquals(Math.max((int)(66.5*factor),5), table.getColumn(2).getWidth());
            Assert.assertEquals(Math.max((int)(66.5*factor),10), table.getColumn(3).getWidth());
        }

        table.dispose();
    }

    public void testScratchOnly() {
        doTestScratchOnly(false);
    }

    public void testScratchOnlyScroll() {
        doTestScratchOnly(true);
    }

    private void doTestScratchOnly(boolean scroll) {
        int border = this.border;
        if(scroll) border+=scrollbar;
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        if(scroll) {
            new TableItem(table, SWT.DEFAULT);
            new TableItem(table, SWT.DEFAULT);
        }
        table.setSize((int)(200+border), 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setStrechColumn(0);
        man.layout();
        Assert.assertEquals(200, table.getColumn(0).getWidth());
        table.setSize(315+border,10);
        man.layout(false);
        Assert.assertEquals(315, table.getColumn(0).getWidth());
        table.setSize(15+border,10);
        man.layout(false);
        Assert.assertEquals(15, table.getColumn(0).getWidth());
        table.dispose();
    }

    public void testScratch() {
        doTestScratch(false);
    }

    public void testScratchScroll() {
        doTestScratch(true);
    }

    private void doTestScratch(boolean scroll) {
        int border = this.border;
        if(scroll) border+=scrollbar;
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        if(scroll) {
            new TableItem(table, SWT.DEFAULT);
            new TableItem(table, SWT.DEFAULT);
        }
        table.setSize((int)(200+border), 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setStrechColumn(0);
        man.setPercent(1, 25);
        man.setAbsolutWidth(2,50);
        man.setPercent(3, 25);
        man.setMinWidth(0, 25);
        man.setMinWidth(1, 50);
        man.setMaxWidth(0,100);
        man.setMaxWidth(3, 50);
        man.layout();
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth());

        table.setSize(250+border,10);
        man.layout(false);
        // col 1 scales up
        // col 2 is fixed size
        // col 3 is allready on its maxWidth
        // col 0 fills up the rest
        Assert.assertEquals(88, table.getColumn(0).getWidth());
        Assert.assertEquals(62, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth());

        table.setSize(300+border,10);
        man.layout(false);
        // col 1 scales up
        // col 2 is fixed size
        // col 3 is allready on its maxWidth
        // col 0 try to fill up the rest, but reaches its maxWidth
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(75, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth());

        table.setSize(175+border,10);
        man.layout(false);
        // col 1 is on its minWidht
        // col 2 is fixed size
        // col 3 scales down
        // col 0 scales down to fill up the small rest
        Assert.assertEquals(32, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(43, table.getColumn(3).getWidth());

        table.setSize(150+border,10);
        man.layout(false);
        // col 1 is on its minWidth
        // col 2 is fixed size
        // col 3 scales down
        // col 0 scales down to its minWidth
        Assert.assertEquals(25, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(37, table.getColumn(3).getWidth());

        table.setSize(200+border,10);
        man.layout(false);
        table.getColumn(2).setWidth(60);
        man.columnResized(table.getColumn(2));
        // col 1 is unchanged
        // col 2 is on its new fixed size
        // col 3 scales down
        // col 0 is unchanged
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(60, table.getColumn(2).getWidth());
        Assert.assertEquals(40, table.getColumn(3).getWidth());

        table.getColumn(0).setWidth(60);
        man.columnResized(table.getColumn(0));
        // col 1 is on its minWitdh
        // col 2 keeps its fixed size
        // col 3 scales down
        // col 0 is not as wide as expected, because it only gets the
        //       free space and col 1 did not scale down becaus it is
        //       allready on its minWidth
        Assert.assertEquals(55, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(60, table.getColumn(2).getWidth());
        Assert.assertEquals(35, table.getColumn(3).getWidth());

        table.dispose();
    }


    public void testManageTableAbsolut() {
        Table table = getTable(false,1);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.ABSOLUT);
        man.layout();
        verifyPercentEmpty(man);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsolut(man,1);
        verifyTable(table,1);
        table.dispose();

        table = getTable(false,2);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.ABSOLUT);
        man.layout();
        verifyPercentEmpty(man);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsolut(man,2);
        verifyTable(table,2);
        table.dispose();

        table = getTable(false,0.5f);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.ABSOLUT);
        man.layout();
        verifyPercentEmpty(man);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsolut(man,0.5f);
        verifyTable(table,0.5f);
        table.dispose();
    }

    public void testManageTablePercent() {
        Table table = getTable(false,1);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT);
        man.layout();
        verifyPercent(man,1);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();

        table = getTable(false,2);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT);
        man.layout();
        verifyPercent(man,2);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,2);
        table.dispose();

        table = getTable(false,0.5f);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT);
        man.layout();
        verifyPercent(man,0.5f);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,0.5f);
        table.dispose();
    }

    public void testManageTablePercentMax() {
        Table table = getTable(false,1);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT|TableLayoutManager.MAX);
        man.layout();
        verifyPercent(man,1);
        verifyMinEmpty(man);
        verifyMax(man,1);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();

        table = getTable(false,2);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT|TableLayoutManager.MAX);
        man.layout();
        verifyPercent(man,2);
        verifyMinEmpty(man);
        verifyMax(man,2);
        verifyAbsoluteEmpty(man);
        verifyTable(table,2);
        table.dispose();

        table = getTable(false,0.5f);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.PERCENT|TableLayoutManager.MAX);
        man.layout();
        verifyPercent(man,0.5f);
        verifyMinEmpty(man);
        verifyMax(man,0.5f);
        verifyAbsoluteEmpty(man);
        verifyTable(table,0.5f);
        table.dispose();
    }

    public void testManageTableScaledPercent() {
        Table table = getTable(false,1);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT);
        man.layout();
        verifyPercent(man,1);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();

        table = getTable(false,2);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT);
        man.layout();
        verifyPercent(man,1);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();

        table = getTable(false,0.5f);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT);
        man.layout();
        verifyPercent(man,1);
        verifyMinEmpty(man);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();
    }

    public void testManageTableScaledPercentMin() {
        Table table = getTable(false,1);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT|TableLayoutManager.MIN);
        man.layout();
        verifyPercent(man,1);
        verifyMin(man,1);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();

        table = getTable(false,2);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT|TableLayoutManager.MIN);
        man.layout();
        verifyPercent(man,1);
        verifyMin(man,2);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,2);
        table.dispose();

        table = getTable(false,0.5f);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.SCALEDPERCENT|TableLayoutManager.MIN);
        man.layout();
        verifyPercent(man,1);
        verifyMin(man,0.5f);
        verifyMaxEmpty(man);
        verifyAbsoluteEmpty(man);
        verifyTable(table,1);
        table.dispose();
    }

    private Table getTable(boolean scroll,float factor) {
        int border = this.border;
        if(scroll) border+=scrollbar;
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT).setWidth((int)(20*factor));
        new TableColumn(table,SWT.DEFAULT).setWidth((int)(60*factor));
        new TableColumn(table,SWT.DEFAULT).setWidth((int)(100*factor));
        new TableColumn(table,SWT.DEFAULT).setWidth((int)(20*factor));
        if(scroll) {
            new TableItem(table, SWT.DEFAULT);
            new TableItem(table, SWT.DEFAULT);
        }
        table.setSize((int)(200+border), 10);
        return table;
    }

    private void verifyPercent(TableLayoutManager man,float factor) {
        Assert.assertEquals(10*factor, man.getPercent(0),0.00001f);
        Assert.assertEquals(30*factor, man.getPercent(1),0.00001f);
        Assert.assertEquals(50*factor, man.getPercent(2),0.00001f);
        Assert.assertEquals(10*factor, man.getPercent(3),0.00001f);
    }

    private void verifyMin(TableLayoutManager man,float factor) {
        Assert.assertEquals(20*factor, man.getMinWidth(0),0);
        Assert.assertEquals(60*factor, man.getMinWidth(1),0);
        Assert.assertEquals(100*factor, man.getMinWidth(2),0);
        Assert.assertEquals(20*factor, man.getMinWidth(3),0);
    }

    private void verifyMax(TableLayoutManager man,float factor) {
        Assert.assertEquals(20*factor, man.getMaxWidth(0),0);
        Assert.assertEquals(60*factor, man.getMaxWidth(1),0);
        Assert.assertEquals(100*factor, man.getMaxWidth(2),0);
        Assert.assertEquals(20*factor, man.getMaxWidth(3),0);
    }

    private void verifyAbsolut(TableLayoutManager man,float factor) {
        Assert.assertEquals(20*factor, man.getAbsolutWidth(0),0);
        Assert.assertEquals(60*factor, man.getAbsolutWidth(1),0);
        Assert.assertEquals(100*factor, man.getAbsolutWidth(2),0);
        Assert.assertEquals(20*factor, man.getAbsolutWidth(3),0);
    }

    private void verifyTable(Table table,float factor) {
        Assert.assertEquals((int)(20*factor),table.getColumn(0).getWidth());
        Assert.assertEquals((int)(60*factor),table.getColumn(1).getWidth());
        Assert.assertEquals((int)(100*factor),table.getColumn(2).getWidth());
        Assert.assertEquals((int)(20*factor),table.getColumn(3).getWidth());
    }

    private void verifyPercentEmpty(TableLayoutManager man) {
        for(int i=0;i<4;i++) {
            Assert.assertEquals(-1,man.getPercent(i),0);
        }
    }

    private void verifyMinEmpty(TableLayoutManager man) {
        for(int i=0;i<4;i++) {
            Assert.assertEquals(10,man.getMinWidth(i));
        }
    }

    private void verifyMaxEmpty(TableLayoutManager man) {
        for(int i=0;i<4;i++) {
            Assert.assertEquals(Short.MAX_VALUE,man.getMaxWidth(i));
        }
    }

    private void verifyAbsoluteEmpty(TableLayoutManager man) {
        for(int i=0;i<4;i++) {
            Assert.assertEquals(-1,man.getAbsolutWidth(i));
        }
    }


    public void testCollapseColumnAllPercents() {
        Table table = getTable(false, 1);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table, TableLayoutManager.SCALEDPERCENT|TableLayoutManager.MIN);
        man.layout();
        verifyTable(table, 1);
        man.collapseColumn(2);
        Assert.assertEquals(40,table.getColumn(0).getWidth());
        Assert.assertEquals(120,table.getColumn(1).getWidth());
        Assert.assertEquals(0,table.getColumn(2).getWidth());
        Assert.assertEquals(40,table.getColumn(3).getWidth());
        man.collapseColumn(1);
        Assert.assertEquals(100,table.getColumn(0).getWidth());
        Assert.assertEquals(0,table.getColumn(1).getWidth());
        Assert.assertEquals(0,table.getColumn(2).getWidth());
        Assert.assertEquals(100,table.getColumn(3).getWidth());

        table = getTable(false, 1);
        man = new TableLayoutManager();
        man.manageTable(table, TableLayoutManager.SCALEDPERCENT|TableLayoutManager.MIN);
        man.layout();
        verifyTable(table, 1);
        man.collapseColumn(0);
        Assert.assertEquals(0,table.getColumn(0).getWidth());
        Assert.assertEquals(66,table.getColumn(1).getWidth());
        Assert.assertEquals(111,table.getColumn(2).getWidth());
        Assert.assertEquals(22,table.getColumn(3).getWidth());
    }

    public void testCollapseColumnMixed() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize((int)(200+border), 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setStrechColumn(0);
        man.setPercent(1, 25);
        man.setAbsolutWidth(2,50);
        man.setPercent(3, 25);
        man.setMinWidth(0, 25);
        man.setMinWidth(1, 50);
        man.setMaxWidth(0,100);
        man.setMaxWidth(3, 50);
        man.layout();
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth());

        man.collapseColumn(0);
        Assert.assertEquals(0, table.getColumn(0).getWidth());
        Assert.assertEquals(75, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth()); // would be 75, but limited to 50 by maxWidth
        man.collapseColumn(3);
        Assert.assertEquals(0, table.getColumn(0).getWidth());
        Assert.assertEquals(125, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());

        // other collapse sequence
        table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize((int)(200+border), 10);
        man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setStrechColumn(0);
        man.setPercent(1, 25);
        man.setAbsolutWidth(2,50);
        man.setPercent(3, 25);
        man.setMinWidth(0, 25);
        man.setMinWidth(1, 50);
        man.setMaxWidth(0,100);
        man.setMaxWidth(3, 50);
        man.layout();
        Assert.assertEquals(50, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(50, table.getColumn(3).getWidth());

        man.collapseColumn(3);
        // stretch column takes up all the free space
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(50, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());
        man.collapseColumn(2);
        // stretch column is already on its maxWidth so the column with percentage has to scale up
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());
        Assert.assertEquals(0, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());

    }

    public void testCollapseColumnMixedOver100() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize((int)(200+border), 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setStrechColumn(0);
        man.setPercent(1, 50);
        man.setAbsolutWidth(2,100);
        man.setPercent(3, 50);
        man.layout();
        Assert.assertEquals(10, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());
        Assert.assertEquals(100, table.getColumn(2).getWidth());
        Assert.assertEquals(100, table.getColumn(3).getWidth());

        man.collapseColumn(3);
        // stretch column on minWidth, percentages unchanged since still to big for the table width
        Assert.assertEquals(10, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());
        Assert.assertEquals(100, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());

        man.collapseColumn(2);
        // stretch column now takes up all the free space
        Assert.assertEquals(100, table.getColumn(0).getWidth());
        Assert.assertEquals(100, table.getColumn(1).getWidth());
        Assert.assertEquals(0, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());
    }

    public void testCollapseColumnMixedUnder100() {
        Table table = new Table(shell,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        new TableColumn(table,SWT.DEFAULT);
        table.setSize((int)(200+border), 10);
        TableLayoutManager man = new TableLayoutManager();
        man.manageTable(table,TableLayoutManager.NONE);
        man.setPercent(0, 20);
        man.setPercent(1, 20);
        man.setAbsolutWidth(2,50);
        man.setPercent(3, 20);
        man.layout();
        Assert.assertEquals(40, table.getColumn(0).getWidth());
        Assert.assertEquals(40, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(40, table.getColumn(3).getWidth());

        man.collapseColumn(3);
        //fill up only the part of the table, previously occupied by columns
        Assert.assertEquals(60, table.getColumn(0).getWidth());
        Assert.assertEquals(60, table.getColumn(1).getWidth());
        Assert.assertEquals(50, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());

        man.collapseColumn(2);
        //fill up only the part of the table, previously occupied by columns
        Assert.assertEquals(85, table.getColumn(0).getWidth());
        Assert.assertEquals(85, table.getColumn(1).getWidth());
        Assert.assertEquals(0, table.getColumn(2).getWidth());
        Assert.assertEquals(0, table.getColumn(3).getWidth());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy