]> source.dussan.org Git - vaadin-framework.git/commitdiff
Updated book tests: editable tables.
authorMarko Grönroos <magi@iki.fi>
Wed, 23 Jul 2008 09:06:17 +0000 (09:06 +0000)
committerMarko Grönroos <magi@iki.fi>
Wed, 23 Jul 2008 09:06:17 +0000 (09:06 +0000)
svn changeset:5113/svn branch:trunk

src/com/itmill/toolkit/tests/book/BookTestApplication.java
src/com/itmill/toolkit/tests/book/TableEditableBean.java [new file with mode: 0644]
src/com/itmill/toolkit/tests/book/TableExample3.java

index eb4b708acddb852c81de4fde2d942e355cad2997..0e22af8d9adc87b17b54fa65a620a7a16da60b1b 100644 (file)
@@ -308,30 +308,30 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
 
         final Tree tree = new Tree();
 
-        /* Add planets as root items in the tree. */
+        // Add planets as root items in the tree.
         for (int i = 0; i < planets.length; i++) {
             final String planet = (String) (planets[i][0]);
             tree.addItem(planet);
 
             if (planets[i].length == 1) {
-                /* The planet has no moons so make it a leaf. */
+                // The planet has no moons so make it a leaf.
                 tree.setChildrenAllowed(planet, false);
             } else {
-                /* Add children (moons) under the planets. */
+                // Add children (moons) under the planets.
                 for (int j = 1; j < planets[i].length; j++) {
                     final String moon = (String) planets[i][j];
 
-                    /* Add the item as a regular item. */
+                    // Add the item as a regular item.
                     tree.addItem(moon);
 
-                    /* Set it to be a child. */
+                    // Set it to be a child.
                     tree.setParent(moon, planet);
 
-                    /* Make the moons look like leaves. */
+                    // Make the moons look like leaves.
                     tree.setChildrenAllowed(moon, false);
                 }
 
-                /* Expand the subtree. */
+                // Expand the subtree.
                 tree.expandItemsRecursively(planet);
             }
         }
@@ -352,17 +352,31 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
         final OrderedLayout detailslayout = new OrderedLayout();
         detailspanel.setLayout(detailslayout);
         
-        // When a tree item (planet or moon) is clicked, open the item in Details view.
-        tree.setImmediate(true);
-        tree.addListener(new ValueChangeListener() {
-            public void valueChange(ValueChangeEvent event) {
-                String planet = (String) tree.getValue();
+               // Allow null selection - this is the default actually.
+               tree.setNullSelectionAllowed(true);
+               
+               // When a tree item (planet or moon) is clicked, open the item in Details view.
+               tree.setImmediate(true);
+               tree.addListener(new ValueChangeListener() {
+                   String lastselected = null;
+                   
+                   public void valueChange(ValueChangeEvent event) {
+                       String planet = (String) tree.getValue();
+               
+                       // Reselect a selected item if it is unselected by clicking it.
+                       if (planet == null) {
+                               planet = lastselected;
+                               tree.setValue(planet);
+                       }
+                       lastselected = planet;
+                
                 detailspanel.setCaption("Details on " + planet);
                 detailslayout.removeAllComponents();
                 
                 // Put some stuff in the Details view.
                 detailslayout.addComponent(new Label("Where is the cat?"));
                 detailslayout.addComponent(new Label("The cat is in " + planet + "."));
+                
             }
         });
 
@@ -563,6 +577,8 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
                 main.addComponent(new TableExample3());
             } else if (param.equals("editable")) {
                 main.addComponent(new TableEditable());
+            } else if (param.equals("bean")) {
+                main.addComponent(new TableEditableBean());
             } else if (param.equals("paging")) {
                 PagingTable table = new PagingTable();
                 table.addContainerProperty("Column 1", String.class, null);
diff --git a/src/com/itmill/toolkit/tests/book/TableEditableBean.java b/src/com/itmill/toolkit/tests/book/TableEditableBean.java
new file mode 100644 (file)
index 0000000..966095a
--- /dev/null
@@ -0,0 +1,92 @@
+/* 
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+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.CheckBox;
+import com.itmill.toolkit.ui.CustomComponent;
+import com.itmill.toolkit.ui.Label;
+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;
+               
+               public MyBean() {
+                       selected1 = false;
+                       selected2 = false;
+               }
+
+               public boolean isSelected1() {
+                       return selected1;
+               }
+
+               public void setSelected1(boolean selected) {
+                       this.selected1 = selected;
+                       System.out.println("setSelected1("+selected1+") called.");
+               }
+
+               public boolean isSelected2() {
+                       return selected2;
+               }
+
+               public void setSelected2(boolean selected) {
+                       this.selected2 = selected;
+                       System.out.println("setSelected2("+selected+") called.");
+               }
+       };
+
+    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);
+        
+        // Define the names and data types of columns.
+        table.addContainerProperty("selected1", Boolean.class, null);
+        table.addContainerProperty("selected2", Boolean.class, null);
+        
+        // Add a few items in the table.
+        for (int i=0; i<100; i++) {
+               MyBean item = new MyBean();
+               BeanItem bitem = new BeanItem(item);
+            table.addItem(new Object[] {bitem,bitem},
+                       new Integer(i)); // Item identifier
+        }
+        
+        table.setWriteThrough(true);
+        table.setReadThrough(true);
+        table.setPageLength(8);
+        
+        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());
+            }
+        });
+        switchEditable.setImmediate(true);
+        layout.addComponent(switchEditable);
+    }
+}
index 43ce9f6790b72abc84008597552c9c7245332abe..cf20a8bf49e950706d9ce8a4b61d1f8afbc30558 100644 (file)
@@ -33,7 +33,7 @@ public class TableExample3 extends CustomComponent {
         table.addContainerProperty("Is Transferred", CheckBox.class,  null);
         table.addContainerProperty("Comments",       TextField.class, null);
         table.addContainerProperty("Details",        Button.class,    null);
-
+        
         /* Add a few items in the table. */
         for (int i=0; i<100; i++) {
             // Create the fields for the current table row
@@ -69,10 +69,14 @@ public class TableExample3 extends CustomComponent {
                                         commentsField, detailsField},
                           itemId);
         }
-        
-        /* Show just three rows because they are so high. */
+
+        // Show just three rows because they are so high.
         table.setPageLength(3);
 
+        // Initially show the 50th item in the top of the table.
+        table.setCurrentPageFirstItemIndex(50);
+        //table.setCurrentPageFirstItemId(initial);
+        
         layout.addComponent(table);
     }
 }