author | Claus Gittinger <cg@exept.de> |
Thu, 20 May 1999 17:24:18 +0200 | |
changeset 1909 | 27778cfa4bda |
parent 1541 | d90417495d52 |
child 2251 | 419939054e77 |
permissions | -rw-r--r-- |
133 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
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 |
" |
|
114 | 12 |
|
129 | 13 |
ValueHolder subclass:#SelectionInList |
359 | 14 |
instanceVariableNames:'listHolder selectionIndexHolder' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Interface-Support-Models' |
|
114 | 18 |
! |
19 |
||
133 | 20 |
!SelectionInList class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
Instances of SelectionInList can be used as model for |
|
39 |
a SelectionInListView or a PopUpList. |
|
40 |
They keep two values: a list value and a selection value; |
|
41 |
both are referred to via valueHolders. |
|
42 |
||
577 | 43 |
If any of those two changes, the selectionInList notifies its |
44 |
dependents via a change notification, |
|
45 |
using #list or #selectionIndex as update aspect respectively. |
|
46 |
||
47 |
A popupList also knows how to deal with a selectionInList model; |
|
133 | 48 |
this makes it possible to have popupLists be somewhat exchangable |
49 |
with selectionInListViews. |
|
576 | 50 |
|
577 | 51 |
SelectionInLists only support a single selection within the list; |
52 |
use MultiSelectionInList, if multiple selections are needed. |
|
53 |
||
54 |
[instance variables:] |
|
55 |
listHolder <ValueHolder> holds the list |
|
56 |
selectionIndexHolder <ValueHolder> holdes the selectionIndex |
|
57 |
||
58 |
||
576 | 59 |
[see also:] |
577 | 60 |
SelectionInListView PopUpList |
61 |
MultiSelectionInList Model ValueHolder |
|
583 | 62 |
|
63 |
[author:] |
|
64 |
Claus Gittinger |
|
133 | 65 |
" |
66 |
! |
|
67 |
||
68 |
examples |
|
69 |
" |
|
576 | 70 |
basic setup using a selectionInList as model of a selectionInListView: |
590 | 71 |
[exBegin] |
133 | 72 |
|m v| |
73 |
||
74 |
m := SelectionInList new. |
|
75 |
m list:#('one' 'two' 'three' 'four'). |
|
76 |
m selectionIndex:2. |
|
77 |
||
78 |
v := SelectionInListView on:m. |
|
79 |
v open |
|
590 | 80 |
[exEnd] |
133 | 81 |
|
576 | 82 |
|
83 |
similar, a selectionInList as model of a popUpList: |
|
590 | 84 |
[exBegin] |
576 | 85 |
|m v| |
86 |
||
87 |
m := SelectionInList new. |
|
88 |
m list:#('one' 'two' 'three' 'four'). |
|
89 |
m selectionIndex:2. |
|
90 |
||
91 |
v := PopUpList on:m. |
|
92 |
v open |
|
590 | 93 |
[exEnd] |
576 | 94 |
|
95 |
||
96 |
using a combination-instance creation method: |
|
590 | 97 |
[exBegin] |
576 | 98 |
|m v| |
99 |
||
100 |
m := SelectionInList |
|
101 |
with:#('one' 'two' 'three' 'four') |
|
102 |
initialSelection:2. |
|
103 |
||
104 |
v := PopUpList on:m. |
|
105 |
v open |
|
590 | 106 |
[exEnd] |
576 | 107 |
|
108 |
||
109 |
two different views on the same selectionInList model: |
|
590 | 110 |
[exBegin] |
576 | 111 |
|m v1 v2| |
112 |
||
113 |
m := SelectionInList new. |
|
114 |
m list:#('one' 'two' 'three' 'four'). |
|
115 |
m selectionIndex:2. |
|
116 |
||
117 |
v1 := PopUpList on:m. |
|
118 |
v1 open. |
|
119 |
||
120 |
v2 := SelectionInListView on:m. |
|
121 |
v2 open |
|
590 | 122 |
[exEnd] |
576 | 123 |
|
124 |
||
133 | 125 |
two views on the same selectionInList: |
126 |
and a button, which adds an item to the list. |
|
590 | 127 |
[exBegin] |
133 | 128 |
|m v1 v2 b numItems| |
129 |
||
130 |
numItems := 4. |
|
131 |
||
132 |
m := SelectionInList new. |
|
133 |
m list:((1 to:numItems) collect:[:i | i printString]). |
|
134 |
m selectionIndex:2. |
|
135 |
||
136 |
v1 := ScrollableView forView:(SelectionInListView on:m). |
|
137 |
v1 open. |
|
138 |
||
139 |
v2 := ScrollableView forView:(SelectionInListView on:m). |
|
140 |
v2 open. |
|
141 |
||
142 |
b := Button label:'add item'. |
|
143 |
b action:[numItems := numItems + 1. |
|
576 | 144 |
m list:((1 to:numItems) collect:[:i | i printString]). |
145 |
]. |
|
133 | 146 |
b open |
590 | 147 |
[exEnd] |
133 | 148 |
" |
149 |
! ! |
|
150 |
||
114 | 151 |
!SelectionInList class methodsFor:'instance creation'! |
152 |
||
576 | 153 |
with:aList |
154 |
"return a new instance holding aList" |
|
155 |
||
1541 | 156 |
^ self new listHolder:(ValueHolder with:aList) |
576 | 157 |
|
1541 | 158 |
"Modified: / 21.5.1998 / 03:17:16 / cg" |
576 | 159 |
! |
160 |
||
161 |
with:aList initialSelection:index |
|
162 |
"return a new instance holding aList and initially selecting |
|
163 |
the item at index." |
|
164 |
||
165 |
^ (self with:aList) |
|
1541 | 166 |
selectionIndexHolder:(ValueHolder with:index) |
576 | 167 |
|
1541 | 168 |
"Created: / 24.4.1996 / 08:47:33 / cg" |
169 |
"Modified: / 21.5.1998 / 03:17:31 / cg" |
|
114 | 170 |
! ! |
171 |
||
200 | 172 |
!SelectionInList methodsFor:'accessing-holders'! |
173 |
||
174 |
listHolder |
|
576 | 175 |
"return the valueHolder which holds the list" |
200 | 176 |
|
177 |
^ listHolder |
|
576 | 178 |
|
179 |
"Modified: 24.4.1996 / 08:39:44 / cg" |
|
200 | 180 |
! |
181 |
||
182 |
listHolder:aValueHolder |
|
576 | 183 |
"set the valueHolder which holds the list. |
200 | 184 |
Q: should we forward a change-notification ?" |
185 |
||
186 |
listHolder notNil ifTrue:[ |
|
576 | 187 |
listHolder removeDependent:self |
200 | 188 |
]. |
189 |
listHolder := aValueHolder. |
|
190 |
listHolder addDependent:self |
|
576 | 191 |
|
192 |
"Modified: 24.4.1996 / 08:39:59 / cg" |
|
200 | 193 |
! |
114 | 194 |
|
200 | 195 |
selectionHolder |
576 | 196 |
"return someone holding on the selection itself (not the index). |
197 |
Since we have no one, create an adapter, to get up-to-date values." |
|
200 | 198 |
|
199 |
^ AspectAdaptor |
|
544 | 200 |
subject:self |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
201 |
sendsUpdates:false |
544 | 202 |
accessWith:#selection |
203 |
assignWith:#'selection:' |
|
204 |
aspect:#selectionIndex |
|
205 |
||
576 | 206 |
"Modified: 24.4.1996 / 08:40:19 / cg" |
200 | 207 |
! |
208 |
||
209 |
selectionIndexHolder |
|
576 | 210 |
"return the valueHolder which holds the index" |
200 | 211 |
|
212 |
^ selectionIndexHolder |
|
576 | 213 |
|
214 |
"Modified: 24.4.1996 / 08:40:31 / cg" |
|
200 | 215 |
! |
216 |
||
217 |
selectionIndexHolder:aValueHolder |
|
576 | 218 |
"set the valueHolder which holdes the index. |
200 | 219 |
Q: should we forward a change-notification ?" |
220 |
||
221 |
selectionIndexHolder notNil ifTrue:[ |
|
576 | 222 |
selectionIndexHolder removeDependent:self |
200 | 223 |
]. |
224 |
selectionIndexHolder := aValueHolder. |
|
225 |
selectionIndexHolder addDependent:self |
|
576 | 226 |
|
227 |
"Modified: 24.4.1996 / 08:40:42 / cg" |
|
114 | 228 |
! ! |
229 |
||
133 | 230 |
!SelectionInList methodsFor:'accessing-values'! |
114 | 231 |
|
129 | 232 |
list |
576 | 233 |
"return the list - thats the thingy held by the listHolder" |
234 |
||
129 | 235 |
^ listHolder value |
576 | 236 |
|
237 |
"Modified: 24.4.1996 / 08:41:05 / cg" |
|
129 | 238 |
! |
239 |
||
133 | 240 |
list:aCollection |
577 | 241 |
"set the list - thats the thingy held by the listHolder" |
242 |
||
1436
72e392844594
send change notification if same list is assigned
Claus Gittinger <cg@exept.de>
parents:
1412
diff
changeset
|
243 |
aCollection == listHolder value ifTrue:[ |
72e392844594
send change notification if same list is assigned
Claus Gittinger <cg@exept.de>
parents:
1412
diff
changeset
|
244 |
"/ same value set again - send change notification |
1440 | 245 |
"/ manually (valueHolder does not ...) |
246 |
self changed:#list. |
|
247 |
listHolder changed. |
|
1436
72e392844594
send change notification if same list is assigned
Claus Gittinger <cg@exept.de>
parents:
1412
diff
changeset
|
248 |
] ifFalse:[ |
72e392844594
send change notification if same list is assigned
Claus Gittinger <cg@exept.de>
parents:
1412
diff
changeset
|
249 |
listHolder value:aCollection. |
72e392844594
send change notification if same list is assigned
Claus Gittinger <cg@exept.de>
parents:
1412
diff
changeset
|
250 |
]. |
577 | 251 |
|
1440 | 252 |
"Modified: / 2.2.1998 / 13:05:56 / cg" |
133 | 253 |
! |
254 |
||
255 |
selection |
|
577 | 256 |
"return the selections value (i.e. the entry in the list - not its index). |
257 |
If nothing is selected, nil is returned." |
|
129 | 258 |
|
259 |
|idx| |
|
260 |
||
261 |
idx := self selectionIndex. |
|
262 |
(idx isNil or:[idx == 0]) ifTrue:[^ nil]. |
|
263 |
^ self list at:idx |
|
576 | 264 |
|
577 | 265 |
"Modified: 24.4.1996 / 08:53:23 / cg" |
114 | 266 |
! |
267 |
||
133 | 268 |
selection:anObject |
269 |
"set the selection to be anObject. |
|
270 |
If anObject is not in the list, the selection is cleared" |
|
271 |
||
272 |
^ self selectionIndex:(self list indexOf:anObject ifAbsent:0) |
|
200 | 273 |
! |
274 |
||
576 | 275 |
selectionIndex |
577 | 276 |
"return the selections index (1..). Thats the thingy held by the indexHolder. |
277 |
For ST-80 compatibility, 0 is returned if nothing is selected." |
|
576 | 278 |
|
200 | 279 |
^ selectionIndexHolder value |
576 | 280 |
|
577 | 281 |
"Modified: 24.4.1996 / 08:53:45 / cg" |
200 | 282 |
! |
283 |
||
576 | 284 |
selectionIndex:newIndex |
285 |
"set the selectionIndex" |
|
286 |
||
200 | 287 |
selectionIndexHolder value ~= newIndex ifTrue:[ |
576 | 288 |
selectionIndexHolder value:newIndex |
200 | 289 |
] |
576 | 290 |
|
291 |
"Modified: 24.4.1996 / 08:42:04 / cg" |
|
133 | 292 |
! ! |
293 |
||
294 |
!SelectionInList methodsFor:'change & update'! |
|
295 |
||
296 |
update:something with:aParameter from:changedObject |
|
297 |
"whenever one of my holders value changes, |
|
298 |
tell my dependents about this" |
|
299 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
300 |
|oldSelection| |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
301 |
|
133 | 302 |
changedObject == selectionIndexHolder ifTrue:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
303 |
self changed:#selectionIndex |
133 | 304 |
] ifFalse:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
305 |
changedObject == listHolder ifTrue:[ |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
306 |
something == #value ifTrue:[ |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
307 |
oldSelection := selectionIndexHolder value. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
308 |
self clearSelection. "/ clears without update |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
309 |
self changed:#list. |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
310 |
oldSelection ~= (selectionIndexHolder value) ifTrue:[ |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
311 |
selectionIndexHolder changed:#value |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
312 |
] |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
313 |
] |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
314 |
] |
133 | 315 |
] |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
316 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
317 |
"Modified: 20.4.1996 / 13:08:32 / cg" |
133 | 318 |
! ! |
319 |
||
200 | 320 |
!SelectionInList methodsFor:'initialization'! |
321 |
||
322 |
initialize |
|
576 | 323 |
"initialize; create the valueHolders for the index and the list" |
324 |
||
1541 | 325 |
self listHolder:(ValueHolder with:List new). |
326 |
self selectionIndexHolder:(ValueHolder with:self zeroIndex). |
|
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
327 |
|
1541 | 328 |
"Modified: / 21.5.1998 / 03:17:56 / cg" |
200 | 329 |
! ! |
330 |
||
133 | 331 |
!SelectionInList methodsFor:'obsolete backward compatibility'! |
332 |
||
333 |
index |
|
576 | 334 |
"return the selections index. |
335 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 336 |
|
337 |
self obsoleteMethodWarning:'use #selectionIndex'. |
|
338 |
^ self selectionIndex |
|
576 | 339 |
|
340 |
"Modified: 24.4.1996 / 08:43:10 / cg" |
|
114 | 341 |
! |
342 |
||
129 | 343 |
index:newIndex |
576 | 344 |
"set the selections index. |
345 |
This is an OBSOLETE backward compatibility interface" |
|
129 | 346 |
|
133 | 347 |
self obsoleteMethodWarning:'use #selectionIndex:'. |
129 | 348 |
^ self selectionIndex:newIndex |
576 | 349 |
|
350 |
"Modified: 24.4.1996 / 08:43:06 / cg" |
|
129 | 351 |
! |
352 |
||
133 | 353 |
indexHolder |
576 | 354 |
"return the valueHolder of the selections index. |
355 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 356 |
|
357 |
self obsoleteMethodWarning:'use #selectionIndexHolder'. |
|
358 |
^ self selectionIndexHolder |
|
576 | 359 |
|
360 |
"Modified: 24.4.1996 / 08:43:35 / cg" |
|
129 | 361 |
! |
362 |
||
133 | 363 |
indexHolder:aValueHolder |
576 | 364 |
"set the valueHolder of the selections index. |
365 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 366 |
|
367 |
self obsoleteMethodWarning:'use #selectionIndexHolder:'. |
|
368 |
^ self selectionIndexHolder:aValueHolder |
|
576 | 369 |
|
370 |
"Modified: 24.4.1996 / 08:43:43 / cg" |
|
133 | 371 |
! ! |
372 |
||
1032 | 373 |
!SelectionInList methodsFor:'printing & storing'! |
374 |
||
375 |
displayString |
|
1332 | 376 |
"return a string for display in inspectors etc." |
377 |
||
1032 | 378 |
^ self class name , '(' , self selectionIndex displayString , ')' |
379 |
||
1332 | 380 |
"Modified: 20.9.1997 / 11:41:31 / cg" |
1032 | 381 |
! ! |
382 |
||
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
383 |
!SelectionInList methodsFor:'private'! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
384 |
|
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
385 |
clearSelection |
576 | 386 |
"clear the selection. |
387 |
For ST-80 compatibility, a non-selection has an index of 0 |
|
388 |
although, nil sounds more natural to me ... (sigh)" |
|
389 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
390 |
selectionIndexHolder setValue:self zeroIndex. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
391 |
|
576 | 392 |
"Modified: 24.4.1996 / 08:44:37 / cg" |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
393 |
! ! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
394 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
395 |
!SelectionInList methodsFor:'queries'! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
396 |
|
1412 | 397 |
hasSelection |
398 |
"return true, if there is a selection |
|
399 |
" |
|
400 |
^ self numberOfSelections ~~ 0 |
|
401 |
! |
|
402 |
||
403 |
numberOfSelections |
|
404 |
"return the number of selected entries |
|
405 |
" |
|
406 |
|s| |
|
407 |
||
408 |
s:= selectionIndexHolder value. |
|
409 |
^ (s ~~ 0 and:[s notNil]) ifTrue:[1] ifFalse:[0] |
|
410 |
! |
|
411 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
412 |
zeroIndex |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
413 |
"return the selections index returned when nothing |
576 | 414 |
is selected. This method is provided to allow applications |
415 |
to deal transparently with SelectionInList models AND with |
|
416 |
MultSelectionInList models, which use different no-selection values. |
|
417 |
Although I would prefer nil, ST-80 uses 0 to represent `no-selection'. (sigh)" |
|
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
418 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
419 |
^ 0 |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
420 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
421 |
"Created: 20.4.1996 / 13:10:53 / cg" |
576 | 422 |
"Modified: 24.4.1996 / 08:46:18 / cg" |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
423 |
! ! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
424 |
|
204 | 425 |
!SelectionInList class methodsFor:'documentation'! |
426 |
||
427 |
version |
|
1541 | 428 |
^ '$Header: /cvs/stx/stx/libwidg/SelectionInList.st,v 1.24 1998-05-21 01:18:07 cg Exp $' |
204 | 429 |
! ! |