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