Selecting an item in a TreeTable

I try to select an item in a TreeTable (through api - not with my mouse)… I am using Vaadin 6.8.6

The task is to (automatically) select the parent item of the item I click. The UI is not refreshed, although finding the parent of an item and selecting it works.

I used the ItemClickListener in this example… How can find out if the item I clicked is selected or unselected?

Here is the complete code:

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.AbstractSelect.MultiSelectMode;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.VerticalLayout;

public class TreeGui extends CustomComponent {
	private static final long serialVersionUID = -602900588162916989L;
	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private TreeTable treeTable;

	private class Person {
		private String name;
		public Person(String name) {
			this.name = name;
		}
		public String getName() {
			return name;
		}
		@Override
		public String toString() {
			return name;
		}
	}

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public TreeGui() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		treeTable.setSelectable(true);
		treeTable.setMultiSelect(true);
		treeTable.setMultiSelectMode(MultiSelectMode.SIMPLE);
		treeTable.setNullSelectionAllowed(true);
		treeTable.addContainerProperty("Name", String.class, null);
		treeTable.setColumnHeader("Name", "Family");
		treeTable.addContainerProperty("Name", String.class, null);
		
		Person grandfather = new Person("Grandfather");
		Person father = new Person("Father");
		Person son = new Person("Son");
		Person grandmother = new Person("Grandmother");
		Person mother = new Person("Mother");
		Person daughter = new Person("Daughter");
		
		treeTable.addItem(new Object [] {grandfather.getName()}, grandfather);
		treeTable.addItem(new Object [] {father.getName()}, father);
		treeTable.addItem(new Object [] {son.getName()}, son);
		treeTable.addItem(new Object [] {grandmother.getName()}, grandmother);
		treeTable.addItem(new Object [] {mother.getName()}, mother);
		treeTable.addItem(new Object [] {daughter.getName()}, daughter);
		
		treeTable.setParent(son, father);
		treeTable.setParent(father, grandfather);
		treeTable.setParent(daughter, mother);
		treeTable.setParent(mother, grandmother);
		
		treeTable.setChildrenAllowed(son, false);
		treeTable.setChildrenAllowed(daughter, false);
		treeTable.setCollapsed(grandfather, false);
		treeTable.setCollapsed(father, false);
		treeTable.setCollapsed(grandmother, false);
		treeTable.setCollapsed(mother, false);
		
		treeTable.addListener(new ItemClickListener() {
			private static final long serialVersionUID = -3820290353547897436L;
			@Override
			public void itemClick(ItemClickEvent event) {
				if (event.getButton() == ItemClickEvent.BUTTON_LEFT) {
					Person person = (Person)event.getItemId();
					Person parent = (Person)treeTable.getParent(person);
					if (parent != null) {
						System.out.println(parent + " is parent of " + person);
						treeTable.select(parent);
					}
				}
			}
		});
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// treeTable
		treeTable = new TreeTable();
		treeTable.setImmediate(false);
		treeTable.setWidth("400px");
		treeTable.setHeight("300px");
		mainLayout.addComponent(treeTable);
		
		return mainLayout;
	}
}

the getValue() method will return the selected itemId, if any (a Person object in this case). Just check if that matches the provided itemId.

getValue() and getParent() deliver the correct values for the TreeTable and for the parent element.

As a workaround I found out that the setValue() method works when called outside of a treeTable listener. So I created another button (Submit changes) with a listener and called the treetable.setValue(object) method.

After that the TreeTable gui was updated and the corrent value was shown (clicked element plus all parent elements).

The select method method did not work…

If I remember correctly, an ItemClickListener is called before a possible ValueChangeListener and other value change processing.
I wonder if that might lead to some conflict where you set the value before it is reset by the value change mechanism in your use case.