From: Marko Grönroos Date: Wed, 23 Jul 2008 09:06:17 +0000 (+0000) Subject: Updated book tests: editable tables. X-Git-Tag: 6.7.0.beta1~4416 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=df5b8e42709c801f9bfeb92636bfbb94829712e4;p=vaadin-framework.git Updated book tests: editable tables. svn changeset:5113/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/tests/book/BookTestApplication.java b/src/com/itmill/toolkit/tests/book/BookTestApplication.java index eb4b708acd..0e22af8d9a 100644 --- a/src/com/itmill/toolkit/tests/book/BookTestApplication.java +++ b/src/com/itmill/toolkit/tests/book/BookTestApplication.java @@ -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 index 0000000000..966095a481 --- /dev/null +++ b/src/com/itmill/toolkit/tests/book/TableEditableBean.java @@ -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); + } +} diff --git a/src/com/itmill/toolkit/tests/book/TableExample3.java b/src/com/itmill/toolkit/tests/book/TableExample3.java index 43ce9f6790..cf20a8bf49 100644 --- a/src/com/itmill/toolkit/tests/book/TableExample3.java +++ b/src/com/itmill/toolkit/tests/book/TableExample3.java @@ -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); } }