aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/sampler/features/table/TableMainFeaturesExample.java
blob: a7cbad0dc13c8b07cd1b0d82cac2cfad6466c876 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.itmill.toolkit.demo.sampler.features.table;

import java.util.HashSet;
import java.util.Set;

import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.demo.sampler.ExampleUtil;
import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.terminal.ThemeResource;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Table;
import com.itmill.toolkit.ui.VerticalLayout;
import com.itmill.toolkit.ui.Table.CellStyleGenerator;

public class TableMainFeaturesExample extends VerticalLayout {

    Table table = new Table("ISO-3166 Country Codes and flags");

    HashSet<Object> markedRows = new HashSet<Object>();

    static final Action ACTION_MARK = new Action("Mark");
    static final Action ACTION_UNMARK = new Action("Unmark");
    static final Action ACTION_LOG = new Action("Save");
    static final Action[] ACTIONS_UNMARKED = new Action[] { ACTION_MARK,
            ACTION_LOG };
    static final Action[] ACTIONS_MARKED = new Action[] { ACTION_UNMARK,
            ACTION_LOG };

    public TableMainFeaturesExample() {
        addComponent(table);

        // Label to indicate current selection
        final Label selected = new Label("No selection");
        addComponent(selected);

        // set a style name, so we can style rows and cells
        table.setStyleName("iso3166");

        // size
        table.setWidth("100%");
        table.setPageLength(7);

        // selectable
        table.setSelectable(true);
        table.setMultiSelect(true);
        table.setImmediate(true); // react at once when something is selected

        // connect data source
        table.setContainerDataSource(ExampleUtil.getISO3166Container());

        // turn on column reordering and collapsing
        table.setColumnReorderingAllowed(true);
        table.setColumnCollapsingAllowed(true);

        // set column headers
        table.setColumnHeaders(new String[] { "Country", "Code", "Icon file" });

        // Icons for column headers
        table.setColumnIcon(ExampleUtil.iso3166_PROPERTY_FLAG,
                new ThemeResource("icons/action_save.gif"));
        table.setColumnIcon(ExampleUtil.iso3166_PROPERTY_NAME,
                new ThemeResource("icons/icon_get_world.gif"));
        table.setColumnIcon(ExampleUtil.iso3166_PROPERTY_SHORT,
                new ThemeResource("icons/page_code.gif"));

        // Column alignment
        table.setColumnAlignment(ExampleUtil.iso3166_PROPERTY_SHORT,
                Table.ALIGN_CENTER);

        // Column width
        table.setColumnWidth(ExampleUtil.iso3166_PROPERTY_SHORT, 70);

        // Collapse one column - the user can make it visible again
        try {
            table.setColumnCollapsed(ExampleUtil.iso3166_PROPERTY_FLAG, true);
        } catch (IllegalAccessException e) {
            // Not critical, but strange
            System.err.println(e);
        }

        // show row header w/ icon
        table.setRowHeaderMode(Table.ROW_HEADER_MODE_ICON_ONLY);
        table.setItemIconPropertyId(ExampleUtil.iso3166_PROPERTY_FLAG);

        // Actions (a.k.a context menu)
        table.addActionHandler(new Action.Handler() {
            public Action[] getActions(Object target, Object sender) {
                if (markedRows.contains(target)) {
                    return ACTIONS_MARKED;
                } else {
                    return ACTIONS_UNMARKED;
                }
            }

            public void handleAction(Action action, Object sender, Object target) {
                if (ACTION_MARK.equals(action)) {
                    markedRows.add(target);
                    table.requestRepaint();
                } else if (ACTION_UNMARK.equals(action)) {
                    markedRows.remove(target);
                    table.requestRepaint();
                } else if (ACTION_LOG.equals(action)) {
                    Item item = table.getItem(target);
                    addComponent(new Label("Saved: "
                            + target
                            + ", "
                            + item.getItemProperty(
                                    ExampleUtil.iso3166_PROPERTY_NAME)
                                    .getValue()));
                }

            }

        });

        // style generator
        table.setCellStyleGenerator(new CellStyleGenerator() {
            public String getStyle(Object itemId, Object propertyId) {
                if (propertyId == null) {
                    // no propertyId, styling row
                    return (markedRows.contains(itemId) ? "marked" : null);
                } else if (ExampleUtil.iso3166_PROPERTY_NAME.equals(propertyId)) {
                    return "bold";
                } else {
                    // no style
                    return null;
                }

            }

        });

        // listen for valueChange, a.k.a 'select' and update the label
        table.addListener(new Table.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                // in multiselect mode, a Set of itemIds is returned,
                // in singleselect mode the itemId is returned directly
                Set value = (Set) event.getProperty().getValue();
                if (null == value || value.size() == 0) {
                    selected.setValue("No selection");
                } else {
                    selected.setValue("Selected: " + table.getValue());
                }
            }
        });

    }
}