You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsingObjectsInSelect.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.tests;
  5. import java.util.LinkedList;
  6. import java.util.Random;
  7. import com.vaadin.data.Property.ValueChangeEvent;
  8. import com.vaadin.data.Property.ValueChangeListener;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.ui.Panel;
  11. import com.vaadin.ui.Select;
  12. import com.vaadin.ui.Window;
  13. public class UsingObjectsInSelect extends com.vaadin.Application implements
  14. ValueChangeListener {
  15. private final Select select = new Select();
  16. private final Label selectedTask = new Label("Selected task",
  17. Label.CONTENT_XHTML);
  18. public LinkedList exampleTasks = new LinkedList();
  19. public static Random random = new Random(1);
  20. @Override
  21. public void init() {
  22. final Window main = new Window("Select demo");
  23. setMainWindow(main);
  24. final Panel panel = new Panel("Select demo");
  25. panel.addComponent(select);
  26. final Panel panel2 = new Panel("Selection");
  27. panel2.addComponent(selectedTask);
  28. select.setCaption("Select component");
  29. select.addListener(this);
  30. select.setImmediate(true);
  31. main.addComponent(panel);
  32. main.addComponent(panel2);
  33. createExampleTasks();
  34. }
  35. public void createExampleTasks() {
  36. final String[] assignedTo = new String[] { "John", "Mary", "Joe",
  37. "Sarah", "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
  38. final String[] type = new String[] { "Enhancement", "Bugfix",
  39. "Testing", "Task" };
  40. for (int j = 0; j < 100; j++) {
  41. final Task task = new Task(
  42. type[(int) (random.nextDouble() * (type.length - 1))],
  43. assignedTo[(int) (random.nextDouble() * (assignedTo.length - 1))],
  44. random.nextInt(100));
  45. select.addItem(task);
  46. }
  47. }
  48. public void valueChange(ValueChangeEvent event) {
  49. final Task task = (Task) select.getValue();
  50. selectedTask.setValue("<b>Type:</b> " + task.getType()
  51. + "<br /><b>Assigned to:</b> " + task.getAssignedTo()
  52. + "<br /><b>Estimated hours: </b>" + task.getEstimatedHours());
  53. }
  54. /**
  55. * Sample class which is bound to Vaadin components
  56. *
  57. */
  58. public class Task {
  59. private String type;
  60. private String assignedTo;
  61. private int estimatedHours;
  62. public Task(String type, String assignedTo, int estimatedHours) {
  63. this.type = type;
  64. this.assignedTo = assignedTo;
  65. this.estimatedHours = estimatedHours;
  66. }
  67. @Override
  68. public String toString() {
  69. return type + ", " + assignedTo;
  70. }
  71. public String getType() {
  72. return type;
  73. }
  74. public void setType(String type) {
  75. this.type = type;
  76. }
  77. public String getAssignedTo() {
  78. return assignedTo;
  79. }
  80. public void setAssignedTo(String assignedTo) {
  81. this.assignedTo = assignedTo;
  82. }
  83. public float getEstimatedHours() {
  84. return estimatedHours;
  85. }
  86. public void setEstimatedHours(int estimatedHours) {
  87. this.estimatedHours = estimatedHours;
  88. }
  89. }
  90. }