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