如何使用 GWT 2.1 数据演示小部件

2022-09-01 00:38:58

在2010年的Google IO上,宣布GWT 2.1将包括新的数据演示小部件。2.1M可供下载,并且可能包含小部件,但尚未显示任何文档。

是否有有关如何使用它们的简短教程或示例?我看到一个谣言,说CellListCellTable是有问题的类。他们的Javadoc充满了大量的TODO,所以在使用方面仍然缺少很多东西。


答案 1

Google I/O 2010 - GWT 的 UI 大修

javadocs package com.google.gwt.cell.client in 2.1

里程碑 2 的 Eclipse 更新站点

当代码位于 bikeshed 中时,将此行添加到 gwt.xml 文件中:

<inherits name='com.google.gwt.requestfactory.RequestFactory'/>

下面是以下示例:

  • 文本单元格列表具有页面大小页面
  • 文本文本的单元格列表具有简单页面
  • 带有SimplePager和PageSizePager(buggy)的文本单元格列表和
  • 具有字符串标题和文本单元格标题的单元格表

package dpw.client;

import java.util.ArrayList;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.PageSizePager;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ListViewAdapter;

public class Index implements EntryPoint {

    public void onModuleLoad() {

        // create some data
        ArrayList<String> values = new ArrayList<String>();
        values.add("one");
        values.add("two");
        values.add("three");
        values.add("four");
        values.add("five");
        values.add("six");

        // create a ListViewAdapter
        ListViewAdapter<String> lva = new ListViewAdapter<String>();
        // give the ListViewAdapter our data
        lva.setList(values);

        {
            // CellList of TextCells with PageSizePager
            CellList<String> cl = new CellList<String>(new TextCell());
            // set the initial pagesize to 2
            cl.setPageSize(2);

            // add the CellLists to the adaptor
            lva.addView(cl);

            // create a PageSizePager, giving it a handle to the CellList
            PageSizePager<String> psp = new PageSizePager<String>(cl, 2);

            // add the CellList to the page
            RootPanel.get().add(cl);

            // add the PageSizePager to the page
            RootPanel.get().add(psp);
        }

        RootPanel.get().add(new HTML("<hr />"));

        {
            // CellList of TextCells with a SimplePager
            CellList<String> cl = new CellList<String>(new TextCell());
            // set the initial pageSize to 2
            cl.setPageSize(2);

            // add the CellLists to the adaptor
            lva.addView(cl);

            // create a pager, giving it a handle to the CellList
            SimplePager<String> pager = new SimplePager<String>(cl,
                    SimplePager.TextLocation.CENTER);

            // add the CellList to the page
            RootPanel.get().add(cl);

            // add the Pager to the page
            RootPanel.get().add(pager);
        }

        RootPanel.get().add(new HTML("<hr />"));

        {
            // CellList of TextCells with a SimplePager and PageSizePager
            CellList<String> cl = new CellList<String>(new TextCell());
            // set the initial pageSize to 2
            cl.setPageSize(2);

            // add the CellLists to the adaptor
            lva.addView(cl);

            // create a PageSizePager, giving it a handle to the CellList
            PageSizePager<String> psp = new PageSizePager<String>(cl, 1);

            // create a pager, giving it a handle to the CellList
            SimplePager<String> pager = new SimplePager<String>(cl,
                    SimplePager.TextLocation.CENTER);

            // add the CellList to the page
            RootPanel.get().add(cl);

            // add the Pager to the page
            RootPanel.get().add(pager);

            // add the PageSizePager to the page
            RootPanel.get().add(psp);
        }

        RootPanel.get().add(new HTML("<hr />"));

        {
            // CellTable
            CellTable<String> ct = new CellTable<String>();
            ct.setPageSize(2);
            lva.addView(ct);

            // add a column with a simple string header
        ct.addColumn(new TextColumn<String>() {

            @Override
            public String getValue(String object) {
                return object;
            }
        }, "String Header");

        //add a column with a TextCell header
        ct.addColumn(new TextColumn<String>() {

            @Override
            public String getValue(String object) {
                return "%" + object + "%";
            }
        }, new Header<String>(new TextCell()) {

            @Override
            public String getValue() {
                return "TextCell Header";
            }
        });

            // create a pager, giving it a handle to the CellTable
            SimplePager<String> pager = new SimplePager<String>(ct,
                    SimplePager.TextLocation.CENTER);

            // add the CellList to the page
            RootPanel.get().add(ct);

            // add the Pager to the page
            RootPanel.get().add(pager);
        }
    }
}

答案 2

我有一个可编辑的CellTable的工作原型。原型有一个表,显示字符串、布尔值、日期、整数列,每个列都有编辑器。编辑每个单元格会更新相应的模型。

public class CellTableDemo implements EntryPoint
{
    public void onModuleLoad( )
    {
        CellTable<SomeDTO> cellTable = createTable( );

        addColumns( cellTable );

        ListViewAdapter<SomeDTO> listViewAdapter = new ListViewAdapter<SomeDTO>( );
        listViewAdapter.setList( getData( ) );
        listViewAdapter.addView( cellTable );

        RootPanel.get( ).add( new SimplePager<SomeDTO>( cellTable, SimplePager.TextLocation.CENTER ) );
        RootPanel.get( ).add( cellTable );
    }

    private CellTable<SomeDTO> createTable( )
    {
        CellTable<SomeDTO> cellTable = new CellTable<SomeDTO>( );
        cellTable.setSelectionEnabled( true );
        cellTable.setSelectionModel( new SingleSelectionModel<SomeDTO>( ) );
        cellTable.setPageSize( 5 );
        cellTable.setPageStart( 0 );
        return cellTable;
    }

    private void addColumns( CellTable<SomeDTO> cellTable )
    {
        Column<SomeDTO, String> colA = new Column<SomeDTO, String>( new TextInputCell( ) )
        {
            public String getValue( SomeDTO object )
            {
                return object.getA( );
            }
        };
        colA.setFieldUpdater( new FieldUpdater<SomeDTO, String>( ) // updates changes into the backing bean
                {
                    public void update( int index, SomeDTO object, String value )
                    {
                        object.setA( value );
                    }
                } );
        cellTable.addColumn( colA, "String Column A" );

        cellTable.addColumn( new Column<SomeDTO, Integer>( new CurrencyCell( ) )
        {
            public Integer getValue( SomeDTO object )
            {
                return object.getB( );
            }
        }, "Currency Column B" );

        Column<SomeDTO, Boolean> colC = new Column<SomeDTO, Boolean>( new CheckboxCell( ) )
        {
            public Boolean getValue( SomeDTO object )
            {
                return object.getC( );
            }
        };
        colC.setFieldUpdater( new FieldUpdater<SomeDTO, Boolean>( )
        {
            public void update( int index, SomeDTO object, Boolean value )
            {
                object.setC( value );
            }
        } );
        cellTable.addColumn( colC, "Boolean Column C" );

        Column<SomeDTO, Date> colD = new Column<SomeDTO, Date>( new DatePickerCell( ) )
        {
            public Date getValue( SomeDTO object )
            {
                return object.getD( );
            }
        };
        colD.setFieldUpdater( new FieldUpdater<SomeDTO, Date>( )
        {
            public void update( int index, SomeDTO object, Date value )
            {
                object.setD( value );
            }
        } );
        cellTable.addColumn( colD, "Date Column D" );

        cellTable.addColumn( new Column<SomeDTO, String>( new ActionCell<String>( "Click of summary of this row", new Delegate<String>( )
        {
            public void execute( String row )
            {
                Window.alert( row );
            }
        } ) )
        {
            public String getValue( SomeDTO row )
            {
                return row.getSummary( );
            }
        } );
    }

    private ArrayList<SomeDTO> getData( )
    {
        ArrayList<SomeDTO> tableData = new ArrayList<SomeDTO>( );
        tableData.add( new SomeDTO( "A", 10, true, new Date( ) ) );
        tableData.add( new SomeDTO( "AA", 200, false, new Date( ) ) );
        tableData.add( new SomeDTO( "AAA", 3000, true, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAA", 40, false, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAA", 500, true, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAAA", 6000, false, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAAAA", 70, true, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAAAAA", 800, false, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAAAAAA", 9000, true, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAAAAAAA", 10, false, new Date( ) ) );
        tableData.add( new SomeDTO( "AAAAAAAAAAA", 11, true, new Date( ) ) );
        return tableData;
    }

    public class SomeDTO
    {
        private String a;
        private Integer b;
        private Boolean c;
        private Date d;

        public SomeDTO( String a, Integer b, Boolean c, Date d )
        {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }

        public String getA( )
        {
            return a;
        }

        public void setA( String a )
        {
            this.a = a;
        }

        public Integer getB( )
        {
            return b;
        }

        public void setB( Integer b )
        {
            this.b = b;
        }

        public Boolean getC( )
        {
            return c;
        }

        public void setC( Boolean c )
        {
            this.c = c;
        }

        public Date getD( )
        {
            return d;
        }

        public void setD( Date d )
        {
            this.d = d;
        }

        public String getSummary( )
        {
            return getA( ) + "  " + getB( ) + "  " + getC( ) + "  " + getD( );
        }

    }

}

推荐