/** * */ package test; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.faces.bean.CustomScoped; import javax.faces.bean.ManagedBean; import org.icefaces.ace.model.table.LazyDataModel; import org.icefaces.ace.model.table.SortCriteria; /** * */ @ManagedBean(name="listBean") @CustomScoped(value = "#{window}") public class TestDataModel extends LazyDataModel { /** * */ private static final long serialVersionUID = 1L; private List data; private List columns; private Date date; private int dataSize = 5000; private RowItem selectedItem; private RowItem[] selectedItems; public TestDataModel() { this.setPageSize(5); generateList(dataSize); } @Override public List load(int first, int pageSize, SortCriteria[] sort, Map filters) { System.out.println(first + ", " + pageSize + ", " + sort + ", " + filters); List ret = getData().subList(first, Math.min(first + pageSize,dataSize)); // now that filtering is applied, we know the total amount of items so we can set the rowcount: setRowCount(dataSize); return ret; } private List getData() { if( data == null ) { generateList(getRowCount()); } return data; } public List getColumns() { if( columns == null ) { columns = new ArrayList(); columns.add(new Column("Name_Col", "name")); columns.add(new Column("ID_Col", "ID")); } return columns; } public void generateList(int c) { this.data = new ArrayList(c); for( int i = 0; i < c; i++ ) { this.data.add(new RowItem()); } } public static class Column { String name; String property; public Column( String name, String prop ) { this.name = name; this.property = prop; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } public String toString() { return this.name + ": " + this.property; } } public static class RowItem { private String ID; private String name; private int amount; private Date date; private BigDecimal cost; private static int count = 100000; private static final String[] superlatives = new String[] {"Ordinary ", "Awesome ", "Mediocre ", "Worn ", "Magic ", "Extraordinary ", "Plain Old ", "Socketed ", "Possessed ", "Strong ", "Giant ", "Flaming ", "Ancient "}; private static final String[] names = new String[] {"Long Sword", "Buckler", "Chain Mail", "Long Bow", "Wand", "Staff", "Long Staff", "Spiked Club", "Sceptre", "Sword of the Locust", "Maul", "Ice Wand", "Elvish Amulet", "Ring of Power", "Flail", "Club"}; public RowItem(String name, int amount, BigDecimal cost, Date date) { this.ID = "" + count++; this.name = name; this.amount = amount; this.cost = cost; this.date = date; } public RowItem() { this(generateName(), (int) (Math.random() * 10), new BigDecimal(100.0).add( new BigDecimal(Math.random() * 100)).subtract( new BigDecimal(50)), new Date(System.currentTimeMillis() + (int)(Math.random() * 864000000))); } private static String generateName() { return superlatives[count%superlatives.length] + names[count%names.length]; } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public BigDecimal getCost() { return cost; } public void setCost(BigDecimal cost) { this.cost = cost; } public String toString() { return this.name + " (" + this.ID + ")"; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } public RowItem getSelectedItem() { return selectedItem; } public void setSelectedItem(RowItem selectedItem) { this.selectedItem = selectedItem; } public RowItem[] getSelectedItems() { return selectedItems; } public void setSelectedItems(RowItem[] selectedItems) { this.selectedItems = selectedItems; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }