| 1 | package com.itmill.toolkit.demo.jspexample; |
|---|
| 2 | |
|---|
| 3 | import java.io.ByteArrayInputStream; |
|---|
| 4 | import java.net.URL; |
|---|
| 5 | import java.util.Random; |
|---|
| 6 | |
|---|
| 7 | import com.itmill.toolkit.Application; |
|---|
| 8 | import com.itmill.toolkit.data.Item; |
|---|
| 9 | import com.itmill.toolkit.terminal.DownloadStream; |
|---|
| 10 | import com.itmill.toolkit.terminal.URIHandler; |
|---|
| 11 | import com.itmill.toolkit.ui.Table; |
|---|
| 12 | import com.itmill.toolkit.ui.Window; |
|---|
| 13 | |
|---|
| 14 | public class TableApplication extends Application implements URIHandler { |
|---|
| 15 | |
|---|
| 16 | private Table table; |
|---|
| 17 | |
|---|
| 18 | private final int PERSON_COUNT = 1000; |
|---|
| 19 | private final Person[] persons = new Person[PERSON_COUNT]; |
|---|
| 20 | |
|---|
| 21 | private String[] firstNames = { |
|---|
| 22 | "John", "Jack", "Bill", "Steven", "Timothy", "Clint", |
|---|
| 23 | "Rachel", "Tea", "Mary", "Lisa", "Jane", "Jill", "Tina", |
|---|
| 24 | "Ben", "Michael", "Michelle" |
|---|
| 25 | }; |
|---|
| 26 | private String[] lastNames = { |
|---|
| 27 | "Doe", "Bauer", "Torvalds", "Gates", "Jobs" |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | // from abstract Application class |
|---|
| 32 | @Override |
|---|
| 33 | public void init() { |
|---|
| 34 | initPersons(); |
|---|
| 35 | |
|---|
| 36 | Window window = new Window(); |
|---|
| 37 | setMainWindow(window); |
|---|
| 38 | |
|---|
| 39 | // create and populate the table |
|---|
| 40 | table = new Table(); |
|---|
| 41 | table.setWidth(500); |
|---|
| 42 | table.addContainerProperty("First name", String.class, ""); |
|---|
| 43 | table.addContainerProperty("Last name", String.class, ""); |
|---|
| 44 | refreshTable(null); |
|---|
| 45 | |
|---|
| 46 | window.addComponent(table); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | private void initPersons() { |
|---|
| 51 | // generate random persons |
|---|
| 52 | Random random = new Random(); |
|---|
| 53 | for (int i = 0; i < PERSON_COUNT; i++) { |
|---|
| 54 | persons[i] = new Person(firstNames[random.nextInt(firstNames.length)], |
|---|
| 55 | lastNames[random.nextInt(lastNames.length)]); |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | private void refreshTable(String filterString) { |
|---|
| 61 | // empty the table |
|---|
| 62 | table.removeAllItems(); |
|---|
| 63 | |
|---|
| 64 | // we want to ignore case |
|---|
| 65 | if (filterString != null) { |
|---|
| 66 | filterString = filterString.toLowerCase(); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | for (int i = 0; i < persons.length; i++) { |
|---|
| 70 | Person person = persons[i]; |
|---|
| 71 | |
|---|
| 72 | // check for a matching first name |
|---|
| 73 | boolean matches = true; |
|---|
| 74 | if (filterString != null && filterString.length() > 0) { |
|---|
| 75 | matches = (person.getFirstName().toLowerCase().startsWith(filterString)); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | // add to table if a match was found |
|---|
| 79 | if (matches) { |
|---|
| 80 | Item row = (Item) table.addItem(Integer.valueOf(i)); |
|---|
| 81 | row.getItemProperty("First name").setValue(person.getFirstName()); |
|---|
| 82 | row.getItemProperty("Last name").setValue(person.getLastName()); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | // from URIHandler interface |
|---|
| 89 | @Override |
|---|
| 90 | public DownloadStream handleURI(URL context, String relativeUri) { |
|---|
| 91 | if (relativeUri.startsWith("refresh/")) { |
|---|
| 92 | refreshTable(relativeUri.substring("refresh/".length())); |
|---|
| 93 | return new DownloadStream(new ByteArrayInputStream(Integer.toString(table.size()) |
|---|
| 94 | .getBytes()), "text/plain", null); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | return super.handleURI(context, relativeUri); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * A simple Person class with first and last name. |
|---|
| 104 | * |
|---|
| 105 | * @author Teemu Pöntelin / IT Mill Ltd |
|---|
| 106 | */ |
|---|
| 107 | private static class Person { |
|---|
| 108 | private final String firstName; |
|---|
| 109 | private final String lastName; |
|---|
| 110 | |
|---|
| 111 | public Person(String fname, String lname) { |
|---|
| 112 | firstName = fname; |
|---|
| 113 | lastName = lname; |
|---|
| 114 | } |
|---|
| 115 | public String getFirstName() { |
|---|
| 116 | return firstName; |
|---|
| 117 | } |
|---|
| 118 | public String getLastName() { |
|---|
| 119 | return lastName; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|