author | Claus Gittinger <cg@exept.de> |
Sat, 20 Apr 1996 15:55:13 +0200 | |
changeset 564 | 0739243fcc1c |
parent 544 | c6be88ace7c8 |
child 576 | b4b9b6c3a90c |
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 |
||
43 |
If any changes, the selectionInList notifies its dependents via a |
|
44 |
self changed: notification, |
|
45 |
using #list or #selectionIndex as aspect respectively. |
|
46 |
A popupList also knows how to deal with a slectionInList model; |
|
47 |
this makes it possible to have popupLists be somewhat exchangable |
|
48 |
with selectionInListViews. |
|
49 |
" |
|
50 |
! |
|
51 |
||
52 |
examples |
|
53 |
" |
|
54 |
basic setup: |
|
55 |
||
56 |
|m v| |
|
57 |
||
58 |
m := SelectionInList new. |
|
59 |
m list:#('one' 'two' 'three' 'four'). |
|
60 |
m selectionIndex:2. |
|
61 |
||
62 |
v := SelectionInListView on:m. |
|
63 |
v open |
|
64 |
||
65 |
||
66 |
two views on the same selectionInList: |
|
67 |
and a button, which adds an item to the list. |
|
68 |
||
69 |
|m v1 v2 b numItems| |
|
70 |
||
71 |
numItems := 4. |
|
72 |
||
73 |
m := SelectionInList new. |
|
74 |
m list:((1 to:numItems) collect:[:i | i printString]). |
|
75 |
m selectionIndex:2. |
|
76 |
||
77 |
v1 := ScrollableView forView:(SelectionInListView on:m). |
|
78 |
v1 open. |
|
79 |
||
80 |
v2 := ScrollableView forView:(SelectionInListView on:m). |
|
81 |
v2 open. |
|
82 |
||
83 |
b := Button label:'add item'. |
|
84 |
b action:[numItems := numItems + 1. |
|
85 |
m list:((1 to:numItems) collect:[:i | i printString]). |
|
86 |
]. |
|
87 |
b open |
|
88 |
" |
|
89 |
! ! |
|
90 |
||
114 | 91 |
!SelectionInList class methodsFor:'instance creation'! |
92 |
||
93 |
with:aList |
|
94 |
^ self new listHolder:(ValueHolder with:aList) |
|
95 |
! ! |
|
96 |
||
200 | 97 |
!SelectionInList methodsFor:'accessing-holders'! |
98 |
||
99 |
listHolder |
|
100 |
"return the one holding the list" |
|
101 |
||
102 |
^ listHolder |
|
103 |
! |
|
104 |
||
105 |
listHolder:aValueHolder |
|
106 |
"set the one holding the list. |
|
107 |
Q: should we forward a change-notification ?" |
|
108 |
||
109 |
listHolder notNil ifTrue:[ |
|
110 |
listHolder removeDependent:self |
|
111 |
]. |
|
112 |
listHolder := aValueHolder. |
|
113 |
listHolder addDependent:self |
|
114 |
! |
|
114 | 115 |
|
200 | 116 |
selectionHolder |
117 |
"return someone holding on the selection itself |
|
118 |
(not the index). Since we have no one, we need an adapter, |
|
119 |
to get up-to-date values." |
|
120 |
||
121 |
^ AspectAdaptor |
|
544 | 122 |
subject:self |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
123 |
sendsUpdates:false |
544 | 124 |
accessWith:#selection |
125 |
assignWith:#'selection:' |
|
126 |
aspect:#selectionIndex |
|
127 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
128 |
"Modified: 20.4.1996 / 12:59:23 / cg" |
200 | 129 |
! |
130 |
||
131 |
selectionIndexHolder |
|
132 |
"return the one holding the index" |
|
133 |
||
134 |
^ selectionIndexHolder |
|
135 |
! |
|
136 |
||
137 |
selectionIndexHolder:aValueHolder |
|
138 |
"set the one holding the index. |
|
139 |
Q: should we forward a change-notification ?" |
|
140 |
||
141 |
selectionIndexHolder notNil ifTrue:[ |
|
142 |
selectionIndexHolder removeDependent:self |
|
143 |
]. |
|
144 |
selectionIndexHolder := aValueHolder. |
|
145 |
selectionIndexHolder addDependent:self |
|
114 | 146 |
! ! |
147 |
||
133 | 148 |
!SelectionInList methodsFor:'accessing-values'! |
114 | 149 |
|
129 | 150 |
list |
151 |
^ listHolder value |
|
152 |
! |
|
153 |
||
133 | 154 |
list:aCollection |
155 |
listHolder value:aCollection. |
|
156 |
! |
|
157 |
||
158 |
selection |
|
159 |
"return the selections value (i.e. the entry in the list" |
|
129 | 160 |
|
161 |
|idx| |
|
162 |
||
163 |
idx := self selectionIndex. |
|
164 |
(idx isNil or:[idx == 0]) ifTrue:[^ nil]. |
|
165 |
^ self list at:idx |
|
114 | 166 |
! |
167 |
||
133 | 168 |
selection:anObject |
169 |
"set the selection to be anObject. |
|
170 |
If anObject is not in the list, the selection is cleared" |
|
171 |
||
172 |
^ self selectionIndex:(self list indexOf:anObject ifAbsent:0) |
|
200 | 173 |
! |
174 |
||
175 |
selectionIndex |
|
176 |
^ selectionIndexHolder value |
|
177 |
! |
|
178 |
||
179 |
selectionIndex:newIndex |
|
180 |
selectionIndexHolder value ~= newIndex ifTrue:[ |
|
181 |
selectionIndexHolder value:newIndex |
|
182 |
] |
|
133 | 183 |
! ! |
184 |
||
185 |
!SelectionInList methodsFor:'change & update'! |
|
186 |
||
187 |
update:something with:aParameter from:changedObject |
|
188 |
"whenever one of my holders value changes, |
|
189 |
tell my dependents about this" |
|
190 |
||
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
191 |
|oldSelection| |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
192 |
|
133 | 193 |
changedObject == selectionIndexHolder ifTrue:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
194 |
self changed:#selectionIndex |
133 | 195 |
] ifFalse:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
196 |
changedObject == listHolder ifTrue:[ |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
197 |
something == #value ifTrue:[ |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
198 |
oldSelection := selectionIndexHolder value. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
199 |
self clearSelection. "/ clears without update |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
200 |
self changed:#list. |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
201 |
oldSelection ~= (selectionIndexHolder value) ifTrue:[ |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
202 |
selectionIndexHolder changed:#value |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
203 |
] |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
204 |
] |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
205 |
] |
133 | 206 |
] |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
207 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
208 |
"Modified: 20.4.1996 / 13:08:32 / cg" |
133 | 209 |
! ! |
210 |
||
200 | 211 |
!SelectionInList methodsFor:'initialization'! |
212 |
||
213 |
initialize |
|
214 |
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
|
215 |
self selectionIndexHolder:(self zeroIndex asValue). |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
216 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
217 |
"Modified: 20.4.1996 / 13:11:02 / cg" |
200 | 218 |
! ! |
219 |
||
133 | 220 |
!SelectionInList methodsFor:'obsolete backward compatibility'! |
221 |
||
222 |
index |
|
223 |
"OBSOLETE interface" |
|
224 |
||
225 |
self obsoleteMethodWarning:'use #selectionIndex'. |
|
226 |
^ self selectionIndex |
|
114 | 227 |
! |
228 |
||
129 | 229 |
index:newIndex |
230 |
"OBSOLETE interface" |
|
231 |
||
133 | 232 |
self obsoleteMethodWarning:'use #selectionIndex:'. |
129 | 233 |
^ self selectionIndex:newIndex |
234 |
! |
|
235 |
||
133 | 236 |
indexHolder |
237 |
"OBSOLETE interface" |
|
238 |
||
239 |
self obsoleteMethodWarning:'use #selectionIndexHolder'. |
|
240 |
^ self selectionIndexHolder |
|
129 | 241 |
! |
242 |
||
133 | 243 |
indexHolder:aValueHolder |
244 |
"OBSOLETE interface" |
|
245 |
||
246 |
self obsoleteMethodWarning:'use #selectionIndexHolder:'. |
|
247 |
^ self selectionIndexHolder:aValueHolder |
|
248 |
! ! |
|
249 |
||
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
250 |
!SelectionInList methodsFor:'private'! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
251 |
|
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
252 |
clearSelection |
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
253 |
selectionIndexHolder setValue:self zeroIndex. |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
254 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
255 |
"Modified: 20.4.1996 / 13:11:34 / cg" |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
256 |
! ! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
257 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
258 |
!SelectionInList methodsFor:'queries'! |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
259 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
260 |
zeroIndex |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
261 |
"return the selections index returned when nothing |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
262 |
is selected. Although I would prefer nil, |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
263 |
ST-80 uses 0 to represent `no-selection'. (sigh)" |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
264 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
265 |
^ 0 |
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
266 |
|
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
267 |
"Created: 20.4.1996 / 13:10:53 / cg" |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
268 |
! ! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
269 |
|
204 | 270 |
!SelectionInList class methodsFor:'documentation'! |
271 |
||
272 |
version |
|
564
0739243fcc1c
zeroIndex stuff / ST-80 compatible 0 for empty selection
Claus Gittinger <cg@exept.de>
parents:
544
diff
changeset
|
273 |
^ '$Header: /cvs/stx/stx/libwidg/SelectionInList.st,v 1.11 1996-04-20 13:55:13 cg Exp $' |
204 | 274 |
! ! |