author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 03:26:58 +0100 | |
changeset 197 | 00927189c882 |
parent 174 | d80a6cc3f9b2 |
child 200 | aa3e56929a5a |
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 |
133 | 14 |
instanceVariableNames:'listHolder selectionIndexHolder' |
114 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
129 | 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 |
version |
|
174
d80a6cc3f9b2
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
166
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libwidg/SelectionInList.st,v 1.5 1995-11-11 16:22:48 cg Exp $' |
133 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Instances of SelectionInList can be used as model for |
|
43 |
a SelectionInListView or a PopUpList. |
|
44 |
They keep two values: a list value and a selection value; |
|
45 |
both are referred to via valueHolders. |
|
46 |
||
47 |
If any changes, the selectionInList notifies its dependents via a |
|
48 |
self changed: notification, |
|
49 |
using #list or #selectionIndex as aspect respectively. |
|
50 |
A popupList also knows how to deal with a slectionInList model; |
|
51 |
this makes it possible to have popupLists be somewhat exchangable |
|
52 |
with selectionInListViews. |
|
53 |
" |
|
54 |
! |
|
55 |
||
56 |
examples |
|
57 |
" |
|
58 |
basic setup: |
|
59 |
||
60 |
|m v| |
|
61 |
||
62 |
m := SelectionInList new. |
|
63 |
m list:#('one' 'two' 'three' 'four'). |
|
64 |
m selectionIndex:2. |
|
65 |
||
66 |
v := SelectionInListView on:m. |
|
67 |
v open |
|
68 |
||
69 |
||
70 |
two views on the same selectionInList: |
|
71 |
and a button, which adds an item to the list. |
|
72 |
||
73 |
|m v1 v2 b numItems| |
|
74 |
||
75 |
numItems := 4. |
|
76 |
||
77 |
m := SelectionInList new. |
|
78 |
m list:((1 to:numItems) collect:[:i | i printString]). |
|
79 |
m selectionIndex:2. |
|
80 |
||
81 |
v1 := ScrollableView forView:(SelectionInListView on:m). |
|
82 |
v1 open. |
|
83 |
||
84 |
v2 := ScrollableView forView:(SelectionInListView on:m). |
|
85 |
v2 open. |
|
86 |
||
87 |
b := Button label:'add item'. |
|
88 |
b action:[numItems := numItems + 1. |
|
89 |
m list:((1 to:numItems) collect:[:i | i printString]). |
|
90 |
]. |
|
91 |
b open |
|
92 |
" |
|
93 |
! ! |
|
94 |
||
114 | 95 |
!SelectionInList class methodsFor:'instance creation'! |
96 |
||
97 |
new |
|
98 |
^ super new initialize |
|
99 |
! |
|
100 |
||
101 |
with:aList |
|
102 |
^ self new listHolder:(ValueHolder with:aList) |
|
103 |
! ! |
|
104 |
||
105 |
!SelectionInList methodsFor:'initialization'! |
|
106 |
||
107 |
initialize |
|
133 | 108 |
self listHolder:(nil asValue). "/ could also use an empty collection here |
109 |
self selectionIndexHolder:(nil asValue). |
|
114 | 110 |
! ! |
111 |
||
133 | 112 |
!SelectionInList methodsFor:'accessing-values'! |
114 | 113 |
|
129 | 114 |
list |
115 |
^ listHolder value |
|
116 |
! |
|
117 |
||
133 | 118 |
list:aCollection |
119 |
listHolder value:aCollection. |
|
120 |
! |
|
121 |
||
122 |
selectionIndex |
|
123 |
^ selectionIndexHolder value |
|
129 | 124 |
! |
125 |
||
126 |
selectionIndex:newIndex |
|
166 | 127 |
selectionIndexHolder value ~= newIndex ifTrue:[ |
133 | 128 |
selectionIndexHolder value:newIndex |
129 |
] |
|
129 | 130 |
! |
131 |
||
133 | 132 |
selection |
133 |
"return the selections value (i.e. the entry in the list" |
|
129 | 134 |
|
135 |
|idx| |
|
136 |
||
137 |
idx := self selectionIndex. |
|
138 |
(idx isNil or:[idx == 0]) ifTrue:[^ nil]. |
|
139 |
^ self list at:idx |
|
114 | 140 |
! |
141 |
||
133 | 142 |
selection:anObject |
143 |
"set the selection to be anObject. |
|
144 |
If anObject is not in the list, the selection is cleared" |
|
145 |
||
146 |
^ self selectionIndex:(self list indexOf:anObject ifAbsent:0) |
|
147 |
! ! |
|
148 |
||
149 |
!SelectionInList methodsFor:'change & update'! |
|
150 |
||
151 |
update:something with:aParameter from:changedObject |
|
152 |
"whenever one of my holders value changes, |
|
153 |
tell my dependents about this" |
|
154 |
||
155 |
changedObject == selectionIndexHolder ifTrue:[ |
|
156 |
"/ self notifyChange:#selectionIndex |
|
157 |
self changed:#selectionIndex |
|
158 |
] ifFalse:[ |
|
159 |
changedObject == listHolder ifTrue:[ |
|
160 |
something == #value ifTrue:[ |
|
161 |
selectionIndexHolder setValue:0. |
|
162 |
"/ self notifyChange:#list. |
|
163 |
"/ selectionIndexHolder notifyChange:#value |
|
164 |
self changed:#list. |
|
165 |
selectionIndexHolder changed:#value |
|
166 |
] |
|
167 |
] |
|
168 |
] |
|
169 |
! ! |
|
170 |
||
171 |
!SelectionInList methodsFor:'obsolete backward compatibility'! |
|
172 |
||
173 |
index |
|
174 |
"OBSOLETE interface" |
|
175 |
||
176 |
self obsoleteMethodWarning:'use #selectionIndex'. |
|
177 |
^ self selectionIndex |
|
114 | 178 |
! |
179 |
||
129 | 180 |
index:newIndex |
181 |
"OBSOLETE interface" |
|
182 |
||
133 | 183 |
self obsoleteMethodWarning:'use #selectionIndex:'. |
129 | 184 |
^ self selectionIndex:newIndex |
185 |
! |
|
186 |
||
133 | 187 |
indexHolder |
188 |
"OBSOLETE interface" |
|
189 |
||
190 |
self obsoleteMethodWarning:'use #selectionIndexHolder'. |
|
191 |
^ self selectionIndexHolder |
|
129 | 192 |
! |
193 |
||
133 | 194 |
indexHolder:aValueHolder |
195 |
"OBSOLETE interface" |
|
196 |
||
197 |
self obsoleteMethodWarning:'use #selectionIndexHolder:'. |
|
198 |
^ self selectionIndexHolder:aValueHolder |
|
199 |
! ! |
|
200 |
||
201 |
!SelectionInList methodsFor:'accessing-holders'! |
|
202 |
||
129 | 203 |
listHolder |
133 | 204 |
"return the one holding the list" |
205 |
||
129 | 206 |
^ listHolder |
207 |
! |
|
208 |
||
133 | 209 |
listHolder:aValueHolder |
210 |
"set the one holding the list. |
|
211 |
Q: should we forward a change-notification ?" |
|
212 |
||
213 |
listHolder notNil ifTrue:[ |
|
214 |
listHolder removeDependent:self |
|
215 |
]. |
|
216 |
listHolder := aValueHolder. |
|
217 |
listHolder addDependent:self |
|
218 |
! |
|
219 |
||
220 |
selectionIndexHolder |
|
221 |
"return the one holding the index" |
|
222 |
||
223 |
^ selectionIndexHolder |
|
114 | 224 |
! |
225 |
||
226 |
selectionIndexHolder:aValueHolder |
|
133 | 227 |
"set the one holding the index. |
228 |
Q: should we forward a change-notification ?" |
|
229 |
||
230 |
selectionIndexHolder notNil ifTrue:[ |
|
231 |
selectionIndexHolder removeDependent:self |
|
232 |
]. |
|
233 |
selectionIndexHolder := aValueHolder. |
|
234 |
selectionIndexHolder addDependent:self |
|
129 | 235 |
! |
236 |
||
133 | 237 |
selectionHolder |
238 |
"return someone holding on the selection itself |
|
239 |
(not the index). Since we have no one, we need an adapter, |
|
240 |
to get up-to-date values." |
|
241 |
||
242 |
^ AspectAdaptor |
|
243 |
subject:self sendsUpdates:true |
|
244 |
accessWith:#selection assignWith:#'selection:' aspect:#selectionIndex |
|
114 | 245 |
! ! |