aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/demo/featurebrowser/ComboBoxExample.java
blob: 60ccd23f2756e3c20a33ee51dc00e36d59ad4b13 (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
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.demo.featurebrowser;

import java.util.Random;

import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractSelect.Filtering;

/**
 * 
 */
@SuppressWarnings("serial")
public class ComboBoxExample extends CustomComponent {

    private static final String[] firstnames = new String[] { "John", "Mary",
            "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc", "Robert", "Paula",
            "Lenny", "Kenny", "Nathan", "Nicole", "Laura", "Jos", "Josie",
            "Linus" };

    private static final String[] lastnames = new String[] { "Torvalds",
            "Smith", "Adams", "Black", "Wilson", "Richards", "Thompson",
            "McGoff", "Halas", "Jones", "Beck", "Sheridan", "Picard", "Hill",
            "Fielding", "Einstein" };

    public ComboBoxExample() {
        final VerticalLayout main = new VerticalLayout();
        main.setMargin(true);
        setCompositionRoot(main);

        // starts-with filter
        final ComboBox s1 = new ComboBox("Select with starts-with filter");
        s1.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
        s1.setWidth("20em");
        Random r = new Random(5);
        for (int i = 0; i < 105; i++) {
            s1
                    .addItem(firstnames[(int) (r.nextDouble() * (firstnames.length - 1))]
                            + " "
                            + lastnames[(int) (r.nextDouble() * (lastnames.length - 1))]);
        }
        s1.setImmediate(true);
        main.addComponent(s1);

        // contains filter
        final ComboBox s2 = new ComboBox("Select with contains filter");
        s2.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
        s2.setWidth("20em");
        for (int i = 0; i < 500; i++) {
            s2
                    .addItem(firstnames[(int) (r.nextDouble() * (firstnames.length - 1))]
                            + " "
                            + lastnames[(int) (r.nextDouble() * (lastnames.length - 1))]);
        }
        s2.setImmediate(true);
        main.addComponent(s2);

        // initially empty
        final ComboBox s3 = new ComboBox("Initially empty; enter your own");
        s3.setWidth("20em");
        s3.setImmediate(true);
        s3.setNewItemsAllowed(true);
        main.addComponent(s3);

    }

}