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