author | ca |
Fri, 10 Oct 1997 15:05:02 +0200 | |
changeset 1340 | 3a659d298b3d |
parent 1332 | 0794ee877118 |
child 1412 | e2134a704cca |
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) |
|
975 | 167 |
selectionIndexHolder:index asValue |
576 | 168 |
|
169 |
"Created: 24.4.1996 / 08:47:33 / cg" |
|
975 | 170 |
"Modified: 25.1.1997 / 18:51:56 / cg" |
114 | 171 |
! ! |
172 |
||
200 | 173 |
!SelectionInList methodsFor:'accessing-holders'! |
174 |
||
175 |
listHolder |
|
576 | 176 |
"return the valueHolder which holds the list" |
200 | 177 |
|
178 |
^ listHolder |
|
576 | 179 |
|
180 |
"Modified: 24.4.1996 / 08:39:44 / cg" |
|
200 | 181 |
! |
182 |
||
183 |
listHolder:aValueHolder |
|
576 | 184 |
"set the valueHolder which holds the list. |
200 | 185 |
Q: should we forward a change-notification ?" |
186 |
||
187 |
listHolder notNil ifTrue:[ |
|
576 | 188 |
listHolder removeDependent:self |
200 | 189 |
]. |
190 |
listHolder := aValueHolder. |
|
191 |
listHolder addDependent:self |
|
576 | 192 |
|
193 |
"Modified: 24.4.1996 / 08:39:59 / cg" |
|
200 | 194 |
! |
114 | 195 |
|
200 | 196 |
selectionHolder |
576 | 197 |
"return someone holding on the selection itself (not the index). |
198 |
Since we have no one, create an adapter, to get up-to-date values." |
|
200 | 199 |
|
200 |
^ AspectAdaptor |
|
544 | 201 |
subject:self |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
202 |
sendsUpdates:false |
544 | 203 |
accessWith:#selection |
204 |
assignWith:#'selection:' |
|
205 |
aspect:#selectionIndex |
|
206 |
||
576 | 207 |
"Modified: 24.4.1996 / 08:40:19 / cg" |
200 | 208 |
! |
209 |
||
210 |
selectionIndexHolder |
|
576 | 211 |
"return the valueHolder which holds the index" |
200 | 212 |
|
213 |
^ selectionIndexHolder |
|
576 | 214 |
|
215 |
"Modified: 24.4.1996 / 08:40:31 / cg" |
|
200 | 216 |
! |
217 |
||
218 |
selectionIndexHolder:aValueHolder |
|
576 | 219 |
"set the valueHolder which holdes the index. |
200 | 220 |
Q: should we forward a change-notification ?" |
221 |
||
222 |
selectionIndexHolder notNil ifTrue:[ |
|
576 | 223 |
selectionIndexHolder removeDependent:self |
200 | 224 |
]. |
225 |
selectionIndexHolder := aValueHolder. |
|
226 |
selectionIndexHolder addDependent:self |
|
576 | 227 |
|
228 |
"Modified: 24.4.1996 / 08:40:42 / cg" |
|
114 | 229 |
! ! |
230 |
||
133 | 231 |
!SelectionInList methodsFor:'accessing-values'! |
114 | 232 |
|
129 | 233 |
list |
576 | 234 |
"return the list - thats the thingy held by the listHolder" |
235 |
||
129 | 236 |
^ listHolder value |
576 | 237 |
|
238 |
"Modified: 24.4.1996 / 08:41:05 / cg" |
|
129 | 239 |
! |
240 |
||
133 | 241 |
list:aCollection |
577 | 242 |
"set the list - thats the thingy held by the listHolder" |
243 |
||
133 | 244 |
listHolder value:aCollection. |
577 | 245 |
|
246 |
"Modified: 24.4.1996 / 08:54:06 / cg" |
|
133 | 247 |
! |
248 |
||
249 |
selection |
|
577 | 250 |
"return the selections value (i.e. the entry in the list - not its index). |
251 |
If nothing is selected, nil is returned." |
|
129 | 252 |
|
253 |
|idx| |
|
254 |
||
255 |
idx := self selectionIndex. |
|
256 |
(idx isNil or:[idx == 0]) ifTrue:[^ nil]. |
|
257 |
^ self list at:idx |
|
576 | 258 |
|
577 | 259 |
"Modified: 24.4.1996 / 08:53:23 / cg" |
114 | 260 |
! |
261 |
||
133 | 262 |
selection:anObject |
263 |
"set the selection to be anObject. |
|
264 |
If anObject is not in the list, the selection is cleared" |
|
265 |
||
266 |
^ self selectionIndex:(self list indexOf:anObject ifAbsent:0) |
|
200 | 267 |
! |
268 |
||
576 | 269 |
selectionIndex |
577 | 270 |
"return the selections index (1..). Thats the thingy held by the indexHolder. |
271 |
For ST-80 compatibility, 0 is returned if nothing is selected." |
|
576 | 272 |
|
200 | 273 |
^ selectionIndexHolder value |
576 | 274 |
|
577 | 275 |
"Modified: 24.4.1996 / 08:53:45 / cg" |
200 | 276 |
! |
277 |
||
576 | 278 |
selectionIndex:newIndex |
279 |
"set the selectionIndex" |
|
280 |
||
200 | 281 |
selectionIndexHolder value ~= newIndex ifTrue:[ |
576 | 282 |
selectionIndexHolder value:newIndex |
200 | 283 |
] |
576 | 284 |
|
285 |
"Modified: 24.4.1996 / 08:42:04 / cg" |
|
133 | 286 |
! ! |
287 |
||
288 |
!SelectionInList methodsFor:'change & update'! |
|
289 |
||
290 |
update:something with:aParameter from:changedObject |
|
291 |
"whenever one of my holders value changes, |
|
292 |
tell my dependents about this" |
|
293 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
294 |
|oldSelection| |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
295 |
|
133 | 296 |
changedObject == selectionIndexHolder ifTrue:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
297 |
self changed:#selectionIndex |
133 | 298 |
] ifFalse:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
299 |
changedObject == listHolder ifTrue:[ |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
300 |
something == #value ifTrue:[ |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
301 |
oldSelection := selectionIndexHolder value. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
302 |
self clearSelection. "/ clears without update |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
303 |
self changed:#list. |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
304 |
oldSelection ~= (selectionIndexHolder value) ifTrue:[ |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
305 |
selectionIndexHolder changed:#value |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
306 |
] |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
307 |
] |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
308 |
] |
133 | 309 |
] |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
310 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
311 |
"Modified: 20.4.1996 / 13:08:32 / cg" |
133 | 312 |
! ! |
313 |
||
200 | 314 |
!SelectionInList methodsFor:'initialization'! |
315 |
||
316 |
initialize |
|
576 | 317 |
"initialize; create the valueHolders for the index and the list" |
318 |
||
1340 | 319 |
self listHolder:(List new asValue). |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
320 |
self selectionIndexHolder:(self zeroIndex asValue). |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
321 |
|
576 | 322 |
"Modified: 24.4.1996 / 08:42:33 / cg" |
200 | 323 |
! ! |
324 |
||
133 | 325 |
!SelectionInList methodsFor:'obsolete backward compatibility'! |
326 |
||
327 |
index |
|
576 | 328 |
"return the selections index. |
329 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 330 |
|
331 |
self obsoleteMethodWarning:'use #selectionIndex'. |
|
332 |
^ self selectionIndex |
|
576 | 333 |
|
334 |
"Modified: 24.4.1996 / 08:43:10 / cg" |
|
114 | 335 |
! |
336 |
||
129 | 337 |
index:newIndex |
576 | 338 |
"set the selections index. |
339 |
This is an OBSOLETE backward compatibility interface" |
|
129 | 340 |
|
133 | 341 |
self obsoleteMethodWarning:'use #selectionIndex:'. |
129 | 342 |
^ self selectionIndex:newIndex |
576 | 343 |
|
344 |
"Modified: 24.4.1996 / 08:43:06 / cg" |
|
129 | 345 |
! |
346 |
||
133 | 347 |
indexHolder |
576 | 348 |
"return the valueHolder of the selections index. |
349 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 350 |
|
351 |
self obsoleteMethodWarning:'use #selectionIndexHolder'. |
|
352 |
^ self selectionIndexHolder |
|
576 | 353 |
|
354 |
"Modified: 24.4.1996 / 08:43:35 / cg" |
|
129 | 355 |
! |
356 |
||
133 | 357 |
indexHolder:aValueHolder |
576 | 358 |
"set the valueHolder of the selections index. |
359 |
This is an OBSOLETE backward compatibility interface" |
|
133 | 360 |
|
361 |
self obsoleteMethodWarning:'use #selectionIndexHolder:'. |
|
362 |
^ self selectionIndexHolder:aValueHolder |
|
576 | 363 |
|
364 |
"Modified: 24.4.1996 / 08:43:43 / cg" |
|
133 | 365 |
! ! |
366 |
||
1032 | 367 |
!SelectionInList methodsFor:'printing & storing'! |
368 |
||
369 |
displayString |
|
1332 | 370 |
"return a string for display in inspectors etc." |
371 |
||
1032 | 372 |
^ self class name , '(' , self selectionIndex displayString , ')' |
373 |
||
1332 | 374 |
"Modified: 20.9.1997 / 11:41:31 / cg" |
1032 | 375 |
! ! |
376 |
||
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
377 |
!SelectionInList methodsFor:'private'! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
378 |
|
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
379 |
clearSelection |
576 | 380 |
"clear the selection. |
381 |
For ST-80 compatibility, a non-selection has an index of 0 |
|
382 |
although, nil sounds more natural to me ... (sigh)" |
|
383 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
384 |
selectionIndexHolder setValue:self zeroIndex. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
385 |
|
576 | 386 |
"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
|
387 |
! ! |
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 |
!SelectionInList methodsFor:'queries'! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
390 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
391 |
zeroIndex |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
392 |
"return the selections index returned when nothing |
576 | 393 |
is selected. This method is provided to allow applications |
394 |
to deal transparently with SelectionInList models AND with |
|
395 |
MultSelectionInList models, which use different no-selection values. |
|
396 |
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
|
397 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
398 |
^ 0 |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
399 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
400 |
"Created: 20.4.1996 / 13:10:53 / cg" |
576 | 401 |
"Modified: 24.4.1996 / 08:46:18 / cg" |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
402 |
! ! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
403 |
|
204 | 404 |
!SelectionInList class methodsFor:'documentation'! |
405 |
||
406 |
version |
|
1340 | 407 |
^ '$Header: /cvs/stx/stx/libwidg/SelectionInList.st,v 1.20 1997-10-10 13:05:02 ca Exp $' |
204 | 408 |
! ! |