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