summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/tests/book/SelectExample.java
blob: 7b9cec14ccce86e5c64e9ee89c68d54db0581b86 (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
116
117
118
119
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.tests.book;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.ui.AbstractSelect;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.NativeSelect;
import com.itmill.toolkit.ui.OptionGroup;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Select;
import com.itmill.toolkit.ui.TwinColSelect;

/* Let us add an implementation of the ValueChangeListener interface. */
public class SelectExample extends CustomComponent implements
        Property.ValueChangeListener {

    class Planet extends Object {
        String planetName;

        Planet(String name) {
            planetName = name;
        }

        @Override
        public String toString() {
            return "The Planet " + planetName;
        }
    }

    /* Create the Select object with a caption. */
    AbstractSelect select;

    OrderedLayout layout = new OrderedLayout(OrderedLayout.ORIENTATION_VERTICAL);
    Label status = new Label("");

    SelectExample(Application application, String param, String caption,
            boolean multiselect) {
        if (param.equals("optiongroup")) {
            select = new OptionGroup(caption);
            select.setMultiSelect(multiselect);
        } else if (param.equals("twincol")) {
            select = new TwinColSelect(caption);
        } else if (param.equals("native")) {
            select = new NativeSelect(caption);
        } else if (param.equals("filter")) {
            select = new Select(caption);
            ((Select) select)
                    .setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_CONTAINS);
        } else {
            select = new Select(caption);
            select.setMultiSelect(multiselect);
        }

        layout.addComponent(select);
        setCompositionRoot(layout);

        /* Fill the component with some items. */
        final String[] planets = new String[] { "Mercury", "Venus", "Earth",
                "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

        for (int i = 0; i < planets.length; i++) {
            select.addItem(planets[i]);

            /* Create an item with an Integer as the Item ID. */
            // select.addItem(i);
            // select.addItem(new Planet(planets[i]));
            /* Set the visible caption of the item. */
            // select.setItemCaption(i, planets[i]);
            /*
             * ClassResource icon = new ClassResource
             * ("images/"+planets[i]+"_symbol.png", application);
             * layout.addComponent(new Embedded ("Icon", icon));
             * select.setItemIcon(i, icon);
             */
        }

        /*
         * By default, the change event is not triggered immediately when the
         * selection changes. This enables it.
         */
        select.setImmediate(true);

        /* Listen for changes in the selection. */
        select.addListener(this);

        // select.setStyle("twincol");
        // select.setMultiSelect(true);
        // select.setNewItemsAllowed(true);
        // int a=1;

        // select.setItemCaptionMode(Select.ITEM_CAPTION_MODE_ICON_ONLY);
        // select.setNullSelectionItemId("-- select somethingd --");
        // select.setNullSelectionAllowed(false);

        layout.addComponent(status);
    }

    /* Respond to change in the selection. */
    public void valueChange(Property.ValueChangeEvent event) {
        /*
         * The event.getProperty() returns the component. The currently selected
         * item is the property of the component, retrievable with getValue().
         */
        if (false) {
            status.setValue("Currently selected item ID: "
                    + event.getProperty().getValue() + "<br/>"
                    + "Class of the Item ID: "
                    + event.getProperty().getValue().getClass().getName()
                    + "<br/>" + "Caption: "
                    + select.getItemCaption(event.getProperty().getValue()));
            status.setContentMode(Label.CONTENT_XHTML);
        }
    }
}