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