Spec TreeModel get selected items in order
Hi, Is there a way to retrive the selected nodes of a TreeModel in the same order they are shown in the widget ? For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect . Now, if I try to collect the selected items with t selectedItems collect:#contents they appear in the order I selected them, but I would like to get the order 1/2/3/4/5 thanks Nicolai
On Sun, Aug 07, 2016 at 04:55:05PM +0200, Nicolai Hess wrote:
Hi,
Is there a way to retrive the selected nodes of a TreeModel in the same order they are shown in the widget ?
For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect .
Now, if I try to collect the selected items with
t selectedItems collect:#contents they appear in the order I selected them, but I would like to get the order 1/2/3/4/5
I vaguely remember that I had this problem long time ago and what I ended up doing was to filter against the original collection. E.g. something like this t := TreeModel new. t beCheckList; autoMultiSelection: true. d := 1 to: 10. t roots: d. t openWithSpec. sel := (t selectedItems collect: #content). d select: [ :each | sel includes: each ] Also looking at the code it seems that TreeModel is really heavily tied to Morphic... so maybe not so simple to fix without rewrite. Peter
ListModel has that: #selectedItemsSorted, but looking at the tree model structure I guess the simplest way is to just traverse the whole structure until the selected nodes are found. Something like this should do the trick: -------------------------------------------------- TreeModel >> selectionSorted | ordered lookingFor search | ordered := OrderedCollection new. lookingFor := self selectedItems asIdentitySet. LookingFor ifEmpty: [ ^ #() ] search := [ :nodes | nodes do: [ :node | (lookingFor includes: node) ifTrue: [ ordered add: node. lookingFor remove: node. lookingFor isEmpty ifTrue: [ ^ ordered asArray ] ]. search value: node children value ] ]. search value: self roots. self error: 'should not happen' -------------------------------------- Best regards, Henrik From: Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org] On Behalf Of Nicolai Hess Sent: Sunday, August 7, 2016 4:55 PM To: Pharo Development List <pharo-dev@lists.pharo.org> Subject: [Pharo-dev] Spec TreeModel get selected items in order Hi, Is there a way to retrive the selected nodes of a TreeModel in the same order they are shown in the widget ? For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect . Now, if I try to collect the selected items with t selectedItems collect:#contents they appear in the order I selected them, but I would like to get the order 1/2/3/4/5 thanks Nicolai
2016-08-07 17:53 GMT+02:00 Henrik Nergaard <henrik.nergaard@uia.no>:
ListModel has that: #selectedItemsSorted, but looking at the tree model structure I guess the simplest way is to just traverse the whole structure until the selected nodes are found.
Something like this should do the trick:
--------------------------------------------------
TreeModel >> selectionSorted
| ordered lookingFor search |
ordered := OrderedCollection new.
lookingFor := self selectedItems asIdentitySet.
LookingFor ifEmpty: [ ^ #() ]
search := [ :nodes |
nodes do: [ :node |
(lookingFor includes: node) ifTrue: [
ordered add: node.
lookingFor remove: node.
lookingFor isEmpty ifTrue: [ ^ ordered asArray ]
].
search value: node children value
]
].
search value: self roots.
self error: 'should not happen'
--------------------------------------
Thanks Peter, Henrik, yes I was afraid there is no other way.
Best regards,
Henrik
*From:* Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org] *On Behalf Of *Nicolai Hess *Sent:* Sunday, August 7, 2016 4:55 PM *To:* Pharo Development List <pharo-dev@lists.pharo.org> *Subject:* [Pharo-dev] Spec TreeModel get selected items in order
Hi,
Is there a way to retrive the selected nodes of a TreeModel in the same order they are
shown in the widget ?
For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect .
Now, if I try to collect the selected items with
t selectedItems collect:#contents
they appear in the order I selected them, but I would like to get the order 1/2/3/4/5
thanks
Nicolai
2016-08-07 18:23 GMT+02:00 Nicolai Hess <nicolaihess@gmail.com>:
2016-08-07 17:53 GMT+02:00 Henrik Nergaard <henrik.nergaard@uia.no>:
ListModel has that: #selectedItemsSorted, but looking at the tree model structure I guess the simplest way is to just traverse the whole structure until the selected nodes are found.
Something like this should do the trick:
--------------------------------------------------
TreeModel >> selectionSorted
| ordered lookingFor search |
ordered := OrderedCollection new.
lookingFor := self selectedItems asIdentitySet.
LookingFor ifEmpty: [ ^ #() ]
search := [ :nodes |
nodes do: [ :node |
(lookingFor includes: node) ifTrue: [
ordered add: node.
lookingFor remove: node.
lookingFor isEmpty ifTrue: [ ^ ordered asArray ]
].
search value: node children value
]
].
search value: self roots.
self error: 'should not happen'
--------------------------------------
Thanks Peter, Henrik, yes I was afraid there is no other way.
I tried to fix this, but there is a test case for the ChangesBrowser that tests the selection. The problem is, that the test case selects an item by calling #selectedItem: and the behavior is different for when this is called by the view. The test case just selects the "change", whereas when this is called by the view, the selected item is TreeNodeModel (with #contents). And I can not fix #pickedChanges without breaking this test. Any Idea ?
Best regards,
Henrik
*From:* Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org] *On Behalf Of *Nicolai Hess *Sent:* Sunday, August 7, 2016 4:55 PM *To:* Pharo Development List <pharo-dev@lists.pharo.org> *Subject:* [Pharo-dev] Spec TreeModel get selected items in order
Hi,
Is there a way to retrive the selected nodes of a TreeModel in the same order they are
shown in the widget ?
For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect .
Now, if I try to collect the selected items with
t selectedItems collect:#contents
they appear in the order I selected them, but I would like to get the order 1/2/3/4/5
thanks
Nicolai
The test #testPickedChangesAfterSelect selects an item which is not in the browser, so I would consider that a bug. Adding the item to the browser before selecting it should be correct and make the test work. ChangesBrowserTest >> testPickedChangesAfterSelect | item | Item := RBAddClassChange. Item definition: âtestâ controller: nil. Browser changes: {item}. Self assert: â¦.. â¦. Best regards, Henrik From: Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org] On Behalf Of Nicolai Hess Sent: Sunday, August 7, 2016 7:46 PM To: Pharo Development List <pharo-dev@lists.pharo.org> Subject: Re: [Pharo-dev] Spec TreeModel get selected items in order 2016-08-07 18:23 GMT+02:00 Nicolai Hess <nicolaihess@gmail.com<mailto:nicolaihess@gmail.com>>: 2016-08-07 17:53 GMT+02:00 Henrik Nergaard <henrik.nergaard@uia.no<mailto:henrik.nergaard@uia.no>>: ListModel has that: #selectedItemsSorted, but looking at the tree model structure I guess the simplest way is to just traverse the whole structure until the selected nodes are found. Something like this should do the trick: -------------------------------------------------- TreeModel >> selectionSorted | ordered lookingFor search | ordered := OrderedCollection new. lookingFor := self selectedItems asIdentitySet. LookingFor ifEmpty: [ ^ #() ] search := [ :nodes | nodes do: [ :node | (lookingFor includes: node) ifTrue: [ ordered add: node. lookingFor remove: node. lookingFor isEmpty ifTrue: [ ^ ordered asArray ] ]. search value: node children value ] ]. search value: self roots. self error: 'should not happen' -------------------------------------- Thanks Peter, Henrik, yes I was afraid there is no other way. I tried to fix this, but there is a test case for the ChangesBrowser that tests the selection. The problem is, that the test case selects an item by calling #selectedItem: and the behavior is different for when this is called by the view. The test case just selects the "change", whereas when this is called by the view, the selected item is TreeNodeModel (with #contents). And I can not fix #pickedChanges without breaking this test. Any Idea ? Best regards, Henrik From: Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org<mailto:pharo-dev-bounces@lists.pharo.org>] On Behalf Of Nicolai Hess Sent: Sunday, August 7, 2016 4:55 PM To: Pharo Development List <pharo-dev@lists.pharo.org<mailto:pharo-dev@lists.pharo.org>> Subject: [Pharo-dev] Spec TreeModel get selected items in order Hi, Is there a way to retrive the selected nodes of a TreeModel in the same order they are shown in the widget ? For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect . Now, if I try to collect the selected items with t selectedItems collect:#contents they appear in the order I selected them, but I would like to get the order 1/2/3/4/5 thanks Nicolai
2016-08-07 20:06 GMT+02:00 Henrik Nergaard <henrik.nergaard@uia.no>:
The test #testPickedChangesAfterSelect selects an item which is not in the browser, so I would consider that a bug.
Adding the item to the browser before selecting it should be correct and make the test work.
ChangesBrowserTest >> testPickedChangesAfterSelect
| item |
Item := RBAddClassChange.
Item definition: âtestâ controller: nil.
Browser changes: {item}.
Self assert: â¦..
â¦.
Best regards,
Henrik
did you try it ? This is how I changed #pickedChanges pickedChanges | selectedChanges | selectedChanges := changesTree selectedItems collect: [ :item | item content ]. ^ selectedChanges ifNotEmpty: [ changes select: [ :change | selectedChanges includes: change ] ] The test does not work, even if I add the item to the changes browser.
*From:* Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org] *On Behalf Of *Nicolai Hess *Sent:* Sunday, August 7, 2016 7:46 PM *To:* Pharo Development List <pharo-dev@lists.pharo.org> *Subject:* Re: [Pharo-dev] Spec TreeModel get selected items in order
2016-08-07 18:23 GMT+02:00 Nicolai Hess <nicolaihess@gmail.com>:
2016-08-07 17:53 GMT+02:00 Henrik Nergaard <henrik.nergaard@uia.no>:
ListModel has that: #selectedItemsSorted, but looking at the tree model structure I guess the simplest way is to just traverse the whole structure until the selected nodes are found.
Something like this should do the trick:
--------------------------------------------------
TreeModel >> selectionSorted
| ordered lookingFor search |
ordered := OrderedCollection new.
lookingFor := self selectedItems asIdentitySet.
LookingFor ifEmpty: [ ^ #() ]
search := [ :nodes |
nodes do: [ :node |
(lookingFor includes: node) ifTrue: [
ordered add: node.
lookingFor remove: node.
lookingFor isEmpty ifTrue: [ ^ ordered asArray ]
].
search value: node children value
]
].
search value: self roots.
self error: 'should not happen'
--------------------------------------
Thanks Peter, Henrik,
yes I was afraid there is no other way.
I tried to fix this, but there is a test case for the ChangesBrowser that tests the selection.
The problem is, that the test case selects an item by calling #selectedItem: and the behavior is different for when this is called by the view.
The test case just selects the "change", whereas when this is called by the view, the selected item is TreeNodeModel (with #contents).
And I can not fix #pickedChanges without breaking this test.
Any Idea ?
Best regards,
Henrik
*From:* Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org] *On Behalf Of *Nicolai Hess *Sent:* Sunday, August 7, 2016 4:55 PM *To:* Pharo Development List <pharo-dev@lists.pharo.org> *Subject:* [Pharo-dev] Spec TreeModel get selected items in order
Hi,
Is there a way to retrive the selected nodes of a TreeModel in the same order they are
shown in the widget ?
For example, open this tree model, and select item 5/4/3/1 |t| Transcript clear. t := TreeModel new. t beCheckList ; autoMultiSelection: true. t roots:((1 to:10) collect:[:c | TreeNodeModel new content:c;hasContentToShow:true]). t openWithSpec. t inspect .
Now, if I try to collect the selected items with
t selectedItems collect:#contents
they appear in the order I selected them, but I would like to get the order 1/2/3/4/5
thanks
Nicolai
participants (3)
-
Henrik Nergaard -
Nicolai Hess -
Peter Uhnak