author | Claus Gittinger <cg@exept.de> |
Sun, 03 Mar 2019 22:54:48 +0100 | |
changeset 6022 | ccbc2453e82d |
parent 6019 | 3f48805b8cac |
child 6026 | 1a089d0cbdcc |
permissions | -rw-r--r-- |
6009 | 1 |
"{ Encoding: utf8 }" |
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:] |
|
5740 | 74 |
Claus Atzkern |
4733 | 75 |
Claus Gittinger (redesign and refactoring) |
76 |
||
77 |
[see also:] |
|
78 |
HierarchicalItem (the old item class) |
|
79 |
HierarchicalList (typical model) |
|
80 |
HierarchicalListView (typical user of me) |
|
81 |
" |
|
82 |
! ! |
|
83 |
||
84 |
!AbstractHierarchicalItem class methodsFor:'instance creation'! |
|
85 |
||
86 |
new |
|
87 |
^ (self basicNew) initialize |
|
88 |
! |
|
89 |
||
90 |
parent:aParent |
|
91 |
|item| |
|
92 |
||
93 |
item := self new. |
|
94 |
item parent:aParent. |
|
95 |
^ item |
|
96 |
! ! |
|
97 |
||
98 |
!AbstractHierarchicalItem class methodsFor:'protocol'! |
|
99 |
||
100 |
doResetExtentOnChange |
|
101 |
"true: the extent of the item is reset if a change |
|
102 |
notification is raised from the item. the default is true |
|
103 |
" |
|
104 |
^ true |
|
105 |
! ! |
|
106 |
||
5461 | 107 |
!AbstractHierarchicalItem class methodsFor:'queries'! |
108 |
||
109 |
isAbstract |
|
110 |
"Return if this class is an abstract class. |
|
111 |
True is returned here for myself only; false for subclasses. |
|
112 |
Abstract subclasses must redefine this again." |
|
113 |
||
114 |
^ self == AbstractHierarchicalItem. |
|
115 |
! ! |
|
116 |
||
4733 | 117 |
!AbstractHierarchicalItem methodsFor:'accessing'! |
118 |
||
119 |
getChildren |
|
5996 | 120 |
"returns the children as they are present (or not); |
121 |
not going to the model, and especially not creating autocreated children..." |
|
4733 | 122 |
|
123 |
^ children |
|
5996 | 124 |
|
125 |
"Modified (comment): / 12-02-2019 / 17:41:06 / Claus Gittinger" |
|
4733 | 126 |
! |
127 |
||
128 |
level |
|
129 |
"returns the level starting with 0 for the root" |
|
130 |
||
131 |
|item level| |
|
132 |
||
133 |
item := self. |
|
134 |
level := 0. |
|
135 |
||
136 |
[ (item := item parentOrModel) notNil] whileTrue:[ |
|
137 |
level := level + 1. |
|
5739 | 138 |
level > 10000 ifTrue:[ |
139 |
self halt:'possibly recursive item hierarchy'. |
|
140 |
^ self. |
|
4733 | 141 |
]. |
142 |
]. |
|
143 |
^ level |
|
144 |
||
145 |
"Modified: / 09-07-2010 / 08:56:27 / cg" |
|
146 |
! |
|
147 |
||
6000 | 148 |
nextSibling |
149 |
"returns the next child of my parent or nil" |
|
150 |
||
151 |
|siblings myIndex| |
|
152 |
||
153 |
(parent notNil and:[parent isHierarchicalItem]) ifTrue:[ |
|
154 |
siblings := parent getChildren. |
|
155 |
myIndex := siblings identityIndexOf:self. |
|
156 |
myIndex ~~ 0 ifTrue:[ |
|
157 |
^ siblings at:myIndex+1 ifAbsent:nil |
|
158 |
]. |
|
159 |
]. |
|
160 |
^ nil |
|
161 |
||
162 |
"Created: / 13-02-2019 / 15:17:39 / Claus Gittinger" |
|
163 |
! |
|
164 |
||
4733 | 165 |
parent |
166 |
"returns the parent or nil" |
|
167 |
||
5865 | 168 |
|p| |
5870 | 169 |
|
170 |
p := parent. |
|
171 |
^ (p notNil and:[p isHierarchicalItem]) |
|
5865 | 172 |
ifTrue:[p] |
4733 | 173 |
ifFalse:[nil] |
5865 | 174 |
|
5870 | 175 |
"Modified: / 01-08-2018 / 10:18:37 / Claus Gittinger" |
176 |
"Modified: / 01-08-2018 / 16:15:07 / Stefan Vogel" |
|
4733 | 177 |
! |
178 |
||
179 |
parent:aParent |
|
180 |
"set the parent (or the model if the item is the root item)" |
|
181 |
||
182 |
parent := aParent |
|
183 |
! |
|
184 |
||
6000 | 185 |
previousSibling |
186 |
"returns the previous child of my parent or nil" |
|
187 |
||
188 |
|siblings myIndex| |
|
189 |
||
190 |
(parent notNil and:[parent isHierarchicalItem]) ifTrue:[ |
|
191 |
siblings := parent getChildren. |
|
192 |
myIndex := siblings identityIndexOf:self. |
|
193 |
myIndex ~~ 0 ifTrue:[ |
|
194 |
^ siblings at:myIndex-1 ifAbsent:nil |
|
195 |
]. |
|
196 |
]. |
|
197 |
^ nil |
|
198 |
||
199 |
"Created: / 13-02-2019 / 15:17:57 / Claus Gittinger" |
|
200 |
! |
|
201 |
||
4733 | 202 |
rootItem |
203 |
"returns the root item" |
|
204 |
||
205 |
parent isHierarchicalItem ifTrue:[ |
|
206 |
^ parent rootItem |
|
207 |
]. |
|
208 |
^ self |
|
209 |
! ! |
|
210 |
||
211 |
!AbstractHierarchicalItem methodsFor:'accessing-children'! |
|
212 |
||
213 |
at:anIndex |
|
214 |
"return the child at anIndex if valid; |
|
215 |
if the index is invalid, nil is returned" |
|
216 |
||
217 |
^ self at:anIndex ifAbsent:nil |
|
218 |
! |
|
219 |
||
220 |
at:anIndex ifAbsent:exceptionBlock |
|
221 |
"return the child at anIndex if valid; if the index is |
|
222 |
invalid, the result of evaluating the exceptionBlock is returned." |
|
223 |
||
224 |
|list| |
|
225 |
||
226 |
(list := self children) notNil ifTrue:[ |
|
227 |
^ list at:anIndex ifAbsent:exceptionBlock |
|
228 |
]. |
|
229 |
^ exceptionBlock value |
|
230 |
! |
|
231 |
||
232 |
at:anIndex put:anItem |
|
233 |
"replace a child by a new item. return anItem (sigh)" |
|
234 |
||
235 |
|children oldItem visIndex model expFlag| |
|
236 |
||
237 |
anItem isNil ifTrue:[ |
|
238 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
239 |
^ nil |
|
240 |
]. |
|
241 |
anItem parent:self. |
|
242 |
||
243 |
(model := self model) isNil ifTrue:[ |
|
244 |
self children at:anIndex put:anItem. |
|
245 |
^ anItem |
|
246 |
]. |
|
247 |
||
5840 | 248 |
model synchronized:[ |
4733 | 249 |
children := self children. |
250 |
oldItem := children at:anIndex. |
|
251 |
||
252 |
oldItem isExpanded ifTrue:[ |
|
253 |
oldItem collapse |
|
254 |
]. |
|
255 |
visIndex := model identityIndexOf:oldItem. |
|
256 |
expFlag := anItem isExpanded. |
|
257 |
anItem setExpanded:false. |
|
258 |
||
259 |
children at:anIndex put:anItem. |
|
260 |
||
261 |
visIndex ~~ 0 ifTrue:[ |
|
262 |
model at:visIndex put:anItem. |
|
263 |
]. |
|
264 |
self changed:#redraw. |
|
265 |
expFlag ifTrue:[ anItem expand ]. |
|
266 |
]. |
|
267 |
^ anItem |
|
5840 | 268 |
|
269 |
"Modified: / 28-07-2018 / 15:51:06 / Claus Gittinger" |
|
4733 | 270 |
! |
271 |
||
272 |
children:aListOfChildren |
|
273 |
"set a new list of children" |
|
274 |
||
5840 | 275 |
self synchronized:[ |
4733 | 276 |
self removeAll. |
5994 | 277 |
self addAll:aListOfChildren beforeIndex:1. |
4733 | 278 |
]. |
279 |
^ aListOfChildren |
|
5840 | 280 |
|
5994 | 281 |
"Modified: / 06-02-2019 / 15:59:15 / Claus Gittinger" |
4733 | 282 |
! |
283 |
||
284 |
first |
|
285 |
"returns the first child |
|
286 |
" |
|
287 |
^ self at:1 |
|
288 |
! |
|
289 |
||
290 |
last |
|
291 |
"returns the last child" |
|
292 |
||
293 |
^ self at:(self size) |
|
294 |
! |
|
295 |
||
296 |
second |
|
297 |
"returns the second child" |
|
298 |
||
299 |
^ self at:2 |
|
300 |
! ! |
|
301 |
||
302 |
!AbstractHierarchicalItem methodsFor:'accessing-hierarchy'! |
|
303 |
||
304 |
collapse |
|
305 |
"hide all my subitems" |
|
306 |
||
307 |
self canCollapse ifTrue:[ |
|
5840 | 308 |
self synchronized:[ |
5996 | 309 |
|overAllNrOfVisibleChildren nrOfVisibleChildren index| |
310 |
||
311 |
overAllNrOfVisibleChildren := self numberOfVisibleChildren. |
|
312 |
||
313 |
self setExpanded:false. |
|
314 |
||
4733 | 315 |
(index := self listIndex) notNil ifTrue:[ |
5996 | 316 |
"/ do not call self size or self children, |
317 |
"/ as this might autocreate children !!!! |
|
318 |
(nrOfVisibleChildren := children size) ~~ 0 ifTrue:[ |
|
319 |
children do:[:el| |
|
320 |
nrOfVisibleChildren := nrOfVisibleChildren + el numberOfVisibleChildren |
|
4733 | 321 |
]. |
5996 | 322 |
self assert:(nrOfVisibleChildren = overAllNrOfVisibleChildren). |
323 |
self model itemRemoveFromIndex:(index + 1) toIndex:(index + nrOfVisibleChildren). |
|
4733 | 324 |
]. |
325 |
index ~~ 0 ifTrue:[self hierarchyChanged]. |
|
326 |
] |
|
327 |
] |
|
328 |
] |
|
5840 | 329 |
|
5996 | 330 |
"Modified: / 12-02-2019 / 18:53:55 / Claus Gittinger" |
4733 | 331 |
! |
332 |
||
333 |
enforcedExpand |
|
334 |
"expand children - even if there are no children, |
|
335 |
the item is initially expanded (but this might be undone later, |
|
336 |
when we know that no children are there" |
|
337 |
||
338 |
self expand:true |
|
339 |
! |
|
340 |
||
341 |
expand |
|
342 |
"expand children - but only if there are children |
|
343 |
(i.e. this cannot be used before the childInfo is valid; |
|
344 |
aka not before the updateTask came along this item)" |
|
345 |
||
346 |
self expand:false |
|
347 |
! |
|
348 |
||
349 |
expand:enforced |
|
350 |
"expand children" |
|
351 |
||
352 |
"/ test whether the item already is expanded; #canExpand could be redefined |
|
353 |
"/ without checking whether the node is expanded (happens already) !! |
|
354 |
||
355 |
self isExpanded ifTrue:[ ^ self ]. |
|
356 |
(enforced not and:[self canExpand not]) ifTrue:[ ^ self ]. |
|
357 |
||
5840 | 358 |
self synchronized:[ |
5720 | 359 |
|index list| |
360 |
||
6010 | 361 |
self isExpanded ifFalse:[ |
362 |
(index := self listIndex) notNil ifTrue:[ |
|
363 |
"/ must set expand-flag to false, otherwise change notifications |
|
364 |
"/ are raised during lazy auto creation (to the list). |
|
365 |
self setExpanded:false. |
|
366 |
list := self children. |
|
367 |
list notEmptyOrNil ifTrue:[ |
|
368 |
self setExpanded:true. |
|
369 |
list := OrderedCollection new. |
|
370 |
self addVisibleChildrenTo:list. |
|
371 |
self model itemAddAll:list beforeIndex:(index + 1). |
|
372 |
]. |
|
373 |
index ~~ 0 ifTrue:[self hierarchyChanged]. |
|
374 |
] ifFalse:[ |
|
375 |
self setExpanded:true |
|
376 |
] |
|
377 |
]. |
|
4733 | 378 |
]. |
5720 | 379 |
|
380 |
"Modified: / 08-03-2018 / 21:06:13 / stefan" |
|
6010 | 381 |
"Modified: / 20-02-2019 / 00:23:49 / Claus Gittinger" |
4733 | 382 |
! |
383 |
||
5728 | 384 |
expandLevels:numLevels |
385 |
"expand children numLevels down" |
|
386 |
||
387 |
numLevels == 0 ifTrue:[^ self]. |
|
388 |
self children do:[:each | |
|
389 |
each expand. |
|
390 |
each expandLevels:(numLevels - 1). |
|
391 |
]. |
|
392 |
! |
|
393 |
||
394 |
expandLevels:numLevels max:maxNumExpandedHolder |
|
395 |
"expand children numLevels down" |
|
396 |
||
5998 | 397 |
|childrenToExpand| |
398 |
||
5728 | 399 |
numLevels == 0 ifTrue:[^ self]. |
400 |
maxNumExpandedHolder value <= 0 ifTrue:[^ self]. |
|
401 |
||
5998 | 402 |
childrenToExpand := self children. |
403 |
childrenToExpand do:[:each | |
|
5728 | 404 |
each expand. |
405 |
]. |
|
406 |
||
5998 | 407 |
maxNumExpandedHolder value:(maxNumExpandedHolder value - childrenToExpand size). |
408 |
childrenToExpand do:[:each | |
|
5728 | 409 |
each expandLevels:(numLevels - 1) max:maxNumExpandedHolder. |
410 |
]. |
|
5998 | 411 |
|
412 |
"Modified: / 13-02-2019 / 12:33:40 / Claus Gittinger" |
|
5728 | 413 |
! |
414 |
||
5992 | 415 |
expandWithoutUpdate |
416 |
"in case expand is redefined to do any update |
|
417 |
(as in guiBrowser's applicationTreeItem), |
|
418 |
this can be used to expand without updating" |
|
419 |
||
420 |
self expand |
|
421 |
||
422 |
"Created: / 02-02-2019 / 19:59:55 / Claus Gittinger" |
|
423 |
"Modified (comment): / 03-02-2019 / 01:28:33 / Claus Gittinger" |
|
424 |
! |
|
425 |
||
5623 | 426 |
labelPath |
427 |
"return my label-path as an ordered collection of individual labels" |
|
428 |
||
429 |
^ ({self} , self allParents) reverse collect:#label |
|
430 |
||
431 |
"Created: / 16-10-2017 / 13:09:10 / cg" |
|
432 |
"Modified: / 16-10-2017 / 14:16:46 / cg" |
|
433 |
! |
|
434 |
||
4733 | 435 |
makeVisible |
436 |
"expand all my parents" |
|
437 |
||
438 |
(parent notNil and:[parent isHierarchicalItem]) ifTrue:[ |
|
5840 | 439 |
self synchronized:[ |
4733 | 440 |
parent makeVisible. |
441 |
parent expand. |
|
442 |
] |
|
443 |
]. |
|
5840 | 444 |
|
445 |
"Modified: / 28-07-2018 / 15:47:52 / Claus Gittinger" |
|
4733 | 446 |
! |
447 |
||
448 |
recursiveCollapse |
|
5998 | 449 |
"collapse item and recursively all sub items" |
450 |
||
451 |
self canCollapse ifFalse:[^ self]. |
|
5999 | 452 |
|
453 |
"/ self checkConsistency. |
|
5840 | 454 |
self synchronized:[ |
5996 | 455 |
|nrOfVisibleChildren index| |
5840 | 456 |
|
457 |
self canCollapse ifTrue:[ |
|
4733 | 458 |
(index := self listIndex) notNil ifTrue:[ |
5996 | 459 |
"/ do not call self size or self children here; |
460 |
"/ otherwise, lazy children would be autocreated !!!! |
|
5998 | 461 |
nrOfVisibleChildren := self numberOfVisibleChildren. |
5996 | 462 |
self recursiveSetCollapsedHelper. |
463 |
||
464 |
nrOfVisibleChildren ~~ 0 ifTrue:[ |
|
5998 | 465 |
self model removeFromIndex:(index + 1) toIndex:(index + nrOfVisibleChildren) |
4733 | 466 |
]. |
467 |
index ~~ 0 ifTrue:[ |
|
468 |
self hierarchyChanged |
|
469 |
] |
|
470 |
] ifFalse:[ |
|
5998 | 471 |
"/ not visible |
5996 | 472 |
self recursiveSetCollapsedHelper |
4733 | 473 |
] |
474 |
] |
|
5998 | 475 |
]. |
5999 | 476 |
"/ self checkConsistency. |
477 |
||
478 |
"Modified: / 13-02-2019 / 12:59:40 / Claus Gittinger" |
|
4733 | 479 |
! |
480 |
||
481 |
recursiveExpand |
|
5998 | 482 |
"expand children and sub-children; |
483 |
WARNING: refetches children. |
|
5870 | 484 |
Precondition: must be collapsed" |
485 |
||
5998 | 486 |
|index todo toExpand wasExpanded myList prevChildren childrenOfToExpand| |
487 |
||
5999 | 488 |
"/ self checkConsistency. |
5998 | 489 |
self synchronized:[ |
5996 | 490 |
myList := self model. |
491 |
todo := OrderedCollection with:self. |
|
492 |
[todo notEmpty] whileTrue:[ |
|
493 |
toExpand := todo removeFirst. |
|
494 |
||
495 |
"/ test whether the item already is expanded; #canExpand could be redefined |
|
5998 | 496 |
"/ without checking whether the node is already expanded (happens actually) !! |
497 |
wasExpanded := toExpand isExpanded. |
|
498 |
(wasExpanded or:[toExpand canExpand]) ifTrue:[ |
|
5996 | 499 |
toExpand setExpanded:true. |
6009 | 500 |
prevChildren := toExpand getChildren ? #(). |
5996 | 501 |
childrenOfToExpand := toExpand children. |
502 |
||
503 |
index := toExpand listIndex. "/ get the visible list index |
|
504 |
index isNil ifTrue:[ |
|
505 |
"/ not visible |
|
506 |
childrenOfToExpand do:[:eachChild | |
|
507 |
(eachChild isExpanded not and:[eachChild canExpand]) ifTrue:[ |
|
508 |
eachChild setExpanded:true |
|
509 |
]. |
|
510 |
]. |
|
511 |
] ifFalse:[ |
|
512 |
"/ visible |
|
5998 | 513 |
wasExpanded ifTrue:[ |
514 |
prevChildren = childrenOfToExpand ifTrue:[ |
|
515 |
childrenOfToExpand do:[:eachChild | |
|
516 |
(eachChild isExpanded not and:[eachChild canExpand]) ifTrue:[ |
|
517 |
todo add:eachChild |
|
518 |
]. |
|
519 |
]. |
|
520 |
] ifFalse:[ |
|
521 |
"/ remove the previous children |
|
522 |
prevChildren do:[:each | each recursiveCollapse]. |
|
523 |
"/ add the new children |
|
524 |
myList itemAddAll:childrenOfToExpand afterIndex:index. |
|
525 |
]. |
|
526 |
] ifFalse:[ |
|
527 |
"/ add the new children |
|
528 |
myList itemAddAll:childrenOfToExpand afterIndex:index. |
|
529 |
]. |
|
5996 | 530 |
childrenOfToExpand do:[:eachChild | |
5998 | 531 |
(eachChild isExpanded not and:[eachChild canExpand]) ifTrue:[ |
532 |
todo add:eachChild |
|
5996 | 533 |
]. |
5998 | 534 |
]. |
4733 | 535 |
]. |
536 |
] |
|
537 |
] |
|
538 |
]. |
|
5999 | 539 |
"/ self checkConsistency. |
5840 | 540 |
|
5870 | 541 |
"Modified (comment): / 02-08-2018 / 16:16:17 / Stefan Vogel" |
6009 | 542 |
"Modified: / 18-02-2019 / 18:37:36 / Claus Gittinger" |
4733 | 543 |
! |
544 |
||
545 |
recursiveToggleExpand |
|
546 |
"if the item is collapsed, the item and all its sub-items |
|
547 |
are expanded otherwise collapsed" |
|
548 |
||
549 |
self isExpanded ifTrue:[ |
|
550 |
self recursiveCollapse |
|
551 |
] ifFalse:[ |
|
552 |
self recursiveExpand |
|
553 |
] |
|
554 |
! |
|
555 |
||
556 |
toggleExpand |
|
557 |
"if the item is collapsed, the item is expanded otherwise collapsed" |
|
558 |
||
559 |
self isExpanded ifTrue:[ |
|
560 |
self collapse |
|
561 |
] ifFalse:[ |
|
562 |
self expand |
|
563 |
]. |
|
564 |
! ! |
|
565 |
||
566 |
!AbstractHierarchicalItem methodsFor:'accessing-mvc'! |
|
567 |
||
568 |
application |
|
569 |
"returns the responsible application or nil" |
|
570 |
||
571 |
|model| |
|
572 |
||
573 |
(model := self model) notNil ifTrue:[ |
|
574 |
^ model application |
|
575 |
]. |
|
576 |
^ nil |
|
577 |
! |
|
578 |
||
579 |
applicationsDo:aOneArgBlock |
|
580 |
"evaluate the block for each dependent application" |
|
581 |
||
582 |
|model| |
|
583 |
||
584 |
(model := self model) notNil ifTrue:[ |
|
585 |
model applicationsDo:aOneArgBlock |
|
586 |
] |
|
587 |
! |
|
588 |
||
5840 | 589 |
fetchModel |
4733 | 590 |
"returns the hierachicalList model or nil. |
591 |
This is a stupid implementation here, in that the top-item's parent is assumed to |
|
592 |
be the model of the tree, and that is returned. |
|
593 |
This saves a slot in every node, but makes some algorithms O(n*log n) or even O(n^2). |
|
594 |
So be aware of the performance penalty" |
|
595 |
||
596 |
|item next| |
|
597 |
||
598 |
item := self. |
|
599 |
[ (next := item parentOrModel) notNil ] whileTrue:[ |
|
600 |
item := next. |
|
601 |
]. |
|
602 |
||
603 |
item isHierarchicalItem ifFalse:[^ item]. |
|
604 |
^ nil |
|
5840 | 605 |
! |
606 |
||
607 |
model |
|
608 |
"returns the hierachicalList model or nil. |
|
609 |
This uses fetchModel, which has a stupid implementation here, |
|
610 |
in that the top-item's parent is assumed to be the model of the tree, and that is returned. |
|
611 |
This saves a slot in every node, but makes some algorithms O(n*log n) or even O(n^2). |
|
612 |
So be aware of the performance penalty or redefined fetchModel" |
|
613 |
||
614 |
^ self fetchModel |
|
615 |
||
616 |
"Modified (comment): / 28-07-2018 / 13:30:24 / Claus Gittinger" |
|
4733 | 617 |
! ! |
618 |
||
619 |
!AbstractHierarchicalItem methodsFor:'adding & removing'! |
|
620 |
||
621 |
add:aChildItem |
|
622 |
"add a child at end" |
|
623 |
||
5857 | 624 |
^ self synchronized:[ |
5840 | 625 |
self add:aChildItem beforeIndex:(self children size + 1). |
626 |
]. |
|
627 |
||
628 |
"Modified: / 28-07-2018 / 13:35:08 / Claus Gittinger" |
|
5857 | 629 |
"Modified: / 29-07-2018 / 20:23:01 / Stefan Vogel" |
4733 | 630 |
! |
631 |
||
632 |
add:aChildItem after:aChild |
|
633 |
"add an item after an existing item" |
|
634 |
||
5857 | 635 |
^ self synchronized:[ |
636 |
|index| |
|
637 |
||
5840 | 638 |
index := self identityIndexOf:aChild. |
639 |
index == 0 ifTrue:[ self subscriptBoundsError:index ]. |
|
640 |
||
641 |
self add:aChildItem beforeIndex:(index + 1). |
|
642 |
]. |
|
643 |
||
644 |
"Modified: / 28-07-2018 / 13:35:18 / Claus Gittinger" |
|
5857 | 645 |
"Modified: / 29-07-2018 / 20:23:26 / Stefan Vogel" |
4733 | 646 |
! |
647 |
||
648 |
add:aChildItem afterIndex:anIndex |
|
649 |
"add an item after an index" |
|
650 |
||
5857 | 651 |
^ self add:aChildItem beforeIndex:(anIndex + 1). |
5840 | 652 |
|
653 |
"Modified: / 28-07-2018 / 13:37:32 / Claus Gittinger" |
|
5857 | 654 |
"Modified: / 29-07-2018 / 20:23:39 / Stefan Vogel" |
4733 | 655 |
! |
656 |
||
657 |
add:aChildItem before:aChild |
|
658 |
"add an item before an existing item" |
|
659 |
||
5857 | 660 |
^ self synchronized:[ |
5840 | 661 |
|index| |
662 |
||
663 |
index := self identityIndexOf:aChild. |
|
664 |
index == 0 ifTrue:[ self subscriptBoundsError:index ]. |
|
665 |
||
666 |
self add:aChildItem beforeIndex:index. |
|
667 |
]. |
|
668 |
||
669 |
"Modified: / 28-07-2018 / 13:35:47 / Claus Gittinger" |
|
5857 | 670 |
"Modified: / 29-07-2018 / 20:22:28 / Stefan Vogel" |
4733 | 671 |
! |
672 |
||
673 |
add:aChildItem beforeIndex:anIndex |
|
674 |
"add an item before an index" |
|
675 |
||
676 |
aChildItem notNil ifTrue:[ |
|
677 |
self addAll:(Array with:aChildItem) beforeIndex:anIndex |
|
678 |
]. |
|
5857 | 679 |
^ aChildItem. |
5840 | 680 |
|
681 |
"Modified: / 28-07-2018 / 13:35:01 / Claus Gittinger" |
|
5857 | 682 |
"Modified: / 29-07-2018 / 20:24:07 / Stefan Vogel" |
4733 | 683 |
! |
684 |
||
685 |
add:aChild sortBlock:aBlock |
|
686 |
"add a child sorted" |
|
687 |
||
5857 | 688 |
^ self synchronized:[ |
4733 | 689 |
self basicAdd:aChild sortBlock:aBlock |
690 |
]. |
|
5840 | 691 |
|
692 |
"Modified: / 28-07-2018 / 13:37:12 / Claus Gittinger" |
|
5857 | 693 |
"Modified: / 29-07-2018 / 20:24:31 / Stefan Vogel" |
4733 | 694 |
! |
695 |
||
696 |
addAll:aList |
|
697 |
"add children at the end" |
|
698 |
||
5857 | 699 |
^ self synchronized:[ |
5840 | 700 |
self addAll:aList beforeIndex:(self children size + 1) |
701 |
] |
|
702 |
||
703 |
"Modified: / 28-07-2018 / 13:37:25 / Claus Gittinger" |
|
5857 | 704 |
"Modified: / 29-07-2018 / 20:25:09 / Stefan Vogel" |
4733 | 705 |
! |
706 |
||
707 |
addAll:aList before:aChild |
|
708 |
"add an item before an existing item" |
|
709 |
||
5857 | 710 |
^ self synchronized:[ |
5840 | 711 |
|index| |
712 |
||
713 |
index := self identityIndexOf:aChild. |
|
714 |
index == 0 ifTrue:[ self subscriptBoundsError:index ]. |
|
715 |
||
716 |
self addAll:aList beforeIndex:index |
|
717 |
]. |
|
718 |
||
719 |
"Modified: / 28-07-2018 / 13:37:53 / Claus Gittinger" |
|
5857 | 720 |
"Modified: / 29-07-2018 / 20:25:18 / Stefan Vogel" |
4733 | 721 |
! |
722 |
||
723 |
addAll:aList beforeIndex:anIndex |
|
724 |
"add children before an index" |
|
725 |
||
5857 | 726 |
aList notEmptyOrNil ifTrue:[ |
5840 | 727 |
self synchronized:[ |
4733 | 728 |
self basicAddAll:aList beforeIndex:anIndex |
729 |
] |
|
730 |
]. |
|
731 |
^ aList |
|
5840 | 732 |
|
733 |
"Modified: / 28-07-2018 / 13:38:02 / Claus Gittinger" |
|
5857 | 734 |
"Modified: / 29-07-2018 / 20:25:38 / Stefan Vogel" |
4733 | 735 |
! |
736 |
||
737 |
addAll:aList sortBlock:aBlock |
|
738 |
"add children sorted" |
|
739 |
||
5857 | 740 |
aList notEmptyOrNil ifTrue:[ ^ aList ]. |
4733 | 741 |
|
742 |
aBlock isNil ifTrue:[ |
|
743 |
self addAll:aList. |
|
744 |
] ifFalse:[ |
|
5840 | 745 |
self synchronized:[ |
4733 | 746 |
aList do:[:el| self basicAdd:el sortBlock:aBlock ] |
747 |
] |
|
748 |
]. |
|
5857 | 749 |
^ aList |
5840 | 750 |
|
751 |
"Modified: / 28-07-2018 / 13:38:14 / Claus Gittinger" |
|
5857 | 752 |
"Modified: / 29-07-2018 / 20:26:24 / Stefan Vogel" |
4733 | 753 |
! |
754 |
||
755 |
addAllFirst:aCollectionOfItems |
|
756 |
"add children at the beginning" |
|
757 |
||
5857 | 758 |
^ self addAll:aCollectionOfItems beforeIndex:1 |
5840 | 759 |
|
760 |
"Modified: / 28-07-2018 / 13:38:24 / Claus Gittinger" |
|
5857 | 761 |
"Modified: / 29-07-2018 / 20:26:47 / Stefan Vogel" |
4733 | 762 |
! |
763 |
||
764 |
addAllLast:aCollectionOfItems |
|
765 |
"add children at the end" |
|
766 |
||
5857 | 767 |
^ self addAll:aCollectionOfItems |
5840 | 768 |
|
769 |
"Modified: / 28-07-2018 / 13:38:32 / Claus Gittinger" |
|
5857 | 770 |
"Modified: / 29-07-2018 / 20:26:55 / Stefan Vogel" |
4733 | 771 |
! |
772 |
||
773 |
addFirst:aChildItem |
|
774 |
"add a child at the beginning" |
|
775 |
||
5857 | 776 |
^ self add:aChildItem beforeIndex:1. |
5840 | 777 |
|
778 |
"Modified: / 28-07-2018 / 13:38:47 / Claus Gittinger" |
|
5857 | 779 |
"Modified: / 29-07-2018 / 20:27:01 / Stefan Vogel" |
4733 | 780 |
! |
781 |
||
782 |
addLast:anItem |
|
783 |
"add a child at the end" |
|
784 |
||
5857 | 785 |
^ self add:anItem |
5840 | 786 |
|
787 |
"Modified: / 28-07-2018 / 13:38:52 / Claus Gittinger" |
|
5857 | 788 |
"Modified: / 29-07-2018 / 20:27:09 / Stefan Vogel" |
4733 | 789 |
! |
790 |
||
791 |
remove |
|
792 |
"remove the item" |
|
793 |
||
794 |
parent notNil ifTrue:[ "check whether parent exists" |
|
795 |
parent isHierarchicalItem ifTrue:[parent remove:self] "parent is HierarchicalItem" |
|
796 |
ifFalse:[parent root:nil] "parent is HierarchicalList" |
|
797 |
]. |
|
5857 | 798 |
|
799 |
"Modified: / 29-07-2018 / 20:31:18 / Stefan Vogel" |
|
4733 | 800 |
! |
801 |
||
802 |
remove:aChild |
|
803 |
"remove a child" |
|
804 |
||
5840 | 805 |
self synchronized:[ |
806 |
self removeIndex:(self identityIndexOf:aChild) |
|
807 |
]. |
|
808 |
||
809 |
"Modified: / 28-07-2018 / 13:39:47 / Claus Gittinger" |
|
5857 | 810 |
"Modified: / 29-07-2018 / 20:40:47 / Stefan Vogel" |
4733 | 811 |
! |
812 |
||
813 |
removeAll |
|
5840 | 814 |
"remove all children. |
815 |
Returns the receiver." |
|
816 |
||
817 |
self synchronized:[ |
|
818 |
|size| |
|
819 |
||
820 |
(size := children size) ~~ 0 ifTrue:[ |
|
821 |
self removeFromIndex:1 toIndex:size |
|
822 |
] |
|
4733 | 823 |
] |
5840 | 824 |
|
825 |
"Modified (comment): / 28-07-2018 / 13:44:24 / Claus Gittinger" |
|
4733 | 826 |
! |
827 |
||
828 |
removeAll:aList |
|
5840 | 829 |
"remove all children in the collection. |
830 |
Returns the receiver." |
|
4733 | 831 |
|
832 |
aList size ~~ 0 ifTrue:[ |
|
5840 | 833 |
self synchronized:[ |
4733 | 834 |
aList do:[:el| |
5840 | 835 |
|index| |
836 |
||
4733 | 837 |
(index := self identityIndexOf:el) ~~ 0 ifTrue:[ |
838 |
self removeIndex:index |
|
839 |
] |
|
840 |
] |
|
841 |
] |
|
842 |
]. |
|
5840 | 843 |
|
844 |
"Modified (format): / 28-07-2018 / 13:44:51 / Claus Gittinger" |
|
5857 | 845 |
"Modified: / 29-07-2018 / 20:39:01 / Stefan Vogel" |
4733 | 846 |
! |
847 |
||
848 |
removeAllIdentical:aList |
|
849 |
"remove all children in the collection" |
|
850 |
||
5840 | 851 |
^ self removeAll:aList. |
4733 | 852 |
|
853 |
"Created: / 20-09-2010 / 09:43:06 / sr" |
|
5840 | 854 |
"Modified: / 28-07-2018 / 13:41:48 / Claus Gittinger" |
4733 | 855 |
! |
856 |
||
857 |
removeFromIndex:startIndex |
|
5840 | 858 |
"remove the children from startIndex up to end of children. |
859 |
Returns the receiver." |
|
860 |
||
861 |
self synchronized:[ |
|
862 |
self removeFromIndex:startIndex toIndex:(children size) |
|
863 |
]. |
|
864 |
||
865 |
"Modified: / 28-07-2018 / 13:44:07 / Claus Gittinger" |
|
4733 | 866 |
! |
867 |
||
868 |
removeFromIndex:startIndex toIndex:stopIndex |
|
869 |
"remove the children from startIndex up to and including |
|
870 |
the child under stopIndex. |
|
871 |
Returns the receiver." |
|
872 |
||
5840 | 873 |
self synchronized:[ |
5870 | 874 |
|nrOfChildren| |
5840 | 875 |
|
876 |
nrOfChildren := children size. |
|
877 |
||
878 |
(startIndex <= stopIndex and:[startIndex <= nrOfChildren]) ifTrue:[ |
|
5870 | 879 |
self basicRemoveFromIndex:startIndex toIndex:(stopIndex min:nrOfChildren) |
5840 | 880 |
]. |
5870 | 881 |
(children isEmptyOrNil and:[self clearExpandedWhenLastChildWasRemoved]) ifTrue:[ |
882 |
self setExpanded:false. |
|
5840 | 883 |
]. |
4733 | 884 |
]. |
885 |
||
5840 | 886 |
"Modified: / 28-07-2018 / 13:45:07 / Claus Gittinger" |
5870 | 887 |
"Modified: / 02-08-2018 / 15:51:19 / Stefan Vogel" |
4733 | 888 |
! |
889 |
||
890 |
removeIndex:anIndex |
|
891 |
"remove the child at an index" |
|
892 |
||
893 |
anIndex > 0 ifTrue:[ |
|
894 |
self removeFromIndex:anIndex toIndex:anIndex |
|
895 |
] |
|
896 |
! ! |
|
897 |
||
898 |
!AbstractHierarchicalItem methodsFor:'basic adding & removing'! |
|
899 |
||
5870 | 900 |
basicAdd:aChild sortBlock:aBlockOrNil |
5840 | 901 |
"add a child sorted. |
902 |
Not synchronized - should only be called internally |
|
903 |
within a synchronized region." |
|
4733 | 904 |
|
905 |
|size list| |
|
906 |
||
907 |
size := children size. |
|
908 |
list := Array with:aChild. |
|
909 |
||
5870 | 910 |
(aBlockOrNil notNil and:[size ~~ 0]) ifTrue:[ |
4733 | 911 |
children keysAndValuesDo:[:i :el| |
5870 | 912 |
(aBlockOrNil value:aChild value:el) ifTrue:[ |
4733 | 913 |
self basicAddAll:list beforeIndex:i. |
914 |
^ aChild |
|
915 |
] |
|
916 |
] |
|
917 |
]. |
|
5870 | 918 |
|
919 |
"insert at the end" |
|
4733 | 920 |
self basicAddAll:list beforeIndex:(size + 1). |
921 |
^ aChild. |
|
5840 | 922 |
|
923 |
"Modified (comment): / 28-07-2018 / 13:46:12 / Claus Gittinger" |
|
5870 | 924 |
"Modified (comment): / 01-08-2018 / 16:07:16 / Stefan Vogel" |
4733 | 925 |
! |
926 |
||
927 |
basicAddAll:aList beforeIndex:anIndex |
|
5840 | 928 |
"add children before an index. |
929 |
Not synchronized - should only be called internally |
|
930 |
within a synchronized region." |
|
4733 | 931 |
|
5994 | 932 |
|coll model notify index oldSize| |
933 |
||
934 |
oldSize := children size. |
|
4733 | 935 |
|
936 |
anIndex == 1 ifTrue:[ |
|
937 |
notify := self |
|
938 |
] ifFalse:[ |
|
5994 | 939 |
anIndex > oldSize ifTrue:[ |
940 |
anIndex > (1 + oldSize) ifTrue:[ |
|
4733 | 941 |
^ self subscriptBoundsError:index |
942 |
]. |
|
5994 | 943 |
notify := self at:oldSize |
4733 | 944 |
] ifFalse:[ |
945 |
notify := nil |
|
946 |
] |
|
947 |
]. |
|
5870 | 948 |
|
949 |
aList do:[:eachItem| eachItem parent:self ]. |
|
950 |
||
5994 | 951 |
oldSize == 0 ifTrue:[ |
5993 | 952 |
self setChildren:(OrderedCollection withAll:aList). |
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
953 |
] ifFalse:[ |
5870 | 954 |
children isArray ifTrue:[ |
5993 | 955 |
self setChildren:(children asOrderedCollection) |
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
956 |
]. |
5870 | 957 |
children addAll:aList beforeIndex:anIndex. |
4733 | 958 |
]. |
5993 | 959 |
|
4733 | 960 |
(model := self model) isNil ifTrue:[ |
961 |
^ aList |
|
962 |
]. |
|
963 |
||
964 |
self isExpanded ifFalse:[ |
|
965 |
notify notNil ifTrue:[ |
|
966 |
notify changed |
|
967 |
]. |
|
968 |
^ aList |
|
969 |
]. |
|
970 |
(index := self listIndex) isNil ifTrue:[ |
|
971 |
^ aList |
|
972 |
]. |
|
973 |
||
974 |
children from:1 to:(anIndex - 1) do:[:anItem| |
|
975 |
index := 1 + index + anItem numberOfVisibleChildren |
|
976 |
]. |
|
5857 | 977 |
|
4733 | 978 |
coll := OrderedCollection new. |
979 |
aList do:[:anItem| |
|
980 |
coll add:anItem. |
|
981 |
anItem addVisibleChildrenTo:coll. |
|
982 |
]. |
|
983 |
model itemAddAll:coll beforeIndex:(index + 1). |
|
984 |
||
985 |
notify notNil ifTrue:[ |
|
986 |
notify changed |
|
987 |
]. |
|
988 |
^ aList |
|
5840 | 989 |
|
5870 | 990 |
"Modified: / 01-08-2018 / 15:31:54 / Stefan Vogel" |
991 |
"Modified (format): / 02-08-2018 / 15:52:16 / Stefan Vogel" |
|
5993 | 992 |
"Modified: / 03-02-2019 / 19:32:25 / Claus Gittinger" |
5994 | 993 |
"Modified (format): / 06-02-2019 / 15:58:41 / Claus Gittinger" |
4733 | 994 |
! |
995 |
||
996 |
basicRemoveFromIndex:startIndex toIndex:stopIndex |
|
997 |
"remove the children from startIndex up to and including |
|
5840 | 998 |
the child under stopIndex. |
999 |
Not synchronized - should only be called internally |
|
1000 |
within a synchronized region." |
|
4733 | 1001 |
|
1002 |
|model notify |
|
1003 |
index "{ Class:SmallInteger }" |
|
1004 |
start "{ Class:SmallInteger }" |
|
1005 |
stop "{ Class:SmallInteger }" |
|
1006 |
size "{ Class:SmallInteger }" |
|
1007 |
| |
|
1008 |
size := self children size. |
|
1009 |
stop := stopIndex. |
|
1010 |
start := startIndex. |
|
1011 |
||
1012 |
(stop <= size and:[start between:1 and:stop]) ifFalse:[ |
|
1013 |
^ self subscriptBoundsError:index |
|
1014 |
]. |
|
1015 |
start == 1 ifTrue:[ |
|
1016 |
notify := self |
|
1017 |
] ifFalse:[ |
|
1018 |
stop == size ifTrue:[ |
|
1019 |
notify := self at:(start - 1) |
|
1020 |
] ifFalse:[ |
|
1021 |
notify := nil |
|
1022 |
] |
|
1023 |
]. |
|
1024 |
||
1025 |
(model := self model) notNil ifTrue:[ |
|
1026 |
index := model identityIndexOf:(children at:start). |
|
1027 |
size := stop - start + 1. |
|
1028 |
] ifFalse:[ |
|
1029 |
index := 0 |
|
1030 |
]. |
|
1031 |
||
1032 |
children from:start to:stop do:[:aChild| |
|
1033 |
index ~~ 0 ifTrue:[ |
|
1034 |
size := size + aChild numberOfVisibleChildren |
|
1035 |
]. |
|
1036 |
aChild parent:nil |
|
1037 |
]. |
|
1038 |
children removeFromIndex:start toIndex:stop. |
|
1039 |
||
1040 |
index ~~ 0 ifTrue:[ |
|
1041 |
model itemRemoveFromIndex:index toIndex:(index + size - 1) |
|
1042 |
]. |
|
1043 |
notify notNil ifTrue:[ |
|
1044 |
notify changed |
|
1045 |
]. |
|
5840 | 1046 |
|
1047 |
"Modified (format): / 28-07-2018 / 13:46:41 / Claus Gittinger" |
|
4733 | 1048 |
! ! |
1049 |
||
1050 |
!AbstractHierarchicalItem methodsFor:'change & update'! |
|
1051 |
||
1052 |
changed:what with:anArgument |
|
1053 |
"the item changed; raise change notification |
|
1054 |
#icon icon is modified; height and width are unchanged |
|
1055 |
#hierarchy collapsed/expanded; height and width are unchanged |
|
1056 |
#redraw redraw but height and width are unchanged |
|
1057 |
....... all others: the height and width are reset |
|
1058 |
" |
|
5886 | 1059 |
|
4733 | 1060 |
|model| |
1061 |
||
5860 | 1062 |
(what ~~ #redraw |
1063 |
and:[what ~~ #hierarchy |
|
1064 |
and:[what ~~ #icon |
|
1065 |
and:[self class doResetExtentOnChange]]]) ifTrue:[ |
|
1066 |
self makeWidthAndHeightUnknown |
|
1067 |
]. |
|
1068 |
||
5886 | 1069 |
(model := self model) notNil ifTrue:[ |
1070 |
self synchronized:[ |
|
5860 | 1071 |
model itemChanged:what with:anArgument from:self. |
4733 | 1072 |
]. |
1073 |
]. |
|
1074 |
super changed:what with:anArgument |
|
1075 |
||
1076 |
"Modified: / 24-11-2010 / 17:21:20 / cg" |
|
5861 | 1077 |
"Modified: / 30-07-2018 / 15:58:18 / Stefan Vogel" |
5886 | 1078 |
"Modified (format): / 15-08-2018 / 12:37:03 / Claus Gittinger" |
4733 | 1079 |
! |
1080 |
||
1081 |
childrenOrderChanged |
|
1082 |
"called if the order of the children changed by a user |
|
1083 |
operation. Update the model and raise a change notification for |
|
1084 |
each item which has changed its position |
|
1085 |
triggered by the user operation !!" |
|
1086 |
||
1087 |
|model visStart list| |
|
1088 |
||
1089 |
self isExpanded ifFalse:[ ^ self ]. "/ not expanded |
|
1090 |
children size > 1 ifFalse:[ ^ self ]. |
|
1091 |
||
1092 |
model := self model. |
|
1093 |
model isNil ifTrue:[^ self]. "/ no model |
|
1094 |
||
1095 |
visStart := model identityIndexOf:self. |
|
1096 |
visStart == 0 ifTrue:[ |
|
1097 |
model root ~~ self ifTrue:[ ^ self ]. |
|
5840 | 1098 |
"/ I'am the root but switched of by setting #showRoot to false |
4733 | 1099 |
]. |
1100 |
||
5840 | 1101 |
self synchronized:[ |
4733 | 1102 |
list := OrderedCollection new. |
1103 |
self addVisibleChildrenTo:list. |
|
1104 |
||
1105 |
list do:[:el| |
|
1106 |
visStart := visStart + 1. |
|
1107 |
||
1108 |
(model at:visStart ifAbsent:el) ~~ el ifTrue:[ |
|
1109 |
model at:visStart put:el |
|
1110 |
]. |
|
1111 |
] |
|
1112 |
]. |
|
5840 | 1113 |
|
1114 |
"Modified (format): / 28-07-2018 / 13:47:27 / Claus Gittinger" |
|
4733 | 1115 |
! |
1116 |
||
1117 |
fontChanged |
|
1118 |
"called if the font has changed. |
|
1119 |
Clear the precomputed width and height" |
|
1120 |
||
1121 |
self makeWidthAndHeightUnknown. |
|
1122 |
||
5870 | 1123 |
children notNil ifTrue:[ |
4733 | 1124 |
children do:[:el| el fontChanged]. |
1125 |
]. |
|
5870 | 1126 |
|
1127 |
"Modified: / 02-08-2018 / 15:53:52 / Stefan Vogel" |
|
4733 | 1128 |
! |
1129 |
||
1130 |
hierarchyChanged |
|
1131 |
"hierarchy changed; optimize redrawing" |
|
1132 |
||
1133 |
self changed:#hierarchy with:nil |
|
1134 |
! |
|
1135 |
||
1136 |
iconChanged |
|
1137 |
"icon changed; optimize redrawing" |
|
1138 |
||
1139 |
self changed:#icon with:nil |
|
1140 |
! |
|
1141 |
||
5886 | 1142 |
invalidate |
1143 |
"force redrawing of me" |
|
1144 |
||
1145 |
self changed:#redraw with:nil |
|
1146 |
||
1147 |
"Created: / 15-08-2018 / 12:38:48 / Claus Gittinger" |
|
1148 |
! |
|
1149 |
||
4733 | 1150 |
labelChanged |
1151 |
"called if the label has changed. |
|
5887 | 1152 |
Clear the precomputed width and height, and invalidate" |
4733 | 1153 |
|
1154 |
self makeWidthAndHeightUnknown. |
|
5887 | 1155 |
self invalidate |
4733 | 1156 |
|
1157 |
"Created: / 17-01-2011 / 17:43:42 / cg" |
|
5887 | 1158 |
"Modified: / 15-08-2018 / 12:43:20 / Claus Gittinger" |
4733 | 1159 |
! ! |
1160 |
||
1161 |
!AbstractHierarchicalItem methodsFor:'enumerating'! |
|
1162 |
||
5623 | 1163 |
allExpandedItemsDo:aBlock |
1164 |
"recursively enumerate all expanded nodes |
|
1165 |
(depth first; parent before children)" |
|
1166 |
||
1167 |
self recursiveDo:[:each | |
|
1168 |
each isExpanded ifTrue:[ |
|
1169 |
aBlock value:each. |
|
1170 |
]. |
|
1171 |
]. |
|
1172 |
||
1173 |
"Created: / 16-10-2017 / 14:19:00 / cg" |
|
1174 |
! |
|
1175 |
||
4733 | 1176 |
collect:aBlock |
5363 | 1177 |
"for each child in the receiver (non recursive), evaluate the argument, aBlock |
4733 | 1178 |
and return a new collection with the results" |
1179 |
||
1180 |
|coll| |
|
1181 |
||
1182 |
coll := OrderedCollection new. |
|
1183 |
self do:[:el| coll add:(aBlock value:el) ]. |
|
1184 |
^ coll |
|
5363 | 1185 |
|
1186 |
"Modified (comment): / 25-11-2016 / 08:42:19 / cg" |
|
4733 | 1187 |
! |
1188 |
||
1189 |
contains:aBlock |
|
5363 | 1190 |
"evaluate aOneArgBlock for each of the receiver's children (non recursive). |
4733 | 1191 |
Return true and skip remaining elements, if aBlock ever returns true, |
1192 |
otherwise return false" |
|
1193 |
||
1194 |
self do:[:el | (aBlock value:el) ifTrue:[^ true] ]. |
|
1195 |
^ false |
|
5362 | 1196 |
|
5363 | 1197 |
"Modified (comment): / 25-11-2016 / 08:43:31 / cg" |
4733 | 1198 |
! |
1199 |
||
1200 |
do:aOneArgBlock |
|
5363 | 1201 |
"evaluate a block for each child (non recursive)" |
4733 | 1202 |
|
1203 |
self from:1 to:nil do:aOneArgBlock |
|
5363 | 1204 |
|
1205 |
"Modified (comment): / 25-11-2016 / 08:43:36 / cg" |
|
4733 | 1206 |
! |
1207 |
||
1208 |
from:startIndex do:aOneArgBlock |
|
1209 |
"evaluate a block on each child starting with the |
|
5362 | 1210 |
child at startIndex to the end." |
1211 |
||
4733 | 1212 |
self from:startIndex to:nil do:aOneArgBlock |
5362 | 1213 |
|
1214 |
"Modified (comment): / 25-11-2016 / 07:45:55 / cg" |
|
4733 | 1215 |
! |
1216 |
||
1217 |
from:startIndex reverseDo:aOneArgBlock |
|
5363 | 1218 |
"evaluate a block on each child (non recursive) starting at end to the startIndex" |
5362 | 1219 |
|
4733 | 1220 |
self from:startIndex to:nil reverseDo:aOneArgBlock |
5362 | 1221 |
|
5363 | 1222 |
"Modified (comment): / 25-11-2016 / 08:43:42 / cg" |
4733 | 1223 |
! |
1224 |
||
1225 |
from:startIndex to:endIndex do:aOneArgBlock |
|
5996 | 1226 |
"WARNING: may fetch lazy children |
1227 |
evaluate a block on each child (non recursive), |
|
5870 | 1228 |
starting with the child at startIndex to the endIndex (last index if nil). |
1229 |
Answer the value of the block executed on the last element." |
|
1230 |
||
1231 |
^ self synchronized:[ |
|
1232 |
self nonCriticalFrom:startIndex to:endIndex do:aOneArgBlock |
|
4733 | 1233 |
]. |
5363 | 1234 |
|
1235 |
"Modified (comment): / 25-11-2016 / 08:43:51 / cg" |
|
5840 | 1236 |
"Modified: / 28-07-2018 / 13:48:49 / Claus Gittinger" |
5870 | 1237 |
"Modified: / 02-08-2018 / 16:04:04 / Stefan Vogel" |
5996 | 1238 |
"Modified (comment): / 12-02-2019 / 18:49:58 / Claus Gittinger" |
4733 | 1239 |
! |
1240 |
||
1241 |
from:startIndex to:endIndex reverseDo:aOneArgBlock |
|
5996 | 1242 |
"WARNING: may fetch lazy children |
1243 |
evaluate a block on each child (non recursive), |
|
5363 | 1244 |
starting with the child at endIndex to the startIndex." |
4733 | 1245 |
|
5840 | 1246 |
self synchronized:[ |
1247 |
self size >= startIndex ifTrue:[ |
|
1248 |
self nonCriticalFrom:startIndex to:endIndex reverseDo:aOneArgBlock |
|
1249 |
]. |
|
4733 | 1250 |
]. |
5363 | 1251 |
|
1252 |
"Modified (comment): / 25-11-2016 / 08:43:58 / cg" |
|
5840 | 1253 |
"Modified: / 28-07-2018 / 13:49:17 / Claus Gittinger" |
5996 | 1254 |
"Modified (comment): / 12-02-2019 / 18:50:04 / Claus Gittinger" |
4733 | 1255 |
! |
1256 |
||
1257 |
keysAndValuesDo:aTwoArgBlock |
|
5363 | 1258 |
"evaluate the argument, aBlock for every child (non recursive), |
4733 | 1259 |
passing both index and element as arguments." |
1260 |
||
5840 | 1261 |
|key| |
4733 | 1262 |
|
1263 |
key := 1. |
|
1264 |
||
1265 |
self do:[:el| |
|
5840 | 1266 |
el value:key value:el. |
4733 | 1267 |
key := key + 1. |
1268 |
]. |
|
5363 | 1269 |
|
1270 |
"Modified (comment): / 25-11-2016 / 08:44:03 / cg" |
|
5840 | 1271 |
"Modified: / 28-07-2018 / 13:49:35 / Claus Gittinger" |
4733 | 1272 |
! |
1273 |
||
1274 |
keysAndValuesReverseDo:aTwoArgBlock |
|
5996 | 1275 |
"WARNING: may fetch lazy children |
1276 |
evaluate the argument, aBlock in reverse order for every child (non recursive), |
|
5362 | 1277 |
passing both index and element as arguments." |
4733 | 1278 |
|
5840 | 1279 |
self synchronized:[ |
1280 |
self size == 0 ifFalse:[ |
|
1281 |
self nonCriticalKeysAndValuesReverseDo:aTwoArgBlock |
|
1282 |
]. |
|
4733 | 1283 |
]. |
5362 | 1284 |
|
5363 | 1285 |
"Modified (comment): / 25-11-2016 / 08:44:06 / cg" |
5840 | 1286 |
"Modified: / 28-07-2018 / 13:50:14 / Claus Gittinger" |
5996 | 1287 |
"Modified (comment): / 12-02-2019 / 18:50:08 / Claus Gittinger" |
4733 | 1288 |
! |
1289 |
||
1290 |
recursiveCollect:aBlock |
|
1291 |
"for each child in the receiver, evaluate the argument, aBlock |
|
5363 | 1292 |
and return a new collection with the results. |
1293 |
Warning: this only enumerates already visible child elements |
|
1294 |
i.e. any collapsed items are not visited." |
|
4733 | 1295 |
|
1296 |
|coll| |
|
1297 |
||
1298 |
coll := OrderedCollection new. |
|
1299 |
self recursiveDo:[:el| coll add:(aBlock value:el) ]. |
|
1300 |
^ coll |
|
5363 | 1301 |
|
1302 |
"Modified (comment): / 25-11-2016 / 08:43:20 / cg" |
|
4733 | 1303 |
! |
1304 |
||
1305 |
recursiveDo:aOneArgBlock |
|
5996 | 1306 |
"WARNING: may fetch lazy children |
1307 |
evaluate a block on each item and all the sub-items. |
|
5363 | 1308 |
Warning: this only enumerates already visible child elements |
1309 |
i.e. any collapsed items are not visited." |
|
4733 | 1310 |
|
5840 | 1311 |
self do:[:aChild| "/ notice: do does it synchronized |
4733 | 1312 |
aOneArgBlock value:aChild. |
1313 |
aChild nonCriticalRecursiveDo:aOneArgBlock |
|
1314 |
]. |
|
5363 | 1315 |
|
1316 |
"Modified (comment): / 25-11-2016 / 08:43:04 / cg" |
|
5996 | 1317 |
"Modified (comment): / 12-02-2019 / 18:51:21 / Claus Gittinger" |
4733 | 1318 |
! |
1319 |
||
1320 |
recursiveReverseDo:aOneArgBlock |
|
5996 | 1321 |
"WARNING: may fetch lazy children |
1322 |
evaluate a block on each item and all the sub-items; |
|
5363 | 1323 |
proccesing children in reverse direction. |
1324 |
Warning: this only enumerates already visible child elements |
|
1325 |
i.e. any collapsed items are not visited." |
|
4733 | 1326 |
|
5840 | 1327 |
self reverseDo:[:aChild| "/ notice: reverseDo does it synchronized |
4733 | 1328 |
aChild nonCriticalRecursiveReverseDo:aOneArgBlock. |
1329 |
aOneArgBlock value:aChild. |
|
1330 |
]. |
|
5363 | 1331 |
|
1332 |
"Modified (comment): / 25-11-2016 / 08:43:09 / cg" |
|
5996 | 1333 |
"Modified (comment): / 12-02-2019 / 18:51:09 / Claus Gittinger" |
4733 | 1334 |
! |
1335 |
||
1336 |
recursiveSelect:aBlock |
|
5362 | 1337 |
"return a new collection with all children and subChildren from the receiver, |
5363 | 1338 |
for which the argument aBlock evaluates to true. |
1339 |
Warning: this only enumerates already visible child elements |
|
1340 |
i.e. any collapsed items are not visited." |
|
4733 | 1341 |
|
1342 |
|coll| |
|
1343 |
||
1344 |
coll := OrderedCollection new. |
|
1345 |
self recursiveDo:[:el| (aBlock value:el) ifTrue:[coll add:el] ]. |
|
1346 |
^ coll |
|
5362 | 1347 |
|
5363 | 1348 |
"Modified (comment): / 25-11-2016 / 08:43:12 / cg" |
4733 | 1349 |
! |
1350 |
||
1351 |
reverseDo:aOneArgBlock |
|
5363 | 1352 |
"evaluate a block on each child (non recursive) in reverse direction" |
4733 | 1353 |
|
1354 |
self from:1 reverseDo:aOneArgBlock |
|
5363 | 1355 |
|
1356 |
"Modified (comment): / 25-11-2016 / 08:44:12 / cg" |
|
4733 | 1357 |
! |
1358 |
||
1359 |
select:aBlock |
|
5363 | 1360 |
"return a new collection with all items from the receiver (non recursive), |
1361 |
for which the argument aBlock evaluates to true." |
|
4733 | 1362 |
|
1363 |
|coll| |
|
1364 |
||
1365 |
coll := OrderedCollection new. |
|
1366 |
self do:[:el| (aBlock value:el) ifTrue:[coll add:el] ]. |
|
1367 |
^ coll |
|
5363 | 1368 |
|
1369 |
"Modified (comment): / 25-11-2016 / 08:44:20 / cg" |
|
4733 | 1370 |
! |
1371 |
||
1372 |
withAllDo:aOneArgBlock |
|
5996 | 1373 |
"WARNING: may fetch lazy children |
1374 |
recursively evaluate aOneArgBlock on each item and subitem including self" |
|
4733 | 1375 |
|
1376 |
aOneArgBlock value:self. |
|
1377 |
||
5840 | 1378 |
self do:[:el| "/ do does it synchronized |
4733 | 1379 |
aOneArgBlock value:el. |
1380 |
el nonCriticalRecursiveDo:aOneArgBlock. |
|
1381 |
]. |
|
5363 | 1382 |
|
1383 |
"Modified (comment): / 25-11-2016 / 08:44:49 / cg" |
|
5996 | 1384 |
"Modified (comment): / 12-02-2019 / 18:50:40 / Claus Gittinger" |
4733 | 1385 |
! ! |
1386 |
||
1387 |
!AbstractHierarchicalItem methodsFor:'enumerating parents'! |
|
1388 |
||
5623 | 1389 |
allParents |
5925 | 1390 |
"return a collection of all parents (bottom to top, ie. in parent, grandparent, ... order)" |
5623 | 1391 |
|
1392 |
|parents| |
|
1393 |
||
1394 |
parents := OrderedCollection new. |
|
1395 |
self parentsDo:[:p | parents add:p]. |
|
1396 |
^ parents. |
|
1397 |
||
1398 |
"Created: / 16-10-2017 / 13:08:45 / cg" |
|
1399 |
! |
|
1400 |
||
4733 | 1401 |
parentsDetect:aBlock |
5925 | 1402 |
"find the first parent (bottom to top), for which evaluation of the block returns |
4733 | 1403 |
true; if none does so, report an error" |
1404 |
||
1405 |
^ self parentsDetect:aBlock ifNone:[self errorNotFound] |
|
1406 |
! |
|
1407 |
||
1408 |
parentsDetect:aBlock ifNone:anExceptionBlock |
|
5925 | 1409 |
"find the first parent (bottom to top), for which evaluation of the block returns |
4733 | 1410 |
true; if none does so, return the evaluation of anExceptionBlock" |
1411 |
||
5925 | 1412 |
self parentsDo:[:prnt | |
1413 |
(aBlock value:prnt) ifTrue:[^ prnt] |
|
4733 | 1414 |
]. |
1415 |
^ anExceptionBlock value |
|
5840 | 1416 |
|
1417 |
"Modified: / 28-07-2018 / 13:51:06 / Claus Gittinger" |
|
4733 | 1418 |
! |
1419 |
||
1420 |
parentsDo:aBlock |
|
5925 | 1421 |
"evaluate a block for each parent (bottom to top)" |
4733 | 1422 |
|
1423 |
|prnt| |
|
1424 |
||
1425 |
prnt := self. |
|
1426 |
||
5840 | 1427 |
self synchronized:[ |
4733 | 1428 |
[(prnt := prnt parent) notNil and:[prnt isHierarchicalItem]] whileTrue:[ |
1429 |
aBlock value:prnt |
|
1430 |
] |
|
1431 |
]. |
|
5840 | 1432 |
|
1433 |
"Modified: / 28-07-2018 / 13:51:22 / Claus Gittinger" |
|
4733 | 1434 |
! ! |
1435 |
||
1436 |
!AbstractHierarchicalItem methodsFor:'initialization'! |
|
1437 |
||
1438 |
initialize |
|
1439 |
self setExpanded:false |
|
1440 |
! ! |
|
1441 |
||
1442 |
!AbstractHierarchicalItem methodsFor:'private'! |
|
1443 |
||
1444 |
addVisibleChildrenTo:aList |
|
1445 |
"add all visible children and sub-children to the list" |
|
1446 |
||
1447 |
self isExpanded ifFalse:[^ self]. |
|
1448 |
||
5840 | 1449 |
self from:1 to:nil do:[:el| |
4733 | 1450 |
aList add:el. |
5996 | 1451 |
el isExpanded ifTrue:[ |
1452 |
el addVisibleChildrenTo:aList. |
|
1453 |
]. |
|
4733 | 1454 |
]. |
5840 | 1455 |
|
5996 | 1456 |
"Modified: / 12-02-2019 / 19:00:25 / Claus Gittinger" |
4733 | 1457 |
! |
1458 |
||
5998 | 1459 |
checkConsistency |
1460 |
|allChildrenDo idx expectedIndex| |
|
1461 |
||
1462 |
allChildrenDo := |
|
1463 |
[:item :action | |
|
1464 |
action value:item. |
|
1465 |
(item getChildren ? #()) do:[:subItem | |
|
1466 |
allChildrenDo value:subItem value:action |
|
1467 |
]. |
|
1468 |
]. |
|
1469 |
||
1470 |
idx := self listIndex. |
|
1471 |
self isExpanded ifFalse:[ |
|
1472 |
(self getChildren ? #()) do:[:eachChild | |
|
1473 |
allChildrenDo value:eachChild value:[:subItem | |
|
1474 |
self assert:(subItem listIndex isNil). |
|
1475 |
]. |
|
1476 |
]. |
|
1477 |
^ self. |
|
1478 |
]. |
|
1479 |
||
1480 |
expectedIndex := idx+1. |
|
1481 |
self getChildren do:[:eachChild | |
|
1482 |
self assert:(eachChild listIndex == expectedIndex). |
|
1483 |
expectedIndex := expectedIndex + 1 + eachChild numberOfVisibleChildren. |
|
1484 |
]. |
|
1485 |
||
1486 |
"Created: / 13-02-2019 / 12:31:24 / Claus Gittinger" |
|
1487 |
! |
|
1488 |
||
4733 | 1489 |
clearExpandedWhenLastChildWasRemoved |
5712 | 1490 |
"https://expeccoalm.exept.de/D227397 |
1491 |
Do not set #isExpanded to false just because #children is empty (may children appear 'again' later). |
|
1492 |
Do modify #isExpanded ONLY when a user presses the expand/collapse toggle, otherwise #isExpanded should be persistent. |
|
1493 |
||
1494 |
The user's preference if the item is expanded or collapsed should be kept, |
|
1495 |
regardless if there are chilren or not (even regardless anything else). |
|
1496 |
All other related things, like the drawing in case for #isExpanded is true and children is empty, |
|
1497 |
has to be solved within the drawing (or within any feature requesting this information)" |
|
1498 |
||
1499 |
^ false |
|
4733 | 1500 |
! |
1501 |
||
1502 |
criticalDo:aBlock |
|
5996 | 1503 |
<resource: #obsolete> |
5840 | 1504 |
self synchronized:aBlock |
5834 | 1505 |
|
1506 |
"Modified: / 23-07-2018 / 13:25:11 / Stefan Vogel" |
|
5840 | 1507 |
"Modified: / 28-07-2018 / 15:51:23 / Claus Gittinger" |
4733 | 1508 |
! |
1509 |
||
1510 |
listIndex |
|
5998 | 1511 |
"returns the index in the full list or nil; |
1512 |
for a non-visible root, 0 is returned" |
|
4733 | 1513 |
|
1514 |
|index model| |
|
1515 |
||
1516 |
(model := self model) notNil ifTrue:[ |
|
1517 |
index := model identityIndexOf:self. |
|
1518 |
||
1519 |
(index ~~ 0 or:[parent == model]) ifTrue:[ |
|
1520 |
^ index |
|
1521 |
] |
|
1522 |
]. |
|
1523 |
^ nil |
|
5998 | 1524 |
|
1525 |
"Modified (comment): / 13-02-2019 / 11:34:56 / Claus Gittinger" |
|
4733 | 1526 |
! |
1527 |
||
1528 |
numberOfVisibleChildren |
|
5996 | 1529 |
"returns the number of all visible children including subchildren, |
5998 | 1530 |
but excluding myself. |
1531 |
PseudoRecursive to avoid recursionError for very deep hierarchies." |
|
1532 |
||
1533 |
|count todo toCount itsChildren| |
|
4733 | 1534 |
|
1535 |
self isExpanded ifFalse:[^ 0]. |
|
5840 | 1536 |
|
1537 |
count := 0. |
|
5996 | 1538 |
todo := OrderedCollection with:self. |
1539 |
[todo notEmpty] whileTrue:[ |
|
1540 |
toCount := todo removeFirst. |
|
5998 | 1541 |
|
1542 |
itsChildren := toCount getChildren. |
|
6008 | 1543 |
itsChildren notNil ifTrue:[ |
1544 |
count := count + itsChildren size. |
|
1545 |
itsChildren do:[:each | |
|
1546 |
each isExpanded ifTrue:[ |
|
1547 |
todo add:each |
|
1548 |
]. |
|
5996 | 1549 |
]. |
6008 | 1550 |
]. |
4733 | 1551 |
]. |
5840 | 1552 |
^ count |
1553 |
||
6008 | 1554 |
"Modified: / 18-02-2019 / 18:36:17 / Claus Gittinger" |
4733 | 1555 |
! |
1556 |
||
1557 |
parentOrModel |
|
1558 |
"returns the parent without checking for item or model" |
|
1559 |
||
1560 |
^ parent |
|
5840 | 1561 |
! |
1562 |
||
5993 | 1563 |
setChildren:aCollectionOfChildrenOrNil |
5994 | 1564 |
children := aCollectionOfChildrenOrNil. |
1565 |
||
1566 |
Smalltalk isSmalltalkDevelopmentSystem ifTrue:[ |
|
1567 |
children notNil ifTrue:[ |
|
1568 |
self assert:(children conform:[:each | each parent == self]). |
|
1569 |
]. |
|
1570 |
]. |
|
5993 | 1571 |
|
1572 |
"Created: / 03-02-2019 / 19:22:26 / Claus Gittinger" |
|
5994 | 1573 |
"Modified: / 06-02-2019 / 16:00:51 / Claus Gittinger" |
5993 | 1574 |
! |
1575 |
||
5840 | 1576 |
synchronized:aBlock |
1577 |
|model| |
|
1578 |
||
1579 |
(model := self model) notNil ifTrue:[ |
|
5857 | 1580 |
^ model synchronized:aBlock |
5840 | 1581 |
] ifFalse:[ |
5977 | 1582 |
"/ self breakPoint:#cg. "/ should not happen |
5857 | 1583 |
^ aBlock value |
5840 | 1584 |
] |
1585 |
||
1586 |
"Created: / 28-07-2018 / 13:58:54 / Claus Gittinger" |
|
5857 | 1587 |
"Modified: / 29-07-2018 / 21:02:22 / Stefan Vogel" |
5977 | 1588 |
"Modified: / 21-12-2018 / 20:01:54 / Claus Gittinger" |
4733 | 1589 |
! ! |
1590 |
||
1591 |
!AbstractHierarchicalItem methodsFor:'private-displaying'! |
|
1592 |
||
1593 |
displayLabel:aLabel h:lH on:aGC x:x y:y h:h |
|
1594 |
<resource: #obsolete> |
|
1595 |
||
1596 |
"display the label at x@y" |
|
1597 |
||
1598 |
"/ obsolete - left in for backward compatibility |
|
1599 |
self displayLabel:aLabel h:lH on:aGC x:x y:y h:h isHighlightedAsSelected:false |
|
1600 |
! |
|
1601 |
||
1602 |
displayLabel:aLabel h:lH on:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
1603 |
"display the label at x@y" |
|
1604 |
||
4758
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1605 |
|y0| |
4733 | 1606 |
|
1607 |
lH ~~ 0 ifTrue:[ |
|
1608 |
y0 := y - ((lH + 1 - h) // 2). |
|
1609 |
y0 := y0 + (aLabel ascentOn:aGC). |
|
1610 |
||
1611 |
(aLabel isString not |
|
5821 | 1612 |
or:[(aLabel includes:(Character cr)) not]) ifTrue:[ |
4758
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1613 |
|labelShown| |
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1614 |
|
4733 | 1615 |
labelShown := aLabel. |
1616 |
isHighlightedAsSelected ifTrue:[ |
|
1617 |
(aLabel isText and:[aLabel hasChangeOfEmphasis]) ifTrue:[ |
|
1618 |
labelShown := Text string:aLabel string emphasisCollection:aLabel emphasis asArray. |
|
1619 |
labelShown emphasisAllRemove:#color. |
|
1620 |
]. |
|
1621 |
]. |
|
5821 | 1622 |
labelShown displayOn:aGC x:x y:y0. |
1623 |
^ self. |
|
4733 | 1624 |
]. |
1625 |
||
4758
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1626 |
aLabel asCollectionOfLines do:[:el| |
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1627 |
|labelShown| |
1095f8fdf167
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4751
diff
changeset
|
1628 |
|
4733 | 1629 |
labelShown := el. |
1630 |
isHighlightedAsSelected ifTrue:[ |
|
1631 |
(el isText and:[el hasChangeOfEmphasis]) ifTrue:[ |
|
1632 |
labelShown := Text string:el string emphasisCollection:el emphasis asArray. |
|
1633 |
labelShown emphasisAllRemove:#color. |
|
1634 |
]. |
|
1635 |
]. |
|
1636 |
labelShown displayOn:aGC x:x y:y0. |
|
1637 |
y0 := y0 + (el heightOn:aGC) |
|
1638 |
] |
|
1639 |
]. |
|
1640 |
! |
|
1641 |
||
1642 |
heightOf:aLabel on:aGC |
|
1643 |
"returns the height of the label or 0" |
|
1644 |
||
1645 |
|h| |
|
1646 |
||
1647 |
aLabel isSequenceable ifFalse:[ |
|
1648 |
^ aLabel notNil ifTrue:[aLabel heightOn:aGC] ifFalse:[0] |
|
1649 |
]. |
|
1650 |
||
1651 |
aLabel isString ifFalse:[ |
|
1652 |
h := 0. |
|
1653 |
aLabel do:[:el|h := h max:(self heightOf:el on:aGC)]. |
|
4746
25341fd92e38
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4733
diff
changeset
|
1654 |
^ h |
4733 | 1655 |
]. |
1656 |
||
1657 |
h := 1 + (aLabel occurrencesOf:(Character cr)). |
|
1658 |
^ h * (aGC font height) |
|
1659 |
! |
|
1660 |
||
1661 |
widthOf:aLabel on:aGC |
|
1662 |
"returns the height of the label or 0" |
|
1663 |
||
1664 |
|w| |
|
1665 |
||
1666 |
aLabel isSequenceable ifFalse:[ |
|
1667 |
^ aLabel notNil ifTrue:[aLabel widthOn:aGC] ifFalse:[0] |
|
1668 |
]. |
|
1669 |
||
1670 |
aLabel isString ifFalse:[ |
|
1671 |
w := -5. |
|
1672 |
aLabel do:[:el|w := w + 5 + (self widthOf:el on:aGC)]. |
|
1673 |
^ w |
|
1674 |
]. |
|
1675 |
||
1676 |
(aLabel indexOf:(Character cr)) == 0 ifTrue:[ |
|
1677 |
^ aLabel widthOn:aGC |
|
1678 |
]. |
|
1679 |
||
1680 |
w := 0. |
|
1681 |
aLabel asCollectionOfLines do:[:el|w := w max:(el widthOn:aGC)]. |
|
1682 |
^ w |
|
1683 |
! ! |
|
1684 |
||
1685 |
!AbstractHierarchicalItem methodsFor:'private-enumerating'! |
|
1686 |
||
1687 |
nonCriticalDo:aOneArgBlock |
|
5996 | 1688 |
"WARNING: may fetch lazy children |
1689 |
evaluate a block noncritical for each child. |
|
5840 | 1690 |
Not synchronized - should only be called internally |
1691 |
within a synchronized region." |
|
4733 | 1692 |
|
1693 |
^ self nonCriticalFrom:1 to:nil do:aOneArgBlock |
|
5840 | 1694 |
|
5996 | 1695 |
"Modified (comment): / 12-02-2019 / 18:49:05 / Claus Gittinger" |
4733 | 1696 |
! |
1697 |
||
1698 |
nonCriticalFrom:startIndex to:endIndex do:aOneArgBlock |
|
5996 | 1699 |
"WARNING: may fetch lazy children |
1700 |
evaluate a block noncritical for each child starting with the |
|
5840 | 1701 |
child at startIndex to the endIndex (if nil to end of list). |
1702 |
Not synchronized - should only be called internally |
|
5870 | 1703 |
within a synchronized region. |
1704 |
Answer the value of the block executed on the last element." |
|
4733 | 1705 |
|
1706 |
|list size resp| |
|
1707 |
||
1708 |
list := self children. |
|
1709 |
size := list size. |
|
1710 |
||
1711 |
startIndex > size ifTrue:[^ nil]. |
|
1712 |
resp := nil. |
|
1713 |
||
1714 |
endIndex notNil ifTrue:[ |
|
1715 |
size := size min:endIndex |
|
1716 |
]. |
|
1717 |
startIndex to:size do:[:i| |
|
1718 |
|item| |
|
1719 |
||
1720 |
item := list at:i ifAbsent:nil. |
|
5996 | 1721 |
item isNil ifTrue:[self halt:'oops - lost child item?'. ^ resp]. |
4733 | 1722 |
resp := aOneArgBlock value:item. |
1723 |
]. |
|
1724 |
^ resp |
|
5840 | 1725 |
|
5870 | 1726 |
"Modified (comment): / 02-08-2018 / 16:02:13 / Stefan Vogel" |
5996 | 1727 |
"Modified: / 12-02-2019 / 16:55:20 / Claus Gittinger" |
1728 |
"Modified (comment): / 12-02-2019 / 18:48:58 / Claus Gittinger" |
|
4733 | 1729 |
! |
1730 |
||
1731 |
nonCriticalFrom:startIndex to:endIndex reverseDo:aOneArgBlock |
|
5996 | 1732 |
"WARNING: may fetch lazy children |
1733 |
evaluate a block non critical for each child starting with the |
|
5840 | 1734 |
child at endIndex (if nil to end of list) to startIndex. |
1735 |
Not synchronized - should only be called internally |
|
1736 |
within a synchronized region." |
|
4733 | 1737 |
|
1738 |
|list size resp| |
|
1739 |
||
1740 |
list := self children. |
|
1741 |
size := list size. |
|
1742 |
resp := nil. |
|
1743 |
||
1744 |
endIndex notNil ifTrue:[ |
|
1745 |
size := size min:endIndex |
|
1746 |
]. |
|
1747 |
size to:startIndex by:-1 do:[:i| |
|
1748 |
|item| |
|
1749 |
||
1750 |
item := list at:i ifAbsent:nil. |
|
1751 |
item isNil ifTrue:[^ resp]. |
|
1752 |
resp := aOneArgBlock value:item. |
|
1753 |
]. |
|
1754 |
^ resp |
|
5840 | 1755 |
|
5996 | 1756 |
"Modified (comment): / 12-02-2019 / 18:49:11 / Claus Gittinger" |
4733 | 1757 |
! |
1758 |
||
1759 |
nonCriticalKeysAndValuesReverseDo:aOneArgBlock |
|
5996 | 1760 |
"WARNING: may fetch lazy children |
1761 |
evaluate the argument, aBlock in reverse order for every |
|
5840 | 1762 |
child, passing both index and element as arguments. |
1763 |
Not synchronized - should only be called internally |
|
1764 |
within a synchronized region." |
|
4733 | 1765 |
|
1766 |
|list size resp| |
|
1767 |
||
1768 |
list := self children. |
|
1769 |
size := list size. |
|
1770 |
resp := nil. |
|
1771 |
||
1772 |
size to:1 by:-1 do:[:i| |
|
1773 |
|item| |
|
1774 |
||
1775 |
item := list at:i ifAbsent:nil. |
|
1776 |
item isNil ifTrue:[^ resp]. |
|
1777 |
resp := aOneArgBlock value:i value:item. |
|
1778 |
]. |
|
1779 |
^ resp |
|
5840 | 1780 |
|
5996 | 1781 |
"Modified (comment): / 12-02-2019 / 18:49:16 / Claus Gittinger" |
4733 | 1782 |
! |
1783 |
||
1784 |
nonCriticalRecursiveDo:aOneArgBlock |
|
5996 | 1785 |
"WARNING: may fetch lazy children |
1786 |
evaluate the block non critical for each item and all the sub-items. |
|
5840 | 1787 |
Not synchronized - should only be called internally |
1788 |
within a synchronized region." |
|
4733 | 1789 |
|
1790 |
self nonCriticalFrom:1 to:nil do:[:eachChild| |
|
1791 |
aOneArgBlock value:eachChild. |
|
1792 |
eachChild nonCriticalRecursiveDo:aOneArgBlock |
|
1793 |
]. |
|
5840 | 1794 |
|
5996 | 1795 |
"Modified (comment): / 12-02-2019 / 18:49:23 / Claus Gittinger" |
4733 | 1796 |
! |
1797 |
||
1798 |
nonCriticalRecursiveReverseDo:aOneArgBlock |
|
5996 | 1799 |
"WARNING: may fetch lazy children |
1800 |
evaluate the block non critical for each item and all the sub-items; |
|
5840 | 1801 |
proccesing children in reverse direction. |
1802 |
Not synchronized - should only be called internally |
|
1803 |
within a synchronized region." |
|
4733 | 1804 |
|
1805 |
self nonCriticalFrom:1 to:nil reverseDo:[:eachChild| |
|
1806 |
eachChild nonCriticalRecursiveReverseDo:aOneArgBlock. |
|
1807 |
aOneArgBlock value:eachChild. |
|
1808 |
]. |
|
5840 | 1809 |
|
5996 | 1810 |
"Modified (comment): / 12-02-2019 / 18:48:45 / Claus Gittinger" |
4733 | 1811 |
! |
1812 |
||
1813 |
nonCriticalRecursiveSort:aSortBlock |
|
5840 | 1814 |
"evaluate a block noncritical for each child. |
1815 |
Not synchronized - should only be called internally |
|
1816 |
within a synchronized region." |
|
4733 | 1817 |
|
1818 |
|unsorted sorted| |
|
1819 |
||
1820 |
unsorted := children. |
|
1821 |
||
1822 |
unsorted size ~~ 0 ifTrue:[ |
|
1823 |
sorted := unsorted sort:aSortBlock. |
|
1824 |
sorted do:[:el| el nonCriticalRecursiveSort:aSortBlock ]. |
|
5993 | 1825 |
self setChildren:sorted. |
4733 | 1826 |
]. |
5840 | 1827 |
|
5993 | 1828 |
"Modified: / 03-02-2019 / 19:24:03 / Claus Gittinger" |
4733 | 1829 |
! ! |
1830 |
||
1831 |
!AbstractHierarchicalItem methodsFor:'private-hierarchy'! |
|
1832 |
||
1833 |
recursiveSetCollapsed |
|
5996 | 1834 |
<resource: #obsolete> |
4733 | 1835 |
"collapse all children and sub-children without notifications" |
1836 |
||
5840 | 1837 |
self synchronized:[ |
4733 | 1838 |
self recursiveSetCollapsedHelper |
1839 |
] |
|
5840 | 1840 |
|
1841 |
"Modified: / 28-07-2018 / 13:56:58 / Claus Gittinger" |
|
4733 | 1842 |
! |
1843 |
||
1844 |
recursiveSetCollapsedHelper |
|
1845 |
"private helper. |
|
1846 |
collapse all children and sub-children without notifications. |
|
5840 | 1847 |
Helper; not synchronized - should only be called internally |
5998 | 1848 |
within a synchronized region. |
1849 |
PseudoRecursive to avoid recursionError with deep hierarchies." |
|
4733 | 1850 |
|
5996 | 1851 |
|todo toCollapse| |
5998 | 1852 |
|
1853 |
self canRecursiveCollapse ifFalse:[^ self]. |
|
5996 | 1854 |
|
1855 |
todo := OrderedCollection with:self. |
|
1856 |
[todo notEmpty] whileTrue:[ |
|
1857 |
"/ do not call self size or self children; |
|
1858 |
"/ otherwise, children might will be autocreated !!!! |
|
1859 |
toCollapse := todo removeFirst. |
|
5998 | 1860 |
|
5996 | 1861 |
toCollapse setExpanded:false. |
6009 | 1862 |
(toCollapse getChildren ? #()) do:[:eachChild | |
5996 | 1863 |
eachChild canRecursiveCollapse ifTrue:[ |
1864 |
todo add:eachChild |
|
1865 |
]. |
|
5870 | 1866 |
] |
1867 |
]. |
|
5840 | 1868 |
|
5870 | 1869 |
"Modified: / 02-08-2018 / 16:07:35 / Stefan Vogel" |
6009 | 1870 |
"Modified: / 18-02-2019 / 18:37:49 / Claus Gittinger" |
4733 | 1871 |
! |
1872 |
||
1873 |
recursiveSetExpandedAndAddToList:aList |
|
5996 | 1874 |
<resource: #obsolete> |
4733 | 1875 |
"expand all children and sub-children without notifications; |
1876 |
add children to list" |
|
1877 |
||
5840 | 1878 |
self synchronized:[ |
4733 | 1879 |
self recursiveSetExpandedAndAddToListHelper:aList |
1880 |
]. |
|
5840 | 1881 |
|
1882 |
"Modified: / 28-07-2018 / 13:59:52 / Claus Gittinger" |
|
4733 | 1883 |
! |
1884 |
||
1885 |
recursiveSetExpandedAndAddToListHelper:aList |
|
1886 |
"private helper. |
|
1887 |
expand all children and sub-children without notifications; adds children to aList |
|
5840 | 1888 |
Helper; not synchronized - should only be called internally |
1889 |
within a synchronized region." |
|
4733 | 1890 |
|
5996 | 1891 |
|toDo work toExpand indexToAddAfter| |
1892 |
||
1893 |
toDo := OrderedCollection with:(self -> aList size). |
|
5925 | 1894 |
[ toDo notEmpty ] whileTrue:[ |
5996 | 1895 |
work := toDo removeFirst. |
1896 |
toExpand := work key. |
|
1897 |
indexToAddAfter := work value. |
|
5925 | 1898 |
toExpand setExpanded:true. |
5996 | 1899 |
(children := toExpand children) size > 0 ifTrue:[ |
1900 |
aList addAll:children afterIndex:indexToAddAfter. |
|
1901 |
children doWithIndex:[:eachChild :index | |
|
1902 |
eachChild canRecursiveExpand ifTrue:[ |
|
1903 |
toDo add:(eachChild -> (indexToAddAfter+index)) |
|
1904 |
]. |
|
5925 | 1905 |
]. |
4733 | 1906 |
]. |
1907 |
]. |
|
5840 | 1908 |
|
5996 | 1909 |
"Modified: / 12-02-2019 / 17:08:48 / Claus Gittinger" |
4733 | 1910 |
! ! |
1911 |
||
1912 |
!AbstractHierarchicalItem methodsFor:'private-to be redefined'! |
|
1913 |
||
4751
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1914 |
fetchChildren |
6019 | 1915 |
"should return the list of children via the model. |
1916 |
The default here is to return nil (no children). |
|
1917 |
*** redefine in subClass" |
|
1918 |
||
4751
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1919 |
^ nil |
6019 | 1920 |
|
1921 |
"Modified (comment): / 01-03-2019 / 19:53:38 / Claus Gittinger" |
|
4751
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1922 |
! |
a1b6112d33ca
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4749
diff
changeset
|
1923 |
|
4733 | 1924 |
heightOn:aGC |
1925 |
"return the height of the receiver, if it is to be displayed on aGC" |
|
1926 |
||
1927 |
self subclassResponsibility. |
|
1928 |
||
1929 |
"/ could in theory compute it for every draw operation; |
|
1930 |
"/ in practice, this is not a good idea, as it could make drawing very slow |
|
1931 |
"/ (unless that info can be easily computed) |
|
1932 |
||
1933 |
"/ so we force programmers to think about that issue and redefine this method |
|
1934 |
"/ as required. If compuation is really cheap, it can be redefined as: |
|
1935 |
"/ ^ self heightOf:(self label) on:aGC |
|
1936 |
! |
|
1937 |
||
1938 |
isExpanded |
|
1939 |
"returns true if the item is expanded" |
|
1940 |
||
1941 |
self subclassResponsibility |
|
1942 |
! |
|
1943 |
||
1944 |
makeWidthAndHeightUnknown |
|
1945 |
"invalidate any cached with/height information" |
|
1946 |
||
1947 |
"see comments in widthOn/heightOn" |
|
1948 |
self subclassResponsibility |
|
1949 |
! |
|
1950 |
||
1951 |
setExpanded:aBoolean |
|
1952 |
"set expanded flag without any computation or notification. |
|
1953 |
It is left to the subclasses responsibility, where this expanded state is stored; |
|
1954 |
could be in the model (as a list of expanded items), in the item itself (as boolean flag), |
|
1955 |
or somewhere else. |
|
1956 |
For huge trees, it may make sense to not store the expanded flag in a slot |
|
1957 |
(in order to save space). See CompactHierarchicalItem as a clever example of how it can be |
|
1958 |
stored without ANY additional space requirements." |
|
1959 |
||
1960 |
self subclassResponsibility |
|
1961 |
! |
|
1962 |
||
1963 |
widthOn:aGC |
|
1964 |
"return the width of the receiver, if it is to be displayed on aGC" |
|
1965 |
||
1966 |
self subclassResponsibility. |
|
1967 |
||
1968 |
"/ could in theory compute it for every draw operation; |
|
1969 |
"/ in practice, this is not a good idea, as it could make drawing very slow |
|
1970 |
"/ (unless that info can be easily computed) |
|
1971 |
||
1972 |
"/ so we force programmers to think about that issue and redefine this method |
|
1973 |
"/ as required. If compuation is really cheap, it can be redefined as: |
|
1974 |
"/ ^ self widthOf:(self label) on:aGC |
|
1975 |
! ! |
|
1976 |
||
1977 |
!AbstractHierarchicalItem methodsFor:'protocol-accessing'! |
|
1978 |
||
1979 |
children |
|
1980 |
"returns a list of children. When first asked, the list is fetched, if it was |
|
5116 | 1981 |
built lazily. |
4733 | 1982 |
*** to optimize: either redefine this or fetchChildren by subClass" |
1983 |
||
1984 |
children isNil ifTrue:[ |
|
5993 | 1985 |
self setChildren:(self fetchChildren) |
4733 | 1986 |
]. |
1987 |
^ children |
|
5993 | 1988 |
|
1989 |
"Modified: / 03-02-2019 / 19:23:55 / Claus Gittinger" |
|
4733 | 1990 |
! |
1991 |
||
1992 |
icon |
|
1993 |
"returns the icon or nil; |
|
1994 |
*** to optimize:redefine by subClass" |
|
1995 |
||
1996 |
|model| |
|
1997 |
||
1998 |
(model := self model) notNil ifTrue:[ |
|
1999 |
^ model iconFor:self |
|
2000 |
]. |
|
2001 |
^ nil |
|
2002 |
! |
|
2003 |
||
2004 |
label |
|
2005 |
"returns the label displayed on aGC; |
|
2006 |
*** to optimize:redefine by subClass" |
|
2007 |
||
2008 |
|model| |
|
2009 |
||
2010 |
(model := self model) notNil ifTrue:[ |
|
2011 |
^ model labelFor:self |
|
2012 |
]. |
|
2013 |
^ nil |
|
2014 |
! |
|
2015 |
||
2016 |
middleButtonMenu |
|
2017 |
"returns the items middleButtonMenu or nil if no menu is defined. |
|
2018 |
If nil is returned, the view is asked for a menu." |
|
2019 |
||
2020 |
<resource: #programMenu> |
|
2021 |
||
2022 |
|model| |
|
2023 |
||
2024 |
(model := self model) notNil ifTrue:[ |
|
2025 |
^ model middleButtonMenuFor:self |
|
2026 |
]. |
|
2027 |
^ nil |
|
2028 |
! |
|
2029 |
||
2030 |
recursiveSortChildren:aSortBlock |
|
5840 | 2031 |
self synchronized:[ |
2032 |
|children| |
|
2033 |
||
2034 |
(children := self children) notEmptyOrNil ifTrue:[ |
|
4733 | 2035 |
children sort:aSortBlock. |
2036 |
children do:[:aChild| aChild recursiveSortChildren:aSortBlock ] |
|
2037 |
] |
|
2038 |
]. |
|
5840 | 2039 |
|
2040 |
"Modified: / 28-07-2018 / 14:01:15 / Claus Gittinger" |
|
4733 | 2041 |
! |
2042 |
||
2043 |
sortChildren:aSortBlock |
|
2044 |
"sort the children inplace using the 2-arg block sortBlock for comparison" |
|
2045 |
||
2046 |
<resource: #obsolete> |
|
2047 |
self obsoleteMethodWarning:'use #sort:'. |
|
2048 |
self sort:aSortBlock. |
|
2049 |
! ! |
|
2050 |
||
2051 |
!AbstractHierarchicalItem methodsFor:'protocol-displaying'! |
|
2052 |
||
2053 |
displayIcon:anIcon atX:x y:y on:aGC |
|
2054 |
"called to draw the icon - can be redefined to manipulate the icon" |
|
2055 |
||
2056 |
anIcon displayOn:aGC x:x y:y. |
|
2057 |
! |
|
2058 |
||
2059 |
displayOn:aGC x:x y:y h:h |
|
2060 |
<resource: #obsolete> |
|
2061 |
"draw the receiver in the graphicsContext, aGC" |
|
2062 |
||
2063 |
"/ obsolete - left here for backward compatibility |
|
2064 |
self displayOn:aGC x:x y:y h:h isHighlightedAsSelected:false |
|
2065 |
! |
|
2066 |
||
2067 |
displayOn:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
|
2068 |
"draw the receiver in the graphicsContext, aGC" |
|
2069 |
||
2070 |
|label |
|
2071 |
x0 "{ Class:SmallInteger }"| |
|
2072 |
||
2073 |
label := self label. |
|
2074 |
label isNil ifTrue:[^ self]. |
|
2075 |
||
5325 | 2076 |
label isNonByteCollection ifFalse:[ |
4733 | 2077 |
^ self displayLabel:label h:(self heightOn:aGC) on:aGC x:x y:y h:h isHighlightedAsSelected:isHighlightedAsSelected |
2078 |
]. |
|
2079 |
||
2080 |
x0 := x. |
|
2081 |
label do:[:el| |
|
2082 |
el notNil ifTrue:[ |
|
2083 |
self displayLabel:el h:(self heightOf:el on:aGC) on:aGC x:x0 y:y h:h isHighlightedAsSelected:isHighlightedAsSelected. |
|
2084 |
x0 := x0 + 5 + (el widthOn:aGC). |
|
2085 |
]. |
|
2086 |
] |
|
2087 |
! ! |
|
2088 |
||
2089 |
!AbstractHierarchicalItem methodsFor:'protocol-event processing'! |
|
2090 |
||
2091 |
processButtonPress:button visibleX:visX visibleY: visY on: view |
|
2092 |
"A mouse button was pressed on myself. The visX/visY coordinates |
|
2093 |
are relative to the viewOrigin. |
|
2094 |
||
2095 |
If this method returns TRUE, the other method |
|
2096 |
#processButtonPress:x:y:on: IS NOT CALLED !!!!!! |
|
2097 |
" |
|
2098 |
||
2099 |
^false |
|
2100 |
||
2101 |
"Created: / 18-04-2013 / 09:56:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2102 |
"Modified (comment): / 18-04-2013 / 11:04:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2103 |
! |
|
2104 |
||
2105 |
processButtonPress:button x:x y:y |
|
2106 |
"a mouse button was pressed in my label. |
|
2107 |
Return true, if the event is eaten (ignored by the gc). |
|
2108 |
By default, false is returned (should be handled by the gc)." |
|
2109 |
||
2110 |
^ false |
|
2111 |
! |
|
2112 |
||
2113 |
processButtonPress:button x:x y:y on:aGC |
|
2114 |
"a mouse button was pressed in my label. |
|
2115 |
Return true, if the event is eaten (ignored by the gc). |
|
2116 |
By default, false is returned (should be handled by the gc)." |
|
2117 |
||
2118 |
^ self processButtonPress:button x:x y:y |
|
2119 |
! |
|
2120 |
||
2121 |
processButtonPressOnIcon:button on:aGC |
|
2122 |
"a mouse button was pressed in my icon. |
|
2123 |
Return true, if the event is eaten (ignored by the gc). |
|
2124 |
By default, false is returned (should be handled by the gc)." |
|
2125 |
||
2126 |
^ false |
|
2127 |
! ! |
|
2128 |
||
2129 |
!AbstractHierarchicalItem methodsFor:'protocol-monitoring'! |
|
2130 |
||
2131 |
monitoringCycle |
|
2132 |
"called every 'n' seconds by the model, if the monitoring |
|
2133 |
cycle is enabled. The item can perform some checks, .. |
|
2134 |
**** can be redefined by subclass to perform some actions |
|
2135 |
" |
|
2136 |
! ! |
|
2137 |
||
2138 |
!AbstractHierarchicalItem methodsFor:'protocol-queries'! |
|
2139 |
||
2140 |
canCollapse |
|
2141 |
"called before collapsing the item; can be redefined |
|
2142 |
by subclass to omit the collapse operation" |
|
2143 |
||
2144 |
^ self isExpanded |
|
2145 |
! |
|
2146 |
||
2147 |
canExpand |
|
2148 |
"called before expanding the item; can be redefined |
|
2149 |
by subclass to omit the expand operation" |
|
2150 |
||
2151 |
^ self hasChildren |
|
2152 |
! |
|
2153 |
||
2154 |
canRecursiveCollapse |
|
2155 |
"called before collapsing the item; can be redefined |
|
5998 | 2156 |
by subclass to omit the collapse operation. |
2157 |
Q: why is there an extra canRecursiveCollapse (i.e. not always using canCollapse)?" |
|
4733 | 2158 |
|
2159 |
^ self canCollapse |
|
5998 | 2160 |
|
2161 |
"Modified (comment): / 13-02-2019 / 12:14:03 / Claus Gittinger" |
|
4733 | 2162 |
! |
2163 |
||
2164 |
canRecursiveExpand |
|
2165 |
"called before expanding the item; can be redefined |
|
2166 |
by subclass to omit the collapse operation" |
|
2167 |
||
2168 |
^ self canExpand |
|
2169 |
! |
|
2170 |
||
2171 |
drawHorizontalLineUpToText |
|
5802 | 2172 |
"draw the horizontal line for the selected item up to the text |
2173 |
or (by default) to the start of the vertical line; |
|
2174 |
only used by the hierarchical view |
|
4733 | 2175 |
" |
2176 |
^ false |
|
5802 | 2177 |
|
2178 |
"Modified (comment): / 12-06-2018 / 10:41:54 / Claus Gittinger" |
|
4733 | 2179 |
! |
2180 |
||
2181 |
hasChildren |
|
5991 | 2182 |
"checks whether the item has children; |
2183 |
here the children are fetched, and thus this may take some time. |
|
2184 |
*** to optimize: redefine in subClass (example: FileDirectory)" |
|
4733 | 2185 |
|
5720 | 2186 |
^ self children notEmptyOrNil |
2187 |
||
2188 |
"Modified: / 08-03-2018 / 19:37:45 / stefan" |
|
5991 | 2189 |
"Modified (comment): / 02-02-2019 / 14:51:59 / Claus Gittinger" |
4733 | 2190 |
! |
2191 |
||
2192 |
hasIndicator |
|
2193 |
"on default the indicator is drawn if the item has children" |
|
2194 |
||
2195 |
^ self hasChildren |
|
2196 |
! |
|
2197 |
||
2198 |
isSelectable |
|
2199 |
"returns true if the item is selectable. Can be redefined in subclasses" |
|
2200 |
||
2201 |
^ true |
|
2202 |
! |
|
2203 |
||
2204 |
string |
|
2205 |
"access the printable string used for stepping through a list |
|
2206 |
searching for an entry starting with a character. |
|
2207 |
*** to optimize:redefine by subClass" |
|
2208 |
||
2209 |
|label| |
|
2210 |
||
2211 |
(label := self label) notNil ifTrue:[ |
|
2212 |
label isString ifTrue:[ ^ label string ]. |
|
2213 |
label isImageOrForm ifTrue:[ ^ nil ]. |
|
2214 |
||
2215 |
label isSequenceable ifFalse:[ |
|
2216 |
^ label perform:#string ifNotUnderstood:nil |
|
2217 |
]. |
|
2218 |
||
2219 |
label do:[:el||s| |
|
2220 |
(el notNil and:[el isImageOrForm not]) ifTrue:[ |
|
2221 |
s := el perform:#string ifNotUnderstood:nil. |
|
2222 |
s notNil ifTrue:[^ s]. |
|
2223 |
] |
|
2224 |
] |
|
2225 |
]. |
|
2226 |
^ nil |
|
2227 |
! ! |
|
2228 |
||
2229 |
!AbstractHierarchicalItem methodsFor:'queries'! |
|
2230 |
||
2231 |
isChildOf:anItem |
|
5884 | 2232 |
"returns true if the item is a child (or grandchild or ...) of anItem" |
4733 | 2233 |
|
5840 | 2234 |
self synchronized:[ |
2235 |
|item| |
|
2236 |
||
2237 |
item := self. |
|
2238 |
||
2239 |
[anItem ~~ item] whileTrue:[ |
|
2240 |
((item := item parent) notNil and:[item isHierarchicalItem]) ifFalse:[ |
|
2241 |
^ false |
|
2242 |
] |
|
2243 |
]. |
|
4733 | 2244 |
]. |
2245 |
^ true |
|
5840 | 2246 |
|
2247 |
"Modified: / 28-07-2018 / 14:02:32 / Claus Gittinger" |
|
5884 | 2248 |
"Modified (comment): / 12-08-2018 / 12:49:16 / Claus Gittinger" |
4733 | 2249 |
! |
2250 |
||
2251 |
isCollapsed |
|
2252 |
"returns true if the item is collapsed" |
|
2253 |
||
2254 |
^ self isExpanded not |
|
2255 |
! |
|
2256 |
||
2257 |
isDirectoryItem |
|
2258 |
^ false |
|
2259 |
||
2260 |
"Created: / 23-06-2006 / 12:47:05 / fm" |
|
2261 |
"Modified: / 23-02-2007 / 12:04:23 / User" |
|
2262 |
! |
|
2263 |
||
2264 |
isHierarchicalItem |
|
2265 |
"used to decide if the parent is a hierarchical item or the model" |
|
2266 |
||
2267 |
^ true |
|
2268 |
! |
|
2269 |
||
2270 |
isRealChildOf:anItem |
|
2271 |
"returns true if the item is a child of anItem" |
|
2272 |
||
5840 | 2273 |
self synchronized:[ |
2274 |
|item| |
|
2275 |
item := self parent. |
|
2276 |
||
2277 |
[item notNil] whileTrue:[ |
|
2278 |
item == anItem ifTrue:[^ true]. |
|
2279 |
item := item parent. |
|
2280 |
]. |
|
4733 | 2281 |
]. |
2282 |
^ false |
|
5840 | 2283 |
|
2284 |
"Modified: / 28-07-2018 / 14:03:22 / Claus Gittinger" |
|
4733 | 2285 |
! |
2286 |
||
2287 |
isRootItem |
|
2288 |
"returns true if the item is the root item" |
|
2289 |
||
2290 |
^ parent isHierarchicalItem not |
|
2291 |
! |
|
2292 |
||
2293 |
size |
|
2294 |
"return the number of children" |
|
2295 |
||
2296 |
^ self children size |
|
2297 |
! ! |
|
2298 |
||
2299 |
!AbstractHierarchicalItem methodsFor:'searching'! |
|
2300 |
||
2301 |
detect:aOneArgBlock |
|
5363 | 2302 |
"find the first child (not recursive), for which evaluation of the block returns |
4733 | 2303 |
true; if none does so, report an error" |
2304 |
||
2305 |
^ self detect:aOneArgBlock ifNone:[self errorNotFound] |
|
5363 | 2306 |
|
2307 |
"Modified (comment): / 25-11-2016 / 08:40:16 / cg" |
|
4733 | 2308 |
! |
2309 |
||
2310 |
detect:aOneArgBlock ifNone:exceptionValue |
|
5363 | 2311 |
"find the first child (not recursive), for which evaluation of the block returns |
4733 | 2312 |
true; if none does so, return the value of anExceptionValue" |
2313 |
||
2314 |
self do:[:el| |
|
2315 |
(aOneArgBlock value:el) ifTrue:[^ el] |
|
2316 |
]. |
|
2317 |
^ exceptionValue value |
|
5363 | 2318 |
|
2319 |
"Modified (comment): / 25-11-2016 / 08:40:18 / cg" |
|
4733 | 2320 |
! |
2321 |
||
2322 |
detectLast:aOneArgBlock |
|
5363 | 2323 |
"find the last child (not recursive), for which evaluation of the block returns |
4733 | 2324 |
true; if none does so, an exception is raised" |
2325 |
||
2326 |
^ self detectLast:aOneArgBlock ifNone:[self errorNotFound] |
|
5363 | 2327 |
|
2328 |
"Modified (comment): / 25-11-2016 / 08:40:21 / cg" |
|
4733 | 2329 |
! |
2330 |
||
2331 |
detectLast:aOneArgBlock ifNone:anExceptionValue |
|
5363 | 2332 |
"find the last child (not recursive), for which evaluation of the block returns |
4733 | 2333 |
true; if none does so, return the value of anExceptionValue" |
2334 |
||
2335 |
self reverseDo:[:el| (aOneArgBlock value:el) ifTrue:[^ el] ]. |
|
2336 |
^ anExceptionValue value |
|
5363 | 2337 |
|
2338 |
"Modified (comment): / 25-11-2016 / 08:40:23 / cg" |
|
4733 | 2339 |
! |
2340 |
||
5624 | 2341 |
findChildByLabelPath:aLabelPath |
2342 |
"recursivly find the child by its label path. |
|
2343 |
A label path is an ordered collection (or array) of strings, |
|
2344 |
which are the labels of items (i.e. as retrived by labelPath)." |
|
2345 |
||
2346 |
|restPath searchedLabel childSearched| |
|
2347 |
||
2348 |
childSearched := self. |
|
2349 |
restPath := aLabelPath. |
|
2350 |
||
2351 |
[ |
|
2352 |
|subChild| |
|
2353 |
||
2354 |
restPath isEmptyOrNil ifTrue:[^ childSearched]. |
|
2355 |
||
2356 |
searchedLabel := restPath first string. |
|
2357 |
||
2358 |
subChild := childSearched detect:[:eachSubChild | eachSubChild label string = searchedLabel] ifNone:nil. |
|
2359 |
subChild isNil ifTrue:[^ nil]. |
|
2360 |
||
2361 |
childSearched := subChild. |
|
2362 |
restPath := restPath from:2. |
|
2363 |
] loop. |
|
2364 |
||
2365 |
"Created: / 16-10-2017 / 14:30:11 / cg" |
|
2366 |
! |
|
2367 |
||
4733 | 2368 |
findFirst:aOneArgBlock |
5363 | 2369 |
"find the first child (not recursive), for which evaluation of the argument, aOneArgBlock |
4733 | 2370 |
returns true; return its index or 0 if none detected." |
2371 |
||
2372 |
||
2373 |
self keysAndValuesDo:[:i :el| (aOneArgBlock value:el) ifTrue:[^ i] ]. |
|
2374 |
^ 0 |
|
5363 | 2375 |
|
2376 |
"Modified (comment): / 25-11-2016 / 08:40:25 / cg" |
|
4733 | 2377 |
! |
2378 |
||
5363 | 2379 |
findLast:aOneArgBlock |
2380 |
"find the last child (not recursive), for which evaluation of the argument, aOneArgBlock |
|
4733 | 2381 |
returns true; return its index or 0 if none detected." |
2382 |
||
5363 | 2383 |
self keysAndValuesReverseDo:[:i :el| (aOneArgBlock value:el) ifTrue:[^ i] ]. |
4733 | 2384 |
^ 0 |
5363 | 2385 |
|
2386 |
"Modified (format): / 25-11-2016 / 08:40:33 / cg" |
|
4733 | 2387 |
! |
2388 |
||
5864 | 2389 |
findNextInTreeForWhich:aBlock |
2390 |
"does a breadth-first search in the tree starting at the receiver |
|
2391 |
i.e. first goes down recursively, then up to the next child of the parent |
|
2392 |
(doing a recursive search there), |
|
2393 |
up until all of the top item's children have been visited. |
|
2394 |
Returns the found item or nil." |
|
2395 |
||
2396 |
|item nextItem itemsParent| |
|
2397 |
||
2398 |
item := self. |
|
2399 |
[item notNil] whileTrue:[ |
|
2400 |
item recursiveDo:[:eachChild | |
|
2401 |
(aBlock value:eachChild) ifTrue:[ |
|
2402 |
^ eachChild. |
|
2403 |
]. |
|
2404 |
]. |
|
2405 |
||
2406 |
itemsParent := item parent. |
|
2407 |
nextItem := nil. |
|
2408 |
||
2409 |
[ |
|
2410 |
|idx siblings| |
|
2411 |
||
2412 |
itemsParent isNil ifTrue:[ |
|
2413 |
^ nil |
|
2414 |
]. |
|
2415 |
siblings := itemsParent children. |
|
2416 |
idx := siblings identityIndexOf:item. |
|
2417 |
nextItem := siblings at:idx+1 ifAbsent:nil. |
|
2418 |
nextItem isNil ifTrue:[ |
|
2419 |
item := itemsParent. |
|
2420 |
itemsParent := itemsParent parent. |
|
2421 |
]. |
|
2422 |
] doWhile:[ nextItem isNil ]. |
|
2423 |
||
2424 |
item := nextItem. |
|
2425 |
(aBlock value:item) ifTrue:[ |
|
2426 |
^ item. |
|
2427 |
]. |
|
2428 |
]. |
|
2429 |
^ nil |
|
2430 |
||
2431 |
"Created: / 31-07-2018 / 09:01:11 / Claus Gittinger" |
|
2432 |
! |
|
2433 |
||
5885 | 2434 |
findPreviousInTreeForWhich:aBlock |
2435 |
"does a breadth-first search in the tree starting at the receiver |
|
2436 |
i.e. first goes down recursively, then up to the next child of the parent |
|
2437 |
(doing a recursive search there), |
|
2438 |
up until all of the top item's children have been visited. |
|
2439 |
Returns the found item or nil." |
|
2440 |
||
2441 |
|item itemsParent idx siblings prevItem| |
|
2442 |
||
2443 |
item := self. |
|
2444 |
[item notNil] whileTrue:[ |
|
2445 |
"/ previous sibling |
|
2446 |
itemsParent := item parent. |
|
2447 |
itemsParent isNil ifTrue:[ |
|
2448 |
^ nil |
|
2449 |
]. |
|
2450 |
siblings := itemsParent children. |
|
2451 |
idx := siblings identityIndexOf:item. |
|
2452 |
idx <= 1 ifTrue:[ |
|
2453 |
"/ continue in parent |
|
2454 |
item := itemsParent. |
|
2455 |
] ifFalse:[ |
|
2456 |
prevItem := siblings at:idx-1 ifAbsent:nil. |
|
2457 |
"/ continue in prev sibling's last child |
|
2458 |
item := prevItem. |
|
2459 |
[item children notEmptyOrNil] whileTrue:[ |
|
2460 |
item := item children last. |
|
2461 |
] |
|
2462 |
]. |
|
2463 |
item isNil ifTrue:[^ nil]. |
|
2464 |
item isRootItem ifTrue:[^ nil]. |
|
2465 |
||
2466 |
(aBlock value:item) ifTrue:[ |
|
2467 |
^ item. |
|
2468 |
]. |
|
2469 |
]. |
|
2470 |
^ nil |
|
2471 |
||
2472 |
"Created: / 13-08-2018 / 10:59:12 / Claus Gittinger" |
|
2473 |
! |
|
2474 |
||
4733 | 2475 |
identityIndexOf:aChild |
2476 |
"return the index of aChild or 0 if not found. Compare using ==" |
|
2477 |
||
2478 |
^ self identityIndexOf:aChild startingAt:1 |
|
2479 |
! |
|
2480 |
||
2481 |
identityIndexOf:aChild startingAt:startIndex |
|
2482 |
"return the index of aChild, starting search at startIndex. |
|
2483 |
Compare using ==; return 0 if not found" |
|
2484 |
||
2485 |
|index| |
|
2486 |
||
2487 |
index := startIndex. |
|
2488 |
||
2489 |
self from:startIndex do:[:el| |
|
2490 |
el == aChild ifTrue:[^ index ]. |
|
2491 |
index := index + 1. |
|
2492 |
]. |
|
2493 |
^ 0 |
|
2494 |
! |
|
2495 |
||
2496 |
recursiveDetect:aOneArgBlock |
|
2497 |
"recursive find the first child, for which evaluation |
|
2498 |
of the block returns true; if none, nil is returned. |
|
2499 |
Warning: this only searches in already visible child elements |
|
2500 |
i.e. any collapsed items are not searched." |
|
2501 |
||
2502 |
self recursiveDo:[:aChild| |
|
2503 |
(aOneArgBlock value:aChild) ifTrue:[^ aChild] |
|
2504 |
]. |
|
2505 |
^ nil |
|
2506 |
! |
|
2507 |
||
2508 |
recursiveDetectLast:aBlock |
|
5363 | 2509 |
"recursive find the last child, for which evaluation of the block returns true; |
2510 |
if none does so, nil is returned. |
|
2511 |
Warning: this only searches in already visible child elements |
|
2512 |
i.e. any collapsed items are not searched." |
|
4733 | 2513 |
|
2514 |
self recursiveReverseDo:[:aChild| |
|
2515 |
(aBlock value:aChild) ifTrue:[^ aChild]. |
|
2516 |
]. |
|
2517 |
^ nil |
|
5363 | 2518 |
|
2519 |
"Modified (comment): / 25-11-2016 / 08:41:42 / cg" |
|
4733 | 2520 |
! |
2521 |
||
2522 |
withAllDetect:aOneArgBlock |
|
5363 | 2523 |
"recursive find the first item including self, |
2524 |
for which evaluation of the block returns true; if none nil is returned. |
|
2525 |
Warning: this only searches in already visible child elements |
|
2526 |
i.e. any collapsed items are not searched." |
|
4733 | 2527 |
|
2528 |
(aOneArgBlock value:self) ifTrue:[^ self]. |
|
2529 |
||
2530 |
^ self recursiveDetect:aOneArgBlock |
|
5363 | 2531 |
|
2532 |
"Modified (comment): / 25-11-2016 / 08:41:53 / cg" |
|
4733 | 2533 |
! ! |
2534 |
||
2535 |
!AbstractHierarchicalItem methodsFor:'sorting & reordering'! |
|
2536 |
||
2537 |
recursiveSort:aSortBlock |
|
2538 |
"recursive sort the children inplace using the 2-arg block sortBlock for comparison" |
|
2539 |
||
5840 | 2540 |
children notEmptyOrNil ifTrue:[ |
2541 |
self synchronized:[ |
|
2542 |
"/ check again (asynchronous update was possible before) |
|
2543 |
children notEmptyOrNil ifTrue:[ |
|
2544 |
self nonCriticalRecursiveSort:aSortBlock. |
|
2545 |
self childrenOrderChanged. |
|
2546 |
] |
|
4733 | 2547 |
] |
2548 |
]. |
|
5840 | 2549 |
|
2550 |
"Modified (format): / 28-07-2018 / 15:46:06 / Claus Gittinger" |
|
4733 | 2551 |
! |
2552 |
||
2553 |
sort:aSortBlock |
|
2554 |
"sort the children inplace using the 2-arg block sortBlock for comparison" |
|
2555 |
||
2556 |
children notEmptyOrNil ifTrue: [ |
|
5840 | 2557 |
self synchronized:[ |
4733 | 2558 |
"/ check again (asynchronous update was possible before) |
2559 |
children notEmptyOrNil ifTrue: [ |
|
5993 | 2560 |
self setChildren:(children sort:aSortBlock). |
4733 | 2561 |
self childrenOrderChanged. |
2562 |
] |
|
2563 |
]. |
|
2564 |
] |
|
5840 | 2565 |
|
5993 | 2566 |
"Modified: / 03-02-2019 / 19:24:20 / Claus Gittinger" |
4733 | 2567 |
! ! |
2568 |
||
2569 |
!AbstractHierarchicalItem class methodsFor:'documentation'! |
|
2570 |
||
2571 |
version |
|
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
2572 |
^ '$Header$' |
4733 | 2573 |
! |
2574 |
||
2575 |
version_CVS |
|
4795
eead012852f9
class: AbstractHierarchicalItem
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
2576 |
^ '$Header$' |
4733 | 2577 |
! ! |
2578 |