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