author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sun, 23 Jun 2019 23:04:24 +0000 | |
branch | jv |
changeset 6071 | 5182891c9a14 |
parent 5363 | 4e25de452cea |
child 5461 | d259ac0f3ba6 |
permissions | -rw-r--r-- |
4733 | 1 |
" |
2 |
COPYRIGHT (c) 1999/2015 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libwidg2' }" |
|
13 |
||
14 |
"{ NameSpace: Smalltalk }" |
|
15 |
||
16 |
Object subclass:#AbstractHierarchicalItem |
|
17 |
instanceVariableNames:'parent children' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Views-Support' |
|
21 |
! |
|
22 |
||
23 |
!AbstractHierarchicalItem class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1999/2015 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Hierarchical Items are mostly like Models, but the list of |
|
42 |
dependencies are kept by its HierarchicalList. |
|
43 |
The class is used to build up hierarchical trees. |
|
44 |
||
45 |
2015 update: |
|
46 |
the original HierarchicalItem has been refactored into this abstract class, |
|
47 |
which provides all the mechanisms, but leaves the concrete representation |
|
48 |
of some slots open. |
|
49 |
These are: |
|
50 |
- if and how the geometry information (width + height) are cached, |
|
51 |
- if and how the expanded-state is remembered. |
|
52 |
- if and how the underlying model is fetched |
|
53 |
||
54 |
The old class used private slots for the first three (width-height-isExpanded), |
|
55 |
but did not keep a reference to the model. This leads to a very poor performance, |
|
56 |
as many algorithms degenerated to O(n log(n)) or even O(n^2) time behavior, |
|
57 |
as the model was fetched by walking along the parent chain - sometimes for every item |
|
58 |
in a long list. |
|
59 |
||
60 |
The old class is still around and may be used for small trees, |
|
61 |
but we recommend rewriting applications to use the new CompactHierarchicalItem |
|
62 |
class, which behaves the same on the outside, but uses clever tricks to be both more |
|
63 |
space efficient (saving 2 slots) and time efficient (caching the model). |
|
4749
13a7e6db823b
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4746
diff
changeset
|
64 |
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
|
65 |
directly, but uses the getters isExpanded, width and height and the setters setExpanded:, width: and height: |
4733 | 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 |
|
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
707 |
] ifFalse:[ |
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
708 |
size == 0 ifTrue:[ |
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
709 |
children := OrderedCollection new |
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
710 |
]. |
4733 | 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 |
|
5363 | 891 |
"for each child in the receiver (non recursive), evaluate the argument, aBlock |
4733 | 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 |
|
5363 | 899 |
|
900 |
"Modified (comment): / 25-11-2016 / 08:42:19 / cg" |
|
4733 | 901 |
! |
902 |
||
903 |
contains:aBlock |
|
5363 | 904 |
"evaluate aOneArgBlock for each of the receiver's children (non recursive). |
4733 | 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 |
|
5362 | 910 |
|
5363 | 911 |
"Modified (comment): / 25-11-2016 / 08:43:31 / cg" |
4733 | 912 |
! |
913 |
||
914 |
do:aOneArgBlock |
|
5363 | 915 |
"evaluate a block for each child (non recursive)" |
4733 | 916 |
|
917 |
self from:1 to:nil do:aOneArgBlock |
|
5363 | 918 |
|
919 |
"Modified (comment): / 25-11-2016 / 08:43:36 / cg" |
|
4733 | 920 |
! |
921 |
||
922 |
from:startIndex do:aOneArgBlock |
|
923 |
"evaluate a block on each child starting with the |
|
5362 | 924 |
child at startIndex to the end." |
925 |
||
4733 | 926 |
self from:startIndex to:nil do:aOneArgBlock |
5362 | 927 |
|
928 |
"Modified (comment): / 25-11-2016 / 07:45:55 / cg" |
|
4733 | 929 |
! |
930 |
||
931 |
from:startIndex reverseDo:aOneArgBlock |
|
5363 | 932 |
"evaluate a block on each child (non recursive) starting at end to the startIndex" |
5362 | 933 |
|
4733 | 934 |
self from:startIndex to:nil reverseDo:aOneArgBlock |
5362 | 935 |
|
5363 | 936 |
"Modified (comment): / 25-11-2016 / 08:43:42 / cg" |
4733 | 937 |
! |
938 |
||
939 |
from:startIndex to:endIndex do:aOneArgBlock |
|
5363 | 940 |
"evaluate a block on each child (non recursive), |
941 |
starting with the child at startIndex to the endIndex." |
|
942 |
||
4733 | 943 |
|res| |
944 |
||
945 |
self size < startIndex ifTrue:[^ self "nil"]. |
|
946 |
res := nil. |
|
947 |
||
948 |
self criticalDo:[ |
|
949 |
res := self nonCriticalFrom:startIndex to:endIndex do:aOneArgBlock |
|
950 |
]. |
|
951 |
"/ ^ res - return the receiver, as all other collections do |
|
5363 | 952 |
|
953 |
"Modified (comment): / 25-11-2016 / 08:43:51 / cg" |
|
4733 | 954 |
! |
955 |
||
956 |
from:startIndex to:endIndex reverseDo:aOneArgBlock |
|
5363 | 957 |
"evaluate a block on each child (non recursive), |
958 |
starting with the child at endIndex to the startIndex." |
|
4733 | 959 |
|
960 |
|res| |
|
961 |
||
962 |
self size < startIndex ifTrue:[^ self "nil"]. |
|
963 |
res := nil. |
|
964 |
||
965 |
self criticalDo:[ |
|
966 |
res := self nonCriticalFrom:startIndex to:endIndex reverseDo:aOneArgBlock |
|
967 |
]. |
|
968 |
"/ ^ res - return the receiver, as all other collections do |
|
5363 | 969 |
|
970 |
"Modified (comment): / 25-11-2016 / 08:43:58 / cg" |
|
4733 | 971 |
! |
972 |
||
973 |
keysAndValuesDo:aTwoArgBlock |
|
5363 | 974 |
"evaluate the argument, aBlock for every child (non recursive), |
4733 | 975 |
passing both index and element as arguments." |
976 |
||
977 |
|key res| |
|
978 |
||
979 |
key := 1. |
|
980 |
res := nil. |
|
981 |
||
982 |
self do:[:el| |
|
983 |
res := el value:key value:el. |
|
984 |
key := key + 1. |
|
985 |
]. |
|
986 |
"/ ^ res - no: return the receiver, as all other collections do |
|
5363 | 987 |
|
988 |
"Modified (comment): / 25-11-2016 / 08:44:03 / cg" |
|
4733 | 989 |
! |
990 |
||
991 |
keysAndValuesReverseDo:aTwoArgBlock |
|
5363 | 992 |
"evaluate the argument, aBlock in reverse order for every child (non recursive), |
5362 | 993 |
passing both index and element as arguments." |
4733 | 994 |
|
995 |
|res| |
|
996 |
||
997 |
self size == 0 ifTrue:[^ self "nil"]. |
|
998 |
res := nil. |
|
999 |
||
1000 |
self criticalDo:[ |
|
1001 |
res := self nonCriticalKeysAndValuesReverseDo:aTwoArgBlock |
|
1002 |
]. |
|
1003 |
"/ ^ res - no: return the receiver, as all other collections do |
|
5362 | 1004 |
|
5363 | 1005 |
"Modified (comment): / 25-11-2016 / 08:44:06 / cg" |
4733 | 1006 |
! |
1007 |
||
1008 |
recursiveCollect:aBlock |
|
1009 |
"for each child in the receiver, evaluate the argument, aBlock |
|
5363 | 1010 |
and return a new collection with the results. |
1011 |
Warning: this only enumerates already visible child elements |
|
1012 |
i.e. any collapsed items are not visited." |
|
4733 | 1013 |
|
1014 |
|coll| |
|
1015 |
||
1016 |
coll := OrderedCollection new. |
|
1017 |
self recursiveDo:[:el| coll add:(aBlock value:el) ]. |
|
1018 |
^ coll |
|
5363 | 1019 |
|
1020 |
"Modified (comment): / 25-11-2016 / 08:43:20 / cg" |
|
4733 | 1021 |
! |
1022 |
||
1023 |
recursiveDo:aOneArgBlock |
|
5363 | 1024 |
"evaluate a block on each item and all the sub-items. |
1025 |
Warning: this only enumerates already visible child elements |
|
1026 |
i.e. any collapsed items are not visited." |
|
4733 | 1027 |
|
1028 |
self do:[:aChild| |
|
1029 |
aOneArgBlock value:aChild. |
|
1030 |
aChild nonCriticalRecursiveDo:aOneArgBlock |
|
1031 |
]. |
|
5363 | 1032 |
|
1033 |
"Modified (comment): / 25-11-2016 / 08:43:04 / cg" |
|
4733 | 1034 |
! |
1035 |
||
1036 |
recursiveReverseDo:aOneArgBlock |
|
1037 |
"evaluate a block on each item and all the sub-items; |
|
5363 | 1038 |
proccesing children in reverse direction. |
1039 |
Warning: this only enumerates already visible child elements |
|
1040 |
i.e. any collapsed items are not visited." |
|
4733 | 1041 |
|
1042 |
self reverseDo:[:aChild| |
|
1043 |
aChild nonCriticalRecursiveReverseDo:aOneArgBlock. |
|
1044 |
aOneArgBlock value:aChild. |
|
1045 |
]. |
|
5363 | 1046 |
|
1047 |
"Modified (comment): / 25-11-2016 / 08:43:09 / cg" |
|
4733 | 1048 |
! |
1049 |
||
1050 |
recursiveSelect:aBlock |
|
5362 | 1051 |
"return a new collection with all children and subChildren from the receiver, |
5363 | 1052 |
for which the argument aBlock evaluates to true. |
1053 |
Warning: this only enumerates already visible child elements |
|
1054 |
i.e. any collapsed items are not visited." |
|
4733 | 1055 |
|
1056 |
|coll| |
|
1057 |
||
1058 |
coll := OrderedCollection new. |
|
1059 |
self recursiveDo:[:el| (aBlock value:el) ifTrue:[coll add:el] ]. |
|
1060 |
^ coll |
|
5362 | 1061 |
|
5363 | 1062 |
"Modified (comment): / 25-11-2016 / 08:43:12 / cg" |
4733 | 1063 |
! |
1064 |
||
1065 |
reverseDo:aOneArgBlock |
|
5363 | 1066 |
"evaluate a block on each child (non recursive) in reverse direction" |
4733 | 1067 |
|
1068 |
self from:1 reverseDo:aOneArgBlock |
|
5363 | 1069 |
|
1070 |
"Modified (comment): / 25-11-2016 / 08:44:12 / cg" |
|
4733 | 1071 |
! |
1072 |
||
1073 |
select:aBlock |
|
5363 | 1074 |
"return a new collection with all items from the receiver (non recursive), |
1075 |
for which the argument aBlock evaluates to true." |
|
4733 | 1076 |
|
1077 |
|coll| |
|
1078 |
||
1079 |
coll := OrderedCollection new. |
|
1080 |
self do:[:el| (aBlock value:el) ifTrue:[coll add:el] ]. |
|
1081 |
^ coll |
|
5363 | 1082 |
|
1083 |
"Modified (comment): / 25-11-2016 / 08:44:20 / cg" |
|
4733 | 1084 |
! |
1085 |
||
1086 |
withAllDo:aOneArgBlock |
|
5363 | 1087 |
"recursively evaluate aOneArgBlock on each item and subitem including self" |
4733 | 1088 |
|
1089 |
aOneArgBlock value:self. |
|
1090 |
||
1091 |
self do:[:el| |
|
1092 |
aOneArgBlock value:el. |
|
1093 |
el nonCriticalRecursiveDo:aOneArgBlock. |
|
1094 |
]. |
|
5363 | 1095 |
|
1096 |
"Modified (comment): / 25-11-2016 / 08:44:49 / cg" |
|
4733 | 1097 |
! ! |
1098 |
||
1099 |
!AbstractHierarchicalItem methodsFor:'enumerating parents'! |
|
1100 |
||
1101 |
parentsDetect:aBlock |
|
1102 |
"find the first parent, for which evaluation of the block returns |
|
1103 |
true; if none does so, report an error" |
|
1104 |
||
1105 |
^ self parentsDetect:aBlock ifNone:[self errorNotFound] |
|
1106 |
! |
|
1107 |
||
1108 |
parentsDetect:aBlock ifNone:anExceptionBlock |
|
1109 |
"find the first parent, for which evaluation of the block returns |
|
1110 |
true; if none does so, return the evaluation of anExceptionBlock" |
|
1111 |
||
1112 |
|prnt| |
|
1113 |
||
1114 |
prnt := self. |
|
1115 |
||
1116 |
self criticalDo:[ |
|
1117 |
[(prnt := prnt parent) notNil and:[prnt isHierarchicalItem]] whileTrue:[ |
|
1118 |
(aBlock value:prnt) ifTrue:[^ prnt] |
|
1119 |
] |
|
1120 |
]. |
|
1121 |
^ anExceptionBlock value |
|
1122 |
! |
|
1123 |
||
1124 |
parentsDo:aBlock |
|
1125 |
"evaluate a block for each parent" |
|
1126 |
||
1127 |
|prnt| |
|
1128 |
||
1129 |
prnt := self. |
|
1130 |
||
1131 |
self criticalDo:[ |
|
1132 |
[(prnt := prnt parent) notNil and:[prnt isHierarchicalItem]] whileTrue:[ |
|
1133 |
aBlock value:prnt |
|
1134 |
] |
|
1135 |
]. |
|
1136 |
! ! |
|
1137 |
||
1138 |
!AbstractHierarchicalItem methodsFor:'initialization'! |
|
1139 |
||
1140 |
initialize |
|
1141 |
self setExpanded:false |
|
1142 |
! ! |
|
1143 |
||
1144 |
!AbstractHierarchicalItem methodsFor:'private'! |
|
1145 |
||
1146 |
addVisibleChildrenTo:aList |
|
1147 |
"add all visible children and sub-children to the list" |
|
1148 |
||
1149 |
self isExpanded ifFalse:[^ self]. |
|
1150 |
||
1151 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
1152 |
aList add:el. |
|
1153 |
el addVisibleChildrenTo:aList. |
|
1154 |
]. |
|
1155 |
! |
|
1156 |
||
1157 |
clearExpandedWhenLastChildWasRemoved |
|
1158 |
^ true |
|
1159 |
! |
|
1160 |
||
1161 |
criticalDo:aBlock |
|
1162 |
|model| |
|
1163 |
||
1164 |
(model := self model) notNil ifTrue:[ |
|
1165 |
model recursionLock critical:aBlock |
|
1166 |
] ifFalse:[ |
|
1167 |
aBlock value |
|
1168 |
] |
|
1169 |
! |
|
1170 |
||
1171 |
listIndex |
|
1172 |
"returns the visible index or nil; for a non-visible root, 0 is returned" |
|
1173 |
||
1174 |
|index model| |
|
1175 |
||
1176 |
(model := self model) notNil ifTrue:[ |
|
1177 |
index := model identityIndexOf:self. |
|
1178 |
||
1179 |
(index ~~ 0 or:[parent == model]) ifTrue:[ |
|
1180 |
^ index |
|
1181 |
] |
|
1182 |
]. |
|
1183 |
^ nil |
|
1184 |
! |
|
1185 |
||
1186 |
numberOfVisibleChildren |
|
1187 |
"returns number of all visible children including subchildren" |
|
1188 |
||
1189 |
|size| |
|
1190 |
||
1191 |
self isExpanded ifFalse:[^ 0]. |
|
1192 |
size := 0. |
|
1193 |
||
1194 |
self nonCriticalFrom:1 to:nil do:[:el| |
|
1195 |
size := 1 + size + el numberOfVisibleChildren |
|
1196 |
]. |
|
1197 |
^ size |
|
1198 |
! |
|
1199 |
||
1200 |
parentOrModel |
|
1201 |
"returns the parent without checking for item or model" |
|
1202 |
||
1203 |
^ parent |
|
1204 |
! ! |
|
1205 |
||
1206 |
!AbstractHierarchicalItem methodsFor:'private-displaying'! |
|
1207 |
||
1208 |
displayLabel:aLabel h:lH on:aGC x:x y:y h:h |
|
1209 |
<resource: #obsolete> |
|
1210 |
||
1211 |
"display the label at x@y" |
|
1212 |
||
1213 |
"/ obsolete - left in for backward compatibility |
|
1214 |
self displayLabel:aLabel h:lH on:aGC x:x y:y h:h isHighlightedAsSelected:false |
|
1215 |
! |
|
1216 |
||
1217 |
displayLabel:aLabel h:lH on:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
1218 |
"display the label at x@y" |
|
1219 |
||
4758
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1220 |
|y0| |
4733 | 1221 |
|
1222 |
lH ~~ 0 ifTrue:[ |
|
1223 |
y0 := y - ((lH + 1 - h) // 2). |
|
1224 |
y0 := y0 + (aLabel ascentOn:aGC). |
|
1225 |
||
1226 |
(aLabel isString not |
|
1227 |
or:[(aLabel includes:(Character cr)) not]) ifTrue:[ |
|
4758
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1228 |
|labelShown| |
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1229 |
|
4733 | 1230 |
labelShown := aLabel. |
1231 |
isHighlightedAsSelected ifTrue:[ |
|
1232 |
(aLabel isText and:[aLabel hasChangeOfEmphasis]) ifTrue:[ |
|
1233 |
labelShown := Text string:aLabel string emphasisCollection:aLabel emphasis asArray. |
|
1234 |
labelShown emphasisAllRemove:#color. |
|
1235 |
]. |
|
1236 |
]. |
|
1237 |
^ labelShown displayOn:aGC x:x y:y0 |
|
1238 |
]. |
|
1239 |
||
4758
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1240 |
aLabel asCollectionOfLines do:[:el| |
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1241 |
|labelShown| |
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1242 |
|
4733 | 1243 |
labelShown := el. |
1244 |
isHighlightedAsSelected ifTrue:[ |
|
1245 |
(el isText and:[el hasChangeOfEmphasis]) ifTrue:[ |
|
1246 |
labelShown := Text string:el string emphasisCollection:el emphasis asArray. |
|
1247 |
labelShown emphasisAllRemove:#color. |
|
1248 |
]. |
|
1249 |
]. |
|
1250 |
labelShown displayOn:aGC x:x y:y0. |
|
1251 |
y0 := y0 + (el heightOn:aGC) |
|
1252 |
] |
|
1253 |
]. |
|
1254 |
! |
|
1255 |
||
1256 |
heightOf:aLabel on:aGC |
|
1257 |
"returns the height of the label or 0" |
|
1258 |
||
1259 |
|h| |
|
1260 |
||
1261 |
aLabel isSequenceable ifFalse:[ |
|
1262 |
^ aLabel notNil ifTrue:[aLabel heightOn:aGC] ifFalse:[0] |
|
1263 |
]. |
|
1264 |
||
1265 |
aLabel isString ifFalse:[ |
|
1266 |
h := 0. |
|
1267 |
aLabel do:[:el|h := h max:(self heightOf:el on:aGC)]. |
|
4746
25341fd92e38
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4733
diff
changeset
|
1268 |
^ h |
4733 | 1269 |
]. |
1270 |
||
1271 |
h := 1 + (aLabel occurrencesOf:(Character cr)). |
|
1272 |
^ h * (aGC font height) |
|
1273 |
! |
|
1274 |
||
1275 |
widthOf:aLabel on:aGC |
|
1276 |
"returns the height of the label or 0" |
|
1277 |
||
1278 |
|w| |
|
1279 |
||
1280 |
aLabel isSequenceable ifFalse:[ |
|
1281 |
^ aLabel notNil ifTrue:[aLabel widthOn:aGC] ifFalse:[0] |
|
1282 |
]. |
|
1283 |
||
1284 |
aLabel isString ifFalse:[ |
|
1285 |
w := -5. |
|
1286 |
aLabel do:[:el|w := w + 5 + (self widthOf:el on:aGC)]. |
|
1287 |
^ w |
|
1288 |
]. |
|
1289 |
||
1290 |
(aLabel indexOf:(Character cr)) == 0 ifTrue:[ |
|
1291 |
^ aLabel widthOn:aGC |
|
1292 |
]. |
|
1293 |
||
1294 |
w := 0. |
|
1295 |
aLabel asCollectionOfLines do:[:el|w := w max:(el widthOn:aGC)]. |
|
1296 |
^ w |
|
1297 |
! ! |
|
1298 |
||
1299 |
!AbstractHierarchicalItem methodsFor:'private-enumerating'! |
|
1300 |
||
1301 |
nonCriticalDo:aOneArgBlock |
|
1302 |
"evaluate a block noncritical for each child." |
|
1303 |
||
1304 |
^ self nonCriticalFrom:1 to:nil do:aOneArgBlock |
|
1305 |
! |
|
1306 |
||
1307 |
nonCriticalFrom:startIndex to:endIndex do:aOneArgBlock |
|
1308 |
"evaluate a block noncritical for each child starting with the |
|
1309 |
child at startIndex to the endIndex (if nil to end of list)." |
|
1310 |
||
1311 |
|list size resp| |
|
1312 |
||
1313 |
list := self children. |
|
1314 |
size := list size. |
|
1315 |
||
1316 |
startIndex > size ifTrue:[^ nil]. |
|
1317 |
resp := nil. |
|
1318 |
||
1319 |
endIndex notNil ifTrue:[ |
|
1320 |
size := size min:endIndex |
|
1321 |
]. |
|
1322 |
startIndex to:size do:[:i| |
|
1323 |
|item| |
|
1324 |
||
1325 |
item := list at:i ifAbsent:nil. |
|
1326 |
item isNil ifTrue:[^ resp]. |
|
1327 |
resp := aOneArgBlock value:item. |
|
1328 |
]. |
|
1329 |
^ resp |
|
1330 |
! |
|
1331 |
||
1332 |
nonCriticalFrom:startIndex to:endIndex reverseDo:aOneArgBlock |
|
1333 |
"evaluate a block non critical for each child starting with the |
|
1334 |
child at endIndex (if nil to end of list) to startIndex." |
|
1335 |
||
1336 |
|list size resp| |
|
1337 |
||
1338 |
list := self children. |
|
1339 |
size := list size. |
|
1340 |
resp := nil. |
|
1341 |
||
1342 |
endIndex notNil ifTrue:[ |
|
1343 |
size := size min:endIndex |
|
1344 |
]. |
|
1345 |
size to:startIndex by:-1 do:[:i| |
|
1346 |
|item| |
|
1347 |
||
1348 |
item := list at:i ifAbsent:nil. |
|
1349 |
item isNil ifTrue:[^ resp]. |
|
1350 |
resp := aOneArgBlock value:item. |
|
1351 |
]. |
|
1352 |
^ resp |
|
1353 |
! |
|
1354 |
||
1355 |
nonCriticalKeysAndValuesReverseDo:aOneArgBlock |
|
1356 |
"evaluate the argument, aBlock in reverse order for every |
|
1357 |
child, passing both index and element as arguments." |
|
1358 |
||
1359 |
|list size resp| |
|
1360 |
||
1361 |
list := self children. |
|
1362 |
size := list size. |
|
1363 |
resp := nil. |
|
1364 |
||
1365 |
size to:1 by:-1 do:[:i| |
|
1366 |
|item| |
|
1367 |
||
1368 |
item := list at:i ifAbsent:nil. |
|
1369 |
item isNil ifTrue:[^ resp]. |
|
1370 |
resp := aOneArgBlock value:i value:item. |
|
1371 |
]. |
|
1372 |
^ resp |
|
1373 |
! |
|
1374 |
||
1375 |
nonCriticalRecursiveDo:aOneArgBlock |
|
1376 |
"evaluate the block non critical for each item and all the sub-items" |
|
1377 |
||
1378 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1379 |
aOneArgBlock value:eachChild. |
|
1380 |
eachChild nonCriticalRecursiveDo:aOneArgBlock |
|
1381 |
]. |
|
1382 |
! |
|
1383 |
||
1384 |
nonCriticalRecursiveReverseDo:aOneArgBlock |
|
1385 |
"evaluate the block non critical for each item and all the sub-items; |
|
1386 |
proccesing children in reverse direction" |
|
1387 |
||
1388 |
self nonCriticalFrom:1 to:nil reverseDo:[:eachChild| |
|
1389 |
eachChild nonCriticalRecursiveReverseDo:aOneArgBlock. |
|
1390 |
aOneArgBlock value:eachChild. |
|
1391 |
]. |
|
1392 |
! |
|
1393 |
||
1394 |
nonCriticalRecursiveSort:aSortBlock |
|
1395 |
"evaluate a block noncritical for each child." |
|
1396 |
||
1397 |
|unsorted sorted| |
|
1398 |
||
1399 |
unsorted := children. |
|
1400 |
||
1401 |
unsorted size ~~ 0 ifTrue:[ |
|
1402 |
sorted := unsorted sort:aSortBlock. |
|
1403 |
sorted do:[:el| el nonCriticalRecursiveSort:aSortBlock ]. |
|
1404 |
children := sorted. |
|
1405 |
]. |
|
1406 |
! ! |
|
1407 |
||
1408 |
!AbstractHierarchicalItem methodsFor:'private-hierarchy'! |
|
1409 |
||
1410 |
recursiveSetCollapsed |
|
1411 |
"collapse all children and sub-children without notifications" |
|
1412 |
||
1413 |
self criticalDo:[ |
|
1414 |
self recursiveSetCollapsedHelper |
|
1415 |
] |
|
1416 |
! |
|
1417 |
||
1418 |
recursiveSetCollapsedHelper |
|
1419 |
"private helper. |
|
1420 |
collapse all children and sub-children without notifications. |
|
1421 |
Helper only - does not lock" |
|
1422 |
||
1423 |
self setExpanded:false. |
|
1424 |
||
1425 |
"/ do not call #size: children will be autoloaded !!!! |
|
1426 |
children size ~~ 0 ifTrue:[ |
|
1427 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1428 |
eachChild canRecursiveCollapse ifTrue:[ |
|
1429 |
eachChild recursiveSetCollapsedHelper |
|
1430 |
] |
|
1431 |
]. |
|
1432 |
] |
|
1433 |
! |
|
1434 |
||
1435 |
recursiveSetExpandedAndAddToList:aList |
|
1436 |
"expand all children and sub-children without notifications; |
|
1437 |
add children to list" |
|
1438 |
||
1439 |
self criticalDo:[ |
|
1440 |
self recursiveSetExpandedAndAddToListHelper:aList |
|
1441 |
]. |
|
1442 |
! |
|
1443 |
||
1444 |
recursiveSetExpandedAndAddToListHelper:aList |
|
1445 |
"private helper. |
|
1446 |
expand all children and sub-children without notifications; adds children to aList |
|
1447 |
Helper only - does not lock" |
|
1448 |
||
1449 |
self setExpanded:true. |
|
1450 |
||
1451 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1452 |
aList add:eachChild. |
|
1453 |
||
1454 |
eachChild canRecursiveExpand ifTrue:[ |
|
1455 |
eachChild recursiveSetExpandedAndAddToListHelper:aList. |
|
1456 |
]. |
|
1457 |
]. |
|
1458 |
! ! |
|
1459 |
||
1460 |
!AbstractHierarchicalItem methodsFor:'private-to be redefined'! |
|
1461 |
||
4751
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1462 |
fetchChildren |
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1463 |
^ nil |
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1464 |
! |
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1465 |
|
4733 | 1466 |
heightOn:aGC |
1467 |
"return the height of the receiver, if it is to be displayed on aGC" |
|
1468 |
||
1469 |
self subclassResponsibility. |
|
1470 |
||
1471 |
"/ could in theory compute it for every draw operation; |
|
1472 |
"/ in practice, this is not a good idea, as it could make drawing very slow |
|
1473 |
"/ (unless that info can be easily computed) |
|
1474 |
||
1475 |
"/ so we force programmers to think about that issue and redefine this method |
|
1476 |
"/ as required. If compuation is really cheap, it can be redefined as: |
|
1477 |
"/ ^ self heightOf:(self label) on:aGC |
|
1478 |
! |
|
1479 |
||
1480 |
isExpanded |
|
1481 |
"returns true if the item is expanded" |
|
1482 |
||
1483 |
self subclassResponsibility |
|
1484 |
! |
|
1485 |
||
1486 |
makeWidthAndHeightUnknown |
|
1487 |
"invalidate any cached with/height information" |
|
1488 |
||
1489 |
"see comments in widthOn/heightOn" |
|
1490 |
self subclassResponsibility |
|
1491 |
! |
|
1492 |
||
1493 |
setExpanded:aBoolean |
|
1494 |
"set expanded flag without any computation or notification. |
|
1495 |
It is left to the subclasses responsibility, where this expanded state is stored; |
|
1496 |
could be in the model (as a list of expanded items), in the item itself (as boolean flag), |
|
1497 |
or somewhere else. |
|
1498 |
For huge trees, it may make sense to not store the expanded flag in a slot |
|
1499 |
(in order to save space). See CompactHierarchicalItem as a clever example of how it can be |
|
1500 |
stored without ANY additional space requirements." |
|
1501 |
||
1502 |
self subclassResponsibility |
|
1503 |
! |
|
1504 |
||
1505 |
widthOn:aGC |
|
1506 |
"return the width of the receiver, if it is to be displayed on aGC" |
|
1507 |
||
1508 |
self subclassResponsibility. |
|
1509 |
||
1510 |
"/ could in theory compute it for every draw operation; |
|
1511 |
"/ in practice, this is not a good idea, as it could make drawing very slow |
|
1512 |
"/ (unless that info can be easily computed) |
|
1513 |
||
1514 |
"/ so we force programmers to think about that issue and redefine this method |
|
1515 |
"/ as required. If compuation is really cheap, it can be redefined as: |
|
1516 |
"/ ^ self widthOf:(self label) on:aGC |
|
1517 |
! ! |
|
1518 |
||
1519 |
!AbstractHierarchicalItem methodsFor:'protocol-accessing'! |
|
1520 |
||
1521 |
children |
|
1522 |
"returns a list of children. When first asked, the list is fetched, if it was |
|
5116 | 1523 |
built lazily. |
4733 | 1524 |
*** to optimize: either redefine this or fetchChildren by subClass" |
1525 |
||
1526 |
children isNil ifTrue:[ |
|
1527 |
children := self fetchChildren |
|
1528 |
]. |
|
1529 |
^ children |
|
1530 |
! |
|
1531 |
||
1532 |
icon |
|
1533 |
"returns the icon or nil; |
|
1534 |
*** to optimize:redefine by subClass" |
|
1535 |
||
1536 |
|model| |
|
1537 |
||
1538 |
(model := self model) notNil ifTrue:[ |
|
1539 |
^ model iconFor:self |
|
1540 |
]. |
|
1541 |
^ nil |
|
1542 |
! |
|
1543 |
||
1544 |
label |
|
1545 |
"returns the label displayed on aGC; |
|
1546 |
*** to optimize:redefine by subClass" |
|
1547 |
||
1548 |
|model| |
|
1549 |
||
1550 |
(model := self model) notNil ifTrue:[ |
|
1551 |
^ model labelFor:self |
|
1552 |
]. |
|
1553 |
^ nil |
|
1554 |
! |
|
1555 |
||
1556 |
middleButtonMenu |
|
1557 |
"returns the items middleButtonMenu or nil if no menu is defined. |
|
1558 |
If nil is returned, the view is asked for a menu." |
|
1559 |
||
1560 |
<resource: #programMenu> |
|
1561 |
||
1562 |
|model| |
|
1563 |
||
1564 |
(model := self model) notNil ifTrue:[ |
|
1565 |
^ model middleButtonMenuFor:self |
|
1566 |
]. |
|
1567 |
^ nil |
|
1568 |
! |
|
1569 |
||
1570 |
recursiveSortChildren:aSortBlock |
|
1571 |
|children| |
|
1572 |
||
1573 |
(children := self children) notEmptyOrNil ifTrue:[ |
|
1574 |
self criticalDo:[ |
|
1575 |
children sort:aSortBlock. |
|
1576 |
children do:[:aChild| aChild recursiveSortChildren:aSortBlock ] |
|
1577 |
] |
|
1578 |
]. |
|
1579 |
! |
|
1580 |
||
1581 |
sortChildren:aSortBlock |
|
1582 |
"sort the children inplace using the 2-arg block sortBlock for comparison" |
|
1583 |
||
1584 |
<resource: #obsolete> |
|
1585 |
self obsoleteMethodWarning:'use #sort:'. |
|
1586 |
self sort:aSortBlock. |
|
1587 |
! ! |
|
1588 |
||
1589 |
!AbstractHierarchicalItem methodsFor:'protocol-displaying'! |
|
1590 |
||
1591 |
displayIcon:anIcon atX:x y:y on:aGC |
|
1592 |
"called to draw the icon - can be redefined to manipulate the icon" |
|
1593 |
||
1594 |
anIcon displayOn:aGC x:x y:y. |
|
1595 |
! |
|
1596 |
||
1597 |
displayOn:aGC x:x y:y h:h |
|
1598 |
<resource: #obsolete> |
|
1599 |
"draw the receiver in the graphicsContext, aGC" |
|
1600 |
||
1601 |
"/ obsolete - left here for backward compatibility |
|
1602 |
self displayOn:aGC x:x y:y h:h isHighlightedAsSelected:false |
|
1603 |
! |
|
1604 |
||
1605 |
displayOn:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
1606 |
"draw the receiver in the graphicsContext, aGC" |
|
1607 |
||
1608 |
|label |
|
1609 |
x0 "{ Class:SmallInteger }"| |
|
1610 |
||
1611 |
label := self label. |
|
1612 |
label isNil ifTrue:[^ self]. |
|
1613 |
||
5325 | 1614 |
label isNonByteCollection ifFalse:[ |
4733 | 1615 |
^ self displayLabel:label h:(self heightOn:aGC) on:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
1616 |
]. |
|
1617 |
||
1618 |
x0 := x. |
|
1619 |
label do:[:el| |
|
1620 |
el notNil ifTrue:[ |
|
1621 |
self displayLabel:el h:(self heightOf:el on:aGC) on:aGC x:x0 y:y h:h isHighlightedAsSelected:isHighlightedAsSelected. |
|
1622 |
x0 := x0 + 5 + (el widthOn:aGC). |
|
1623 |
]. |
|
1624 |
] |
|
1625 |
! ! |
|
1626 |
||
1627 |
!AbstractHierarchicalItem methodsFor:'protocol-event processing'! |
|
1628 |
||
1629 |
processButtonPress:button visibleX:visX visibleY: visY on: view |
|
1630 |
"A mouse button was pressed on myself. The visX/visY coordinates |
|
1631 |
are relative to the viewOrigin. |
|
1632 |
||
1633 |
If this method returns TRUE, the other method |
|
1634 |
#processButtonPress:x:y:on: IS NOT CALLED !!!!!! |
|
1635 |
" |
|
1636 |
||
1637 |
^false |
|
1638 |
||
1639 |
"Created: / 18-04-2013 / 09:56:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1640 |
"Modified (comment): / 18-04-2013 / 11:04:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1641 |
! |
|
1642 |
||
1643 |
processButtonPress:button x:x y:y |
|
1644 |
"a mouse button was pressed in my label. |
|
1645 |
Return true, if the event is eaten (ignored by the gc). |
|
1646 |
By default, false is returned (should be handled by the gc)." |
|
1647 |
||
1648 |
^ false |
|
1649 |
! |
|
1650 |
||
1651 |
processButtonPress:button x:x y:y on:aGC |
|
1652 |
"a mouse button was pressed in my label. |
|
1653 |
Return true, if the event is eaten (ignored by the gc). |
|
1654 |
By default, false is returned (should be handled by the gc)." |
|
1655 |
||
1656 |
^ self processButtonPress:button x:x y:y |
|
1657 |
! |
|
1658 |
||
1659 |
processButtonPressOnIcon:button on:aGC |
|
1660 |
"a mouse button was pressed in my icon. |
|
1661 |
Return true, if the event is eaten (ignored by the gc). |
|
1662 |
By default, false is returned (should be handled by the gc)." |
|
1663 |
||
1664 |
^ false |
|
1665 |
! ! |
|
1666 |
||
1667 |
!AbstractHierarchicalItem methodsFor:'protocol-monitoring'! |
|
1668 |
||
1669 |
monitoringCycle |
|
1670 |
"called every 'n' seconds by the model, if the monitoring |
|
1671 |
cycle is enabled. The item can perform some checks, .. |
|
1672 |
**** can be redefined by subclass to perform some actions |
|
1673 |
" |
|
1674 |
! ! |
|
1675 |
||
1676 |
!AbstractHierarchicalItem methodsFor:'protocol-queries'! |
|
1677 |
||
1678 |
canCollapse |
|
1679 |
"called before collapsing the item; can be redefined |
|
1680 |
by subclass to omit the collapse operation" |
|
1681 |
||
1682 |
^ self isExpanded |
|
1683 |
! |
|
1684 |
||
1685 |
canExpand |
|
1686 |
"called before expanding the item; can be redefined |
|
1687 |
by subclass to omit the expand operation" |
|
1688 |
||
1689 |
^ self hasChildren |
|
1690 |
! |
|
1691 |
||
1692 |
canRecursiveCollapse |
|
1693 |
"called before collapsing the item; can be redefined |
|
1694 |
by subclass to omit the collapse operation " |
|
1695 |
||
1696 |
^ self canCollapse |
|
1697 |
! |
|
1698 |
||
1699 |
canRecursiveExpand |
|
1700 |
"called before expanding the item; can be redefined |
|
1701 |
by subclass to omit the collapse operation" |
|
1702 |
||
1703 |
^ self canExpand |
|
1704 |
! |
|
1705 |
||
1706 |
drawHorizontalLineUpToText |
|
1707 |
"draw the horizizontal line for the selected item up to the text |
|
1708 |
or on default to the start of the the vertical line; only used by |
|
1709 |
the hierarchical view |
|
1710 |
" |
|
1711 |
^ false |
|
1712 |
! |
|
1713 |
||
1714 |
hasChildren |
|
1715 |
"checks whether the item has children; |
|
1716 |
the list needs not to be loaded yet( example. FileDirectory ). |
|
1717 |
*** to optimize: redefine in subClass" |
|
1718 |
||
1719 |
^ self children size ~~ 0 |
|
1720 |
! |
|
1721 |
||
1722 |
hasIndicator |
|
1723 |
"on default the indicator is drawn if the item has children" |
|
1724 |
||
1725 |
^ self hasChildren |
|
1726 |
! |
|
1727 |
||
1728 |
isSelectable |
|
1729 |
"returns true if the item is selectable. Can be redefined in subclasses" |
|
1730 |
||
1731 |
^ true |
|
1732 |
! |
|
1733 |
||
1734 |
string |
|
1735 |
"access the printable string used for stepping through a list |
|
1736 |
searching for an entry starting with a character. |
|
1737 |
*** to optimize:redefine by subClass" |
|
1738 |
||
1739 |
|label| |
|
1740 |
||
1741 |
(label := self label) notNil ifTrue:[ |
|
1742 |
label isString ifTrue:[ ^ label string ]. |
|
1743 |
label isImageOrForm ifTrue:[ ^ nil ]. |
|
1744 |
||
1745 |
label isSequenceable ifFalse:[ |
|
1746 |
^ label perform:#string ifNotUnderstood:nil |
|
1747 |
]. |
|
1748 |
||
1749 |
label do:[:el||s| |
|
1750 |
(el notNil and:[el isImageOrForm not]) ifTrue:[ |
|
1751 |
s := el perform:#string ifNotUnderstood:nil. |
|
1752 |
s notNil ifTrue:[^ s]. |
|
1753 |
] |
|
1754 |
] |
|
1755 |
]. |
|
1756 |
^ nil |
|
1757 |
! ! |
|
1758 |
||
1759 |
!AbstractHierarchicalItem methodsFor:'queries'! |
|
1760 |
||
1761 |
isChildOf:anItem |
|
1762 |
"returns true if the item is a child of anItem" |
|
1763 |
||
1764 |
|item| |
|
1765 |
||
1766 |
item := self. |
|
1767 |
||
1768 |
[anItem ~~ item] whileTrue:[ |
|
1769 |
((item := item parent) notNil and:[item isHierarchicalItem]) ifFalse:[ |
|
1770 |
^ false |
|
1771 |
] |
|
1772 |
]. |
|
1773 |
^ true |
|
1774 |
! |
|
1775 |
||
1776 |
isCollapsed |
|
1777 |
"returns true if the item is collapsed" |
|
1778 |
||
1779 |
^ self isExpanded not |
|
1780 |
! |
|
1781 |
||
1782 |
isDirectoryItem |
|
1783 |
^ false |
|
1784 |
||
1785 |
"Created: / 23-06-2006 / 12:47:05 / fm" |
|
1786 |
"Modified: / 23-02-2007 / 12:04:23 / User" |
|
1787 |
! |
|
1788 |
||
1789 |
isHierarchicalItem |
|
1790 |
"used to decide if the parent is a hierarchical item or the model" |
|
1791 |
||
1792 |
^ true |
|
1793 |
! |
|
1794 |
||
1795 |
isRealChildOf:anItem |
|
1796 |
"returns true if the item is a child of anItem" |
|
1797 |
||
1798 |
|item| |
|
1799 |
item := self parent. |
|
1800 |
||
1801 |
[item notNil] whileTrue:[ |
|
1802 |
item == anItem ifTrue:[^ true]. |
|
1803 |
item := item parent. |
|
1804 |
]. |
|
1805 |
^ false |
|
1806 |
! |
|
1807 |
||
1808 |
isRootItem |
|
1809 |
"returns true if the item is the root item" |
|
1810 |
||
1811 |
^ parent isHierarchicalItem not |
|
1812 |
! |
|
1813 |
||
1814 |
size |
|
1815 |
"return the number of children" |
|
1816 |
||
1817 |
^ self children size |
|
1818 |
! ! |
|
1819 |
||
1820 |
!AbstractHierarchicalItem methodsFor:'searching'! |
|
1821 |
||
1822 |
detect:aOneArgBlock |
|
5363 | 1823 |
"find the first child (not recursive), for which evaluation of the block returns |
4733 | 1824 |
true; if none does so, report an error" |
1825 |
||
1826 |
^ self detect:aOneArgBlock ifNone:[self errorNotFound] |
|
5363 | 1827 |
|
1828 |
"Modified (comment): / 25-11-2016 / 08:40:16 / cg" |
|
4733 | 1829 |
! |
1830 |
||
1831 |
detect:aOneArgBlock ifNone:exceptionValue |
|
5363 | 1832 |
"find the first child (not recursive), for which evaluation of the block returns |
4733 | 1833 |
true; if none does so, return the value of anExceptionValue" |
1834 |
||
1835 |
self do:[:el| |
|
1836 |
(aOneArgBlock value:el) ifTrue:[^ el] |
|
1837 |
]. |
|
1838 |
^ exceptionValue value |
|
5363 | 1839 |
|
1840 |
"Modified (comment): / 25-11-2016 / 08:40:18 / cg" |
|
4733 | 1841 |
! |
1842 |
||
1843 |
detectLast:aOneArgBlock |
|
5363 | 1844 |
"find the last child (not recursive), for which evaluation of the block returns |
4733 | 1845 |
true; if none does so, an exception is raised" |
1846 |
||
1847 |
^ self detectLast:aOneArgBlock ifNone:[self errorNotFound] |
|
5363 | 1848 |
|
1849 |
"Modified (comment): / 25-11-2016 / 08:40:21 / cg" |
|
4733 | 1850 |
! |
1851 |
||
1852 |
detectLast:aOneArgBlock ifNone:anExceptionValue |
|
5363 | 1853 |
"find the last child (not recursive), for which evaluation of the block returns |
4733 | 1854 |
true; if none does so, return the value of anExceptionValue" |
1855 |
||
1856 |
self reverseDo:[:el| (aOneArgBlock value:el) ifTrue:[^ el] ]. |
|
1857 |
^ anExceptionValue value |
|
5363 | 1858 |
|
1859 |
"Modified (comment): / 25-11-2016 / 08:40:23 / cg" |
|
4733 | 1860 |
! |
1861 |
||
1862 |
findFirst:aOneArgBlock |
|
5363 | 1863 |
"find the first child (not recursive), for which evaluation of the argument, aOneArgBlock |
4733 | 1864 |
returns true; return its index or 0 if none detected." |
1865 |
||
1866 |
||
1867 |
self keysAndValuesDo:[:i :el| (aOneArgBlock value:el) ifTrue:[^ i] ]. |
|
1868 |
^ 0 |
|
5363 | 1869 |
|
1870 |
"Modified (comment): / 25-11-2016 / 08:40:25 / cg" |
|
4733 | 1871 |
! |
1872 |
||
5363 | 1873 |
findLast:aOneArgBlock |
1874 |
"find the last child (not recursive), for which evaluation of the argument, aOneArgBlock |
|
4733 | 1875 |
returns true; return its index or 0 if none detected." |
1876 |
||
5363 | 1877 |
self keysAndValuesReverseDo:[:i :el| (aOneArgBlock value:el) ifTrue:[^ i] ]. |
4733 | 1878 |
^ 0 |
5363 | 1879 |
|
1880 |
"Modified (format): / 25-11-2016 / 08:40:33 / cg" |
|
4733 | 1881 |
! |
1882 |
||
1883 |
identityIndexOf:aChild |
|
1884 |
"return the index of aChild or 0 if not found. Compare using ==" |
|
1885 |
||
1886 |
^ self identityIndexOf:aChild startingAt:1 |
|
1887 |
! |
|
1888 |
||
1889 |
identityIndexOf:aChild startingAt:startIndex |
|
1890 |
"return the index of aChild, starting search at startIndex. |
|
1891 |
Compare using ==; return 0 if not found" |
|
1892 |
||
1893 |
|index| |
|
1894 |
||
1895 |
index := startIndex. |
|
1896 |
||
1897 |
self from:startIndex do:[:el| |
|
1898 |
el == aChild ifTrue:[^ index ]. |
|
1899 |
index := index + 1. |
|
1900 |
]. |
|
1901 |
^ 0 |
|
1902 |
! |
|
1903 |
||
1904 |
recursiveDetect:aOneArgBlock |
|
1905 |
"recursive find the first child, for which evaluation |
|
1906 |
of the block returns true; if none, nil is returned. |
|
1907 |
Warning: this only searches in already visible child elements |
|
1908 |
i.e. any collapsed items are not searched." |
|
1909 |
||
1910 |
self recursiveDo:[:aChild| |
|
1911 |
(aOneArgBlock value:aChild) ifTrue:[^ aChild] |
|
1912 |
]. |
|
1913 |
^ nil |
|
1914 |
! |
|
1915 |
||
1916 |
recursiveDetectLast:aBlock |
|
5363 | 1917 |
"recursive find the last child, for which evaluation of the block returns true; |
1918 |
if none does so, nil is returned. |
|
1919 |
Warning: this only searches in already visible child elements |
|
1920 |
i.e. any collapsed items are not searched." |
|
4733 | 1921 |
|
1922 |
self recursiveReverseDo:[:aChild| |
|
1923 |
(aBlock value:aChild) ifTrue:[^ aChild]. |
|
1924 |
]. |
|
1925 |
^ nil |
|
5363 | 1926 |
|
1927 |
"Modified (comment): / 25-11-2016 / 08:41:42 / cg" |
|
4733 | 1928 |
! |
1929 |
||
1930 |
withAllDetect:aOneArgBlock |
|
5363 | 1931 |
"recursive find the first item including self, |
1932 |
for which evaluation of the block returns true; if none nil is returned. |
|
1933 |
Warning: this only searches in already visible child elements |
|
1934 |
i.e. any collapsed items are not searched." |
|
4733 | 1935 |
|
1936 |
(aOneArgBlock value:self) ifTrue:[^ self]. |
|
1937 |
||
1938 |
^ self recursiveDetect:aOneArgBlock |
|
5363 | 1939 |
|
1940 |
"Modified (comment): / 25-11-2016 / 08:41:53 / cg" |
|
4733 | 1941 |
! ! |
1942 |
||
1943 |
!AbstractHierarchicalItem methodsFor:'sorting & reordering'! |
|
1944 |
||
1945 |
recursiveSort:aSortBlock |
|
1946 |
"recursive sort the children inplace using the 2-arg block sortBlock for comparison" |
|
1947 |
||
1948 |
self criticalDo:[ |
|
1949 |
children notEmptyOrNil ifTrue:[ |
|
1950 |
self nonCriticalRecursiveSort:aSortBlock. |
|
1951 |
self childrenOrderChanged. |
|
1952 |
] |
|
1953 |
]. |
|
1954 |
! |
|
1955 |
||
1956 |
sort:aSortBlock |
|
1957 |
"sort the children inplace using the 2-arg block sortBlock for comparison" |
|
1958 |
||
1959 |
children notEmptyOrNil ifTrue: [ |
|
1960 |
self criticalDo:[ |
|
1961 |
"/ check again (asynchronous update was possible before) |
|
1962 |
children notEmptyOrNil ifTrue: [ |
|
1963 |
children := children sort:aSortBlock. |
|
1964 |
self childrenOrderChanged. |
|
1965 |
] |
|
1966 |
]. |
|
1967 |
] |
|
1968 |
! ! |
|
1969 |
||
1970 |
!AbstractHierarchicalItem class methodsFor:'documentation'! |
|
1971 |
||
1972 |
version |
|
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
1973 |
^ '$Header$' |
4733 | 1974 |
! |
1975 |
||
1976 |
version_CVS |
|
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
1977 |
^ '$Header$' |
4733 | 1978 |
! ! |
1979 |