[ 
https://issues.apache.org/jira/browse/PIVOT-718?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13040186#comment-13040186
 ] 

Chris Bartlett commented on PIVOT-718:
--------------------------------------

Before going any further I will try to clarify what appears to be a 
misunderstanding of the data structure that backs a TreeView.

org.apache.pivot.wtk.TreeView will display any kind of object as a 'leaf', but 
it will only display objects as 'branches' if they implement 
org.apache.pivot.collections.List.

The following 2 classes are provided with Pivot for use with TreeViews, but 
custom implementations can also be used.
org.apache.pivot.wtk.content.TreeNode (leaf)
org.apache.pivot.wtk.content.TreeBranch (will be considered a branch as it 
implements List<TreeNode>)

Note that the remove(T) and indexOf(T) methods mentioned in earlier comments 
are defined in org.apache.pivot.collections.Sequence<T> which TreeBranch 
implements via List<T>.


A typical TreeView data structure will consist of a single TreeBranch (or any 
other instance of an org.apache.pivot.collections.List) which in turn will 
contain 0 or more TreeNodes or TreeBranches.  Each List (TreeBranch) will be 
displayed as a 'branch', while other objects (TreeNode) will be displayed as 
'leaves'.  

In order to remove an item from a TreeView, you must determine which List 
(TreeBranch) it belongs to before calling the List#remove(T item) method.
The attached example code appears to expect any selected node to be removed by 
calling 
tree.getTreeData().remove(tree.getSelectedNode());
However this will only work if the selected node exists in the root List 
(TreeBranch) used to back the TreeView.


The following code snippet should work for any selected TreeNode or TreeBranch, 
but assumes that the TreeView will only ever contain TreeNode or TreeBranch 
objects (or subtypes)

// Get the selected node
TreeNode selectedNode = (TreeNode) treeView.getSelectedNode();
if (selectedNode != null) {
  // Get the parent List of the selected node
  org.apache.pivot.collections.List parent = selectedNode.getParent();
  if (parent != null) {
      // Remove the selected node from its parent
      parent.remove(selectedNode);
  }
}


> TreeViewSelectionListener not invoked
> -------------------------------------
>
>                 Key: PIVOT-718
>                 URL: https://issues.apache.org/jira/browse/PIVOT-718
>             Project: Pivot
>          Issue Type: Bug
>          Components: wtk
>    Affects Versions: 2.0
>         Environment: jkd1.6,linux
>            Reporter: laurent Gebus
>            Priority: Minor
>             Fix For: 2.1
>
>         Attachments: TreeSelect.bxml, TreeSelect.java
>
>
> I have a TreeView with a TreeViewSelectionListener, but the listener doesn't 
> get invoked when a item is removed from the tree.
> I do : tree.getTreeData().remove( tree.getSelectedNode());
> tree is refreshed : the removed node is no longer shown, no node is selected 
> but the listener is not invoked. 
> Looked at the code :
> in TreeView#itemsRemoved(), clearAndDecrementPaths() is invoked and returns 
> 0. But Listeners are only invoked when > 0 is returned. 
> see attached example comparing a TreeView to a ListView

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to