Contribution: ActiveTreeModel from TreeModel

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Contribution: ActiveTreeModel from TreeModel

Eric Taylor
Hello Forum!

Has anyone ever needed to explicitly manipulate the children of a node
in a tree?  TreeModel provides for the ability to add, move, and remove
nodes.  But when one adds a child node, the node is added to the _end_
of the collection of children.  Moving child nodes has the limitation of
allowing only a move from one parent to another, not from one position
to another within the same collection of children.


Reply | Threaded
Open this post in threaded view
|

Re: Contribution: ActiveTreeModel from TreeModel

Ian Bartholomew-21
Eric,

> Has anyone ever needed to explicitly manipulate the children of a node
> in a tree?

I have changed the collection used to hold the children (see
TreeNode>>newChildCollection) to be a SortedCollection with a SortBlock that
arranges the children into a specific order.   I subclassed TreeNode, mainly
because I needed to do some more processing on each node, but I would have
thought (without trying) it should be easy enough to do in a normal tree.

If a SortedCollection is no good then I would guess you would have to add
some code that rearranged to child collection every time a new child was
added.

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Contribution: ActiveTreeModel from TreeModel

Ian Bartholomew-21
In reply to this post by Eric Taylor
>              but I would have thought (without trying) it should be easy
> enough to do in a normal tree.

A quick play in a workspace (now that I've had my breakfast)

"create a tree model and add some children"
tm := TreeModel withRoots: #('root') searchPolicy: SearchPolicy equality.
tm
 add: 'abzde' asChildOf: 'root';
 add: 'abxde' asChildOf: 'root';
 add: 'abwde' asChildOf: 'root'.
tp := TreePresenter showOn: tm.
tp expand: 'root'.

"next bit only works when the tree is redrawn so collapse it first"
tp collapse: 'root'.

"replace the childres collection with a SortedCollection - sorted on the
third character for extra excitement!"
root := tm getNodeFor: 'root'.
root children: (root children asSortedCollection: [:a :b | a object third <=
b object third]).

"and expand it again"
tp expand: 'root'.

"now any new nodes are inserted in the correct position"
tm add: 'abyde' asChildOf: 'root'

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Contribution: ActiveTreeModel from TreeModel

Chris Uppal-3
In reply to this post by Eric Taylor
Eric,

> Has anyone ever needed to explicitly manipulate the children of a node
> in a tree?

I know that both Bill and I have needed the ability to move items up and down
in the list of children ('cos we've discussed how to do it ;-).  I, myself,
have not yet needed the ability to add children in other than the "default"
place.

<advertisement>By the way, for anyone doing this kind of thing, my own
ListTreeView works well as View, even if you don't need multiple columns, since
it doesn't react to #treeChanged: notification by closing the parent node[*].
It also copes correctly with changes to the ordering of root nodes.
Unfortunately LTV is not yet available for D6, but I know that OA did a port to
D6 (it was in the beta), perhaps they still have a copy and would be willing to
allow access to it.</advertisement>

    -- chris


([*] Unless #useSmartRefresh: is set to false -- which it is by default, for
compatibility with TreeView)