aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/tests/UsingObjectsInSelect.java
blob: 6a608f17f31afce227dc46ff02fe656110554d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.tests;

import java.util.LinkedList;
import java.util.Random;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Select;
import com.vaadin.ui.Window;

public class UsingObjectsInSelect extends com.vaadin.Application
        implements ValueChangeListener {

    private final Select select = new Select();
    private final Label selectedTask = new Label("Selected task",
            Label.CONTENT_XHTML);

    public LinkedList exampleTasks = new LinkedList();

    public static Random random = new Random(1);

    @Override
    public void init() {
        final Window main = new Window("Select demo");
        setMainWindow(main);

        final Panel panel = new Panel("Select demo");
        panel.addComponent(select);
        final Panel panel2 = new Panel("Selection");
        panel2.addComponent(selectedTask);

        select.setCaption("Select component");
        select.addListener(this);
        select.setImmediate(true);

        main.addComponent(panel);
        main.addComponent(panel2);

        createExampleTasks();
    }

    public void createExampleTasks() {
        final String[] assignedTo = new String[] { "John", "Mary", "Joe",
                "Sarah", "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
        final String[] type = new String[] { "Enhancement", "Bugfix",
                "Testing", "Task" };
        for (int j = 0; j < 100; j++) {
            final Task task = new Task(
                    type[(int) (random.nextDouble() * (type.length - 1))],
                    assignedTo[(int) (random.nextDouble() * (assignedTo.length - 1))],
                    random.nextInt(100));
            select.addItem(task);
        }
    }

    public void valueChange(ValueChangeEvent event) {
        final Task task = (Task) select.getValue();
        selectedTask.setValue("<b>Type:</b> " + task.getType()
                + "<br /><b>Assigned to:</b> " + task.getAssignedTo()
                + "<br /><b>Estimated hours: </b>" + task.getEstimatedHours());
    }

    /**
     * Sample class which is bind in Toolkit components
     * 
     */
    public class Task {

        private String type;
        private String assignedTo;
        private int estimatedHours;

        public Task(String type, String assignedTo, int estimatedHours) {
            this.type = type;
            this.assignedTo = assignedTo;
            this.estimatedHours = estimatedHours;
        }

        @Override
        public String toString() {
            return type + ", " + assignedTo;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getAssignedTo() {
            return assignedTo;
        }

        public void setAssignedTo(String assignedTo) {
            this.assignedTo = assignedTo;
        }

        public float getEstimatedHours() {
            return estimatedHours;
        }

        public void setEstimatedHours(int estimatedHours) {
            this.estimatedHours = estimatedHours;
        }
    }

}