Show
Ignore:
Timestamp:
07/28/08 13:12:15 (5 months ago)
Author:
magi@…
Message:

Updated book examples. Form example, editable Table with beans, QueryContainer? with Select.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/com/itmill/toolkit/tests/book/TableEditableBean.java

    r5114 r5119  
    55package com.itmill.toolkit.tests.book; 
    66 
     7import java.util.Collection; 
     8import java.util.Vector; 
     9 
     10import com.itmill.toolkit.data.Container; 
    711import com.itmill.toolkit.data.Item; 
    812import com.itmill.toolkit.data.Property; 
    913import com.itmill.toolkit.data.Property.ValueChangeEvent; 
    1014import com.itmill.toolkit.data.util.BeanItem; 
     15import com.itmill.toolkit.ui.AbstractField; 
    1116import com.itmill.toolkit.ui.BaseFieldFactory; 
    1217import com.itmill.toolkit.ui.CheckBox; 
     
    1722import com.itmill.toolkit.ui.Table; 
    1823 
     24/** 
     25 *  Shows how to bind a bean to a table and make it editable.  
     26 */ 
    1927public class TableEditableBean extends CustomComponent { 
     28    /** 
     29     * Let's have a simple example bean. 
     30     */ 
    2031        public class MyBean { 
    2132                boolean selected; 
     
    2334                 
    2435                public MyBean() { 
    25                         selected = false; 
    26                         text     = "Hello"; 
     36                        this.selected = false; 
     37                        this.text     = ""; 
    2738                } 
    2839 
     
    4758        } 
    4859        }; 
    49          
     60 
     61    /** 
     62     * Custom field factory that sets the fields as immediate for debugging 
     63     * purposes. This is not normally necessary, unless you want to have some 
     64     * interaction that requires it. 
     65     */ 
    5066        public class MyFieldFactory extends BaseFieldFactory { 
    5167            public Field createField(Class type, Component uiContext) { 
    52                 // Boolean field 
    53             if (Boolean.class.isAssignableFrom(type)) { 
    54                 final CheckBox checkbox = new CheckBox(); 
    55                 checkbox.setSwitchMode(true); 
    56                 checkbox.setImmediate(true); 
    57                 return checkbox; 
    58             } 
    59             return super.createField(type, uiContext); 
     68                // Let the BaseFieldFactory create the fields 
     69                Field field = super.createField(type, uiContext); 
     70                 
     71                // ...and just set them as immediate 
     72                ((AbstractField)field).setImmediate(true); 
     73                 
     74            return field; 
    6075            } 
    6176        } 
    62          
    63         public class MyTable extends Table { 
    64             /** Really adds an item and not just an item id. */ 
    65             public void addItem(Item item, Object itemId) { 
    66                  
     77 
     78    /** 
     79     * This is a custom container that allows adding BeanItems inside it. The 
     80     * BeanItem objects must be bound to a MyBean object. The item ID is an 
     81     * Integer from 0 to 99. 
     82     *  
     83     * Most of the interface methods are implemented with just dummy 
     84     * implementations, as they are not needed in this example. 
     85     */ 
     86        public class MyContainer implements Container { 
     87        Item[] items; 
     88        int    current = 0; 
     89             
     90            public MyContainer() { 
     91                items = new Item[100]; // Yeah this is just a test 
    6792            } 
     93 
     94        public boolean addContainerProperty(Object propertyId, Class type, 
     95                Object defaultValue) throws UnsupportedOperationException { 
     96            throw new UnsupportedOperationException(); 
     97        } 
     98 
     99        public Item addItem(Object itemId) throws UnsupportedOperationException { 
     100            throw new UnsupportedOperationException(); 
     101        } 
     102 
     103        public Object addItem() throws UnsupportedOperationException { 
     104            throw new UnsupportedOperationException(); 
     105        } 
     106 
     107        /** 
     108         *  This addItem method is specific for this container and allows adding 
     109         *  BeanItem objects. The BeanItems must be bound to MyBean objects.    
     110         */ 
     111        public void addItem(BeanItem item) throws UnsupportedOperationException { 
     112            items[current++] = item; 
     113        } 
     114 
     115        public boolean containsId(Object itemId) { 
     116            if (itemId instanceof Integer) { 
     117                int pos = ((Integer)itemId).intValue(); 
     118                if (pos >= 0 && pos < 100) 
     119                    return items[pos] != null; 
     120            } 
     121            return false; 
     122        } 
     123 
     124        /** 
     125         * The Table will call this method to get the property objects for the 
     126         * columns. It uses the property objects to determine the data types of 
     127         * the columns. 
     128         */ 
     129        public Property getContainerProperty(Object itemId, Object propertyId) { 
     130            if (itemId instanceof Integer) { 
     131                int pos = ((Integer)itemId).intValue(); 
     132                if (pos >= 0 && pos < 100) { 
     133                    Item item = items[pos]; 
     134                     
     135                    // The BeanItem provides the property objects for the items. 
     136                    return item.getItemProperty(propertyId); 
     137                } 
     138            } 
     139            return null; 
     140        } 
     141 
     142        /** Table calls this to get the column names. */ 
     143        public Collection getContainerPropertyIds() { 
     144            // This container can contain only BeanItems bound to MyBeans. 
     145            Item item = new BeanItem(new MyBean()); 
     146             
     147            // The BeanItem knows how to get the property names from the bean. 
     148            return item.getItemPropertyIds(); 
     149        } 
     150 
     151        public Item getItem(Object itemId) { 
     152            if (itemId instanceof Integer) { 
     153                int pos = ((Integer)itemId).intValue(); 
     154                if (pos >= 0 && pos < 100) 
     155                    return items[pos]; 
     156            } 
     157            return null; 
     158        } 
     159 
     160        public Collection getItemIds() { 
     161            Vector ids = new Vector(); 
     162            for (int i=0; i<100; i++) 
     163                ids.add(Integer.valueOf(i)); 
     164            return ids; 
     165        } 
     166 
     167        public Class getType(Object propertyId) { 
     168            return BeanItem.class; 
     169        } 
     170 
     171        public boolean removeAllItems() throws UnsupportedOperationException { 
     172            throw new UnsupportedOperationException(); 
     173        } 
     174 
     175        public boolean removeContainerProperty(Object propertyId) 
     176                throws UnsupportedOperationException { 
     177            throw new UnsupportedOperationException(); 
     178        } 
     179 
     180        public boolean removeItem(Object itemId) 
     181                throws UnsupportedOperationException { 
     182            throw new UnsupportedOperationException(); 
     183        } 
     184 
     185        public int size() { 
     186            return current; 
     187        } 
     188             
    68189        } 
    69190 
     
    78199        table.setPageLength(8); 
    79200         
    80         // Define the names and data types of columns. 
    81         table.addContainerProperty("selected", Boolean.class, null); 
    82         table.addContainerProperty("text",      String.class,  null); 
     201        // Use the custom container as the data source 
     202        MyContainer myContainer = new MyContainer(); 
     203        table.setContainerDataSource(myContainer); 
    83204         
    84205        // Add a few items in the table. 
    85206        for (int i=0; i<5; i++) { 
     207            // Create the bean 
    86208                MyBean item = new MyBean(); 
     209                item.setText("MyBean " + i); 
     210                 
     211                // Have an Item that is bound to the bean 
    87212                BeanItem bitem = new BeanItem(item); 
    88                 //table.addItem(bitem); 
    89                 table.addItem(new Object[]{bitem,bitem}, new Integer(i)); 
     213                 
     214                // Add the item directly to the container using the custom addItem() 
     215            // method. We could otherwise add it to the Table as well, but 
     216            // the Container interface of Table does not allow adding items 
     217                // as such, just item IDs. 
     218                myContainer.addItem(bitem); 
    90219        } 
    91220         
    92221        // Use custom field factory that sets the checkboxes in immediate mode. 
     222        // This is just for debugging purposes and is not normally necessary. 
    93223        table.setFieldFactory(new MyFieldFactory()); 
    94224