author | ca |
Mon, 01 Apr 1996 20:56:26 +0200 | |
changeset 526 | 8203430fbadd |
parent 359 | dec35d4fbc35 |
child 544 | c6be88ace7c8 |
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 |
|
122 |
subject:self sendsUpdates:true |
|
123 |
accessWith:#selection assignWith:#'selection:' aspect:#selectionIndex |
|
124 |
! |
|
125 |
||
126 |
selectionIndexHolder |
|
127 |
"return the one holding the index" |
|
128 |
||
129 |
^ selectionIndexHolder |
|
130 |
! |
|
131 |
||
132 |
selectionIndexHolder:aValueHolder |
|
133 |
"set the one holding the index. |
|
134 |
Q: should we forward a change-notification ?" |
|
135 |
||
136 |
selectionIndexHolder notNil ifTrue:[ |
|
137 |
selectionIndexHolder removeDependent:self |
|
138 |
]. |
|
139 |
selectionIndexHolder := aValueHolder. |
|
140 |
selectionIndexHolder addDependent:self |
|
114 | 141 |
! ! |
142 |
||
133 | 143 |
!SelectionInList methodsFor:'accessing-values'! |
114 | 144 |
|
129 | 145 |
list |
146 |
^ listHolder value |
|
147 |
! |
|
148 |
||
133 | 149 |
list:aCollection |
150 |
listHolder value:aCollection. |
|
151 |
! |
|
152 |
||
153 |
selection |
|
154 |
"return the selections value (i.e. the entry in the list" |
|
129 | 155 |
|
156 |
|idx| |
|
157 |
||
158 |
idx := self selectionIndex. |
|
159 |
(idx isNil or:[idx == 0]) ifTrue:[^ nil]. |
|
160 |
^ self list at:idx |
|
114 | 161 |
! |
162 |
||
133 | 163 |
selection:anObject |
164 |
"set the selection to be anObject. |
|
165 |
If anObject is not in the list, the selection is cleared" |
|
166 |
||
167 |
^ self selectionIndex:(self list indexOf:anObject ifAbsent:0) |
|
200 | 168 |
! |
169 |
||
170 |
selectionIndex |
|
171 |
^ selectionIndexHolder value |
|
172 |
! |
|
173 |
||
174 |
selectionIndex:newIndex |
|
175 |
selectionIndexHolder value ~= newIndex ifTrue:[ |
|
176 |
selectionIndexHolder value:newIndex |
|
177 |
] |
|
133 | 178 |
! ! |
179 |
||
180 |
!SelectionInList methodsFor:'change & update'! |
|
181 |
||
182 |
update:something with:aParameter from:changedObject |
|
183 |
"whenever one of my holders value changes, |
|
184 |
tell my dependents about this" |
|
185 |
||
186 |
changedObject == selectionIndexHolder ifTrue:[ |
|
187 |
"/ self notifyChange:#selectionIndex |
|
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
188 |
self changed:#selectionIndex |
133 | 189 |
] ifFalse:[ |
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
190 |
changedObject == listHolder ifTrue:[ |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
191 |
something == #value ifTrue:[ |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
192 |
self clearSelection. |
133 | 193 |
"/ self notifyChange:#list. |
194 |
"/ selectionIndexHolder notifyChange:#value |
|
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
195 |
self changed:#list. |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
196 |
selectionIndexHolder changed:#value |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
197 |
] |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
198 |
] |
133 | 199 |
] |
200 |
! ! |
|
201 |
||
200 | 202 |
!SelectionInList methodsFor:'initialization'! |
203 |
||
204 |
initialize |
|
205 |
self listHolder:(nil asValue). "/ could also use an empty collection here |
|
206 |
self selectionIndexHolder:(nil asValue). |
|
207 |
! ! |
|
208 |
||
133 | 209 |
!SelectionInList methodsFor:'obsolete backward compatibility'! |
210 |
||
211 |
index |
|
212 |
"OBSOLETE interface" |
|
213 |
||
214 |
self obsoleteMethodWarning:'use #selectionIndex'. |
|
215 |
^ self selectionIndex |
|
114 | 216 |
! |
217 |
||
129 | 218 |
index:newIndex |
219 |
"OBSOLETE interface" |
|
220 |
||
133 | 221 |
self obsoleteMethodWarning:'use #selectionIndex:'. |
129 | 222 |
^ self selectionIndex:newIndex |
223 |
! |
|
224 |
||
133 | 225 |
indexHolder |
226 |
"OBSOLETE interface" |
|
227 |
||
228 |
self obsoleteMethodWarning:'use #selectionIndexHolder'. |
|
229 |
^ self selectionIndexHolder |
|
129 | 230 |
! |
231 |
||
133 | 232 |
indexHolder:aValueHolder |
233 |
"OBSOLETE interface" |
|
234 |
||
235 |
self obsoleteMethodWarning:'use #selectionIndexHolder:'. |
|
236 |
^ self selectionIndexHolder:aValueHolder |
|
237 |
! ! |
|
238 |
||
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
239 |
!SelectionInList methodsFor:'private'! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
240 |
|
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
241 |
clearSelection |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
242 |
selectionIndexHolder setValue:0. |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
243 |
! ! |
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
244 |
|
204 | 245 |
!SelectionInList class methodsFor:'documentation'! |
246 |
||
247 |
version |
|
526
8203430fbadd
oops - clearing selection was wrong in MultiSelectionInList
ca
parents:
359
diff
changeset
|
248 |
^ '$Header: /cvs/stx/stx/libwidg/Attic/SelList.st,v 1.9 1996-04-01 18:56:16 ca Exp $' |
204 | 249 |
! ! |