author | Claus Gittinger <cg@exept.de> |
Sat, 25 Jan 1997 18:51:44 +0100 | |
changeset 974 | 1feaad80ab19 |
parent 590 | 8392a7c03352 |
child 975 | b0864b03af4c |
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 |
||
156 |
^ self new |
|
974 | 157 |
listHolder:aList asValue |
576 | 158 |
|
974 | 159 |
"Modified: 25.1.1997 / 18:49:53 / cg" |
576 | 160 |
! |
161 |
||
162 |
with:aList initialSelection:index |
|
163 |
"return a new instance holding aList and initially selecting |
|
164 |
the item at index." |
|
165 |
||
166 |
^ (self with:aList) |
|
167 |
selectionIndexHolder:(ValueHolder with:index) |
|
168 |
||
169 |
"Created: 24.4.1996 / 08:47:33 / 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 |
||
133 | 243 |
listHolder value:aCollection. |
577 | 244 |
|
245 |
"Modified: 24.4.1996 / 08:54:06 / cg" |
|
133 | 246 |
! |
247 |
||
248 |
selection |
|
577 | 249 |
"return the selections value (i.e. the entry in the list - not its index). |
250 |
If nothing is selected, nil is returned." |
|
129 | 251 |
|
252 |
|idx| |
|
253 |
||
254 |
idx := self selectionIndex. |
|
255 |
(idx isNil or:[idx == 0]) ifTrue:[^ nil]. |
|
256 |
^ self list at:idx |
|
576 | 257 |
|
577 | 258 |
"Modified: 24.4.1996 / 08:53:23 / cg" |
114 | 259 |
! |
260 |
||
133 | 261 |
selection:anObject |
262 |
"set the selection to be anObject. |
|
263 |
If anObject is not in the list, the selection is cleared" |
|
264 |
||
265 |
^ self selectionIndex:(self list indexOf:anObject ifAbsent:0) |
|
200 | 266 |
! |
267 |
||
576 | 268 |
selectionIndex |
577 | 269 |
"return the selections index (1..). Thats the thingy held by the indexHolder. |
270 |
For ST-80 compatibility, 0 is returned if nothing is selected." |
|
576 | 271 |
|
200 | 272 |
^ selectionIndexHolder value |
576 | 273 |
|
577 | 274 |
"Modified: 24.4.1996 / 08:53:45 / cg" |
200 | 275 |
! |
276 |
||
576 | 277 |
selectionIndex:newIndex |
278 |
"set the selectionIndex" |
|
279 |
||
200 | 280 |
selectionIndexHolder value ~= newIndex ifTrue:[ |
576 | 281 |
selectionIndexHolder value:newIndex |
200 | 282 |
] |
576 | 283 |
|
284 |
"Modified: 24.4.1996 / 08:42:04 / cg" |
|
133 | 285 |
! ! |
286 |
||
287 |
!SelectionInList methodsFor:'change & update'! |
|
288 |
||
289 |
update:something with:aParameter from:changedObject |
|
290 |
"whenever one of my holders value changes, |
|
291 |
tell my dependents about this" |
|
292 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
293 |
|oldSelection| |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
294 |
|
133 | 295 |
changedObject == selectionIndexHolder ifTrue:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
296 |
self changed:#selectionIndex |
133 | 297 |
] ifFalse:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
298 |
changedObject == listHolder ifTrue:[ |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
299 |
something == #value ifTrue:[ |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
300 |
oldSelection := selectionIndexHolder value. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
301 |
self clearSelection. "/ clears without update |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
302 |
self changed:#list. |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
303 |
oldSelection ~= (selectionIndexHolder value) ifTrue:[ |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
304 |
selectionIndexHolder changed:#value |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
305 |
] |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
306 |
] |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
307 |
] |
133 | 308 |
] |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
309 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
310 |
"Modified: 20.4.1996 / 13:08:32 / cg" |
133 | 311 |
! ! |
312 |
||
200 | 313 |
!SelectionInList methodsFor:'initialization'! |
314 |
||
315 |
initialize |
|
576 | 316 |
"initialize; create the valueHolders for the index and the list" |
317 |
||
200 | 318 |
self listHolder:(nil asValue). "/ could also use an empty collection here |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
319 |
self selectionIndexHolder:(self zeroIndex asValue). |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
320 |
|
576 | 321 |
"Modified: 24.4.1996 / 08:42:33 / cg" |
200 | 322 |
! ! |
323 |
||
133 | 324 |
!SelectionInList methodsFor:'obsolete backward compatibility'! |
325 |
||
326 |
index |
|
576 | 327 |
"return the selections index. |
328 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 329 |
|
330 |
self obsoleteMethodWarning:'use #selectionIndex'. |
|
331 |
^ self selectionIndex |
|
576 | 332 |
|
333 |
"Modified: 24.4.1996 / 08:43:10 / cg" |
|
114 | 334 |
! |
335 |
||
129 | 336 |
index:newIndex |
576 | 337 |
"set the selections index. |
338 |
This is an OBSOLETE backward compatibility interface" |
|
129 | 339 |
|
133 | 340 |
self obsoleteMethodWarning:'use #selectionIndex:'. |
129 | 341 |
^ self selectionIndex:newIndex |
576 | 342 |
|
343 |
"Modified: 24.4.1996 / 08:43:06 / cg" |
|
129 | 344 |
! |
345 |
||
133 | 346 |
indexHolder |
576 | 347 |
"return the valueHolder of the selections index. |
348 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 349 |
|
350 |
self obsoleteMethodWarning:'use #selectionIndexHolder'. |
|
351 |
^ self selectionIndexHolder |
|
576 | 352 |
|
353 |
"Modified: 24.4.1996 / 08:43:35 / cg" |
|
129 | 354 |
! |
355 |
||
133 | 356 |
indexHolder:aValueHolder |
576 | 357 |
"set the valueHolder of the selections index. |
358 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 359 |
|
360 |
self obsoleteMethodWarning:'use #selectionIndexHolder:'. |
|
361 |
^ self selectionIndexHolder:aValueHolder |
|
576 | 362 |
|
363 |
"Modified: 24.4.1996 / 08:43:43 / cg" |
|
133 | 364 |
! ! |
365 |
||
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
366 |
!SelectionInList methodsFor:'private'! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
367 |
|
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
368 |
clearSelection |
576 | 369 |
"clear the selection. |
370 |
For ST-80 compatibility, a non-selection has an index of 0 |
|
371 |
although, nil sounds more natural to me ... (sigh)" |
|
372 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
373 |
selectionIndexHolder setValue:self zeroIndex. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
374 |
|
576 | 375 |
"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
|
376 |
! ! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
377 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
378 |
!SelectionInList methodsFor:'queries'! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
379 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
380 |
zeroIndex |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
381 |
"return the selections index returned when nothing |
576 | 382 |
is selected. This method is provided to allow applications |
383 |
to deal transparently with SelectionInList models AND with |
|
384 |
MultSelectionInList models, which use different no-selection values. |
|
385 |
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
|
386 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
387 |
^ 0 |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
388 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
389 |
"Created: 20.4.1996 / 13:10:53 / cg" |
576 | 390 |
"Modified: 24.4.1996 / 08:46:18 / cg" |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
391 |
! ! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
392 |
|
204 | 393 |
!SelectionInList class methodsFor:'documentation'! |
394 |
||
395 |
version |
|
974 | 396 |
^ '$Header: /cvs/stx/stx/libwidg/SelectionInList.st,v 1.16 1997-01-25 17:51:44 cg Exp $' |
204 | 397 |
! ! |