summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/tests/book/TableEditableBean.java
blob: fdb052197bc1eb03dc8e7272a7b82b56f30df927 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.tests.book;

import java.util.Collection;
import java.util.Vector;

import com.itmill.toolkit.data.Container;
import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.data.util.BeanItem;
import com.itmill.toolkit.ui.AbstractField;
import com.itmill.toolkit.ui.BaseFieldFactory;
import com.itmill.toolkit.ui.CheckBox;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Field;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Table;

/**
 * Shows how to bind a bean to a table and make it editable.
 */
public class TableEditableBean extends CustomComponent {
    /**
     * Let's have a simple example bean.
     */
    public class MyBean {
        boolean selected;
        String text;

        public MyBean() {
            selected = false;
            text = "";
        }

        public boolean isSelected() {
            System.out.println("isSelected() called: " + selected);
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
            System.out.println("setSelected1(" + selected + ") called.");
        }

        public String getText() {
            System.out.println("getText() called: " + text);
            return text;
        }

        public void setText(String text) {
            this.text = text;
            System.out.println("setText(" + text + ") called.");
        }
    };

    /**
     * Custom field factory that sets the fields as immediate for debugging
     * purposes. This is not normally necessary, unless you want to have some
     * interaction that requires it.
     */
    public class MyFieldFactory extends BaseFieldFactory {
        @Override
        public Field createField(Class type, Component uiContext) {
            // Let the BaseFieldFactory create the fields
            Field field = super.createField(type, uiContext);

            // ...and just set them as immediate
            ((AbstractField) field).setImmediate(true);

            return field;
        }
    }

    /**
     * This is a custom container that allows adding BeanItems inside it. The
     * BeanItem objects must be bound to a MyBean object. The item ID is an
     * Integer from 0 to 99.
     * 
     * Most of the interface methods are implemented with just dummy
     * implementations, as they are not needed in this example.
     */
    public class MyContainer implements Container {
        Item[] items;
        int current = 0;

        public MyContainer() {
            items = new Item[100]; // Yeah this is just a test
        }

        public boolean addContainerProperty(Object propertyId, Class type,
                Object defaultValue) throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        public Item addItem(Object itemId) throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        public Object addItem() throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        /**
         * This addItem method is specific for this container and allows adding
         * BeanItem objects. The BeanItems must be bound to MyBean objects.
         */
        public void addItem(BeanItem item) throws UnsupportedOperationException {
            items[current++] = item;
        }

        public boolean containsId(Object itemId) {
            if (itemId instanceof Integer) {
                int pos = ((Integer) itemId).intValue();
                if (pos >= 0 && pos < 100) {
                    return items[pos] != null;
                }
            }
            return false;
        }

        /**
         * The Table will call this method to get the property objects for the
         * columns. It uses the property objects to determine the data types of
         * the columns.
         */
        public Property getContainerProperty(Object itemId, Object propertyId) {
            if (itemId instanceof Integer) {
                int pos = ((Integer) itemId).intValue();
                if (pos >= 0 && pos < 100) {
                    Item item = items[pos];

                    // The BeanItem provides the property objects for the items.
                    return item.getItemProperty(propertyId);
                }
            }
            return null;
        }

        /** Table calls this to get the column names. */
        public Collection getContainerPropertyIds() {
            // This container can contain only BeanItems bound to MyBeans.
            Item item = new BeanItem(new MyBean());

            // The BeanItem knows how to get the property names from the bean.
            return item.getItemPropertyIds();
        }

        public Item getItem(Object itemId) {
            if (itemId instanceof Integer) {
                int pos = ((Integer) itemId).intValue();
                if (pos >= 0 && pos < 100) {
                    return items[pos];
                }
            }
            return null;
        }

        public Collection getItemIds() {
            Vector ids = new Vector();
            for (int i = 0; i < 100; i++) {
                ids.add(Integer.valueOf(i));
            }
            return ids;
        }

        public Class getType(Object propertyId) {
            return BeanItem.class;
        }

        public boolean removeAllItems() throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        public boolean removeContainerProperty(Object propertyId)
                throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        public boolean removeItem(Object itemId)
                throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        public int size() {
            return current;
        }

    }

    TableEditableBean() {
        /* A layout needed for the example. */
        OrderedLayout layout = new OrderedLayout(
                OrderedLayout.ORIENTATION_VERTICAL);
        setCompositionRoot(layout);

        // Create a table. It is by default not editable.
        final Table table = new Table();
        layout.addComponent(table);
        table.setPageLength(8);

        // Use the custom container as the data source
        MyContainer myContainer = new MyContainer();
        table.setContainerDataSource(myContainer);

        // Add a few items in the table.
        for (int i = 0; i < 5; i++) {
            // Create the bean
            MyBean item = new MyBean();
            item.setText("MyBean " + i);

            // Have an Item that is bound to the bean
            BeanItem bitem = new BeanItem(item);

            // Add the item directly to the container using the custom addItem()
            // method. We could otherwise add it to the Table as well, but
            // the Container interface of Table does not allow adding items
            // as such, just item IDs.
            myContainer.addItem(bitem);
        }

        // Use custom field factory that sets the checkboxes in immediate mode.
        // This is just for debugging purposes and is not normally necessary.
        table.setFieldFactory(new MyFieldFactory());

        // Have a check box to switch the table between normal and editable
        // mode.
        final CheckBox switchEditable = new CheckBox("Editable");
        switchEditable.addListener(new Property.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                table.setEditable(((Boolean) event.getProperty().getValue())
                        .booleanValue());
            }
        });
        switchEditable.setImmediate(true);
        layout.addComponent(switchEditable);
    }
}