想要动态调整Vaadin表中的列

时间:2016-10-26 17:17:41

标签: java css vaadin

我的Vaadin表在首次显示时看起来像这样:

first image

请注意,最后三列中较长的数字会被切断。如果我在1年和2年之间移动线,则长数字会正确显示,也就是说,它们在该列中右对齐。该程序不设置任何列的任何大小。所以我的第一个问题是如何正确创建表(或添加数据),以便它允许足够的空间。

如果我点击"更多内容"它看起来像这样:

second image

最后三列中的附加信息被截断,因为它比已存在的信息更长。所以我的第二个问题是如何让桌子再次出现,以便它有足够的空间容纳其内容。

我不知道提前在这些列中输入数据的宽度;如果我可以从数据中获取宽度,我不想在程序中设置像素值。似乎有一些规定,因为第二列更宽,以适应其更广泛的数据,没有该计划的帮助。

所涉及的课程如下:

TableUI2

package com.mycompany.vaadin3;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Property;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;

@Theme("mytheme")
public class TableUI2 extends UI{

    private static String COL1      = "Col 1";
    private static String COL2      = "Col 2";
    private static String COL3      = "Col 3";
    private static String TERM1     = "1 Year";
    private static String TERM2     = "2 Year";
    private static String TERM3     = "3 Year";

    private static String LESS_STUFF = "Less stuff";
    private static String MORE_STUFF = "More Stuff";

    private ResizableTable          table = null;
    private ArrayList<DataRow> rowList = null;
    private OptionGroup        moreOrLessOption = null;

    final private List<Label> hiddenLabels = new ArrayList<>();

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout pageLayout = new VerticalLayout();

        moreOrLessOption = createOptionGroup(); 

        Layout optionsPanelLayout = new HorizontalLayout();
        optionsPanelLayout.addComponents(moreOrLessOption);

        rowList = new DataLoader().getRowList();
        table = createTable(rowList);

        pageLayout.addComponents(optionsPanelLayout, table);

        setContent(pageLayout);
    }

    private OptionGroup createOptionGroup()
    {
        OptionGroup newGroup = new OptionGroup("");
        newGroup.setMultiSelect(false);
        newGroup.addItems(LESS_STUFF, MORE_STUFF);
        newGroup.addValueChangeListener
            ( new Property.ValueChangeListener()
                {
                    @Override
                    public void valueChange(Property.ValueChangeEvent event)
                    {
                        // depending on the value of the option, show more or less
                        boolean showMore = getShowMore(newGroup);
                        for (Label label: hiddenLabels)
                        {
                            label.setVisible(showMore);
                        }
                        table.setColumnWidth(COL1, -1);
                        table.setColumnWidth(COL2, -1);
                        table.setColumnWidth(COL3, -1);
                    }
                }
            );
        return newGroup;
    }

    private boolean getShowMore(OptionGroup mrcOption) throws RuntimeException
    {
        boolean result = false;
        String value = mrcOption.getValue().toString();
        switch(value)
        {
            case "Less stuff": break;
            case "More Stuff": result = true; break;
            default: throw new RuntimeException("OOPS! -- illegal mrcOption value");
        }
        return result;
    }

    private ResizableTable createTable(ArrayList<DataRow> rowList)
    {
        ResizableTable newTable = createNewTable();

        int listIndex = 0;
        for (DataRow row : rowList)
        {
            VerticalLayout c1 = getVerticalLayout(row.getBandwidth());
            VerticalLayout c2   = getVerticalLayout(row.getService());
            VerticalLayout c3  = getVerticalLayout(row.getProvider());
            List<Layout> termLayouts = new ArrayList<>();
            for (int i=1; i<=3; i++)
            {
                Layout termLayout = getTermLayout(1, row, hiddenLabels);
                termLayouts.add(termLayout);
            }

            newTable.addItem
                    (new Object[]
                        { 
                            c1,
                            c2,
                            c3,
                            termLayouts.get(0),
                            termLayouts.get(1),
                            termLayouts.get(2)
                        },
                        listIndex++
                    );
        }
        return newTable;
    }

    private VerticalLayout getVerticalLayout(String s)
    {
        VerticalLayout vl = new VerticalLayout();
        Label l = new Label(s);
        l.setSizeUndefined();
        vl.addComponents(l);
        vl.setComponentAlignment(l,Alignment.TOP_LEFT);
        return vl;
    }

    private Layout getTermLayout(int term, DataRow row, List<Label> costLabels)
    {
        // get charge data
        String charge = null;
        switch (term)
        {
            case 1: charge = row.getOneYearMRCCharge(); break;
            case 2: charge = row.getOneYearMRCCharge(); break;
            case 3: charge = row.getOneYearMRCCharge(); break;
        }
        Label chargeLabel = new Label(charge);

        // get cost data
        String cost = null;
        switch (term)
        {
            case 1: cost = row.getOneYearMRCCost(); break;
            case 2: cost = row.getOneYearMRCCost(); break;
            case 3: cost = row.getOneYearMRCCost(); break;
        }
        String percentage = getPercentage(cost, charge);
        String costString = String.format("(%s) %s", percentage, cost);
        Label costLabel = new Label(costString);
        costLabel.setVisible(false);
        costLabel.addStyleName("pricingtablecost");
        costLabels.add(costLabel);      // put in list so we can hide/reveal it later.

        VerticalLayout result = new VerticalLayout();
        result.setSizeFull();
        result.addComponents(chargeLabel, costLabel);
        chargeLabel.setSizeUndefined();
        costLabel.setSizeUndefined();
        result.setComponentAlignment(chargeLabel, Alignment.TOP_RIGHT);
        result.setComponentAlignment(costLabel, Alignment.TOP_RIGHT);

        return result;
    }

    private String getPercentage(String numerator, String denominator)
    {
        double num = Double.parseDouble(numerator);
        double denom = Double.parseDouble(denominator);
        double percentage = (denom - num) / denom * 100;
        String pString = String.format("%5.1f%%", percentage);
        return pString;
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = TableUI2.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }

    private ResizableTable createNewTable()
    {
        table = new ResizableTable();
        table.setSelectable(true);
        table.setImmediate(true);
        table.addStyleName(ValoTheme.TABLE_COMPACT);
        table.addStyleName(ValoTheme.TABLE_NO_HORIZONTAL_LINES);
        table.setColumnReorderingAllowed(true);
        table.setColumnCollapsingAllowed(true);
        table.setPageLength(0);         // no scrolling for table alone.

        table.addContainerProperty(COL1, Layout.class, null);
        table.addContainerProperty(COL2, Layout.class, null);
        table.addContainerProperty(COL3, Layout.class, null);
        table.addContainerProperty(TERM1, Layout.class, null);
        table.addContainerProperty(TERM2, Layout.class, null);
        table.addContainerProperty(TERM3, Layout.class, null);
        table.setColumnAlignment(COL1,  Table.Align.CENTER);
        table.setColumnAlignment(TERM1, Table.Align.RIGHT);
        table.setColumnAlignment(TERM2, Table.Align.RIGHT);
        table.setColumnAlignment(TERM3, Table.Align.RIGHT);
        table.setColumnWidth(COL1, -1);
        table.setColumnWidth(COL2, -1);
        table.setColumnWidth(COL3, -1);
        table.setColumnWidth(TERM1, -1);
        table.setColumnWidth(TERM2, -1);
        table.setColumnWidth(TERM3, -1);

        return table;
    }

}

的DataLoader

package com.mycompany.vaadin3;

import java.util.ArrayList;

/**
 * @author rcook
 */
public class DataLoader {

    public ArrayList<DataRow> getRowList()
    {
        ArrayList<DataRow> rowList         = new ArrayList<>();
        rowList.add(new DataRow("ABCDE", "This thing",  "one two",          "326.91", "175.76" ));
        rowList.add(new DataRow("",     "",             "four five six",    "1514.11", "282.48"));
        rowList.add(new DataRow("FGHIJ",     "This thing",   "seven eight", "523.29", "287.52"));
        rowList.add(new DataRow("",     "",             "nine ten",         "2926.20", "520.33"));
        return rowList;
    }

}

的DataRow:

package com.mycompany.vaadin3;

public class DataRow {
    private String bandwidth;
    private String provider;

    private String oneYearMRCCharge;
    private String oneYearMRCCost;

    private String service;  

    public String getBandwidth() {
        return bandwidth;
    }

    public void setBandwidth(String bandwidth) {
        this.bandwidth = bandwidth;
    }

    public String getProvider() {
        return provider;
    }

    public void setProvider(String provider) {
        this.provider = provider;
    }

    public String getOneYearMRCCharge() {
        return oneYearMRCCharge;
    }

    public void setOneYearMRCCharge(String oneYearMRCCharge) {
        this.oneYearMRCCharge = oneYearMRCCharge;
    }

    public String getOneYearMRCCost() {
        return oneYearMRCCost;
    }

    public void setOneYearMRCCost(String oneYearMRCCost) {
        this.oneYearMRCCost = oneYearMRCCost;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public DataRow() {}

    public DataRow(String bandwidth, String provider, String service, String charge, String cost) {
        setBandwidth(bandwidth);
        setOneYearMRCCharge(charge);
        setOneYearMRCCost(cost);
        setProvider(provider);
        setService(service);
    }

}

ResizableTable - 我试过的一件事是触发了一个propertySetChange,但是没有用。

package com.mycompany.vaadin3;

import com.vaadin.ui.Table;

/**
 * @author rcook
 */
public class ResizableTable extends Table {
    public void resize() { firePropertySetChange(); }
}

0 个答案:

没有答案
相关问题