author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2015 16:46:44 +0200 | |
changeset 4746 | 25341fd92e38 |
parent 4733 | 9ad52ca7b1c3 |
child 4749 | 13a7e6db823b |
permissions | -rw-r--r-- |
4733 | 1 |
"{ Encoding: utf8 }" |
2 |
||
3 |
" |
|
4 |
COPYRIGHT (c) 1999/2015 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libwidg2' }" |
|
15 |
||
16 |
"{ NameSpace: Smalltalk }" |
|
17 |
||
18 |
Object subclass:#AbstractHierarchicalItem |
|
19 |
instanceVariableNames:'parent children' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Views-Support' |
|
23 |
! |
|
24 |
||
25 |
!AbstractHierarchicalItem class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1999/2015 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
Hierarchical Items are mostly like Models, but the list of |
|
44 |
dependencies are kept by its HierarchicalList. |
|
45 |
The class is used to build up hierarchical trees. |
|
46 |
||
47 |
2015 update: |
|
48 |
the original HierarchicalItem has been refactored into this abstract class, |
|
49 |
which provides all the mechanisms, but leaves the concrete representation |
|
50 |
of some slots open. |
|
51 |
These are: |
|
52 |
- if and how the geometry information (width + height) are cached, |
|
53 |
- if and how the expanded-state is remembered. |
|
54 |
- if and how the underlying model is fetched |
|
55 |
||
56 |
The old class used private slots for the first three (width-height-isExpanded), |
|
57 |
but did not keep a reference to the model. This leads to a very poor performance, |
|
58 |
as many algorithms degenerated to O(n log(n)) or even O(n^2) time behavior, |
|
59 |
as the model was fetched by walking along the parent chain - sometimes for every item |
|
60 |
in a long list. |
|
61 |
||
62 |
The old class is still around and may be used for small trees, |
|
63 |
but we recommend rewriting applications to use the new CompactHierarchicalItem |
|
64 |
class, which behaves the same on the outside, but uses clever tricks to be both more |
|
65 |
space efficient (saving 2 slots) and time efficient (caching the model). |
|
66 |
||
67 |
[Instance variables:] |
|
68 |
parent <Item, List or nil> parent or my HierarchicalList. |
|
69 |
children <Collection or nil> list of children |
|
70 |
||
71 |
[author:] |
|
72 |
Claus Gittinger (redesign and refactoring) |
|
73 |
||
74 |
[see also:] |
|
75 |
HierarchicalItem (the old item class) |
|
76 |
HierarchicalList (typical model) |
|
77 |
HierarchicalListView (typical user of me) |
|
78 |
" |
|
79 |
! ! |
|
80 |
||
81 |
!AbstractHierarchicalItem class methodsFor:'instance creation'! |
|
82 |
||
83 |
new |
|
84 |
^ (self basicNew) initialize |
|
85 |
! |
|
86 |
||
87 |
parent:aParent |
|
88 |
|item| |
|
89 |
||
90 |
item := self new. |
|
91 |
item parent:aParent. |
|
92 |
^ item |
|
93 |
! ! |
|
94 |
||
95 |
!AbstractHierarchicalItem class methodsFor:'protocol'! |
|
96 |
||
97 |
doResetExtentOnChange |
|
98 |
"true: the extent of the item is reset if a change |
|
99 |
notification is raised from the item. the default is true |
|
100 |
" |
|
101 |
^ true |
|
102 |
! ! |
|
103 |
||
104 |
!AbstractHierarchicalItem methodsFor:'accessing'! |
|
105 |
||
106 |
getChildren |
|
107 |
"returns the children as they are present (or not); not going to the model..." |
|
108 |
||
109 |
^ children |
|
110 |
! |
|
111 |
||
112 |
level |
|
113 |
"returns the level starting with 0 for the root" |
|
114 |
||
115 |
|item level| |
|
116 |
||
117 |
item := self. |
|
118 |
level := 0. |
|
119 |
||
120 |
[ (item := item parentOrModel) notNil] whileTrue:[ |
|
121 |
level := level + 1. |
|
122 |
level > 100000 ifTrue:[ |
|
123 |
self halt:'possibly recursive item hierarchy' |
|
124 |
]. |
|
125 |
]. |
|
126 |
^ level |
|
127 |
||
128 |
"Modified: / 09-07-2010 / 08:56:27 / cg" |
|
129 |
! |
|
130 |
||
131 |
parent |
|
132 |
"returns the parent or nil" |
|
133 |
||
134 |
^ (parent notNil and:[parent isHierarchicalItem]) |
|
135 |
ifTrue:[parent] |
|
136 |
ifFalse:[nil] |
|
137 |
! |
|
138 |
||
139 |
parent:aParent |
|
140 |
"set the parent (or the model if the item is the root item)" |
|
141 |
||
142 |
parent := aParent |
|
143 |
! |
|
144 |
||
145 |
rootItem |
|
146 |
"returns the root item" |
|
147 |
||
148 |
parent isHierarchicalItem ifTrue:[ |
|
149 |
^ parent rootItem |
|
150 |
]. |
|
151 |
^ self |
|
152 |
! ! |
|
153 |
||
154 |
!AbstractHierarchicalItem methodsFor:'accessing-children'! |
|
155 |
||
156 |
at:anIndex |
|
157 |
"return the child at anIndex if valid; |
|
158 |
if the index is invalid, nil is returned" |
|
159 |
||
160 |
^ self at:anIndex ifAbsent:nil |
|
161 |
! |
|
162 |
||
163 |
at:anIndex ifAbsent:exceptionBlock |
|
164 |
"return the child at anIndex if valid; if the index is |
|
165 |
invalid, the result of evaluating the exceptionBlock is returned." |
|
166 |
||
167 |
|list| |
|
168 |
||
169 |
(list := self children) notNil ifTrue:[ |
|
170 |
^ list at:anIndex ifAbsent:exceptionBlock |
|
171 |
]. |
|
172 |
^ exceptionBlock value |
|
173 |
! |
|
174 |
||
175 |
at:anIndex put:anItem |
|
176 |
"replace a child by a new item. return anItem (sigh)" |
|
177 |
||
178 |
|children oldItem visIndex model expFlag| |
|
179 |
||
180 |
anItem isNil ifTrue:[ |
|
181 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
182 |
^ nil |
|
183 |
]. |
|
184 |
anItem parent:self. |
|
185 |
||
186 |
(model := self model) isNil ifTrue:[ |
|
187 |
self children at:anIndex put:anItem. |
|
188 |
^ anItem |
|
189 |
]. |
|
190 |
||
191 |
model criticalDo:[ |
|
192 |
children := self children. |
|
193 |
oldItem := children at:anIndex. |
|
194 |
||
195 |
oldItem isExpanded ifTrue:[ |
|
196 |
oldItem collapse |
|
197 |
]. |
|
198 |
visIndex := model identityIndexOf:oldItem. |
|
199 |
expFlag := anItem isExpanded. |
|
200 |
anItem setExpanded:false. |
|
201 |
||
202 |
children at:anIndex put:anItem. |
|
203 |
||
204 |
visIndex ~~ 0 ifTrue:[ |
|
205 |
model at:visIndex put:anItem. |
|
206 |
]. |
|
207 |
self changed:#redraw. |
|
208 |
expFlag ifTrue:[ anItem expand ]. |
|
209 |
]. |
|
210 |
^ anItem |
|
211 |
! |
|
212 |
||
213 |
children:aListOfChildren |
|
214 |
"set a new list of children" |
|
215 |
||
216 |
self criticalDo:[ |
|
217 |
self removeAll. |
|
218 |
self addAll:aListOfChildren beforeIndex:1 |
|
219 |
]. |
|
220 |
^ aListOfChildren |
|
221 |
! |
|
222 |
||
223 |
first |
|
224 |
"returns the first child |
|
225 |
" |
|
226 |
^ self at:1 |
|
227 |
! |
|
228 |
||
229 |
last |
|
230 |
"returns the last child" |
|
231 |
||
232 |
^ self at:(self size) |
|
233 |
! |
|
234 |
||
235 |
second |
|
236 |
"returns the second child" |
|
237 |
||
238 |
^ self at:2 |
|
239 |
! ! |
|
240 |
||
241 |
!AbstractHierarchicalItem methodsFor:'accessing-hierarchy'! |
|
242 |
||
243 |
collapse |
|
244 |
"hide all my subitems" |
|
245 |
||
246 |
|visChd index| |
|
247 |
||
248 |
self canCollapse ifTrue:[ |
|
249 |
self setExpanded:false. |
|
250 |
||
251 |
self criticalDo:[ |
|
252 |
(index := self listIndex) notNil ifTrue:[ |
|
253 |
"/ do not call :#size: children will be autoloaded !!!! |
|
254 |
(visChd := children size) ~~ 0 ifTrue:[ |
|
255 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
256 |
visChd := visChd + el numberOfVisibleChildren |
|
257 |
]. |
|
258 |
self model itemRemoveFromIndex:(index + 1) toIndex:(index + visChd). |
|
259 |
]. |
|
260 |
index ~~ 0 ifTrue:[self hierarchyChanged]. |
|
261 |
] |
|
262 |
] |
|
263 |
] |
|
264 |
! |
|
265 |
||
266 |
enforcedExpand |
|
267 |
"expand children - even if there are no children, |
|
268 |
the item is initially expanded (but this might be undone later, |
|
269 |
when we know that no children are there" |
|
270 |
||
271 |
self expand:true |
|
272 |
! |
|
273 |
||
274 |
expand |
|
275 |
"expand children - but only if there are children |
|
276 |
(i.e. this cannot be used before the childInfo is valid; |
|
277 |
aka not before the updateTask came along this item)" |
|
278 |
||
279 |
self expand:false |
|
280 |
! |
|
281 |
||
282 |
expand:enforced |
|
283 |
"expand children" |
|
284 |
||
285 |
|index list| |
|
286 |
||
287 |
"/ test whether the item already is expanded; #canExpand could be redefined |
|
288 |
"/ without checking whether the node is expanded (happens already) !! |
|
289 |
||
290 |
self isExpanded ifTrue:[ ^ self ]. |
|
291 |
(enforced not and:[self canExpand not]) ifTrue:[ ^ self ]. |
|
292 |
||
293 |
self criticalDo:[ |
|
294 |
(index := self listIndex) notNil ifTrue:[ |
|
295 |
"/ must set expand-flag to false, otherwise change notifications |
|
296 |
"/ are raised during lazy auto creation (to the list). |
|
297 |
self setExpanded:false. |
|
298 |
list := self children. |
|
299 |
self setExpanded:true. |
|
300 |
||
301 |
list notEmptyOrNil ifTrue:[ |
|
302 |
list := OrderedCollection new. |
|
303 |
self addVisibleChildrenTo:list. |
|
304 |
self model itemAddAll:list beforeIndex:(index + 1). |
|
305 |
]. |
|
306 |
index ~~ 0 ifTrue:[self hierarchyChanged]. |
|
307 |
] ifFalse:[ |
|
308 |
self setExpanded:true |
|
309 |
] |
|
310 |
]. |
|
311 |
! |
|
312 |
||
313 |
makeVisible |
|
314 |
"expand all my parents" |
|
315 |
||
316 |
(parent notNil and:[parent isHierarchicalItem]) ifTrue:[ |
|
317 |
self criticalDo:[ |
|
318 |
parent makeVisible. |
|
319 |
parent expand. |
|
320 |
] |
|
321 |
]. |
|
322 |
! |
|
323 |
||
324 |
recursiveCollapse |
|
325 |
"collapse all item and sub items |
|
326 |
**** must be expanded |
|
327 |
" |
|
328 |
|visChd index| |
|
329 |
||
330 |
self canCollapse ifTrue:[ |
|
331 |
self criticalDo:[ |
|
332 |
(index := self listIndex) notNil ifTrue:[ |
|
333 |
"/ do not call :#size: children will be autoloaded !!!! |
|
334 |
(visChd := children size) ~~ 0 ifTrue:[ |
|
335 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
336 |
visChd := visChd + el numberOfVisibleChildren |
|
337 |
]. |
|
338 |
]. |
|
339 |
self recursiveSetCollapsed. |
|
340 |
||
341 |
visChd ~~ 0 ifTrue:[ |
|
342 |
self model itemRemoveFromIndex:(index + 1) |
|
343 |
toIndex:(index + visChd) |
|
344 |
]. |
|
345 |
index ~~ 0 ifTrue:[ |
|
346 |
self hierarchyChanged |
|
347 |
] |
|
348 |
] ifFalse:[ |
|
349 |
self recursiveSetCollapsed |
|
350 |
] |
|
351 |
] |
|
352 |
] |
|
353 |
! |
|
354 |
||
355 |
recursiveExpand |
|
356 |
"expand children and sub-children |
|
357 |
**** must be collapsed" |
|
358 |
||
359 |
"/ test whether the item already is expanded; #canExpand could be redefined |
|
360 |
"/ without checking whether the node is expanded (happens already) !! |
|
361 |
||
362 |
|index list| |
|
363 |
||
364 |
self isExpanded ifTrue:[ ^ self ]. |
|
365 |
||
366 |
self canExpand ifFalse:[ ^ self ]. |
|
367 |
||
368 |
self setExpanded:true. |
|
369 |
||
370 |
self criticalDo:[ |
|
371 |
self size ~~ 0 ifTrue:[ |
|
372 |
index := self listIndex. "/ get the visible list index |
|
373 |
||
374 |
index isNil ifTrue:[ "/ not visible |
|
375 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
376 |
el setExpanded:true |
|
377 |
]. |
|
378 |
] ifFalse:[ |
|
379 |
list := OrderedCollection new. |
|
380 |
self recursiveSetExpandedAndAddToList:list. |
|
381 |
self model itemAddAll:list beforeIndex:(index + 1). |
|
382 |
||
383 |
index ~~ 0 ifTrue:[self hierarchyChanged] |
|
384 |
] |
|
385 |
] |
|
386 |
]. |
|
387 |
! |
|
388 |
||
389 |
recursiveToggleExpand |
|
390 |
"if the item is collapsed, the item and all its sub-items |
|
391 |
are expanded otherwise collapsed" |
|
392 |
||
393 |
self isExpanded ifTrue:[ |
|
394 |
self recursiveCollapse |
|
395 |
] ifFalse:[ |
|
396 |
self recursiveExpand |
|
397 |
] |
|
398 |
! |
|
399 |
||
400 |
toggleExpand |
|
401 |
"if the item is collapsed, the item is expanded otherwise collapsed" |
|
402 |
||
403 |
self isExpanded ifTrue:[ |
|
404 |
self collapse |
|
405 |
] ifFalse:[ |
|
406 |
self expand |
|
407 |
]. |
|
408 |
! ! |
|
409 |
||
410 |
!AbstractHierarchicalItem methodsFor:'accessing-mvc'! |
|
411 |
||
412 |
application |
|
413 |
"returns the responsible application or nil" |
|
414 |
||
415 |
|model| |
|
416 |
||
417 |
(model := self model) notNil ifTrue:[ |
|
418 |
^ model application |
|
419 |
]. |
|
420 |
^ nil |
|
421 |
! |
|
422 |
||
423 |
applicationsDo:aOneArgBlock |
|
424 |
"evaluate the block for each dependent application" |
|
425 |
||
426 |
|model| |
|
427 |
||
428 |
(model := self model) notNil ifTrue:[ |
|
429 |
model applicationsDo:aOneArgBlock |
|
430 |
] |
|
431 |
! |
|
432 |
||
433 |
model |
|
434 |
"returns the hierachicalList model or nil. |
|
435 |
This is a stupid implementation here, in that the top-item's parent is assumed to |
|
436 |
be the model of the tree, and that is returned. |
|
437 |
This saves a slot in every node, but makes some algorithms O(n*log n) or even O(n^2). |
|
438 |
So be aware of the performance penalty" |
|
439 |
||
440 |
|item next| |
|
441 |
||
442 |
item := self. |
|
443 |
[ (next := item parentOrModel) notNil ] whileTrue:[ |
|
444 |
item := next. |
|
445 |
]. |
|
446 |
||
447 |
item isHierarchicalItem ifFalse:[^ item]. |
|
448 |
^ nil |
|
449 |
! ! |
|
450 |
||
451 |
!AbstractHierarchicalItem methodsFor:'adding & removing'! |
|
452 |
||
453 |
add:aChildItem |
|
454 |
"add a child at end" |
|
455 |
||
456 |
^ self add:aChildItem beforeIndex:(self children size + 1). |
|
457 |
! |
|
458 |
||
459 |
add:aChildItem after:aChild |
|
460 |
"add an item after an existing item" |
|
461 |
||
462 |
|index| |
|
463 |
||
464 |
index := self identityIndexOf:aChild. |
|
465 |
index == 0 ifTrue:[ self subscriptBoundsError:index ]. |
|
466 |
||
467 |
self add:aChildItem beforeIndex:(index + 1). |
|
468 |
^ aChildItem |
|
469 |
! |
|
470 |
||
471 |
add:aChildItem afterIndex:anIndex |
|
472 |
"add an item after an index" |
|
473 |
||
474 |
^ self add:aChildItem beforeIndex:(anIndex + 1). |
|
475 |
! |
|
476 |
||
477 |
add:aChildItem before:aChild |
|
478 |
"add an item before an existing item" |
|
479 |
||
480 |
|index| |
|
481 |
||
482 |
index := self identityIndexOf:aChild. |
|
483 |
index == 0 ifTrue:[ self subscriptBoundsError:index ]. |
|
484 |
||
485 |
self add:aChildItem beforeIndex:index. |
|
486 |
^ aChild |
|
487 |
! |
|
488 |
||
489 |
add:aChildItem beforeIndex:anIndex |
|
490 |
"add an item before an index" |
|
491 |
||
492 |
aChildItem notNil ifTrue:[ |
|
493 |
self addAll:(Array with:aChildItem) beforeIndex:anIndex |
|
494 |
]. |
|
495 |
^ aChildItem |
|
496 |
! |
|
497 |
||
498 |
add:aChild sortBlock:aBlock |
|
499 |
"add a child sorted" |
|
500 |
||
501 |
self criticalDo:[ |
|
502 |
self basicAdd:aChild sortBlock:aBlock |
|
503 |
]. |
|
504 |
^ aChild |
|
505 |
! |
|
506 |
||
507 |
addAll:aList |
|
508 |
"add children at the end" |
|
509 |
||
510 |
^ self addAll:aList beforeIndex:(self children size + 1) |
|
511 |
! |
|
512 |
||
513 |
addAll:aList before:aChild |
|
514 |
"add an item before an existing item" |
|
515 |
||
516 |
|index| |
|
517 |
||
518 |
index := self identityIndexOf:aChild. |
|
519 |
index == 0 ifTrue:[ self subscriptBoundsError:index ]. |
|
520 |
||
521 |
^ self addAll:aList beforeIndex:index |
|
522 |
! |
|
523 |
||
524 |
addAll:aList beforeIndex:anIndex |
|
525 |
"add children before an index" |
|
526 |
||
527 |
aList size ~~ 0 ifTrue:[ |
|
528 |
self criticalDo:[ |
|
529 |
self basicAddAll:aList beforeIndex:anIndex |
|
530 |
] |
|
531 |
]. |
|
532 |
^ aList |
|
533 |
! |
|
534 |
||
535 |
addAll:aList sortBlock:aBlock |
|
536 |
"add children sorted" |
|
537 |
||
538 |
aList size == 0 ifTrue:[ ^ aList ]. |
|
539 |
||
540 |
aBlock isNil ifTrue:[ |
|
541 |
self addAll:aList. |
|
542 |
] ifFalse:[ |
|
543 |
self criticalDo:[ |
|
544 |
aList do:[:el| self basicAdd:el sortBlock:aBlock ] |
|
545 |
] |
|
546 |
]. |
|
547 |
^ aList |
|
548 |
! |
|
549 |
||
550 |
addAllFirst:aCollectionOfItems |
|
551 |
"add children at the beginning" |
|
552 |
||
553 |
^ self addAll:aCollectionOfItems beforeIndex:1 |
|
554 |
! |
|
555 |
||
556 |
addAllLast:aCollectionOfItems |
|
557 |
"add children at the end" |
|
558 |
||
559 |
^ self addAll:aCollectionOfItems |
|
560 |
! |
|
561 |
||
562 |
addFirst:aChildItem |
|
563 |
"add a child at the beginning" |
|
564 |
||
565 |
^ self add:aChildItem beforeIndex:1. |
|
566 |
! |
|
567 |
||
568 |
addLast:anItem |
|
569 |
"add a child at the end" |
|
570 |
||
571 |
^ self add:anItem |
|
572 |
! |
|
573 |
||
574 |
remove |
|
575 |
"remove the item" |
|
576 |
||
577 |
parent notNil ifTrue:[ "check whether parent exists" |
|
578 |
parent isHierarchicalItem ifTrue:[parent remove:self] "parent is HierarchicalItem" |
|
579 |
ifFalse:[parent root:nil] "parent is HierarchicalList" |
|
580 |
]. |
|
581 |
^ self |
|
582 |
! |
|
583 |
||
584 |
remove:aChild |
|
585 |
"remove a child" |
|
586 |
||
587 |
self removeIndex:(self identityIndexOf:aChild) |
|
588 |
! |
|
589 |
||
590 |
removeAll |
|
591 |
"remove all children" |
|
592 |
||
593 |
|size| |
|
594 |
||
595 |
(size := children size) ~~ 0 ifTrue:[ |
|
596 |
self removeFromIndex:1 toIndex:size |
|
597 |
] |
|
598 |
! |
|
599 |
||
600 |
removeAll:aList |
|
601 |
"remove all children in the collection" |
|
602 |
||
603 |
|index| |
|
604 |
||
605 |
aList size ~~ 0 ifTrue:[ |
|
606 |
self criticalDo:[ |
|
607 |
aList do:[:el| |
|
608 |
(index := self identityIndexOf:el) ~~ 0 ifTrue:[ |
|
609 |
self removeIndex:index |
|
610 |
] |
|
611 |
] |
|
612 |
] |
|
613 |
]. |
|
614 |
^ aList |
|
615 |
! |
|
616 |
||
617 |
removeAllIdentical:aList |
|
618 |
"remove all children in the collection" |
|
619 |
||
620 |
self removeAll:aList. |
|
621 |
^ aList |
|
622 |
||
623 |
"Created: / 20-09-2010 / 09:43:06 / sr" |
|
624 |
! |
|
625 |
||
626 |
removeFromIndex:startIndex |
|
627 |
"remove the children from startIndex up to end of children" |
|
628 |
||
629 |
^ self removeFromIndex:startIndex toIndex:(children size) |
|
630 |
! |
|
631 |
||
632 |
removeFromIndex:startIndex toIndex:stopIndex |
|
633 |
"remove the children from startIndex up to and including |
|
634 |
the child under stopIndex. |
|
635 |
Returns the receiver." |
|
636 |
||
637 |
|nrOfChildren stop| |
|
638 |
||
639 |
nrOfChildren := children size. |
|
640 |
||
641 |
(startIndex <= stopIndex and:[startIndex <= nrOfChildren]) ifTrue:[ |
|
642 |
stop := stopIndex min:nrOfChildren. |
|
643 |
||
644 |
self criticalDo:[ |
|
645 |
self basicRemoveFromIndex:startIndex toIndex:stop |
|
646 |
] |
|
647 |
]. |
|
648 |
||
649 |
children size == 0 ifTrue:[ |
|
650 |
self clearExpandedWhenLastChildWasRemoved ifTrue:[ |
|
651 |
self setExpanded:false. |
|
652 |
] |
|
653 |
]. |
|
654 |
! |
|
655 |
||
656 |
removeIndex:anIndex |
|
657 |
"remove the child at an index" |
|
658 |
||
659 |
anIndex > 0 ifTrue:[ |
|
660 |
self removeFromIndex:anIndex toIndex:anIndex |
|
661 |
] |
|
662 |
! ! |
|
663 |
||
664 |
!AbstractHierarchicalItem methodsFor:'basic adding & removing'! |
|
665 |
||
666 |
basicAdd:aChild sortBlock:aBlock |
|
667 |
"add a child sorted" |
|
668 |
||
669 |
|size list| |
|
670 |
||
671 |
size := children size. |
|
672 |
list := Array with:aChild. |
|
673 |
||
674 |
(aBlock notNil and:[size ~~ 0]) ifTrue:[ |
|
675 |
children keysAndValuesDo:[:i :el| |
|
676 |
(aBlock value:aChild value:el) ifTrue:[ |
|
677 |
self basicAddAll:list beforeIndex:i. |
|
678 |
^ aChild |
|
679 |
] |
|
680 |
] |
|
681 |
]. |
|
682 |
self basicAddAll:list beforeIndex:(size + 1). |
|
683 |
^ aChild. |
|
684 |
! |
|
685 |
||
686 |
basicAddAll:aList beforeIndex:anIndex |
|
687 |
"add children before an index" |
|
688 |
||
689 |
|coll model notify index size| |
|
690 |
||
691 |
size := children size. |
|
692 |
||
693 |
anIndex == 1 ifTrue:[ |
|
694 |
notify := self |
|
695 |
] ifFalse:[ |
|
696 |
anIndex > size ifTrue:[ |
|
697 |
anIndex > (1 + size) ifTrue:[ |
|
698 |
^ self subscriptBoundsError:index |
|
699 |
]. |
|
700 |
notify := self at:size |
|
701 |
] ifFalse:[ |
|
702 |
notify := nil |
|
703 |
] |
|
704 |
]. |
|
705 |
children isArray ifTrue:[ |
|
706 |
children := children asOrderedCollection |
|
707 |
]. |
|
708 |
||
709 |
size == 0 ifTrue:[ |
|
710 |
children := OrderedCollection new |
|
711 |
]. |
|
712 |
aList do:[:anItem| anItem parent:self ]. |
|
713 |
children addAll:aList beforeIndex:anIndex. |
|
714 |
||
715 |
(model := self model) isNil ifTrue:[ |
|
716 |
^ aList |
|
717 |
]. |
|
718 |
||
719 |
self isExpanded ifFalse:[ |
|
720 |
notify notNil ifTrue:[ |
|
721 |
notify changed |
|
722 |
]. |
|
723 |
^ aList |
|
724 |
]. |
|
725 |
(index := self listIndex) isNil ifTrue:[ |
|
726 |
^ aList |
|
727 |
]. |
|
728 |
||
729 |
children from:1 to:(anIndex - 1) do:[:anItem| |
|
730 |
index := 1 + index + anItem numberOfVisibleChildren |
|
731 |
]. |
|
732 |
coll := OrderedCollection new. |
|
733 |
||
734 |
aList do:[:anItem| |
|
735 |
coll add:anItem. |
|
736 |
anItem addVisibleChildrenTo:coll. |
|
737 |
]. |
|
738 |
model itemAddAll:coll beforeIndex:(index + 1). |
|
739 |
||
740 |
notify notNil ifTrue:[ |
|
741 |
notify changed |
|
742 |
]. |
|
743 |
^ aList |
|
744 |
! |
|
745 |
||
746 |
basicRemoveFromIndex:startIndex toIndex:stopIndex |
|
747 |
"remove the children from startIndex up to and including |
|
748 |
the child under stopIndex." |
|
749 |
||
750 |
|model notify |
|
751 |
index "{ Class:SmallInteger }" |
|
752 |
start "{ Class:SmallInteger }" |
|
753 |
stop "{ Class:SmallInteger }" |
|
754 |
size "{ Class:SmallInteger }" |
|
755 |
| |
|
756 |
size := self children size. |
|
757 |
stop := stopIndex. |
|
758 |
start := startIndex. |
|
759 |
||
760 |
(stop <= size and:[start between:1 and:stop]) ifFalse:[ |
|
761 |
^ self subscriptBoundsError:index |
|
762 |
]. |
|
763 |
start == 1 ifTrue:[ |
|
764 |
notify := self |
|
765 |
] ifFalse:[ |
|
766 |
stop == size ifTrue:[ |
|
767 |
notify := self at:(start - 1) |
|
768 |
] ifFalse:[ |
|
769 |
notify := nil |
|
770 |
] |
|
771 |
]. |
|
772 |
||
773 |
(model := self model) notNil ifTrue:[ |
|
774 |
index := model identityIndexOf:(children at:start). |
|
775 |
size := stop - start + 1. |
|
776 |
] ifFalse:[ |
|
777 |
index := 0 |
|
778 |
]. |
|
779 |
||
780 |
children from:start to:stop do:[:aChild| |
|
781 |
index ~~ 0 ifTrue:[ |
|
782 |
size := size + aChild numberOfVisibleChildren |
|
783 |
]. |
|
784 |
aChild parent:nil |
|
785 |
]. |
|
786 |
children removeFromIndex:start toIndex:stop. |
|
787 |
||
788 |
index ~~ 0 ifTrue:[ |
|
789 |
model itemRemoveFromIndex:index toIndex:(index + size - 1) |
|
790 |
]. |
|
791 |
notify notNil ifTrue:[ |
|
792 |
notify changed |
|
793 |
]. |
|
794 |
! ! |
|
795 |
||
796 |
!AbstractHierarchicalItem methodsFor:'change & update'! |
|
797 |
||
798 |
changed:what with:anArgument |
|
799 |
"the item changed; raise change notification |
|
800 |
#icon icon is modified; height and width are unchanged |
|
801 |
#hierarchy collapsed/expanded; height and width are unchanged |
|
802 |
#redraw redraw but height and width are unchanged |
|
803 |
....... all others: the height and width are reset |
|
804 |
" |
|
805 |
|model| |
|
806 |
||
807 |
what ~~ #redraw ifTrue:[ |
|
808 |
(what ~~ #hierarchy and:[what ~~ #icon]) ifTrue:[ |
|
809 |
self class doResetExtentOnChange ifTrue:[ |
|
810 |
self makeWidthAndHeightUnknown |
|
811 |
]. |
|
812 |
]. |
|
813 |
]. |
|
814 |
(model := self model) notNil ifTrue:[ |
|
815 |
model itemChanged:what with:anArgument from:self |
|
816 |
]. |
|
817 |
super changed:what with:anArgument |
|
818 |
||
819 |
"Modified: / 24-11-2010 / 17:21:20 / cg" |
|
820 |
! |
|
821 |
||
822 |
childrenOrderChanged |
|
823 |
"called if the order of the children changed by a user |
|
824 |
operation. Update the model and raise a change notification for |
|
825 |
each item which has changed its position |
|
826 |
triggered by the user operation !!" |
|
827 |
||
828 |
|model visStart list| |
|
829 |
||
830 |
self isExpanded ifFalse:[ ^ self ]. "/ not expanded |
|
831 |
children size > 1 ifFalse:[ ^ self ]. |
|
832 |
||
833 |
model := self model. |
|
834 |
model isNil ifTrue:[^ self]. "/ no model |
|
835 |
||
836 |
visStart := model identityIndexOf:self. |
|
837 |
visStart == 0 ifTrue:[ |
|
838 |
model root ~~ self ifTrue:[ ^ self ]. |
|
839 |
"/ I'am the root but switched of by setting #showRoot to false |
|
840 |
]. |
|
841 |
||
842 |
self criticalDo:[ |
|
843 |
list := OrderedCollection new. |
|
844 |
self addVisibleChildrenTo:list. |
|
845 |
||
846 |
list do:[:el| |
|
847 |
visStart := visStart + 1. |
|
848 |
||
849 |
(model at:visStart ifAbsent:el) ~~ el ifTrue:[ |
|
850 |
model at:visStart put:el |
|
851 |
]. |
|
852 |
] |
|
853 |
]. |
|
854 |
! |
|
855 |
||
856 |
fontChanged |
|
857 |
"called if the font has changed. |
|
858 |
Clear the precomputed width and height" |
|
859 |
||
860 |
self makeWidthAndHeightUnknown. |
|
861 |
||
862 |
children size ~~ 0 ifTrue:[ |
|
863 |
children do:[:el| el fontChanged]. |
|
864 |
]. |
|
865 |
! |
|
866 |
||
867 |
hierarchyChanged |
|
868 |
"hierarchy changed; optimize redrawing" |
|
869 |
||
870 |
self changed:#hierarchy with:nil |
|
871 |
! |
|
872 |
||
873 |
iconChanged |
|
874 |
"icon changed; optimize redrawing" |
|
875 |
||
876 |
self changed:#icon with:nil |
|
877 |
! |
|
878 |
||
879 |
labelChanged |
|
880 |
"called if the label has changed. |
|
881 |
Clear the precomputed width and height" |
|
882 |
||
883 |
self makeWidthAndHeightUnknown. |
|
884 |
||
885 |
"Created: / 17-01-2011 / 17:43:42 / cg" |
|
886 |
! ! |
|
887 |
||
888 |
!AbstractHierarchicalItem methodsFor:'enumerating'! |
|
889 |
||
890 |
collect:aBlock |
|
891 |
"for each child in the receiver, evaluate the argument, aBlock |
|
892 |
and return a new collection with the results" |
|
893 |
||
894 |
|coll| |
|
895 |
||
896 |
coll := OrderedCollection new. |
|
897 |
self do:[:el| coll add:(aBlock value:el) ]. |
|
898 |
^ coll |
|
899 |
! |
|
900 |
||
901 |
contains:aBlock |
|
902 |
"evaluate aOneArgBlock for each of the receiver's elements |
|
903 |
Return true and skip remaining elements, if aBlock ever returns true, |
|
904 |
otherwise return false" |
|
905 |
||
906 |
self do:[:el | (aBlock value:el) ifTrue:[^ true] ]. |
|
907 |
^ false |
|
908 |
! |
|
909 |
||
910 |
do:aOneArgBlock |
|
911 |
"evaluate a block for each child" |
|
912 |
||
913 |
self from:1 to:nil do:aOneArgBlock |
|
914 |
! |
|
915 |
||
916 |
from:startIndex do:aOneArgBlock |
|
917 |
"evaluate a block on each child starting with the |
|
918 |
child at startIndex to the end. |
|
919 |
" |
|
920 |
self from:startIndex to:nil do:aOneArgBlock |
|
921 |
! |
|
922 |
||
923 |
from:startIndex reverseDo:aOneArgBlock |
|
924 |
"evaluate a block on each child starting at end to the startIndex |
|
925 |
" |
|
926 |
self from:startIndex to:nil reverseDo:aOneArgBlock |
|
927 |
! |
|
928 |
||
929 |
from:startIndex to:endIndex do:aOneArgBlock |
|
930 |
"evaluate a block on each child starting with the |
|
931 |
child at startIndex to the endIndex. |
|
932 |
" |
|
933 |
|res| |
|
934 |
||
935 |
self size < startIndex ifTrue:[^ self "nil"]. |
|
936 |
res := nil. |
|
937 |
||
938 |
self criticalDo:[ |
|
939 |
res := self nonCriticalFrom:startIndex to:endIndex do:aOneArgBlock |
|
940 |
]. |
|
941 |
"/ ^ res - return the receiver, as all other collections do |
|
942 |
! |
|
943 |
||
944 |
from:startIndex to:endIndex reverseDo:aOneArgBlock |
|
945 |
"evaluate a block on each child starting with the |
|
946 |
child at endIndex to the startIndex." |
|
947 |
||
948 |
|res| |
|
949 |
||
950 |
self size < startIndex ifTrue:[^ self "nil"]. |
|
951 |
res := nil. |
|
952 |
||
953 |
self criticalDo:[ |
|
954 |
res := self nonCriticalFrom:startIndex to:endIndex reverseDo:aOneArgBlock |
|
955 |
]. |
|
956 |
"/ ^ res - return the receiver, as all other collections do |
|
957 |
! |
|
958 |
||
959 |
keysAndValuesDo:aTwoArgBlock |
|
960 |
"evaluate the argument, aBlock for every child, |
|
961 |
passing both index and element as arguments." |
|
962 |
||
963 |
|key res| |
|
964 |
||
965 |
key := 1. |
|
966 |
res := nil. |
|
967 |
||
968 |
self do:[:el| |
|
969 |
res := el value:key value:el. |
|
970 |
key := key + 1. |
|
971 |
]. |
|
972 |
"/ ^ res - no: return the receiver, as all other collections do |
|
973 |
! |
|
974 |
||
975 |
keysAndValuesReverseDo:aTwoArgBlock |
|
976 |
"evaluate the argument, aBlock in reverse order for every |
|
977 |
child, passing both index and element as arguments." |
|
978 |
||
979 |
|res| |
|
980 |
||
981 |
self size == 0 ifTrue:[^ self "nil"]. |
|
982 |
res := nil. |
|
983 |
||
984 |
self criticalDo:[ |
|
985 |
res := self nonCriticalKeysAndValuesReverseDo:aTwoArgBlock |
|
986 |
]. |
|
987 |
"/ ^ res - no: return the receiver, as all other collections do |
|
988 |
! |
|
989 |
||
990 |
recursiveCollect:aBlock |
|
991 |
"for each child in the receiver, evaluate the argument, aBlock |
|
992 |
and return a new collection with the results" |
|
993 |
||
994 |
|coll| |
|
995 |
||
996 |
coll := OrderedCollection new. |
|
997 |
self recursiveDo:[:el| coll add:(aBlock value:el) ]. |
|
998 |
^ coll |
|
999 |
! |
|
1000 |
||
1001 |
recursiveDo:aOneArgBlock |
|
1002 |
"evaluate a block on each item and all the sub-items" |
|
1003 |
||
1004 |
self do:[:aChild| |
|
1005 |
aOneArgBlock value:aChild. |
|
1006 |
aChild nonCriticalRecursiveDo:aOneArgBlock |
|
1007 |
]. |
|
1008 |
! |
|
1009 |
||
1010 |
recursiveReverseDo:aOneArgBlock |
|
1011 |
"evaluate a block on each item and all the sub-items; |
|
1012 |
proccesing children in reverse direction" |
|
1013 |
||
1014 |
self reverseDo:[:aChild| |
|
1015 |
aChild nonCriticalRecursiveReverseDo:aOneArgBlock. |
|
1016 |
aOneArgBlock value:aChild. |
|
1017 |
]. |
|
1018 |
! |
|
1019 |
||
1020 |
recursiveSelect:aBlock |
|
1021 |
"return a new collection with all children and subChildren from the receiver, for which |
|
1022 |
the argument aBlock evaluates to true." |
|
1023 |
||
1024 |
|coll| |
|
1025 |
||
1026 |
coll := OrderedCollection new. |
|
1027 |
self recursiveDo:[:el| (aBlock value:el) ifTrue:[coll add:el] ]. |
|
1028 |
^ coll |
|
1029 |
! |
|
1030 |
||
1031 |
reverseDo:aOneArgBlock |
|
1032 |
"evaluate a block on each child in reverse direction" |
|
1033 |
||
1034 |
self from:1 reverseDo:aOneArgBlock |
|
1035 |
! |
|
1036 |
||
1037 |
select:aBlock |
|
1038 |
"return a new collection with all items from the receiver, for which |
|
1039 |
the argument aBlock evaluates to true." |
|
1040 |
||
1041 |
|coll| |
|
1042 |
||
1043 |
coll := OrderedCollection new. |
|
1044 |
self do:[:el| (aBlock value:el) ifTrue:[coll add:el] ]. |
|
1045 |
^ coll |
|
1046 |
! |
|
1047 |
||
1048 |
withAllDo:aOneArgBlock |
|
1049 |
"evaluate the block on each item and subitem including self" |
|
1050 |
||
1051 |
aOneArgBlock value:self. |
|
1052 |
||
1053 |
self do:[:el| |
|
1054 |
aOneArgBlock value:el. |
|
1055 |
el nonCriticalRecursiveDo:aOneArgBlock. |
|
1056 |
]. |
|
1057 |
! ! |
|
1058 |
||
1059 |
!AbstractHierarchicalItem methodsFor:'enumerating parents'! |
|
1060 |
||
1061 |
parentsDetect:aBlock |
|
1062 |
"find the first parent, for which evaluation of the block returns |
|
1063 |
true; if none does so, report an error" |
|
1064 |
||
1065 |
^ self parentsDetect:aBlock ifNone:[self errorNotFound] |
|
1066 |
! |
|
1067 |
||
1068 |
parentsDetect:aBlock ifNone:anExceptionBlock |
|
1069 |
"find the first parent, for which evaluation of the block returns |
|
1070 |
true; if none does so, return the evaluation of anExceptionBlock" |
|
1071 |
||
1072 |
|prnt| |
|
1073 |
||
1074 |
prnt := self. |
|
1075 |
||
1076 |
self criticalDo:[ |
|
1077 |
[(prnt := prnt parent) notNil and:[prnt isHierarchicalItem]] whileTrue:[ |
|
1078 |
(aBlock value:prnt) ifTrue:[^ prnt] |
|
1079 |
] |
|
1080 |
]. |
|
1081 |
^ anExceptionBlock value |
|
1082 |
! |
|
1083 |
||
1084 |
parentsDo:aBlock |
|
1085 |
"evaluate a block for each parent" |
|
1086 |
||
1087 |
|prnt| |
|
1088 |
||
1089 |
prnt := self. |
|
1090 |
||
1091 |
self criticalDo:[ |
|
1092 |
[(prnt := prnt parent) notNil and:[prnt isHierarchicalItem]] whileTrue:[ |
|
1093 |
aBlock value:prnt |
|
1094 |
] |
|
1095 |
]. |
|
1096 |
! ! |
|
1097 |
||
1098 |
!AbstractHierarchicalItem methodsFor:'initialization'! |
|
1099 |
||
1100 |
initialize |
|
1101 |
self setExpanded:false |
|
1102 |
! ! |
|
1103 |
||
1104 |
!AbstractHierarchicalItem methodsFor:'private'! |
|
1105 |
||
1106 |
addVisibleChildrenTo:aList |
|
1107 |
"add all visible children and sub-children to the list" |
|
1108 |
||
1109 |
self isExpanded ifFalse:[^ self]. |
|
1110 |
||
1111 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
1112 |
aList add:el. |
|
1113 |
el addVisibleChildrenTo:aList. |
|
1114 |
]. |
|
1115 |
! |
|
1116 |
||
1117 |
clearExpandedWhenLastChildWasRemoved |
|
1118 |
^ true |
|
1119 |
! |
|
1120 |
||
1121 |
criticalDo:aBlock |
|
1122 |
|model| |
|
1123 |
||
1124 |
(model := self model) notNil ifTrue:[ |
|
1125 |
model recursionLock critical:aBlock |
|
1126 |
] ifFalse:[ |
|
1127 |
aBlock value |
|
1128 |
] |
|
1129 |
! |
|
1130 |
||
1131 |
listIndex |
|
1132 |
"returns the visible index or nil; for a non-visible root, 0 is returned" |
|
1133 |
||
1134 |
|index model| |
|
1135 |
||
1136 |
(model := self model) notNil ifTrue:[ |
|
1137 |
index := model identityIndexOf:self. |
|
1138 |
||
1139 |
(index ~~ 0 or:[parent == model]) ifTrue:[ |
|
1140 |
^ index |
|
1141 |
] |
|
1142 |
]. |
|
1143 |
^ nil |
|
1144 |
! |
|
1145 |
||
1146 |
numberOfVisibleChildren |
|
1147 |
"returns number of all visible children including subchildren" |
|
1148 |
||
1149 |
|size| |
|
1150 |
||
1151 |
self isExpanded ifFalse:[^ 0]. |
|
1152 |
size := 0. |
|
1153 |
||
1154 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
1155 |
size := 1 + size + el numberOfVisibleChildren |
|
1156 |
]. |
|
1157 |
^ size |
|
1158 |
! |
|
1159 |
||
1160 |
parentOrModel |
|
1161 |
"returns the parent without checking for item or model" |
|
1162 |
||
1163 |
^ parent |
|
1164 |
! ! |
|
1165 |
||
1166 |
!AbstractHierarchicalItem methodsFor:'private-displaying'! |
|
1167 |
||
1168 |
displayLabel:aLabel h:lH on:aGC x:x y:y h:h |
|
1169 |
<resource: #obsolete> |
|
1170 |
||
1171 |
"display the label at x@y" |
|
1172 |
||
1173 |
"/ obsolete - left in for backward compatibility |
|
1174 |
self displayLabel:aLabel h:lH on:aGC x:x y:y h:h isHighlightedAsSelected:false |
|
1175 |
! |
|
1176 |
||
1177 |
displayLabel:aLabel h:lH on:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
1178 |
"display the label at x@y" |
|
1179 |
||
1180 |
|y0 labelShown| |
|
1181 |
||
1182 |
lH ~~ 0 ifTrue:[ |
|
1183 |
y0 := y - ((lH + 1 - h) // 2). |
|
1184 |
y0 := y0 + (aLabel ascentOn:aGC). |
|
1185 |
||
1186 |
(aLabel isString not |
|
1187 |
or:[(aLabel includes:(Character cr)) not]) ifTrue:[ |
|
1188 |
labelShown := aLabel. |
|
1189 |
isHighlightedAsSelected ifTrue:[ |
|
1190 |
(aLabel isText and:[aLabel hasChangeOfEmphasis]) ifTrue:[ |
|
1191 |
labelShown := Text string:aLabel string emphasisCollection:aLabel emphasis asArray. |
|
1192 |
labelShown emphasisAllRemove:#color. |
|
1193 |
]. |
|
1194 |
]. |
|
1195 |
^ labelShown displayOn:aGC x:x y:y0 |
|
1196 |
]. |
|
1197 |
||
1198 |
labelShown asCollectionOfLines do:[:el| |
|
1199 |
labelShown := el. |
|
1200 |
isHighlightedAsSelected ifTrue:[ |
|
1201 |
(el isText and:[el hasChangeOfEmphasis]) ifTrue:[ |
|
1202 |
labelShown := Text string:el string emphasisCollection:el emphasis asArray. |
|
1203 |
labelShown emphasisAllRemove:#color. |
|
1204 |
]. |
|
1205 |
]. |
|
1206 |
labelShown displayOn:aGC x:x y:y0. |
|
1207 |
y0 := y0 + (el heightOn:aGC) |
|
1208 |
] |
|
1209 |
]. |
|
1210 |
! |
|
1211 |
||
1212 |
heightOf:aLabel on:aGC |
|
1213 |
"returns the height of the label or 0" |
|
1214 |
||
1215 |
|h| |
|
1216 |
||
1217 |
aLabel isSequenceable ifFalse:[ |
|
1218 |
^ aLabel notNil ifTrue:[aLabel heightOn:aGC] ifFalse:[0] |
|
1219 |
]. |
|
1220 |
||
1221 |
aLabel isString ifFalse:[ |
|
1222 |
h := 0. |
|
1223 |
aLabel do:[:el|h := h max:(self heightOf:el on:aGC)]. |
|
4746
25341fd92e38
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4733
diff
changeset
|
1224 |
^ h |
4733 | 1225 |
]. |
1226 |
||
1227 |
h := 1 + (aLabel occurrencesOf:(Character cr)). |
|
1228 |
^ h * (aGC font height) |
|
1229 |
! |
|
1230 |
||
1231 |
widthOf:aLabel on:aGC |
|
1232 |
"returns the height of the label or 0" |
|
1233 |
||
1234 |
|w| |
|
1235 |
||
1236 |
aLabel isSequenceable ifFalse:[ |
|
1237 |
^ aLabel notNil ifTrue:[aLabel widthOn:aGC] ifFalse:[0] |
|
1238 |
]. |
|
1239 |
||
1240 |
aLabel isString ifFalse:[ |
|
1241 |
w := -5. |
|
1242 |
aLabel do:[:el|w := w + 5 + (self widthOf:el on:aGC)]. |
|
1243 |
^ w |
|
1244 |
]. |
|
1245 |
||
1246 |
(aLabel indexOf:(Character cr)) == 0 ifTrue:[ |
|
1247 |
^ aLabel widthOn:aGC |
|
1248 |
]. |
|
1249 |
||
1250 |
w := 0. |
|
1251 |
aLabel asCollectionOfLines do:[:el|w := w max:(el widthOn:aGC)]. |
|
1252 |
^ w |
|
1253 |
! ! |
|
1254 |
||
1255 |
!AbstractHierarchicalItem methodsFor:'private-enumerating'! |
|
1256 |
||
1257 |
nonCriticalDo:aOneArgBlock |
|
1258 |
"evaluate a block noncritical for each child." |
|
1259 |
||
1260 |
^ self nonCriticalFrom:1 to:nil do:aOneArgBlock |
|
1261 |
! |
|
1262 |
||
1263 |
nonCriticalFrom:startIndex to:endIndex do:aOneArgBlock |
|
1264 |
"evaluate a block noncritical for each child starting with the |
|
1265 |
child at startIndex to the endIndex (if nil to end of list)." |
|
1266 |
||
1267 |
|list size resp| |
|
1268 |
||
1269 |
list := self children. |
|
1270 |
size := list size. |
|
1271 |
||
1272 |
startIndex > size ifTrue:[^ nil]. |
|
1273 |
resp := nil. |
|
1274 |
||
1275 |
endIndex notNil ifTrue:[ |
|
1276 |
size := size min:endIndex |
|
1277 |
]. |
|
1278 |
startIndex to:size do:[:i| |
|
1279 |
|item| |
|
1280 |
||
1281 |
item := list at:i ifAbsent:nil. |
|
1282 |
item isNil ifTrue:[^ resp]. |
|
1283 |
resp := aOneArgBlock value:item. |
|
1284 |
]. |
|
1285 |
^ resp |
|
1286 |
! |
|
1287 |
||
1288 |
nonCriticalFrom:startIndex to:endIndex reverseDo:aOneArgBlock |
|
1289 |
"evaluate a block non critical for each child starting with the |
|
1290 |
child at endIndex (if nil to end of list) to startIndex." |
|
1291 |
||
1292 |
|list size resp| |
|
1293 |
||
1294 |
list := self children. |
|
1295 |
size := list size. |
|
1296 |
resp := nil. |
|
1297 |
||
1298 |
endIndex notNil ifTrue:[ |
|
1299 |
size := size min:endIndex |
|
1300 |
]. |
|
1301 |
size to:startIndex by:-1 do:[:i| |
|
1302 |
|item| |
|
1303 |
||
1304 |
item := list at:i ifAbsent:nil. |
|
1305 |
item isNil ifTrue:[^ resp]. |
|
1306 |
resp := aOneArgBlock value:item. |
|
1307 |
]. |
|
1308 |
^ resp |
|
1309 |
! |
|
1310 |
||
1311 |
nonCriticalKeysAndValuesReverseDo:aOneArgBlock |
|
1312 |
"evaluate the argument, aBlock in reverse order for every |
|
1313 |
child, passing both index and element as arguments." |
|
1314 |
||
1315 |
|list size resp| |
|
1316 |
||
1317 |
list := self children. |
|
1318 |
size := list size. |
|
1319 |
resp := nil. |
|
1320 |
||
1321 |
size to:1 by:-1 do:[:i| |
|
1322 |
|item| |
|
1323 |
||
1324 |
item := list at:i ifAbsent:nil. |
|
1325 |
item isNil ifTrue:[^ resp]. |
|
1326 |
resp := aOneArgBlock value:i value:item. |
|
1327 |
]. |
|
1328 |
^ resp |
|
1329 |
! |
|
1330 |
||
1331 |
nonCriticalRecursiveDo:aOneArgBlock |
|
1332 |
"evaluate the block non critical for each item and all the sub-items" |
|
1333 |
||
1334 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1335 |
aOneArgBlock value:eachChild. |
|
1336 |
eachChild nonCriticalRecursiveDo:aOneArgBlock |
|
1337 |
]. |
|
1338 |
! |
|
1339 |
||
1340 |
nonCriticalRecursiveReverseDo:aOneArgBlock |
|
1341 |
"evaluate the block non critical for each item and all the sub-items; |
|
1342 |
proccesing children in reverse direction" |
|
1343 |
||
1344 |
self nonCriticalFrom:1 to:nil reverseDo:[:eachChild| |
|
1345 |
eachChild nonCriticalRecursiveReverseDo:aOneArgBlock. |
|
1346 |
aOneArgBlock value:eachChild. |
|
1347 |
]. |
|
1348 |
! |
|
1349 |
||
1350 |
nonCriticalRecursiveSort:aSortBlock |
|
1351 |
"evaluate a block noncritical for each child." |
|
1352 |
||
1353 |
|unsorted sorted| |
|
1354 |
||
1355 |
unsorted := children. |
|
1356 |
||
1357 |
unsorted size ~~ 0 ifTrue:[ |
|
1358 |
sorted := unsorted sort:aSortBlock. |
|
1359 |
sorted do:[:el| el nonCriticalRecursiveSort:aSortBlock ]. |
|
1360 |
children := sorted. |
|
1361 |
]. |
|
1362 |
! ! |
|
1363 |
||
1364 |
!AbstractHierarchicalItem methodsFor:'private-hierarchy'! |
|
1365 |
||
1366 |
recursiveSetCollapsed |
|
1367 |
"collapse all children and sub-children without notifications" |
|
1368 |
||
1369 |
self criticalDo:[ |
|
1370 |
self recursiveSetCollapsedHelper |
|
1371 |
] |
|
1372 |
! |
|
1373 |
||
1374 |
recursiveSetCollapsedHelper |
|
1375 |
"private helper. |
|
1376 |
collapse all children and sub-children without notifications. |
|
1377 |
Helper only - does not lock" |
|
1378 |
||
1379 |
self setExpanded:false. |
|
1380 |
||
1381 |
"/ do not call #size: children will be autoloaded !!!! |
|
1382 |
children size ~~ 0 ifTrue:[ |
|
1383 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1384 |
eachChild canRecursiveCollapse ifTrue:[ |
|
1385 |
eachChild recursiveSetCollapsedHelper |
|
1386 |
] |
|
1387 |
]. |
|
1388 |
] |
|
1389 |
! |
|
1390 |
||
1391 |
recursiveSetExpandedAndAddToList:aList |
|
1392 |
"expand all children and sub-children without notifications; |
|
1393 |
add children to list" |
|
1394 |
||
1395 |
self criticalDo:[ |
|
1396 |
self recursiveSetExpandedAndAddToListHelper:aList |
|
1397 |
]. |
|
1398 |
! |
|
1399 |
||
1400 |
recursiveSetExpandedAndAddToListHelper:aList |
|
1401 |
"private helper. |
|
1402 |
expand all children and sub-children without notifications; adds children to aList |
|
1403 |
Helper only - does not lock" |
|
1404 |
||
1405 |
self setExpanded:true. |
|
1406 |
||
1407 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1408 |
aList add:eachChild. |
|
1409 |
||
1410 |
eachChild canRecursiveExpand ifTrue:[ |
|
1411 |
eachChild recursiveSetExpandedAndAddToListHelper:aList. |
|
1412 |
]. |
|
1413 |
]. |
|
1414 |
! ! |
|
1415 |
||
1416 |
!AbstractHierarchicalItem methodsFor:'private-to be redefined'! |
|
1417 |
||
1418 |
heightOn:aGC |
|
1419 |
"return the height of the receiver, if it is to be displayed on aGC" |
|
1420 |
||
1421 |
self subclassResponsibility. |
|
1422 |
||
1423 |
"/ could in theory compute it for every draw operation; |
|
1424 |
"/ in practice, this is not a good idea, as it could make drawing very slow |
|
1425 |
"/ (unless that info can be easily computed) |
|
1426 |
||
1427 |
"/ so we force programmers to think about that issue and redefine this method |
|
1428 |
"/ as required. If compuation is really cheap, it can be redefined as: |
|
1429 |
"/ ^ self heightOf:(self label) on:aGC |
|
1430 |
! |
|
1431 |
||
1432 |
isExpanded |
|
1433 |
"returns true if the item is expanded" |
|
1434 |
||
1435 |
self subclassResponsibility |
|
1436 |
! |
|
1437 |
||
1438 |
makeWidthAndHeightUnknown |
|
1439 |
"invalidate any cached with/height information" |
|
1440 |
||
1441 |
"see comments in widthOn/heightOn" |
|
1442 |
self subclassResponsibility |
|
1443 |
! |
|
1444 |
||
1445 |
setExpanded:aBoolean |
|
1446 |
"set expanded flag without any computation or notification. |
|
1447 |
It is left to the subclasses responsibility, where this expanded state is stored; |
|
1448 |
could be in the model (as a list of expanded items), in the item itself (as boolean flag), |
|
1449 |
or somewhere else. |
|
1450 |
For huge trees, it may make sense to not store the expanded flag in a slot |
|
1451 |
(in order to save space). See CompactHierarchicalItem as a clever example of how it can be |
|
1452 |
stored without ANY additional space requirements." |
|
1453 |
||
1454 |
self subclassResponsibility |
|
1455 |
! |
|
1456 |
||
1457 |
widthOn:aGC |
|
1458 |
"return the width of the receiver, if it is to be displayed on aGC" |
|
1459 |
||
1460 |
self subclassResponsibility. |
|
1461 |
||
1462 |
"/ could in theory compute it for every draw operation; |
|
1463 |
"/ in practice, this is not a good idea, as it could make drawing very slow |
|
1464 |
"/ (unless that info can be easily computed) |
|
1465 |
||
1466 |
"/ so we force programmers to think about that issue and redefine this method |
|
1467 |
"/ as required. If compuation is really cheap, it can be redefined as: |
|
1468 |
"/ ^ self widthOf:(self label) on:aGC |
|
1469 |
! ! |
|
1470 |
||
1471 |
!AbstractHierarchicalItem methodsFor:'protocol-accessing'! |
|
1472 |
||
1473 |
children |
|
1474 |
"returns a list of children. When first asked, the list is fetched, if it was |
|
1475 |
built lazyly. |
|
1476 |
*** to optimize: either redefine this or fetchChildren by subClass" |
|
1477 |
||
1478 |
children isNil ifTrue:[ |
|
1479 |
children := self fetchChildren |
|
1480 |
]. |
|
1481 |
^ children |
|
1482 |
! |
|
1483 |
||
1484 |
fetchChildren |
|
1485 |
"should compute the list of children via the model. |
|
1486 |
Be aware, that the somewhat stupid 'optimization' of how the model is fetched may lead to |
|
1487 |
a O(n*log n) or even O(n^2) behavior here. |
|
1488 |
*** to optimize: redefine by subClass" |
|
1489 |
||
1490 |
|model childrenFromModel| |
|
1491 |
||
1492 |
(model := self model) notNil ifTrue:[ |
|
1493 |
childrenFromModel := model childrenFor:self |
|
1494 |
]. |
|
1495 |
^ childrenFromModel |
|
1496 |
! |
|
1497 |
||
1498 |
icon |
|
1499 |
"returns the icon or nil; |
|
1500 |
*** to optimize:redefine by subClass" |
|
1501 |
||
1502 |
|model| |
|
1503 |
||
1504 |
(model := self model) notNil ifTrue:[ |
|
1505 |
^ model iconFor:self |
|
1506 |
]. |
|
1507 |
^ nil |
|
1508 |
! |
|
1509 |
||
1510 |
label |
|
1511 |
"returns the label displayed on aGC; |
|
1512 |
*** to optimize:redefine by subClass" |
|
1513 |
||
1514 |
|model| |
|
1515 |
||
1516 |
(model := self model) notNil ifTrue:[ |
|
1517 |
^ model labelFor:self |
|
1518 |
]. |
|
1519 |
^ nil |
|
1520 |
! |
|
1521 |
||
1522 |
middleButtonMenu |
|
1523 |
"returns the items middleButtonMenu or nil if no menu is defined. |
|
1524 |
If nil is returned, the view is asked for a menu." |
|
1525 |
||
1526 |
<resource: #programMenu> |
|
1527 |
||
1528 |
|model| |
|
1529 |
||
1530 |
(model := self model) notNil ifTrue:[ |
|
1531 |
^ model middleButtonMenuFor:self |
|
1532 |
]. |
|
1533 |
^ nil |
|
1534 |
! |
|
1535 |
||
1536 |
recursiveSortChildren:aSortBlock |
|
1537 |
|children| |
|
1538 |
||
1539 |
(children := self children) notEmptyOrNil ifTrue:[ |
|
1540 |
self criticalDo:[ |
|
1541 |
children sort:aSortBlock. |
|
1542 |
children do:[:aChild| aChild recursiveSortChildren:aSortBlock ] |
|
1543 |
] |
|
1544 |
]. |
|
1545 |
! |
|
1546 |
||
1547 |
sortChildren:aSortBlock |
|
1548 |
"sort the children inplace using the 2-arg block sortBlock for comparison" |
|
1549 |
||
1550 |
<resource: #obsolete> |
|
1551 |
self obsoleteMethodWarning:'use #sort:'. |
|
1552 |
self sort:aSortBlock. |
|
1553 |
! ! |
|
1554 |
||
1555 |
!AbstractHierarchicalItem methodsFor:'protocol-displaying'! |
|
1556 |
||
1557 |
displayIcon:anIcon atX:x y:y on:aGC |
|
1558 |
"called to draw the icon - can be redefined to manipulate the icon" |
|
1559 |
||
1560 |
anIcon displayOn:aGC x:x y:y. |
|
1561 |
! |
|
1562 |
||
1563 |
displayOn:aGC x:x y:y h:h |
|
1564 |
<resource: #obsolete> |
|
1565 |
"draw the receiver in the graphicsContext, aGC" |
|
1566 |
||
1567 |
"/ obsolete - left here for backward compatibility |
|
1568 |
self displayOn:aGC x:x y:y h:h isHighlightedAsSelected:false |
|
1569 |
! |
|
1570 |
||
1571 |
displayOn:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
1572 |
"draw the receiver in the graphicsContext, aGC" |
|
1573 |
||
1574 |
|label |
|
1575 |
x0 "{ Class:SmallInteger }"| |
|
1576 |
||
1577 |
label := self label. |
|
1578 |
label isNil ifTrue:[^ self]. |
|
1579 |
||
1580 |
(label isString or:[label isSequenceable not]) ifTrue:[ |
|
1581 |
^ self displayLabel:label h:(self heightOn:aGC) on:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
1582 |
]. |
|
1583 |
||
1584 |
x0 := x. |
|
1585 |
label do:[:el| |
|
1586 |
el notNil ifTrue:[ |
|
1587 |
self displayLabel:el h:(self heightOf:el on:aGC) on:aGC x:x0 y:y h:h isHighlightedAsSelected:isHighlightedAsSelected. |
|
1588 |
x0 := x0 + 5 + (el widthOn:aGC). |
|
1589 |
]. |
|
1590 |
] |
|
1591 |
! ! |
|
1592 |
||
1593 |
!AbstractHierarchicalItem methodsFor:'protocol-event processing'! |
|
1594 |
||
1595 |
processButtonPress:button visibleX:visX visibleY: visY on: view |
|
1596 |
"A mouse button was pressed on myself. The visX/visY coordinates |
|
1597 |
are relative to the viewOrigin. |
|
1598 |
||
1599 |
If this method returns TRUE, the other method |
|
1600 |
#processButtonPress:x:y:on: IS NOT CALLED !!!!!! |
|
1601 |
" |
|
1602 |
||
1603 |
^false |
|
1604 |
||
1605 |
"Created: / 18-04-2013 / 09:56:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1606 |
"Modified (comment): / 18-04-2013 / 11:04:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1607 |
! |
|
1608 |
||
1609 |
processButtonPress:button x:x y:y |
|
1610 |
"a mouse button was pressed in my label. |
|
1611 |
Return true, if the event is eaten (ignored by the gc). |
|
1612 |
By default, false is returned (should be handled by the gc)." |
|
1613 |
||
1614 |
^ false |
|
1615 |
! |
|
1616 |
||
1617 |
processButtonPress:button x:x y:y on:aGC |
|
1618 |
"a mouse button was pressed in my label. |
|
1619 |
Return true, if the event is eaten (ignored by the gc). |
|
1620 |
By default, false is returned (should be handled by the gc)." |
|
1621 |
||
1622 |
^ self processButtonPress:button x:x y:y |
|
1623 |
! |
|
1624 |
||
1625 |
processButtonPressOnIcon:button on:aGC |
|
1626 |
"a mouse button was pressed in my icon. |
|
1627 |
Return true, if the event is eaten (ignored by the gc). |
|
1628 |
By default, false is returned (should be handled by the gc)." |
|
1629 |
||
1630 |
^ false |
|
1631 |
! ! |
|
1632 |
||
1633 |
!AbstractHierarchicalItem methodsFor:'protocol-monitoring'! |
|
1634 |
||
1635 |
monitoringCycle |
|
1636 |
"called every 'n' seconds by the model, if the monitoring |
|
1637 |
cycle is enabled. The item can perform some checks, .. |
|
1638 |
**** can be redefined by subclass to perform some actions |
|
1639 |
" |
|
1640 |
! ! |
|
1641 |
||
1642 |
!AbstractHierarchicalItem methodsFor:'protocol-queries'! |
|
1643 |
||
1644 |
canCollapse |
|
1645 |
"called before collapsing the item; can be redefined |
|
1646 |
by subclass to omit the collapse operation" |
|
1647 |
||
1648 |
^ self isExpanded |
|
1649 |
! |
|
1650 |
||
1651 |
canExpand |
|
1652 |
"called before expanding the item; can be redefined |
|
1653 |
by subclass to omit the expand operation" |
|
1654 |
||
1655 |
^ self hasChildren |
|
1656 |
! |
|
1657 |
||
1658 |
canRecursiveCollapse |
|
1659 |
"called before collapsing the item; can be redefined |
|
1660 |
by subclass to omit the collapse operation " |
|
1661 |
||
1662 |
^ self canCollapse |
|
1663 |
! |
|
1664 |
||
1665 |
canRecursiveExpand |
|
1666 |
"called before expanding the item; can be redefined |
|
1667 |
by subclass to omit the collapse operation" |
|
1668 |
||
1669 |
^ self canExpand |
|
1670 |
! |
|
1671 |
||
1672 |
drawHorizontalLineUpToText |
|
1673 |
"draw the horizizontal line for the selected item up to the text |
|
1674 |
or on default to the start of the the vertical line; only used by |
|
1675 |
the hierarchical view |
|
1676 |
" |
|
1677 |
^ false |
|
1678 |
! |
|
1679 |
||
1680 |
hasChildren |
|
1681 |
"checks whether the item has children; |
|
1682 |
the list needs not to be loaded yet( example. FileDirectory ). |
|
1683 |
*** to optimize: redefine in subClass" |
|
1684 |
||
1685 |
^ self children size ~~ 0 |
|
1686 |
! |
|
1687 |
||
1688 |
hasIndicator |
|
1689 |
"on default the indicator is drawn if the item has children" |
|
1690 |
||
1691 |
^ self hasChildren |
|
1692 |
! |
|
1693 |
||
1694 |
isSelectable |
|
1695 |
"returns true if the item is selectable. Can be redefined in subclasses" |
|
1696 |
||
1697 |
^ true |
|
1698 |
! |
|
1699 |
||
1700 |
string |
|
1701 |
"access the printable string used for stepping through a list |
|
1702 |
searching for an entry starting with a character. |
|
1703 |
*** to optimize:redefine by subClass" |
|
1704 |
||
1705 |
|label| |
|
1706 |
||
1707 |
(label := self label) notNil ifTrue:[ |
|
1708 |
label isString ifTrue:[ ^ label string ]. |
|
1709 |
label isImageOrForm ifTrue:[ ^ nil ]. |
|
1710 |
||
1711 |
label isSequenceable ifFalse:[ |
|
1712 |
^ label perform:#string ifNotUnderstood:nil |
|
1713 |
]. |
|
1714 |
||
1715 |
label do:[:el||s| |
|
1716 |
(el notNil and:[el isImageOrForm not]) ifTrue:[ |
|
1717 |
s := el perform:#string ifNotUnderstood:nil. |
|
1718 |
s notNil ifTrue:[^ s]. |
|
1719 |
] |
|
1720 |
] |
|
1721 |
]. |
|
1722 |
^ nil |
|
1723 |
! ! |
|
1724 |
||
1725 |
!AbstractHierarchicalItem methodsFor:'queries'! |
|
1726 |
||
1727 |
isChildOf:anItem |
|
1728 |
"returns true if the item is a child of anItem" |
|
1729 |
||
1730 |
|item| |
|
1731 |
||
1732 |
item := self. |
|
1733 |
||
1734 |
[anItem ~~ item] whileTrue:[ |
|
1735 |
((item := item parent) notNil and:[item isHierarchicalItem]) ifFalse:[ |
|
1736 |
^ false |
|
1737 |
] |
|
1738 |
]. |
|
1739 |
^ true |
|
1740 |
! |
|
1741 |
||
1742 |
isCollapsed |
|
1743 |
"returns true if the item is collapsed" |
|
1744 |
||
1745 |
^ self isExpanded not |
|
1746 |
! |
|
1747 |
||
1748 |
isDirectoryItem |
|
1749 |
^ false |
|
1750 |
||
1751 |
"Created: / 23-06-2006 / 12:47:05 / fm" |
|
1752 |
"Modified: / 23-02-2007 / 12:04:23 / User" |
|
1753 |
! |
|
1754 |
||
1755 |
isHierarchicalItem |
|
1756 |
"used to decide if the parent is a hierarchical item or the model" |
|
1757 |
||
1758 |
^ true |
|
1759 |
! |
|
1760 |
||
1761 |
isRealChildOf:anItem |
|
1762 |
"returns true if the item is a child of anItem" |
|
1763 |
||
1764 |
|item| |
|
1765 |
item := self parent. |
|
1766 |
||
1767 |
[item notNil] whileTrue:[ |
|
1768 |
item == anItem ifTrue:[^ true]. |
|
1769 |
item := item parent. |
|
1770 |
]. |
|
1771 |
^ false |
|
1772 |
! |
|
1773 |
||
1774 |
isRootItem |
|
1775 |
"returns true if the item is the root item" |
|
1776 |
||
1777 |
^ parent isHierarchicalItem not |
|
1778 |
! |
|
1779 |
||
1780 |
size |
|
1781 |
"return the number of children" |
|
1782 |
||
1783 |
^ self children size |
|
1784 |
! ! |
|
1785 |
||
1786 |
!AbstractHierarchicalItem methodsFor:'searching'! |
|
1787 |
||
1788 |
detect:aOneArgBlock |
|
1789 |
"find the first child, for which evaluation of the block returns |
|
1790 |
true; if none does so, report an error" |
|
1791 |
||
1792 |
^ self detect:aOneArgBlock ifNone:[self errorNotFound] |
|
1793 |
! |
|
1794 |
||
1795 |
detect:aOneArgBlock ifNone:exceptionValue |
|
1796 |
"find the first child, for which evaluation of the block returns |
|
1797 |
true; if none does so, return the value of anExceptionValue" |
|
1798 |
||
1799 |
self do:[:el| |
|
1800 |
(aOneArgBlock value:el) ifTrue:[^ el] |
|
1801 |
]. |
|
1802 |
^ exceptionValue value |
|
1803 |
! |
|
1804 |
||
1805 |
detectLast:aOneArgBlock |
|
1806 |
"find the last child, for which evaluation of the block returns |
|
1807 |
true; if none does so, an exception is raised" |
|
1808 |
||
1809 |
^ self detectLast:aOneArgBlock ifNone:[self errorNotFound] |
|
1810 |
! |
|
1811 |
||
1812 |
detectLast:aOneArgBlock ifNone:anExceptionValue |
|
1813 |
"find the last child, for which evaluation of the block returns |
|
1814 |
true; if none does so, return the value of anExceptionValue" |
|
1815 |
||
1816 |
self reverseDo:[:el| (aOneArgBlock value:el) ifTrue:[^ el] ]. |
|
1817 |
^ anExceptionValue value |
|
1818 |
! |
|
1819 |
||
1820 |
findFirst:aOneArgBlock |
|
1821 |
"find the first child, for which evaluation of the argument, aOneArgBlock |
|
1822 |
returns true; return its index or 0 if none detected." |
|
1823 |
||
1824 |
||
1825 |
self keysAndValuesDo:[:i :el| (aOneArgBlock value:el) ifTrue:[^ i] ]. |
|
1826 |
^ 0 |
|
1827 |
! |
|
1828 |
||
1829 |
findLast:anOneArgBlock |
|
1830 |
"find the last child, for which evaluation of the argument, aOneArgBlock |
|
1831 |
returns true; return its index or 0 if none detected." |
|
1832 |
||
1833 |
self keysAndValuesReverseDo:[:i :el| (anOneArgBlock value:el) ifTrue:[^ i] ]. |
|
1834 |
^ 0 |
|
1835 |
! |
|
1836 |
||
1837 |
identityIndexOf:aChild |
|
1838 |
"return the index of aChild or 0 if not found. Compare using ==" |
|
1839 |
||
1840 |
^ self identityIndexOf:aChild startingAt:1 |
|
1841 |
! |
|
1842 |
||
1843 |
identityIndexOf:aChild startingAt:startIndex |
|
1844 |
"return the index of aChild, starting search at startIndex. |
|
1845 |
Compare using ==; return 0 if not found" |
|
1846 |
||
1847 |
|index| |
|
1848 |
||
1849 |
index := startIndex. |
|
1850 |
||
1851 |
self from:startIndex do:[:el| |
|
1852 |
el == aChild ifTrue:[^ index ]. |
|
1853 |
index := index + 1. |
|
1854 |
]. |
|
1855 |
^ 0 |
|
1856 |
! |
|
1857 |
||
1858 |
recursiveDetect:aOneArgBlock |
|
1859 |
"recursive find the first child, for which evaluation |
|
1860 |
of the block returns true; if none, nil is returned. |
|
1861 |
Warning: this only searches in already visible child elements |
|
1862 |
i.e. any collapsed items are not searched." |
|
1863 |
||
1864 |
self recursiveDo:[:aChild| |
|
1865 |
(aOneArgBlock value:aChild) ifTrue:[^ aChild] |
|
1866 |
]. |
|
1867 |
^ nil |
|
1868 |
! |
|
1869 |
||
1870 |
recursiveDetectLast:aBlock |
|
1871 |
"find the last child, for which evaluation of the block returns |
|
1872 |
true; if none does so, nil id returned" |
|
1873 |
||
1874 |
self recursiveReverseDo:[:aChild| |
|
1875 |
(aBlock value:aChild) ifTrue:[^ aChild]. |
|
1876 |
]. |
|
1877 |
^ nil |
|
1878 |
! |
|
1879 |
||
1880 |
withAllDetect:aOneArgBlock |
|
1881 |
"recursive find the first item including self, for which evaluation |
|
1882 |
of the block returns true; if none nil is returned" |
|
1883 |
||
1884 |
(aOneArgBlock value:self) ifTrue:[^ self]. |
|
1885 |
||
1886 |
^ self recursiveDetect:aOneArgBlock |
|
1887 |
! ! |
|
1888 |
||
1889 |
!AbstractHierarchicalItem methodsFor:'sorting & reordering'! |
|
1890 |
||
1891 |
recursiveSort:aSortBlock |
|
1892 |
"recursive sort the children inplace using the 2-arg block sortBlock for comparison" |
|
1893 |
||
1894 |
self criticalDo:[ |
|
1895 |
children notEmptyOrNil ifTrue:[ |
|
1896 |
self nonCriticalRecursiveSort:aSortBlock. |
|
1897 |
self childrenOrderChanged. |
|
1898 |
] |
|
1899 |
]. |
|
1900 |
! |
|
1901 |
||
1902 |
sort:aSortBlock |
|
1903 |
"sort the children inplace using the 2-arg block sortBlock for comparison" |
|
1904 |
||
1905 |
children notEmptyOrNil ifTrue: [ |
|
1906 |
self criticalDo:[ |
|
1907 |
"/ check again (asynchronous update was possible before) |
|
1908 |
children notEmptyOrNil ifTrue: [ |
|
1909 |
children := children sort:aSortBlock. |
|
1910 |
self childrenOrderChanged. |
|
1911 |
] |
|
1912 |
]. |
|
1913 |
] |
|
1914 |
! ! |
|
1915 |
||
1916 |
!AbstractHierarchicalItem class methodsFor:'documentation'! |
|
1917 |
||
1918 |
version |
|
4746
25341fd92e38
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4733
diff
changeset
|
1919 |
^ '$Header: /cvs/stx/stx/libwidg2/AbstractHierarchicalItem.st,v 1.2 2015-05-02 14:46:44 cg Exp $' |
4733 | 1920 |
! |
1921 |
||
1922 |
version_CVS |
|
4746
25341fd92e38
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4733
diff
changeset
|
1923 |
^ '$Header: /cvs/stx/stx/libwidg2/AbstractHierarchicalItem.st,v 1.2 2015-05-02 14:46:44 cg Exp $' |
4733 | 1924 |
! ! |
1925 |