36
|
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 |
"
|
19
|
12 |
|
58
|
13 |
'From Smalltalk/X, Version:2.10.5 on 15-may-1995 at 9:15:44 am'!
|
|
14 |
|
|
15 |
ListEntry subclass:#MultiColListEntry
|
25
|
16 |
instanceVariableNames:'strings tabSpec'
|
89
|
17 |
classVariableNames:'DefaultTabSpec'
|
25
|
18 |
poolDictionaries:''
|
|
19 |
category:'Views-Support'
|
19
|
20 |
!
|
|
21 |
|
25
|
22 |
!MultiColListEntry class methodsFor:'documentation'!
|
|
23 |
|
36
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
version
|
89
|
39 |
^ '$Header: /cvs/stx/stx/libwidg2/MultiColListEntry.st,v 1.9 1995-11-17 17:23:36 cg Exp $'
|
36
|
40 |
!
|
|
41 |
|
25
|
42 |
documentation
|
|
43 |
"
|
|
44 |
Instances of MultiColListEntry can be used in place of strings
|
36
|
45 |
as entries of the list in a ListView or SelectionInListView.
|
34
|
46 |
They allow data to be presented in table (or any other) form.
|
36
|
47 |
See example here and in TabulatorSpecs documentation.
|
|
48 |
Notice, that each entry can have its own tabulator specification;
|
|
49 |
although, usually all share a single spec.
|
|
50 |
Also, notice that each column may align different; making these
|
|
51 |
perfect elements to present table data.
|
25
|
52 |
|
36
|
53 |
MultiColListEntry and TabulatorSpec were originally created to
|
|
54 |
support nice tabular display of file-lists in the FileBrowser;
|
|
55 |
you may many other uses ...
|
|
56 |
"
|
19
|
57 |
!
|
|
58 |
|
36
|
59 |
examples
|
|
60 |
"
|
58
|
61 |
putting multiColListEntries into a ListView
|
|
62 |
(instead of strings)'
|
|
63 |
|
|
64 |
|v e myList tabs|
|
19
|
65 |
|
58
|
66 |
myList := OrderedCollection new.
|
19
|
67 |
|
58
|
68 |
tabs := TabulatorSpecification new.
|
|
69 |
tabs unit:#inch.
|
|
70 |
tabs positions:#(0 3 4).
|
|
71 |
tabs align:#(left #center #left).
|
19
|
72 |
|
58
|
73 |
e := MultiColListEntry
|
|
74 |
fromString:'left centered left'.
|
|
75 |
e tabulatorSpecification:tabs.
|
|
76 |
myList add:e.
|
|
77 |
|
|
78 |
e := MultiColListEntry
|
|
79 |
fromString:'| | |'.
|
|
80 |
e tabulatorSpecification:tabs.
|
|
81 |
myList add:e.
|
|
82 |
myList add:''.
|
19
|
83 |
|
58
|
84 |
e := MultiColListEntry
|
|
85 |
fromString:'hello hallo salut'.
|
|
86 |
e tabulatorSpecification:tabs.
|
|
87 |
myList add:e.
|
|
88 |
|
|
89 |
e := MultiColListEntry
|
|
90 |
fromString:'good morning,guten Morgen,bon jour'
|
|
91 |
separatedBy:$,.
|
|
92 |
e tabulatorSpecification:tabs.
|
|
93 |
myList add:e.
|
19
|
94 |
|
58
|
95 |
e := MultiColListEntry new.
|
|
96 |
e colAt:1 put:'good bye'.
|
|
97 |
e colAt:2 put:'auf Wiedersehen'.
|
|
98 |
e colAt:3 put:'au revoir '.
|
|
99 |
e tabulatorSpecification:tabs.
|
|
100 |
myList add:e.
|
19
|
101 |
|
58
|
102 |
v := ListView new.
|
|
103 |
v setList:myList expandTabs:false.
|
|
104 |
v extent:500@100.
|
|
105 |
v open
|
36
|
106 |
|
|
107 |
|
|
108 |
|
58
|
109 |
many multiColListEntries in a scrollable ListView
|
36
|
110 |
|
58
|
111 |
|v l e myList tabs|
|
19
|
112 |
|
58
|
113 |
myList := OrderedCollection new.
|
19
|
114 |
|
58
|
115 |
tabs := TabulatorSpecification new.
|
|
116 |
tabs unit:#cm.
|
|
117 |
tabs positions:#(1 3 5).
|
|
118 |
tabs align:#(#right #center #left).
|
19
|
119 |
|
58
|
120 |
1 to:100 do:[:i|
|
|
121 |
e := MultiColListEntry new.
|
|
122 |
e colAt:1 put:i printString.
|
|
123 |
e colAt:2 put:i squared printString.
|
|
124 |
e colAt:3 put:i sqrt printString.
|
|
125 |
e tabulatorSpecification:tabs.
|
|
126 |
myList add:e.
|
|
127 |
].
|
|
128 |
l := ListView new.
|
|
129 |
l setList:myList expandTabs:false.
|
|
130 |
v := ScrollableView forView:l.
|
|
131 |
v extent:300@200.
|
|
132 |
v open
|
36
|
133 |
|
|
134 |
|
|
135 |
|
58
|
136 |
like above, but uses nicer decimal alignments
|
36
|
137 |
|
58
|
138 |
|v l e myList tabs|
|
19
|
139 |
|
58
|
140 |
myList := OrderedCollection new.
|
|
141 |
|
|
142 |
tabs := TabulatorSpecification new.
|
|
143 |
tabs unit:#cm.
|
|
144 |
tabs positions:#(1 3 6 9 12).
|
|
145 |
tabs align:#(#right #decimal #decimal #decimal #decimal).
|
19
|
146 |
|
58
|
147 |
1 to:100 do:[:i|
|
|
148 |
e := MultiColListEntry new.
|
|
149 |
e colAt:1 put:i printString.
|
|
150 |
e colAt:2 put:i log printString.
|
|
151 |
e colAt:3 put:i sqrt printString.
|
|
152 |
e colAt:4 put:i sin printString.
|
|
153 |
e colAt:5 put:i cos printString.
|
|
154 |
e tabulatorSpecification:tabs.
|
|
155 |
myList add:e.
|
|
156 |
].
|
|
157 |
l := ListView new.
|
|
158 |
l setList:myList expandTabs:false.
|
|
159 |
v := ScrollableView forView:l.
|
|
160 |
v extent:600@200.
|
|
161 |
v open
|
36
|
162 |
|
|
163 |
|
|
164 |
|
58
|
165 |
specifying tabs in inches
|
36
|
166 |
|
58
|
167 |
|v l e myList tabs|
|
19
|
168 |
|
58
|
169 |
myList := OrderedCollection new.
|
19
|
170 |
|
58
|
171 |
tabs := TabulatorSpecification new.
|
|
172 |
tabs unit:#inch.
|
|
173 |
tabs positions:#(0 2 3.5 4 6 8 10 12).
|
36
|
174 |
|
58
|
175 |
e := MultiColListEntry new.
|
|
176 |
e colAt:1 put:'2'.
|
|
177 |
e colAt:2 put:'3.5'.
|
|
178 |
e colAt:3 put:'4'.
|
|
179 |
e colAt:4 put:'6'.
|
|
180 |
e colAt:5 put:'8'.
|
|
181 |
e colAt:6 put:'10'.
|
|
182 |
e colAt:7 put:'12'.
|
|
183 |
e tabulatorSpecification:tabs.
|
|
184 |
myList add:e.
|
36
|
185 |
|
58
|
186 |
myList add:((MultiColListEntry fromString:'| | | | | | |')
|
|
187 |
tabulatorSpecification:tabs).
|
|
188 |
myList add:((MultiColListEntry fromString:'xxx xxx xxx xxx xxx xxx xxx')
|
|
189 |
tabulatorSpecification:tabs).
|
|
190 |
|
|
191 |
l := ListView new.
|
|
192 |
l setList:myList expandTabs:false.
|
|
193 |
v := HVScrollableView forView:l.
|
|
194 |
v extent:600@200.
|
|
195 |
v open
|
36
|
196 |
|
|
197 |
|
58
|
198 |
if you have the columns available as a collection,
|
|
199 |
setup can be done easier
|
|
200 |
|
|
201 |
|v l e myList tabs|
|
|
202 |
|
|
203 |
myList := OrderedCollection new.
|
|
204 |
|
|
205 |
tabs := TabulatorSpecification new.
|
|
206 |
tabs unit:#inch.
|
|
207 |
tabs positions:#(0 2 3.5 4 6 8 10 12).
|
|
208 |
|
|
209 |
e := MultiColListEntry new.
|
|
210 |
e strings:#('2' '3.5' '4' '6' '8' '10' '12').
|
|
211 |
e tabulatorSpecification:tabs.
|
|
212 |
myList add:e.
|
36
|
213 |
|
58
|
214 |
l := ListView new.
|
|
215 |
l setList:myList expandTabs:false.
|
|
216 |
v := HVScrollableView forView:l.
|
|
217 |
v extent:600@200.
|
|
218 |
v open
|
|
219 |
|
36
|
220 |
|
58
|
221 |
concrete example, show /etc/passwd in a table:
|
|
222 |
|
|
223 |
|v l s myList line e htabs tabs fingerEntry|
|
|
224 |
|
|
225 |
tabs := TabulatorSpecification new.
|
|
226 |
tabs unit:#inch.
|
|
227 |
tabs positions:#(0 2 3.5 4.5 5 8 11).
|
|
228 |
tabs align: #(left left right right left left left).
|
36
|
229 |
|
58
|
230 |
htabs := TabulatorSpecification new.
|
|
231 |
htabs unit:#inch.
|
|
232 |
htabs positions:#(0 2 3.5 4.5 5 8 11).
|
|
233 |
htabs align: #(left center center center left left left).
|
|
234 |
|
|
235 |
myList := OrderedCollection new.
|
|
236 |
|
|
237 |
e := MultiColListEntry
|
|
238 |
fromString:'login-name:password:uid:gid:finger-entry:home-dir:shell'
|
|
239 |
separatedBy:$:.
|
|
240 |
e tabulatorSpecification:htabs.
|
|
241 |
myList add:e.
|
|
242 |
myList add:''.
|
36
|
243 |
|
58
|
244 |
s := '/etc/passwd' asFilename readStream.
|
|
245 |
[s atEnd] whileFalse:[
|
|
246 |
line := s nextLine.
|
|
247 |
e := MultiColListEntry
|
|
248 |
fromString:line
|
|
249 |
separatedBy:$:.
|
|
250 |
fingerEntry := e colAt:5.
|
|
251 |
e colAt:5 put:(fingerEntry asCollectionOfSubstringsSeparatedBy:$,) first.
|
|
252 |
e tabulatorSpecification:tabs.
|
|
253 |
myList add:e.
|
|
254 |
].
|
|
255 |
s close.
|
|
256 |
|
|
257 |
l := ListView new.
|
|
258 |
l setList:myList expandTabs:false.
|
|
259 |
v := HVScrollableView forView:l.
|
|
260 |
v extent:600@200.
|
|
261 |
v open
|
36
|
262 |
"
|
|
263 |
! !
|
|
264 |
|
58
|
265 |
!MultiColListEntry class methodsFor:'instance creation'!
|
36
|
266 |
|
70
|
267 |
new:numberOfColumns
|
|
268 |
|e|
|
|
269 |
|
|
270 |
e := self new.
|
|
271 |
1 to:numberOfColumns do:[:i |
|
|
272 |
e colAt:i put:''.
|
|
273 |
].
|
|
274 |
^ e
|
|
275 |
|
|
276 |
"Modified: 30.8.1995 / 16:33:53 / claus"
|
|
277 |
!
|
|
278 |
|
|
279 |
new:numberOfColumns tabulatorSpecification:aTabSpec
|
|
280 |
^ (self new:numberOfColumns) tabulatorSpecification:aTabSpec
|
|
281 |
|
|
282 |
"Modified: 30.8.1995 / 16:34:23 / claus"
|
|
283 |
!
|
|
284 |
|
58
|
285 |
fromString:aString
|
|
286 |
^ self fromString:aString separatedBy:Character space
|
36
|
287 |
!
|
|
288 |
|
58
|
289 |
fromString:aString separatedBy:separatorCharacter
|
|
290 |
|subStrings e|
|
36
|
291 |
|
58
|
292 |
e := self new.
|
|
293 |
subStrings := aString asCollectionOfSubstringsSeparatedBy:separatorCharacter.
|
|
294 |
subStrings keysAndValuesDo:[:i :sub |
|
|
295 |
e colAt:i put:sub.
|
36
|
296 |
].
|
58
|
297 |
^ e
|
36
|
298 |
! !
|
|
299 |
|
89
|
300 |
!MultiColListEntry methodsFor:'defaults'!
|
|
301 |
|
|
302 |
defaultTabSpec
|
|
303 |
"the default tabulators are 1 inch apart"
|
|
304 |
|
|
305 |
|spec|
|
|
306 |
|
|
307 |
spec := DefaultTabSpec.
|
|
308 |
spec isNil ifTrue:[
|
|
309 |
spec := TabulatorSpecification new.
|
|
310 |
spec unit:#inch.
|
|
311 |
spec positions:(0 to:20).
|
|
312 |
spec align: #left.
|
|
313 |
DefaultTabSpec := spec.
|
|
314 |
].
|
|
315 |
^ spec.
|
|
316 |
|
|
317 |
"Created: 17.11.1995 / 12:23:29 / cg"
|
|
318 |
"Modified: 17.11.1995 / 12:24:20 / cg"
|
|
319 |
! !
|
|
320 |
|
36
|
321 |
!MultiColListEntry methodsFor:'drawing'!
|
|
322 |
|
|
323 |
displayOn:aGC x:x y:y
|
|
324 |
"display the receiver on a GC"
|
|
325 |
|
89
|
326 |
|xPos spec tabPos prevString|
|
36
|
327 |
|
89
|
328 |
spec := tabSpec.
|
|
329 |
spec isNil ifTrue:[
|
|
330 |
spec := self defaultTabSpec
|
|
331 |
].
|
36
|
332 |
xPos := x.
|
|
333 |
prevString := ''.
|
|
334 |
strings keysAndValuesDo:[:index :subString |
|
|
335 |
subString notNil ifTrue:[
|
|
336 |
"
|
|
337 |
find next tab
|
|
338 |
"
|
89
|
339 |
tabPos := spec positionOfTab:index forString:subString on:aGC.
|
36
|
340 |
tabPos isNil ifTrue:[
|
|
341 |
"
|
|
342 |
no tab - just continue where we are ...
|
|
343 |
"
|
|
344 |
xPos := xPos + (aGC font widthOf:prevString).
|
|
345 |
] ifFalse:[
|
|
346 |
xPos := tabPos + x.
|
|
347 |
].
|
|
348 |
aGC displayString:subString x:xPos y:y.
|
|
349 |
prevString := subString.
|
|
350 |
]
|
|
351 |
].
|
89
|
352 |
|
|
353 |
"Modified: 17.11.1995 / 12:21:18 / cg"
|
58
|
354 |
! !
|
36
|
355 |
|
58
|
356 |
!MultiColListEntry methodsFor:'accessing'!
|
|
357 |
|
|
358 |
colAt:index put:aString
|
|
359 |
"replace the substring at column index"
|
|
360 |
|
|
361 |
strings isNil ifTrue:[
|
|
362 |
strings := OrderedCollection new:index
|
|
363 |
].
|
|
364 |
index > strings size ifTrue:[strings grow:index].
|
|
365 |
strings at:index put:aString
|
|
366 |
!
|
|
367 |
|
|
368 |
tabulatorSpecification:aTabSpec
|
|
369 |
"set the tabulator spec"
|
|
370 |
|
|
371 |
tabSpec := aTabSpec
|
|
372 |
!
|
|
373 |
|
|
374 |
strings:aCollectionOfStrings
|
|
375 |
"replace all substrings"
|
|
376 |
|
|
377 |
strings := OrderedCollection withAll:aCollectionOfStrings.
|
|
378 |
!
|
|
379 |
|
|
380 |
colAt:index
|
|
381 |
"return the substring at column index"
|
|
382 |
|
|
383 |
index > strings size ifTrue:[^ nil].
|
|
384 |
^ strings at:index
|
36
|
385 |
! !
|
|
386 |
|
58
|
387 |
!MultiColListEntry methodsFor:'queries'!
|
|
388 |
|
|
389 |
widthIn:aGC
|
|
390 |
"return the width of the receiver when displayed in aGC"
|
|
391 |
|
62
|
392 |
|xPos xMax tabPos prevLen|
|
58
|
393 |
|
|
394 |
"just to make certain:
|
|
395 |
do not assume, that the last col is the rightmost one ...
|
|
396 |
"
|
|
397 |
xPos := 0.
|
|
398 |
xMax := 0.
|
|
399 |
prevLen := 0.
|
|
400 |
strings keysAndValuesDo:[:index :subString |
|
62
|
401 |
|w|
|
|
402 |
|
58
|
403 |
subString notNil ifTrue:[
|
|
404 |
"
|
|
405 |
find next tab
|
|
406 |
"
|
|
407 |
tabPos := tabSpec positionOfTab:index forString:subString on:aGC.
|
|
408 |
tabPos isNil ifTrue:[
|
|
409 |
"
|
|
410 |
no tab - just continue where we are ...
|
|
411 |
"
|
|
412 |
xPos := xPos + prevLen.
|
|
413 |
] ifFalse:[
|
|
414 |
xPos := tabPos.
|
|
415 |
].
|
|
416 |
w := prevLen := aGC font widthOf:subString.
|
|
417 |
xMax := xMax max:(xPos + w).
|
|
418 |
]
|
|
419 |
].
|
|
420 |
^ xMax
|
|
421 |
! !
|
|
422 |
|
|
423 |
!MultiColListEntry methodsFor:'converting'!
|
|
424 |
|
|
425 |
asString
|
|
426 |
"return the receiver as a string with embedded tabs"
|
|
427 |
|
|
428 |
|s sub tab
|
|
429 |
nSub "{ Class: SmallInteger }"|
|
|
430 |
|
|
431 |
s := ''.
|
|
432 |
tab := Character tab asString.
|
|
433 |
nSub := strings size.
|
|
434 |
1 to:nSub do:[:subStringIndex |
|
|
435 |
sub := strings at:subStringIndex.
|
|
436 |
sub notNil ifTrue:[
|
|
437 |
s := s , sub.
|
|
438 |
].
|
|
439 |
subStringIndex == strings size ifFalse:[
|
|
440 |
s := s , tab
|
|
441 |
]
|
|
442 |
].
|
|
443 |
|
|
444 |
^ s
|
|
445 |
! !
|
|
446 |
|