[Pharo-users] How to programatically select an item in a Spec TreeModel ?

Edouard KLEIN edouardklein at gmail.com
Fri Oct 23 11:17:40 EDT 2015


Hi !

I've been playing with Pharo again. In the application I'm building, I
need to load a file from disk, and depending on what is inside this
file, select an item in a tree that is displayed on the main window.

The SearchableTree subclass I use is copied at the end of the email. It
is nothing fancy, just creating the nodes from a fixed tree structure.

The method selectedItemFRomPathArray (e.g. #('a' 'a1')) is the one that
should be responsible for selecting the node, but for now it just
returns the array of TreeNodeModel that correspond to the strings in the
'PathArray' given in argument.

I tried the following in a PlayGround :

w := CRPJCategories2 new. w openWithSpec.
"Taken from packageRemotesManager without understanding it."
w tree selectedItem:(
	(w roots) first  "This line can also be :
	((w selectedItemFromPathArray:#('a')) last)"
	selected:true;
	takeHighlight;
	yourself).

Which works, but when I try to select something that is not a root node,
then it does not work, and I've been pulling my hair trying to
understand why.

w := CRPJCategories2 new. w openWithSpec.
"Taken from packageRemotesManager without understanding it."
w tree selectedItem:(
((w selectedItemFromPathArray:#('a' 'a1')) last) "<- DOES NOT WORK"
selected:true;
takeHighlight;
yourself).


Any pointer would be very much appreciated.

Thenks in advance.

Edouard.




-------------------------------------------------------
'From Pharo4.0 of 18 March 2013 [Latest update: #40623] on 23 October
2015 at 4:55:07.823126 pm'!
SearchableTree subclass: #CRPJCategories2
	instanceVariableNames: 'structure'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'CRPJ'!

!CRPJCategories2 methodsFor: 'as yet unclassified' stamp: 'EdouardKlein
10/23/2015 15:29'!
nodeFromTreeStructure: anArray
	^ (anArray collect:[:x| TreeNodeModel new
		content: (x at:1);
		children: [((x size = 1) ifTrue:[#()] ifFalse:[self
nodeFromTreeStructure:(x at:2)])];
		hasChildren:[x size > 1]]) asOrderedCollection.! !

!CRPJCategories2 methodsFor: 'as yet unclassified' stamp: 'EdouardKlein
10/23/2015 15:10'!
initialize
	|  |
	super initialize.
	structure := {
		{'a' . {
			{'a1'}.
			{'a2'}}}.
		{'b' . {
			{'b1' . {
				{'b1A'}.
				{'b1B'}}}.
			{'b2'}.
			{'b3' . {
				{'b3A'}.
				{'b3B'}}}}}.
		{'c' . {
			{'c1'}.
			{'c2'}.
			{'c3'}}}}.
	self roots:(self nodeFromTreeStructure: structure).! !

!CRPJCategories2 methodsFor: 'as yet unclassified' stamp: 'EdouardKlein
10/23/2015 16:54'!
selectedItemFromPathArray:aPathArray
	"a string description of the currently selected item"
	| recurse |
	recurse := [:path :nodes|
		|nextNode|
		nextNode := (nodes select:[:n| n content = path first]) first.
		(path size = 1) ifTrue:[{nextNode}] ifFalse:[
			{nextNode},(recurse value:(path allButFirst)value:(nextNode children
value))]].
	^(recurse value: aPathArray value:(self roots)).! !

!CRPJCategories2 methodsFor: 'as yet unclassified' stamp: 'EdouardKlein
10/23/2015 16:53'!
selectedItemAsPathArray
	"an array of the path to the currently selected item"
	| recurse |
	recurse := [:node| node ifNil:[{}]
				ifNotNil:[(recurse value:(node parentNode)),{node}]].
	^(recurse value:self selectedItem) collect:[:node| node content].! !




More information about the Pharo-users mailing list