]> source.dussan.org Git - vaadin-framework.git/commitdiff
Updated Table editing test.
authorMarko Grönroos <magi@iki.fi>
Wed, 23 Jul 2008 18:25:30 +0000 (18:25 +0000)
committerMarko Grönroos <magi@iki.fi>
Wed, 23 Jul 2008 18:25:30 +0000 (18:25 +0000)
svn changeset:5114/svn branch:trunk

src/com/itmill/toolkit/tests/book/TableEditableBean.java

index 966095a481a4b7b6f628adfc2bb2549b9a9ea333..4d13fa7f801834ac7909ebe8b323386ca432d8d8 100644 (file)
@@ -4,55 +4,68 @@
 
 package com.itmill.toolkit.tests.book;
 
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.GregorianCalendar;
-
-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.data.util.IndexedContainer;
-import com.itmill.toolkit.ui.Button;
+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.Label;
+import com.itmill.toolkit.ui.Field;
 import com.itmill.toolkit.ui.OrderedLayout;
-import com.itmill.toolkit.ui.RichTextArea;
 import com.itmill.toolkit.ui.Table;
-import com.itmill.toolkit.ui.TextField;
-import com.itmill.toolkit.ui.Button.ClickEvent;
 
 public class TableEditableBean extends CustomComponent {
        public class MyBean {
-               boolean selected1;
-               boolean selected2;
+               boolean selected;
+               String  text;
                
                public MyBean() {
-                       selected1 = false;
-                       selected2 = false;
+                       selected = false;
+                       text     = "Hello";
                }
 
-               public boolean isSelected1() {
-                       return selected1;
-               }
+               public boolean isSelected() {
+                   System.out.println("isSelected() called: " + selected);
+            return selected;
+        }
 
-               public void setSelected1(boolean selected) {
-                       this.selected1 = selected;
-                       System.out.println("setSelected1("+selected1+") called.");
-               }
+        public void setSelected(boolean selected) {
+            this.selected = selected;
+            System.out.println("setSelected1("+selected+") called.");
+        }
 
-               public boolean isSelected2() {
-                       return selected2;
-               }
+        public String getText() {
+            System.out.println("getText() called: " + text);
+            return text;
+        }
 
-               public void setSelected2(boolean selected) {
-                       this.selected2 = selected;
-                       System.out.println("setSelected2("+selected+") called.");
-               }
+        public void setText(String text) {
+            this.text = text;
+            System.out.println("setText("+text+") called.");
+        }
        };
+       
+       public class MyFieldFactory extends BaseFieldFactory {
+           public Field createField(Class type, Component uiContext) {
+               // Boolean field
+            if (Boolean.class.isAssignableFrom(type)) {
+                final CheckBox checkbox = new CheckBox();
+                checkbox.setSwitchMode(true);
+                checkbox.setImmediate(true);
+                return checkbox;
+            }
+            return super.createField(type, uiContext);
+           }
+       }
+       
+       public class MyTable extends Table {
+           /** Really adds an item and not just an item id. */
+           public void addItem(Item item, Object itemId) {
+               
+           }
+       }
 
     TableEditableBean() {
         /* A layout needed for the example. */
@@ -62,27 +75,27 @@ public class TableEditableBean extends CustomComponent {
         // Create a table. It is by default not editable.
         final Table table = new Table();
         layout.addComponent(table);
+        table.setPageLength(8);
         
         // Define the names and data types of columns.
-        table.addContainerProperty("selected1", Boolean.class, null);
-        table.addContainerProperty("selected2", Boolean.class, null);
+        table.addContainerProperty("selected", Boolean.class, null);
+        table.addContainerProperty("text",      String.class,  null);
         
         // Add a few items in the table.
-        for (int i=0; i<100; i++) {
+        for (int i=0; i<5; i++) {
                MyBean item = new MyBean();
                BeanItem bitem = new BeanItem(item);
-            table.addItem(new Object[] {bitem,bitem},
-                       new Integer(i)); // Item identifier
+               //table.addItem(bitem);
+               table.addItem(new Object[]{bitem,bitem}, new Integer(i));
         }
         
-        table.setWriteThrough(true);
-        table.setReadThrough(true);
-        table.setPageLength(8);
-        
+        // Use custom field factory that sets the checkboxes in immediate mode.
+        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.commit();
                 table.setEditable(((Boolean)event.getProperty().getValue()).booleanValue());
             }
         });