[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-smalltalk] How to use trees with OrderedCollections etc
From: |
Stefan Schmiedl |
Subject: |
Re: [Help-smalltalk] How to use trees with OrderedCollections etc |
Date: |
Tue, 10 Nov 2009 13:21:28 +0100 |
On Tue, 10 Nov 2009 08:46:10 +0200
Bèrto ëd Sèra <address@hidden> wrote:
> Hi all,
>
> one more stupid question. I've been building a UI mockup happily
> quoting the sample that reads
>
> tree := (Iliad.Tree new)
> item: Object;
> childrenBlock: [:class | class subclasses];
> contentsBlock: [:e :class | e text: class name]
I guess the main problem is that the above combination of Object and
#subclasses leads to a tree of objects implementing the message #subclasses,
while you have a collection (responding to #contents) and symbols (not
responding to #contents).
One way to get around this is to wrap your different objects into a thin
decorator providing a common interface, so that the following would work:
tree := (Iliad.Tree new)
item: myTreeViewableCollectionOfLocalizableSymbols
childrenBlock: [:item | item children ]
contentsBlock: [:e :item | e text: item displayString ]
I apologize for that looong name :-)
Object subclass: TreeNodeHolder [
| model getter |
class [
on: anObject getter: aBlock [
^ self basicNew model: anObject; getter: aBlock; yourself
]
]
children [ ^ getter value: model ]
]
treeModel := TreeNodeHolder
on: myTVCOLS
getter: [:coll | coll collect: [:elt |
TreeNodeHolder on: elt getter: [:sym |
OrderedCollection new ]
].
tree := (Iliad.Tree new)
item: treeModel
childrenBlock: [:item | item children ]
contentsBlock: [:e :item | e text: item displayString ]
Mind you, this snippet was written with dangerously low blood sugar levels,
so beware :-)
You can also avoid a lot of the uglyness if your application classes implement
a consistent interface for this purpose from the start.
HTH
s.