voyent
Manipulating ace:tree on the server side  XML
Forum Index -> Components
Author Message
kdean

Joined: 12/May/2009 00:00:00
Messages: 37
Offline


I have a tree that displays correctly and I can easily navigate through the tree, select a node, and have the display refresh with data associated with the selected node.

I need the ability to add to the tree. So far, adding a node works fine:

// This gets the currently selected node.
MutableTreeNode parentTreeNode = getSelectedTreeNode();

// A blank tree node is a custom class that displays "<< New >>" in the tree.
BlankTreeNode childTreeNode = new BlankTreeNode(parentTreeNode);

// Add the blank tree node to the end of the children.
parentTreeNode.insert(childTreeNode, parentTreeNode.getChildCount());

The display refreshes and the new node is present in the tree.

I have two problems:

The first is that, if the node being added is the first child, the parent to which it is being added is collapsed. I see '+' indicating that child nodes are available, but I would prefer that the parent be expanded.

The second problem, and solving this would probably solve the first, is that after adding the node I want the new node to be the selected node. This provides additional visual feedback to the user that they are working on a new entry.

In my code I have full access to the Tree, the NodeDataModel, and the NodeStateMap. However, I can't find any method that allows me to expand to and select the new child node.

Any help would be much appreciated.
kdean

Joined: 12/May/2009 00:00:00
Messages: 37
Offline


The solution is in the NodeStateMap. In the code below, selectedTreeNode is the currently selected tree node (not necessarily the same as the parent), parentTreeNode is the parent to which the child is being added, and index is the index within the parent's children where the child is being added:

Code:
 	private void addChild(MutableTreeNode selectedTreeNode, MutableTreeNode parentTreeNode, int index) {
 		BlankTreeNode childTreeNode = new BlankTreeNode(parentTreeNode);
 		parentTreeNode.insert(childTreeNode, index);
 		
 		// Deselect currently selected tree node.
 		nodeStateMap.get(selectedTreeNode).setSelected(false);
 		
 		// Make sure that parent is expanded.
 		nodeStateMap.get(parentTreeNode).setExpanded(true);
 		
 		NodeState childTreeNodeState = new NodeState();
 		
 		childTreeNodeState.setExpansionEnabled(true);
 		childTreeNodeState.setSelectionEnabled(true);
 		childTreeNodeState.setSelected(true);
 		
 		nodeStateMap.put(childTreeNode, childTreeNodeState);
 	}
 
 
Forum Index -> Components
Go to:   
Powered by JForum 2.1.7ice © JForum Team