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);
}
}
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 + "."));
+
}
});
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);
--- /dev/null
+/*
+@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);
+ }
+}
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
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);
}
}