1431
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
|
1390
|
14 |
Object subclass:#HierarchicalItem
|
|
15 |
instanceVariableNames:'parent children isExpanded'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
1430
|
18 |
category:'Views-Support'
|
1390
|
19 |
!
|
|
20 |
|
|
21 |
HierarchicalItem subclass:#Example
|
|
22 |
instanceVariableNames:'label icon'
|
|
23 |
classVariableNames:'PenguinIcon'
|
|
24 |
poolDictionaries:''
|
|
25 |
privateIn:HierarchicalItem
|
|
26 |
!
|
|
27 |
|
|
28 |
!HierarchicalItem class methodsFor:'documentation'!
|
|
29 |
|
1431
|
30 |
copyright
|
|
31 |
"
|
|
32 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
33 |
All Rights Reserved
|
|
34 |
|
|
35 |
This software is furnished under a license and may be used
|
|
36 |
only in accordance with the terms of that license and with the
|
|
37 |
inclusion of the above copyright notice. This software may not
|
|
38 |
be provided or otherwise made available to, or used by, any
|
|
39 |
other person. No title to or ownership of the software is
|
|
40 |
hereby transferred.
|
|
41 |
"
|
|
42 |
|
|
43 |
!
|
|
44 |
|
1390
|
45 |
documentation
|
|
46 |
"
|
|
47 |
Hierarchical Items are mostly like Models, but the list of
|
|
48 |
dependencies are kept by its HierarchicalList.
|
|
49 |
The class is used to build up hierarchical trees.
|
|
50 |
|
|
51 |
[Instance variables:]
|
|
52 |
parent <Item, List or nil> parent or my HierarchicalList.
|
|
53 |
children <Collection or nil> list of children
|
|
54 |
isExpanded <Boolean> indicates whether the item is
|
|
55 |
expanded or collapsed
|
|
56 |
|
|
57 |
[author:]
|
|
58 |
Claus Atzkern
|
|
59 |
|
|
60 |
[see also:]
|
|
61 |
HierarchicalList
|
|
62 |
HierarchicalListView
|
|
63 |
"
|
|
64 |
|
|
65 |
|
|
66 |
! !
|
|
67 |
|
|
68 |
!HierarchicalItem class methodsFor:'instance creation'!
|
|
69 |
|
|
70 |
new
|
|
71 |
^ (self basicNew) initialize
|
|
72 |
! !
|
|
73 |
|
|
74 |
!HierarchicalItem methodsFor:'accessing'!
|
|
75 |
|
|
76 |
level
|
|
77 |
"returns the level starting with 1
|
|
78 |
"
|
|
79 |
|parent
|
|
80 |
level "{ Class:SmallInteger }"
|
|
81 |
|
|
|
82 |
level := 0.
|
|
83 |
parent := self.
|
|
84 |
|
|
85 |
[(parent := parent parentOrModel) notNil] whileTrue:[
|
|
86 |
level := level + 1
|
|
87 |
].
|
|
88 |
^ level
|
|
89 |
|
|
90 |
!
|
|
91 |
|
|
92 |
parent
|
|
93 |
"returns the parent or nil
|
|
94 |
"
|
|
95 |
^ (parent notNil and:[parent isHierarchicalItem]) ifTrue:[parent]
|
|
96 |
ifFalse:[nil]
|
|
97 |
!
|
|
98 |
|
|
99 |
parent:aParent
|
|
100 |
"set the parent (or the model if the item is the root item)
|
|
101 |
"
|
|
102 |
parent := aParent
|
|
103 |
|
|
104 |
! !
|
|
105 |
|
|
106 |
!HierarchicalItem methodsFor:'accessing children'!
|
|
107 |
|
|
108 |
at:anIndex
|
|
109 |
"returns the child under an index or nil
|
|
110 |
"
|
|
111 |
^ self children notNil ifTrue:[children at:anIndex ifAbsent:nil]
|
|
112 |
ifFalse:[nil]
|
|
113 |
!
|
|
114 |
|
|
115 |
at:anIndex put:anItem
|
|
116 |
"replace a child by a new item
|
|
117 |
"
|
|
118 |
|oldItem itemExpanded size|
|
|
119 |
|
|
120 |
size := self children size.
|
|
121 |
|
|
122 |
(anIndex between:1 and:size) ifFalse:[
|
|
123 |
^ self subscriptBoundsError
|
|
124 |
].
|
|
125 |
|
|
126 |
anItem isNil ifTrue:[
|
|
127 |
^ self removeFromIndex:anIndex toIndex:anIndex.
|
|
128 |
].
|
|
129 |
|
|
130 |
oldItem := children at:anIndex.
|
|
131 |
oldItem collapse.
|
|
132 |
itemExpanded := anItem isExpanded.
|
|
133 |
anItem setExpanded:false.
|
|
134 |
children at:anIndex put:anItem.
|
|
135 |
self changed.
|
|
136 |
|
|
137 |
itemExpanded ifTrue:[
|
|
138 |
anItem expand
|
|
139 |
].
|
|
140 |
!
|
|
141 |
|
|
142 |
children:aListOfChildren
|
|
143 |
"set a new list of children
|
|
144 |
"
|
|
145 |
self removeAll.
|
|
146 |
^ self addAll:aListOfChildren beforeIndex:1
|
|
147 |
!
|
|
148 |
|
|
149 |
first
|
|
150 |
"returns the first child
|
|
151 |
"
|
|
152 |
^ self at:1
|
|
153 |
!
|
|
154 |
|
|
155 |
last
|
|
156 |
"returns the last child
|
|
157 |
"
|
|
158 |
^ self at:(self children size)
|
|
159 |
! !
|
|
160 |
|
|
161 |
!HierarchicalItem methodsFor:'accessing hierarchy'!
|
|
162 |
|
|
163 |
collapse
|
|
164 |
"hide children
|
|
165 |
"
|
|
166 |
|visChd model index|
|
|
167 |
|
|
168 |
self canCollapse ifTrue:[
|
|
169 |
isExpanded := false.
|
|
170 |
|
|
171 |
(index := self listIndex) notNil ifTrue:[
|
|
172 |
(visChd := children size) ~~ 0 ifTrue:[
|
|
173 |
children do:[:aChild|
|
|
174 |
visChd := visChd + aChild numberOfVisibleChildren
|
|
175 |
].
|
|
176 |
self model itemRemoveFromIndex:(index + 1)
|
|
177 |
toIndex:(index + visChd).
|
|
178 |
|
|
179 |
index ~~ 0 ifTrue:[
|
|
180 |
self hierarchyChanged
|
|
181 |
]
|
|
182 |
]
|
|
183 |
]
|
|
184 |
]
|
|
185 |
!
|
|
186 |
|
|
187 |
expand
|
|
188 |
"expand children
|
|
189 |
"
|
|
190 |
|index list|
|
|
191 |
|
|
192 |
self canExpand ifTrue:[
|
|
193 |
isExpanded := true.
|
|
194 |
|
|
195 |
(index := self listIndex) notNil ifTrue:[
|
|
196 |
(children notNil or:[self children notNil]) ifTrue:[
|
|
197 |
children notEmpty ifTrue:[
|
|
198 |
list := OrderedCollection new:64.
|
|
199 |
self addVisibleChildrenTo:list.
|
|
200 |
self model itemAddAll:list beforeIndex:(index + 1).
|
|
201 |
].
|
|
202 |
index ~~ 0 ifTrue:[
|
|
203 |
self hierarchyChanged
|
|
204 |
]
|
|
205 |
]
|
|
206 |
]
|
|
207 |
]
|
|
208 |
|
|
209 |
!
|
|
210 |
|
1409
|
211 |
makeVisible
|
|
212 |
"expand all my parents
|
|
213 |
"
|
|
214 |
(parent notNil and:[parent isHierarchicalItem]) ifTrue:[
|
|
215 |
parent expand.
|
|
216 |
parent makeVisible
|
|
217 |
].
|
|
218 |
!
|
|
219 |
|
1390
|
220 |
recursiveCollapse
|
|
221 |
"collapse children and sub-children
|
|
222 |
**** must be expanded
|
|
223 |
"
|
|
224 |
|visChd index|
|
|
225 |
|
|
226 |
self canCollapse ifTrue:[
|
|
227 |
(index := self listIndex) notNil ifTrue:[
|
|
228 |
(visChd := children size) ~~ 0 ifTrue:[
|
|
229 |
children do:[:aChild|
|
|
230 |
visChd := visChd + aChild numberOfVisibleChildren
|
|
231 |
]
|
|
232 |
].
|
|
233 |
self recursiveSetCollapsed.
|
|
234 |
|
|
235 |
visChd ~~ 0 ifTrue:[
|
|
236 |
self model itemRemoveFromIndex:(index + 1)
|
|
237 |
toIndex:(index + visChd)
|
|
238 |
].
|
|
239 |
index ~~ 0 ifTrue:[
|
|
240 |
self hierarchyChanged
|
|
241 |
]
|
|
242 |
] ifFalse:[
|
|
243 |
self recursiveSetCollapsed
|
|
244 |
]
|
|
245 |
]
|
|
246 |
|
|
247 |
!
|
|
248 |
|
|
249 |
recursiveExpand
|
|
250 |
"expand children and sub-children
|
|
251 |
**** must be collapsed
|
|
252 |
"
|
|
253 |
|model index list|
|
|
254 |
|
|
255 |
self canExpand ifTrue:[
|
|
256 |
isExpanded := true.
|
|
257 |
|
|
258 |
self children size ~~ 0 ifTrue:[
|
|
259 |
(index := self listIndex) isNil ifTrue:[
|
|
260 |
children do:[:aChild| aChild setExpanded:true ]
|
|
261 |
] ifFalse:[
|
|
262 |
list := OrderedCollection new:512.
|
|
263 |
self recursiveSetExpandedAndAddToList:list.
|
|
264 |
self model itemAddAll:list beforeIndex:(index + 1).
|
|
265 |
|
|
266 |
index ~~ 0 ifTrue:[
|
|
267 |
self hierarchyChanged
|
|
268 |
]
|
|
269 |
]
|
|
270 |
]
|
|
271 |
]
|
|
272 |
!
|
|
273 |
|
|
274 |
recursiveToggleExpand
|
|
275 |
"if the item is collapsed, the children and all sub-children
|
|
276 |
are expanded otherwise collapsed
|
|
277 |
"
|
|
278 |
isExpanded ifTrue:[self recursiveCollapse] ifFalse:[self recursiveExpand]
|
|
279 |
|
|
280 |
!
|
|
281 |
|
|
282 |
toggleExpand
|
|
283 |
"if the item is collapsed, the children are expanded
|
|
284 |
otherwise collapsed
|
|
285 |
"
|
|
286 |
isExpanded ifTrue:[self collapse] ifFalse:[self expand]
|
|
287 |
|
|
288 |
! !
|
|
289 |
|
|
290 |
!HierarchicalItem methodsFor:'accessing mvc'!
|
|
291 |
|
|
292 |
application
|
|
293 |
"returns the responsible application or nil
|
|
294 |
"
|
|
295 |
|model|
|
|
296 |
|
|
297 |
(model := self model) notNil ifTrue:[
|
|
298 |
^ model application
|
|
299 |
].
|
|
300 |
^ nil
|
|
301 |
!
|
|
302 |
|
|
303 |
applicationsDo:aOneArgBlock
|
|
304 |
"evaluate the block on each dependent application
|
|
305 |
"
|
|
306 |
|model|
|
|
307 |
|
|
308 |
(model := self model) notNil ifTrue:[
|
|
309 |
model applicationsDo:aOneArgBlock
|
|
310 |
]
|
|
311 |
|
|
312 |
!
|
|
313 |
|
|
314 |
model
|
|
315 |
"returns the hierachicalList model or nil
|
|
316 |
"
|
|
317 |
|p model|
|
|
318 |
|
|
319 |
model := self.
|
|
320 |
[(p := model parentOrModel) notNil] whileTrue:[model := p].
|
|
321 |
^ model isHierarchicalItem ifFalse:[model] ifTrue:[nil]
|
|
322 |
! !
|
|
323 |
|
|
324 |
!HierarchicalItem methodsFor:'adding & removing'!
|
|
325 |
|
|
326 |
add:aChildItem
|
|
327 |
"add a child at end
|
|
328 |
"
|
|
329 |
^ self add:aChildItem beforeIndex:(self children size + 1).
|
|
330 |
|
|
331 |
!
|
|
332 |
|
|
333 |
add:aChildItem after:aChild
|
|
334 |
"add an item after an existing item
|
|
335 |
"
|
|
336 |
|index|
|
|
337 |
|
|
338 |
(index := self listIndex) isNil ifTrue:[
|
|
339 |
self subscriptBoundsError
|
|
340 |
] ifFalse:[
|
|
341 |
self add:aChildItem beforeIndex:(index + 1)
|
|
342 |
].
|
|
343 |
^ aChildItem
|
|
344 |
!
|
|
345 |
|
|
346 |
add:aChildItem afterIndex:anIndex
|
|
347 |
"add an item after an index
|
|
348 |
"
|
|
349 |
^ self add:aChildItem beforeIndex:(anIndex + 1).
|
|
350 |
!
|
|
351 |
|
|
352 |
add:aChildItem before:aChild
|
|
353 |
"add an item before an existing item
|
|
354 |
"
|
|
355 |
|index|
|
|
356 |
|
|
357 |
(index := self identityIndexOf:aChild) ~~ 0 ifTrue:[
|
|
358 |
^ self add:aChildItem beforeIndex:index
|
|
359 |
].
|
|
360 |
self subscriptBoundsError
|
|
361 |
!
|
|
362 |
|
|
363 |
add:aChildItem beforeIndex:anIndex
|
|
364 |
"add an item before an index
|
|
365 |
"
|
|
366 |
aChildItem notNil ifTrue:[
|
|
367 |
self addAll:(Array with:aChildItem) beforeIndex:anIndex
|
|
368 |
].
|
|
369 |
^ aChildItem
|
|
370 |
!
|
|
371 |
|
1488
|
372 |
add:aChild sortBlock:aBlock
|
|
373 |
"add a child sorted
|
|
374 |
"
|
|
375 |
(aBlock notNil and:[children size ~~ 0]) ifTrue:[
|
|
376 |
children keysAndValuesDo:[:index :child|
|
|
377 |
(aBlock value:aChild value:child) ifTrue:[
|
|
378 |
^ self add:aChild beforeIndex:index
|
|
379 |
]
|
|
380 |
]
|
|
381 |
].
|
|
382 |
^ self add:aChild
|
|
383 |
!
|
|
384 |
|
1390
|
385 |
addAll:aList
|
|
386 |
"add children at the end
|
|
387 |
"
|
|
388 |
^ self addAll:aList beforeIndex:(self children size + 1)
|
|
389 |
!
|
|
390 |
|
|
391 |
addAll:aList beforeIndex:anIndex
|
|
392 |
"add children before an index
|
|
393 |
"
|
|
394 |
|coll model notify
|
|
395 |
index "{ Class:SmallInteger }"
|
|
396 |
size "{ Class:SmallInteger }"
|
|
397 |
|
|
|
398 |
|
|
399 |
(aList isNil or:[aList isEmpty]) ifTrue:[
|
|
400 |
^ aList
|
|
401 |
].
|
|
402 |
size := self children size.
|
|
403 |
|
|
404 |
anIndex == 1 ifTrue:[
|
|
405 |
notify := self
|
|
406 |
] ifFalse:[
|
|
407 |
anIndex > size ifTrue:[
|
|
408 |
anIndex > (1 + size) ifTrue:[
|
|
409 |
^ self subscriptBoundsError
|
|
410 |
].
|
|
411 |
notify := self at:size
|
|
412 |
] ifFalse:[
|
|
413 |
notify := nil
|
|
414 |
]
|
|
415 |
].
|
|
416 |
|
|
417 |
size == 0 ifTrue:[
|
|
418 |
children := OrderedCollection new
|
|
419 |
].
|
|
420 |
aList do:[:anItem| anItem parent:self ].
|
|
421 |
children addAll:aList beforeIndex:anIndex.
|
|
422 |
|
|
423 |
(model := self model) isNil ifTrue:[
|
|
424 |
^ aList
|
|
425 |
].
|
|
426 |
|
|
427 |
isExpanded ifFalse:[
|
|
428 |
notify notNil ifTrue:[
|
|
429 |
notify changed
|
|
430 |
].
|
|
431 |
^ aList
|
|
432 |
].
|
|
433 |
(index := self listIndex) isNil ifTrue:[
|
|
434 |
^ aList
|
|
435 |
].
|
|
436 |
|
|
437 |
children from:1 to:(anIndex - 1) do:[:anItem|
|
|
438 |
index := 1 + index + anItem numberOfVisibleChildren
|
|
439 |
].
|
|
440 |
coll := OrderedCollection new.
|
|
441 |
|
|
442 |
aList do:[:anItem|
|
|
443 |
coll add:anItem.
|
|
444 |
anItem addVisibleChildrenTo:coll.
|
|
445 |
].
|
|
446 |
model itemAddAll:coll beforeIndex:(index + 1).
|
|
447 |
|
|
448 |
notify notNil ifTrue:[
|
|
449 |
notify changed
|
|
450 |
].
|
|
451 |
^ aList
|
|
452 |
!
|
|
453 |
|
1489
|
454 |
addAll:aList sortBlock:aBlock
|
|
455 |
"add children sorted
|
|
456 |
"
|
|
457 |
aList notNil ifTrue:[
|
|
458 |
aBlock isNil ifTrue:[
|
|
459 |
^ self addAll:aList
|
|
460 |
].
|
|
461 |
aList do:[:el|self add:el sortBlock:aBlock]
|
|
462 |
].
|
|
463 |
^ aList
|
|
464 |
!
|
|
465 |
|
1390
|
466 |
addAllFirst:aCollectionOfItems
|
|
467 |
"add children at the beginning
|
|
468 |
"
|
|
469 |
^ self addAll:aCollectionOfItems beforeIndex:1
|
|
470 |
!
|
|
471 |
|
|
472 |
addAllLast:aCollectionOfItems
|
|
473 |
"add children at the end
|
|
474 |
"
|
|
475 |
^ self addAll:aCollectionOfItems
|
|
476 |
!
|
|
477 |
|
|
478 |
addFirst:aChildItem
|
|
479 |
"add a child at begin
|
|
480 |
"
|
|
481 |
^ self add:aChildItem beforeIndex:1.
|
|
482 |
|
|
483 |
!
|
|
484 |
|
|
485 |
addLast:anItem
|
|
486 |
"add a child at end
|
|
487 |
"
|
|
488 |
^ self add:anItem
|
|
489 |
!
|
|
490 |
|
|
491 |
remove:aChild
|
|
492 |
"remove a child
|
|
493 |
"
|
|
494 |
self removeIndex:(self identityIndexOf:aChild)
|
|
495 |
|
|
496 |
!
|
|
497 |
|
|
498 |
removeAll
|
|
499 |
"remove all children
|
|
500 |
"
|
|
501 |
|size|
|
|
502 |
|
|
503 |
(size := self children size) ~~ 0 ifTrue:[
|
|
504 |
self removeFromIndex:1 toIndex:size
|
|
505 |
]
|
|
506 |
!
|
|
507 |
|
1489
|
508 |
removeAll:aList
|
|
509 |
"remove all children in the collection
|
|
510 |
"
|
|
511 |
|index|
|
|
512 |
|
|
513 |
aList size ~~ 0 ifTrue:[
|
|
514 |
aList do:[:el|
|
|
515 |
index := self identityIndexOf:el.
|
|
516 |
index ~~ 0 ifTrue:[
|
|
517 |
self removeIndex:index
|
|
518 |
]
|
|
519 |
]
|
|
520 |
]
|
|
521 |
!
|
|
522 |
|
1390
|
523 |
removeFromIndex:startIndex toIndex:stopIndex
|
|
524 |
"remove the children from startIndex up to and including
|
|
525 |
the child under stopIndex.
|
|
526 |
"
|
|
527 |
|model notify
|
|
528 |
index "{ Class:SmallInteger }"
|
|
529 |
start "{ Class:SmallInteger }"
|
|
530 |
stop "{ Class:SmallInteger }"
|
|
531 |
size "{ Class:SmallInteger }"
|
|
532 |
|
|
|
533 |
size := self children size.
|
|
534 |
stop := stopIndex.
|
|
535 |
start := startIndex.
|
|
536 |
|
|
537 |
(stop <= size and:[start between:1 and:stop]) ifFalse:[
|
|
538 |
^ self subscriptBoundsError
|
|
539 |
].
|
|
540 |
start == 1 ifTrue:[
|
|
541 |
notify := self
|
|
542 |
] ifFalse:[
|
|
543 |
stop == size ifTrue:[
|
|
544 |
notify := self at:(start - 1)
|
|
545 |
] ifFalse:[
|
|
546 |
notify := nil
|
|
547 |
]
|
|
548 |
].
|
|
549 |
|
|
550 |
(model := self model) notNil ifTrue:[
|
|
551 |
index := model identityIndexOf:(children at:start).
|
|
552 |
size := stop - start + 1.
|
|
553 |
] ifFalse:[
|
|
554 |
index := 0
|
|
555 |
].
|
|
556 |
children from:start to:stop do:[:aChild|
|
|
557 |
index ~~ 0 ifTrue:[
|
|
558 |
size := size + aChild numberOfVisibleChildren
|
|
559 |
].
|
|
560 |
aChild parent:nil
|
|
561 |
].
|
|
562 |
children removeFromIndex:start toIndex:stop.
|
|
563 |
|
|
564 |
index ~~ 0 ifTrue:[
|
|
565 |
model itemRemoveFromIndex:index toIndex:(index + size - 1)
|
|
566 |
].
|
|
567 |
notify notNil ifTrue:[
|
|
568 |
notify changed
|
|
569 |
].
|
|
570 |
!
|
|
571 |
|
|
572 |
removeIndex:anIndex
|
|
573 |
"remove the child at an index
|
|
574 |
"
|
|
575 |
anIndex > 0 ifTrue:[
|
|
576 |
self removeFromIndex:anIndex toIndex:anIndex
|
|
577 |
]
|
|
578 |
! !
|
|
579 |
|
|
580 |
!HierarchicalItem methodsFor:'change & update'!
|
|
581 |
|
|
582 |
changed
|
|
583 |
"the item changed; raise change notification
|
|
584 |
"
|
|
585 |
self changed:nil with:nil
|
|
586 |
!
|
|
587 |
|
|
588 |
changed:aParameter
|
|
589 |
"the item changed; raise change notification
|
|
590 |
"
|
|
591 |
self changed:aParameter with:nil
|
|
592 |
|
|
593 |
!
|
|
594 |
|
|
595 |
changed:aParameter with:anArgument
|
|
596 |
"the item changed; raise change notification
|
|
597 |
"
|
|
598 |
|model|
|
|
599 |
|
|
600 |
model := self model.
|
|
601 |
|
|
602 |
model notNil ifTrue:[
|
|
603 |
model itemChanged:aParameter with:anArgument from:self
|
|
604 |
].
|
|
605 |
super changed:aParameter with:anArgument
|
|
606 |
!
|
|
607 |
|
|
608 |
hierarchyChanged
|
|
609 |
"hierarchy changed; optimize redrawing
|
|
610 |
"
|
|
611 |
self changed:#hierarchy with:nil
|
|
612 |
!
|
|
613 |
|
|
614 |
iconChanged
|
|
615 |
"icon changed; optimize redrawing
|
|
616 |
"
|
|
617 |
self changed:#icon with:nil
|
|
618 |
! !
|
|
619 |
|
|
620 |
!HierarchicalItem methodsFor:'enumerating'!
|
|
621 |
|
|
622 |
do:aOneArgBlock
|
|
623 |
"evaluate a block on each child
|
|
624 |
"
|
|
625 |
self children notNil ifTrue:[ children do:aOneArgBlock ]
|
|
626 |
|
|
627 |
!
|
|
628 |
|
|
629 |
from:startIndex do:aOneArgBlock
|
|
630 |
"evaluate a block on each child starting with the
|
|
631 |
child at startIndex to the end.
|
|
632 |
"
|
|
633 |
self from:startIndex to:self size do:aOneArgBlock
|
|
634 |
!
|
|
635 |
|
|
636 |
from:startIndex to:endIndex do:aOneArgBlock
|
|
637 |
"evaluate a block on each child starting with the
|
|
638 |
child at startIndex to the endIndex.
|
|
639 |
"
|
|
640 |
self children notNil ifTrue:[
|
|
641 |
children from:startIndex to:endIndex do:aOneArgBlock
|
|
642 |
]
|
|
643 |
|
|
644 |
!
|
|
645 |
|
|
646 |
from:startIndex to:endIndex reverseDo:aOneArgBlock
|
|
647 |
"evaluate a block on each child starting with the
|
|
648 |
child at endIndex to the startIndex.
|
|
649 |
"
|
|
650 |
self children notNil ifTrue:[
|
|
651 |
children from:startIndex to:endIndex reverseDo:aOneArgBlock
|
|
652 |
]
|
|
653 |
|
|
654 |
!
|
|
655 |
|
|
656 |
recursiveDo:aOneArgBlock
|
|
657 |
"evaluate a block on each item and all the sub-items
|
|
658 |
"
|
|
659 |
self do:[:aChild|
|
|
660 |
aOneArgBlock value:aChild.
|
|
661 |
aChild recursiveDo:aOneArgBlock
|
|
662 |
].
|
|
663 |
|
|
664 |
!
|
|
665 |
|
|
666 |
reverseDo:aOneArgBlock
|
|
667 |
"evaluate a block on each child
|
|
668 |
procesing children in reverse direction
|
|
669 |
"
|
|
670 |
self children notNil ifTrue:[ children reverseDo:aOneArgBlock ]
|
|
671 |
|
|
672 |
! !
|
|
673 |
|
|
674 |
!HierarchicalItem methodsFor:'initialization'!
|
|
675 |
|
|
676 |
initialize
|
|
677 |
isExpanded := false
|
|
678 |
! !
|
|
679 |
|
|
680 |
!HierarchicalItem methodsFor:'private'!
|
|
681 |
|
|
682 |
addVisibleChildrenTo:aList
|
|
683 |
"add all visible children and sub-children to the list
|
|
684 |
"
|
|
685 |
isExpanded ifTrue:[
|
|
686 |
(children notNil or:[self children notNil]) ifTrue:[
|
|
687 |
children do:[:aChild|
|
|
688 |
aList add:aChild.
|
|
689 |
aChild addVisibleChildrenTo:aList
|
|
690 |
]
|
|
691 |
]
|
|
692 |
]
|
|
693 |
|
|
694 |
|
|
695 |
|
|
696 |
|
|
697 |
|
|
698 |
!
|
|
699 |
|
|
700 |
listIndex
|
|
701 |
"returns the visible index or nil; for a none visible root
|
|
702 |
0 is returned
|
|
703 |
"
|
|
704 |
|index model|
|
|
705 |
|
|
706 |
(model := self model) notNil ifTrue:[
|
|
707 |
index := model identityIndexOf:self.
|
|
708 |
|
|
709 |
(index ~~ 0 or:[parent == model]) ifTrue:[
|
|
710 |
^ index
|
|
711 |
]
|
|
712 |
].
|
|
713 |
^ nil
|
|
714 |
!
|
|
715 |
|
|
716 |
numberOfVisibleChildren
|
|
717 |
"returns number of all visible children including subchildren
|
|
718 |
"
|
|
719 |
|size "{ Class: SmallInteger }"
|
|
720 |
|
|
|
721 |
isExpanded ifTrue:[
|
|
722 |
(children notNil or:[self children notNil]) ifTrue:[
|
|
723 |
(size := children size) ~~ 0 ifTrue:[
|
|
724 |
children do:[:el| size := size + el numberOfVisibleChildren ].
|
|
725 |
^ size
|
|
726 |
]
|
|
727 |
]
|
|
728 |
].
|
|
729 |
^ 0
|
|
730 |
|
|
731 |
|
|
732 |
|
|
733 |
|
|
734 |
!
|
|
735 |
|
|
736 |
parentOrModel
|
|
737 |
"returns the parent without checking for item or model
|
|
738 |
"
|
|
739 |
^ parent
|
|
740 |
!
|
|
741 |
|
|
742 |
setExpanded:aBoolean
|
|
743 |
"set expanded flag without any computation or notification
|
|
744 |
"
|
|
745 |
isExpanded := aBoolean
|
|
746 |
! !
|
|
747 |
|
|
748 |
!HierarchicalItem methodsFor:'private hierarchy'!
|
|
749 |
|
|
750 |
recursiveSetCollapsed
|
|
751 |
"collapse all children and sub-children without notifications
|
|
752 |
"
|
|
753 |
isExpanded := false.
|
|
754 |
|
|
755 |
children notNil ifTrue:[
|
|
756 |
children do:[:aChild| aChild recursiveSetCollapsed ]
|
|
757 |
]
|
|
758 |
!
|
|
759 |
|
|
760 |
recursiveSetExpandedAndAddToList:aList
|
|
761 |
"expand all children and sub-children without notifications;
|
|
762 |
add children to list
|
|
763 |
"
|
|
764 |
isExpanded := true.
|
|
765 |
|
|
766 |
(children notNil or:[self children notNil]) ifTrue:[
|
|
767 |
children do:[:aChild|
|
|
768 |
aList add:aChild.
|
|
769 |
aChild recursiveSetExpandedAndAddToList:aList.
|
|
770 |
]
|
|
771 |
]
|
|
772 |
|
|
773 |
! !
|
|
774 |
|
|
775 |
!HierarchicalItem methodsFor:'protocol accessing'!
|
|
776 |
|
|
777 |
children
|
|
778 |
"returns list of children
|
|
779 |
*** to optimize:redefine by subClass
|
|
780 |
"
|
|
781 |
|model|
|
|
782 |
|
|
783 |
children isNil ifTrue:[
|
|
784 |
(model := self model) notNil ifTrue:[
|
1486
|
785 |
children := model childrenFor:self
|
1390
|
786 |
].
|
|
787 |
].
|
|
788 |
^ children
|
|
789 |
!
|
|
790 |
|
|
791 |
icon
|
|
792 |
"returns the icon or nil;
|
|
793 |
*** to optimize:redefine by subClass
|
|
794 |
"
|
|
795 |
|model|
|
|
796 |
|
|
797 |
(model := self model) notNil ifTrue:[
|
|
798 |
^ model iconFor:self
|
|
799 |
].
|
|
800 |
^ nil
|
|
801 |
!
|
|
802 |
|
|
803 |
label
|
|
804 |
"returns the label displayed on aGC;
|
|
805 |
*** to optimize:redefine by subClass
|
|
806 |
"
|
|
807 |
|model|
|
|
808 |
|
|
809 |
(model := self model) notNil ifTrue:[
|
|
810 |
^ model labelFor:self
|
|
811 |
].
|
|
812 |
^ nil
|
|
813 |
|
|
814 |
!
|
|
815 |
|
|
816 |
middleButtonMenu
|
|
817 |
"returns the middleButtonMenu or nil if no menu is defined
|
|
818 |
"
|
|
819 |
|model|
|
|
820 |
|
|
821 |
(model := self model) notNil ifTrue:[
|
|
822 |
^ model middleButtonMenuFor:self
|
|
823 |
].
|
|
824 |
^ nil
|
|
825 |
! !
|
|
826 |
|
|
827 |
!HierarchicalItem methodsFor:'protocol displaying'!
|
|
828 |
|
1398
|
829 |
displayOn:aGC x:xL y:yT toX:xR y:yB
|
1390
|
830 |
"draw the receiver in the graphicsContext, aGC.
|
|
831 |
"
|
|
832 |
|label
|
1398
|
833 |
yF "{ Class:SmallInteger }"
|
|
834 |
x0 "{ Class:SmallInteger }"
|
1390
|
835 |
y0 "{ Class:SmallInteger }"
|
1398
|
836 |
ascent "{ Class:SmallInteger }"
|
|
837 |
h "{ Class:SmallInteger }"
|
1390
|
838 |
|
|
1398
|
839 |
(label := self label) isNil ifTrue:[
|
|
840 |
^ self
|
|
841 |
].
|
|
842 |
h := yB - yT.
|
|
843 |
|
|
844 |
(label isSequenceable and:[label isString not]) ifFalse:[
|
|
845 |
y0 := yT - ((label heightOn:aGC) + 1 - h // 2).
|
1390
|
846 |
|
1398
|
847 |
label isImageOrForm ifTrue:[
|
|
848 |
label displayOn:aGC x:xL y:y0
|
1390
|
849 |
] ifFalse:[
|
1398
|
850 |
label displayOn:aGC x:xL y:(y0 + aGC font ascent)
|
1390
|
851 |
].
|
1398
|
852 |
^ self
|
|
853 |
].
|
|
854 |
|
|
855 |
x0 := xL.
|
|
856 |
ascent := aGC font ascent.
|
|
857 |
|
|
858 |
label do:[:el|
|
|
859 |
el notNil ifTrue:[
|
|
860 |
y0 := yT - ((el heightOn:aGC) + 1 - h // 2).
|
|
861 |
el isImageOrForm ifFalse:[
|
|
862 |
y0 := y0 + ascent
|
|
863 |
].
|
|
864 |
el displayOn:aGC x:x0 y:y0.
|
|
865 |
x0 := x0 + 5 + (el widthOn:aGC).
|
|
866 |
|
|
867 |
x0 < xR ifFalse:[
|
|
868 |
^ self
|
|
869 |
]
|
|
870 |
]
|
1390
|
871 |
]
|
|
872 |
!
|
|
873 |
|
|
874 |
heightOn:aGC
|
|
875 |
"return the width of the receiver, if it is to be displayed on aGC
|
|
876 |
"
|
1398
|
877 |
|label h "{ Class:SmallInteger }"|
|
1390
|
878 |
|
1398
|
879 |
(label := self label) isNil ifTrue:[
|
|
880 |
^ 0
|
|
881 |
].
|
|
882 |
(label isString or:[label isSequenceable not]) ifTrue:[
|
|
883 |
^ label heightOn:aGC
|
|
884 |
].
|
|
885 |
h := 0.
|
|
886 |
label do:[:el| el notNil ifTrue:[h := (el heightOn:aGC) max:h]].
|
|
887 |
^ h
|
1390
|
888 |
|
|
889 |
!
|
|
890 |
|
|
891 |
widthOn:aGC
|
|
892 |
"return the width of the receiver, if it is to be displayed on aGC
|
|
893 |
"
|
1398
|
894 |
|label w "{ Class:SmallInteger }"|
|
1390
|
895 |
|
1398
|
896 |
(label := self label) isNil ifTrue:[
|
|
897 |
^ 0
|
|
898 |
].
|
|
899 |
(label isString or:[label isSequenceable not]) ifTrue:[
|
|
900 |
^ label widthOn:aGC
|
|
901 |
].
|
1390
|
902 |
|
1398
|
903 |
w := -5.
|
|
904 |
label do:[:el|el notNil ifTrue:[w := w + 5 + (el widthOn:aGC)]].
|
|
905 |
^ w
|
1390
|
906 |
! !
|
|
907 |
|
|
908 |
!HierarchicalItem methodsFor:'protocol monitoring'!
|
|
909 |
|
|
910 |
monitoringCycle
|
|
911 |
"called all 'n' seconds by the model, if the monitoring
|
|
912 |
cycle is enabled. The item can perform some checks, ..
|
|
913 |
**** can be redefined by subclass to perform some actions
|
|
914 |
"
|
|
915 |
! !
|
|
916 |
|
|
917 |
!HierarchicalItem methodsFor:'protocol queries'!
|
|
918 |
|
|
919 |
hasChildren
|
|
920 |
"checks whether the item has a list of children; the list must not
|
|
921 |
be loaded yet( ex. FileDirectory ).
|
|
922 |
*** to optimize:redefine by subClass
|
|
923 |
"
|
|
924 |
^ children notNil ifTrue:[children notEmpty]
|
|
925 |
ifFalse:[self children size ~~ 0]
|
|
926 |
!
|
|
927 |
|
|
928 |
string
|
|
929 |
"access the printable string used for steping through a list
|
|
930 |
searching for an entry starting with a character.
|
|
931 |
*** to optimize:redefine by subClass
|
|
932 |
"
|
1398
|
933 |
|label|
|
|
934 |
|
|
935 |
(label := self label) notNil ifTrue:[
|
|
936 |
label isString ifTrue:[ ^ label string ].
|
|
937 |
label isImageOrForm ifTrue:[ ^ nil ].
|
|
938 |
|
|
939 |
label isSequenceable ifFalse:[
|
|
940 |
^ label perform:#string ifNotUnderstood:nil
|
|
941 |
].
|
|
942 |
|
|
943 |
label do:[:el||s|
|
|
944 |
(el notNil and:[el isImageOrForm not]) ifTrue:[
|
|
945 |
s := el perform:#string ifNotUnderstood:nil.
|
|
946 |
s notNil ifTrue:[^ s].
|
|
947 |
]
|
|
948 |
]
|
|
949 |
].
|
|
950 |
^ nil
|
|
951 |
|
1390
|
952 |
|
|
953 |
! !
|
|
954 |
|
|
955 |
!HierarchicalItem methodsFor:'queries'!
|
|
956 |
|
|
957 |
canCollapse
|
|
958 |
"called before collapsing the item
|
|
959 |
"
|
|
960 |
^ isExpanded
|
|
961 |
!
|
|
962 |
|
|
963 |
canExpand
|
|
964 |
"called before expanding the item
|
|
965 |
"
|
|
966 |
^ isExpanded not and:[self hasChildren]
|
|
967 |
!
|
|
968 |
|
|
969 |
isExpanded
|
|
970 |
"return true if item is expanded
|
|
971 |
"
|
|
972 |
^ isExpanded
|
|
973 |
!
|
|
974 |
|
|
975 |
isHierarchicalItem
|
|
976 |
"used to decide if the parent is a hierarchical item
|
|
977 |
or the model
|
|
978 |
"
|
|
979 |
^ true
|
|
980 |
!
|
|
981 |
|
|
982 |
size
|
|
983 |
"return the number of children
|
|
984 |
"
|
|
985 |
^ children notNil ifTrue:[children size]
|
|
986 |
ifFalse:[self children size]
|
|
987 |
! !
|
|
988 |
|
|
989 |
!HierarchicalItem methodsFor:'searching'!
|
|
990 |
|
|
991 |
detect:aOneArgBlock
|
|
992 |
"find the first child, for which evaluation of the block returns
|
|
993 |
true; if none does so, report an error
|
|
994 |
"
|
|
995 |
^ self detect:aOneArgBlock ifNone:[self errorNotFound]
|
|
996 |
|
|
997 |
!
|
|
998 |
|
|
999 |
detect:aOneArgBlock ifNone:anExceptionBlock
|
|
1000 |
"find the first child, for which evaluation of the block returns
|
|
1001 |
true; if none does so, return the evaluation of anExceptionBlock
|
|
1002 |
"
|
|
1003 |
self children notNil ifTrue:[
|
|
1004 |
^ children detect:aOneArgBlock ifNone:anExceptionBlock
|
|
1005 |
].
|
|
1006 |
^ anExceptionBlock value
|
|
1007 |
|
|
1008 |
|
|
1009 |
!
|
|
1010 |
|
|
1011 |
identityIndexOf:aChild
|
|
1012 |
"return the index of aChild or 0 if not found. Compare using ==
|
|
1013 |
"
|
|
1014 |
self children notNil ifTrue:[
|
|
1015 |
^ children identityIndexOf:aChild
|
|
1016 |
].
|
|
1017 |
^ 0
|
|
1018 |
!
|
|
1019 |
|
|
1020 |
identityIndexOf:aChild startingAt:startIndex
|
|
1021 |
"return the index of aChild, starting search at startIndex.
|
|
1022 |
Compare using ==; return 0 if not found
|
|
1023 |
"
|
|
1024 |
self children notNil ifTrue:[
|
|
1025 |
^ children identityIndexOf:aChild startingAt:startIndex
|
|
1026 |
].
|
|
1027 |
^ 0
|
1489
|
1028 |
!
|
|
1029 |
|
|
1030 |
recursiveDetect:aOneArgBlock
|
|
1031 |
"recursive find the first child, for which evaluation
|
|
1032 |
of the block returns true; if none nil is returned
|
|
1033 |
"
|
|
1034 |
|child|
|
|
1035 |
|
|
1036 |
self children notNil ifTrue:[
|
|
1037 |
children do:[:aChild|
|
|
1038 |
(aOneArgBlock value:aChild) ifTrue:[
|
|
1039 |
^ aChild
|
|
1040 |
].
|
|
1041 |
|
|
1042 |
(child := aChild recursiveDetect:aOneArgBlock) notNil ifTrue:[
|
|
1043 |
^ child
|
|
1044 |
]
|
|
1045 |
]
|
|
1046 |
].
|
|
1047 |
^ nil
|
1390
|
1048 |
! !
|
|
1049 |
|
|
1050 |
!HierarchicalItem::Example class methodsFor:'instance creation'!
|
|
1051 |
|
|
1052 |
labeled:aLabel
|
|
1053 |
^ self new setLabel:aLabel
|
|
1054 |
|
|
1055 |
|
|
1056 |
!
|
|
1057 |
|
|
1058 |
labeled:aLabel icon:anIcon
|
|
1059 |
^ self new setLabel:aLabel icon:anIcon
|
|
1060 |
|
|
1061 |
|
|
1062 |
! !
|
|
1063 |
|
|
1064 |
!HierarchicalItem::Example class methodsFor:'resources'!
|
|
1065 |
|
|
1066 |
iconForLevel:aLevel
|
|
1067 |
"returns an icon
|
|
1068 |
"
|
|
1069 |
aLevel == 2 ifTrue:[ ^ ResourceSelectionBrowser iconPrivateClass ].
|
|
1070 |
aLevel == 3 ifTrue:[ ^ ResourceSelectionBrowser iconClass ].
|
|
1071 |
aLevel == 4 ifTrue:[ ^ ResourceSelectionBrowser iconCategory ].
|
|
1072 |
|
|
1073 |
^ nil
|
|
1074 |
|
|
1075 |
!
|
|
1076 |
|
|
1077 |
penguinIcon
|
|
1078 |
PenguinIcon isNil ifTrue:[
|
|
1079 |
PenguinIcon := Image fromFile:'bitmaps/xpmBitmaps/misc_logos/linux_penguin.xpm'.
|
|
1080 |
].
|
|
1081 |
^ PenguinIcon
|
|
1082 |
! !
|
|
1083 |
|
|
1084 |
!HierarchicalItem::Example methodsFor:'accessing'!
|
|
1085 |
|
|
1086 |
children
|
|
1087 |
|lvl lbl txt image img icon|
|
|
1088 |
|
|
1089 |
children notNil ifTrue:[
|
|
1090 |
^ children
|
|
1091 |
].
|
|
1092 |
|
|
1093 |
(lvl := self level) == 5 ifTrue:[
|
|
1094 |
children := #().
|
|
1095 |
^ children
|
|
1096 |
].
|
|
1097 |
icon := self class iconForLevel:(lvl + 1).
|
|
1098 |
children := OrderedCollection new.
|
|
1099 |
|
|
1100 |
lvl < 4 ifTrue:[
|
|
1101 |
txt := (lvl + 1) printString, ' ['.
|
|
1102 |
img := NewLauncher saveImageIcon.
|
|
1103 |
|
|
1104 |
1 to:5 do:[:i|
|
|
1105 |
(i == 2 or:[i == 3]) ifTrue:[
|
|
1106 |
lbl := img
|
|
1107 |
] ifFalse:[
|
|
1108 |
i == 4 ifTrue:[
|
|
1109 |
lbl := self class penguinIcon
|
|
1110 |
] ifFalse:[
|
|
1111 |
lbl := txt, (i printString), ']'
|
|
1112 |
]
|
|
1113 |
].
|
|
1114 |
children add:(self class labeled:lbl icon:icon)
|
|
1115 |
].
|
|
1116 |
] ifFalse:[
|
|
1117 |
image := ResourceSelectionBrowser iconPrivateClass.
|
|
1118 |
txt := LabelAndIcon icon:image string:'Text'.
|
|
1119 |
img := Icon copyIcon.
|
|
1120 |
|
|
1121 |
1 to:5 do:[:i|
|
|
1122 |
lbl := i odd ifTrue:[txt] ifFalse:[img].
|
1398
|
1123 |
lbl := Array with:lbl with:'test' with:img.
|
1390
|
1124 |
children add:(self class labeled:lbl icon:icon).
|
|
1125 |
]
|
|
1126 |
].
|
|
1127 |
|
|
1128 |
children do:[:aChild| aChild parent:self ].
|
|
1129 |
^ children
|
|
1130 |
!
|
|
1131 |
|
|
1132 |
icon
|
|
1133 |
"returns the icon
|
|
1134 |
"
|
|
1135 |
^ icon
|
|
1136 |
!
|
|
1137 |
|
|
1138 |
icon:anIcon
|
|
1139 |
"set the icon; if icon changed, a notification
|
|
1140 |
is raised.
|
|
1141 |
"
|
|
1142 |
icon ~= anIcon ifTrue:[
|
|
1143 |
icon := anIcon.
|
|
1144 |
self iconChanged
|
|
1145 |
]
|
|
1146 |
!
|
|
1147 |
|
|
1148 |
label
|
|
1149 |
"returns the label
|
|
1150 |
"
|
|
1151 |
^ label
|
|
1152 |
|
|
1153 |
!
|
|
1154 |
|
|
1155 |
label:aLabel
|
|
1156 |
"set the label; if label changed, a notification
|
|
1157 |
is raised.
|
|
1158 |
"
|
|
1159 |
label ~= aLabel ifTrue:[
|
|
1160 |
label := aLabel.
|
|
1161 |
self changed.
|
|
1162 |
]
|
|
1163 |
|
|
1164 |
!
|
|
1165 |
|
|
1166 |
setIcon:anIcon
|
|
1167 |
"set the icon without any change notification
|
|
1168 |
"
|
|
1169 |
icon := anIcon
|
|
1170 |
!
|
|
1171 |
|
|
1172 |
setLabel:aLabel
|
|
1173 |
"set the label without any change notification
|
|
1174 |
"
|
|
1175 |
label := aLabel
|
|
1176 |
!
|
|
1177 |
|
|
1178 |
setLabel:aLabel icon:anIcon
|
|
1179 |
"set the label and icon without any change notification
|
|
1180 |
"
|
|
1181 |
label := aLabel.
|
|
1182 |
icon := anIcon.
|
|
1183 |
! !
|
|
1184 |
|
|
1185 |
!HierarchicalItem class methodsFor:'documentation'!
|
|
1186 |
|
|
1187 |
version
|
1489
|
1188 |
^ '$Header: /cvs/stx/stx/libwidg2/HierarchicalItem.st,v 1.8 1999-08-27 15:34:38 tm Exp $'
|
1390
|
1189 |
! !
|