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.5KB

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