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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests;
  17. import java.util.LinkedList;
  18. import java.util.Random;
  19. import com.vaadin.data.Property.ValueChangeEvent;
  20. import com.vaadin.data.Property.ValueChangeListener;
  21. import com.vaadin.shared.ui.label.ContentMode;
  22. import com.vaadin.ui.Label;
  23. import com.vaadin.ui.LegacyWindow;
  24. import com.vaadin.ui.Panel;
  25. import com.vaadin.ui.Select;
  26. import com.vaadin.ui.VerticalLayout;
  27. public class UsingObjectsInSelect extends com.vaadin.server.LegacyApplication
  28. implements ValueChangeListener {
  29. private final Select select = new Select();
  30. private final Label selectedTask = new Label("Selected task",
  31. ContentMode.HTML);
  32. public LinkedList<?> exampleTasks = new LinkedList<Object>();
  33. public static Random random = new Random(1);
  34. @Override
  35. public void init() {
  36. final LegacyWindow main = new LegacyWindow("Select demo");
  37. setMainWindow(main);
  38. VerticalLayout panelLayout = new VerticalLayout();
  39. panelLayout.setMargin(true);
  40. final Panel panel = new Panel("Select demo", panelLayout);
  41. panelLayout.addComponent(select);
  42. VerticalLayout panel2Layout = new VerticalLayout();
  43. panel2Layout.setMargin(true);
  44. final Panel panel2 = new Panel("Selection", panel2Layout);
  45. panel2Layout.addComponent(selectedTask);
  46. select.setCaption("Select component");
  47. select.addListener(this);
  48. select.setImmediate(true);
  49. main.addComponent(panel);
  50. main.addComponent(panel2);
  51. createExampleTasks();
  52. }
  53. public void createExampleTasks() {
  54. final String[] assignedTo = new String[] { "John", "Mary", "Joe",
  55. "Sarah", "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
  56. final String[] type = new String[] { "Enhancement", "Bugfix", "Testing",
  57. "Task" };
  58. for (int j = 0; j < 100; j++) {
  59. final Task task = new Task(
  60. type[(int) (random.nextDouble() * (type.length - 1))],
  61. assignedTo[(int) (random.nextDouble()
  62. * (assignedTo.length - 1))],
  63. random.nextInt(100));
  64. select.addItem(task);
  65. }
  66. }
  67. @Override
  68. public void valueChange(ValueChangeEvent event) {
  69. final Task task = (Task) select.getValue();
  70. selectedTask.setValue("<b>Type:</b> " + task.getType()
  71. + "<br /><b>Assigned to:</b> " + task.getAssignedTo()
  72. + "<br /><b>Estimated hours: </b>" + task.getEstimatedHours());
  73. }
  74. /**
  75. * Sample class which is bound to Vaadin components
  76. *
  77. */
  78. public class Task {
  79. private String type;
  80. private String assignedTo;
  81. private int estimatedHours;
  82. public Task(String type, String assignedTo, int estimatedHours) {
  83. this.type = type;
  84. this.assignedTo = assignedTo;
  85. this.estimatedHours = estimatedHours;
  86. }
  87. @Override
  88. public String toString() {
  89. return type + ", " + assignedTo;
  90. }
  91. public String getType() {
  92. return type;
  93. }
  94. public void setType(String type) {
  95. this.type = type;
  96. }
  97. public String getAssignedTo() {
  98. return assignedTo;
  99. }
  100. public void setAssignedTo(String assignedTo) {
  101. this.assignedTo = assignedTo;
  102. }
  103. public float getEstimatedHours() {
  104. return estimatedHours;
  105. }
  106. public void setEstimatedHours(int estimatedHours) {
  107. this.estimatedHours = estimatedHours;
  108. }
  109. }
  110. }