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