author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 17:23:54 +0100 | |
changeset 174 | d80a6cc3f9b2 |
parent 165 | 48382122a5b1 |
child 197 | 00927189c882 |
permissions | -rw-r--r-- |
0 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1990 by Claus Gittinger |
59 | 3 |
All Rights Reserved |
0 | 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 |
" |
|
12 |
||
13 |
ListSelectionBox subclass:#FileSelectionBox |
|
12 | 14 |
instanceVariableNames:'patternField' |
0 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
38 | 17 |
category:'Views-DialogBoxes' |
0 | 18 |
! |
19 |
||
7 | 20 |
!FileSelectionBox class methodsFor:'documentation'! |
21 |
||
38 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1990 by Claus Gittinger |
|
59 | 25 |
All Rights Reserved |
38 | 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:
165
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libwidg/FileSelectionBox.st,v 1.22 1995-11-11 16:20:16 cg Exp $' |
38 | 38 |
! |
39 |
||
7 | 40 |
documentation |
41 |
" |
|
38 | 42 |
this class implements file selection boxes. Instances show a list of |
43 |
files, and perform an action block with the selected pathname as |
|
44 |
argument when ok is clicked. |
|
45 |
||
46 |
There is an optional PatternField, which shows itself when a pattern |
|
47 |
is defined (i.e. if there is no pattern, it is hidden). |
|
48 |
If there is a pattern, only files matching the pattern will be shown in |
|
49 |
the list (and directories). |
|
50 |
||
51 |
In addition, there is an optional matchBlock (actually this is defined |
|
52 |
in the FileSelectionList). Only filenames for which this matchblock |
|
53 |
returns true will be presented. |
|
62 | 54 |
" |
55 |
! |
|
38 | 56 |
|
62 | 57 |
examples |
58 |
" |
|
38 | 59 |
Example1: |
60 |
||
59 | 61 |
FileSelectionBox open |
38 | 62 |
|
63 |
Example2: |
|
64 |
||
59 | 65 |
FileSelectionBox new open |
12 | 66 |
|
38 | 67 |
Example3: |
68 |
||
59 | 69 |
|box| |
70 |
box := FileSelectionBox new. |
|
71 |
box title:'Which file ?'. |
|
72 |
box open |
|
38 | 73 |
|
74 |
Example4: |
|
75 |
||
59 | 76 |
|box| |
77 |
box := FileSelectionBox new. |
|
78 |
box title:'Which file ?'. |
|
79 |
box pattern:'*.doc'. |
|
80 |
box open |
|
38 | 81 |
|
82 |
Example5: |
|
83 |
||
59 | 84 |
|box| |
85 |
box := FileSelectionBox new. |
|
86 |
box title:'Which file ?'. |
|
87 |
box pattern:'*'. |
|
88 |
box matchBlock:[:name | name first isLowercase]. |
|
89 |
box open |
|
44 | 90 |
|
120 | 91 |
Example6: |
44 | 92 |
|
59 | 93 |
|box| |
94 |
box := FileSelectionBox new. |
|
95 |
box title:'Which directory ?'. |
|
96 |
box pattern:'l*'. |
|
97 |
box matchBlock:[:name | OperatingSystem isDirectory:name]. |
|
98 |
box open |
|
7 | 99 |
" |
100 |
! ! |
|
0 | 101 |
|
12 | 102 |
!FileSelectionBox class methodsFor:'defaults'! |
103 |
||
77 | 104 |
requestFilename |
105 |
^ self requestFilename:'filename:' |
|
106 |
||
107 |
" |
|
108 |
FileSelectionBox requestFilename |
|
109 |
" |
|
110 |
! |
|
111 |
||
112 |
requestFilename:title |
|
113 |
|fileBox| |
|
114 |
||
115 |
fileBox := self |
|
116 |
title:title |
|
117 |
okText:'ok' |
|
118 |
abortText:'cancel' |
|
119 |
action:[:fileName | ^ fileName]. |
|
120 |
||
121 |
fileBox showAtPointer. |
|
122 |
^ nil |
|
123 |
||
124 |
" |
|
125 |
FileSelectionBox requestFilename:'which file ?' |
|
126 |
" |
|
127 |
! ! |
|
128 |
||
129 |
!FileSelectionBox class methodsFor:'requests'! |
|
130 |
||
12 | 131 |
listViewType |
132 |
"return the type of listView - using a FileSelectionList here" |
|
133 |
||
134 |
^ FileSelectionList |
|
135 |
! ! |
|
136 |
||
0 | 137 |
!FileSelectionBox methodsFor:'initialization'! |
138 |
||
139 |
initialize |
|
101 | 140 |
|corner| |
141 |
||
0 | 142 |
super initialize. |
143 |
||
150 | 144 |
label := resources string:'File dialog'. |
77 | 145 |
|
3 | 146 |
labelField extent:(0.7 @ labelField height). |
147 |
labelField label:(resources string:'select a file:'). |
|
148 |
labelField adjust:#left. |
|
149 |
||
38 | 150 |
patternField := EditField in:self. |
77 | 151 |
self is3D ifTrue:[ |
101 | 152 |
corner := (1.0 @ (labelField origin y+patternField heightIncludingBorder)). |
77 | 153 |
] ifFalse:[ |
101 | 154 |
corner := [(width - ViewSpacing - (patternField borderWidth * 2)) @ (labelField origin y+patternField height"IncludingBorder")]. |
77 | 155 |
]. |
101 | 156 |
patternField origin:(0.7 @ labelField origin y) corner:corner. |
110 | 157 |
patternField rightInset:ViewSpacing. |
3 | 158 |
patternField initialText:'*'. |
21 | 159 |
patternField leaveAction:[:reason | |
59 | 160 |
selectionList pattern:patternField contents. |
161 |
self updateList |
|
21 | 162 |
]. |
130 | 163 |
patternField hiddenOnRealize:true. "delay showing, until a pattern is defined" |
3 | 164 |
|
41 | 165 |
enterField addDependent:self. |
0 | 166 |
|
132 | 167 |
" |
168 |
FileSelectionBox open |
|
169 |
FileSelectionBox new show |
|
170 |
" |
|
38 | 171 |
! |
172 |
||
173 |
createEnterField |
|
121 | 174 |
"if the (optional) class FilenameEditField is present, use |
38 | 175 |
it, since it provides filename completion. Otherwise, we have |
121 | 176 |
to live with the dump (default) field ... |
38 | 177 |
" |
178 |
FilenameEditField notNil ifTrue:[ |
|
121 | 179 |
^ FilenameEditField new. |
180 |
]. |
|
181 |
^ super createEnterField |
|
77 | 182 |
! |
183 |
||
184 |
focusSequence |
|
185 |
patternField shown ifTrue:[ |
|
186 |
^ Array |
|
187 |
with:patternField |
|
188 |
with:enterField |
|
189 |
with:selectionList |
|
190 |
with:abortButton |
|
191 |
with:okButton |
|
192 |
]. |
|
101 | 193 |
^ super focusSequence |
132 | 194 |
! |
195 |
||
196 |
realize |
|
197 |
"if some default is present in the enterField, |
|
198 |
scroll to make this one visible" |
|
199 |
||
200 |
|contents| |
|
201 |
||
202 |
super realize. |
|
203 |
(contents := enterField contents) notNil ifTrue:[ |
|
204 |
contents notEmpty ifTrue:[ |
|
205 |
selectionList makeVisible:contents |
|
206 |
] |
|
207 |
] |
|
38 | 208 |
! ! |
209 |
||
155 | 210 |
!FileSelectionBox methodsFor:'change & update'! |
41 | 211 |
|
77 | 212 |
update:something with:argument from:changedObject |
165 | 213 |
|commonName index s| |
41 | 214 |
|
215 |
something == #directory ifTrue:[ |
|
59 | 216 |
" |
217 |
sent by fileNameEnterField, if a filename |
|
218 |
completion was not possible due to multiple |
|
219 |
matches. |
|
220 |
" |
|
221 |
selectionList directory:argument. |
|
165 | 222 |
s := enterField contents. |
223 |
s notNil ifTrue:[ |
|
224 |
commonName := s asFilename baseName. |
|
225 |
commonName size > 0 ifTrue:[ |
|
226 |
index := selectionList list findFirst:[:entry | entry startsWith:commonName]. |
|
227 |
index ~~ 0 ifTrue:[ |
|
228 |
selectionList makeLineVisible:index |
|
229 |
] |
|
59 | 230 |
] |
231 |
] |
|
41 | 232 |
] |
233 |
! ! |
|
234 |
||
38 | 235 |
!FileSelectionBox methodsFor:'user actions'! |
236 |
||
237 |
okPressed |
|
238 |
"called for both on ok-press and on return-key" |
|
239 |
||
240 |
|dir string fname| |
|
241 |
||
242 |
string := enterField contents. |
|
243 |
string notNil ifTrue:[ |
|
59 | 244 |
string := string withoutSeparators. |
245 |
string asFilename isAbsolute ifTrue:[ |
|
246 |
fname := string asFilename |
|
247 |
] ifFalse:[ |
|
248 |
dir := selectionList directory pathName asFilename. |
|
249 |
fname := dir construct:string |
|
250 |
]. |
|
251 |
fname isDirectory ifTrue:[ |
|
252 |
selectionList directory:fname asString. |
|
253 |
self updateList. |
|
254 |
^ self |
|
255 |
] |
|
38 | 256 |
]. |
257 |
super okPressed |
|
132 | 258 |
! |
259 |
||
260 |
selectionChanged |
|
261 |
|entry| |
|
262 |
||
263 |
entry := selectionList selectionValue. |
|
264 |
enterField contents:entry |
|
265 |
! |
|
266 |
||
267 |
doubleClick |
|
268 |
|entry| |
|
269 |
||
270 |
entry := selectionList selectionValue. |
|
271 |
entry notNil ifTrue:[ |
|
272 |
((selectionList directory typeOf:entry) == #directory) ifFalse:[ |
|
273 |
enterField contents:entry. |
|
274 |
self okPressed |
|
275 |
] |
|
276 |
]. |
|
0 | 277 |
! ! |
278 |
||
21 | 279 |
!FileSelectionBox methodsFor:'private'! |
280 |
||
281 |
updateList |
|
282 |
selectionList updateList |
|
283 |
! ! |
|
284 |
||
77 | 285 |
!FileSelectionBox methodsFor:'queries'! |
286 |
||
130 | 287 |
preferredExtent |
288 |
"return my preferred extent - thats the minimum size |
|
77 | 289 |
to make everything visible" |
290 |
||
291 |
|wWanted hWanted| |
|
292 |
||
293 |
wWanted := ViewSpacing + |
|
130 | 294 |
labelField preferredExtent x + |
77 | 295 |
(ViewSpacing * 2) + |
130 | 296 |
patternField preferredExtent x + |
77 | 297 |
ViewSpacing. |
298 |
(wWanted < width) ifTrue:[ |
|
299 |
wWanted := width |
|
300 |
]. |
|
301 |
hWanted := ViewSpacing + labelField height + |
|
302 |
ViewSpacing + enterField height + |
|
303 |
ViewSpacing + selectionList height + |
|
130 | 304 |
ViewSpacing + buttonPanel preferredExtent y + |
77 | 305 |
ViewSpacing. |
306 |
||
307 |
(hWanted < height) ifTrue:[ |
|
308 |
hWanted := height |
|
309 |
]. |
|
310 |
^ (wWanted @ hWanted) |
|
311 |
! ! |
|
312 |
||
0 | 313 |
!FileSelectionBox methodsFor:'accessing'! |
314 |
||
38 | 315 |
openOn:aPath |
316 |
"open the box showing files in aPath. |
|
317 |
This is only a shortcut message - no new functionality." |
|
318 |
||
319 |
self directory:aPath. |
|
320 |
self showAtPointer |
|
321 |
! |
|
322 |
||
77 | 323 |
directory:directoryName |
38 | 324 |
"change the directory shown in the list." |
0 | 325 |
|
77 | 326 |
selectionList directory:directoryName |
3 | 327 |
! |
328 |
||
329 |
pattern:aPattern |
|
38 | 330 |
"set the pattern - this also enables the PatternField |
331 |
(if the pattern is non-nil) or hides it (if nil)." |
|
7 | 332 |
|
130 | 333 |
|hidePatternField focusSequence| |
334 |
||
3 | 335 |
patternField initialText:aPattern. |
12 | 336 |
selectionList pattern:aPattern. |
337 |
aPattern isNil ifTrue:[ |
|
130 | 338 |
hidePatternField := true. |
59 | 339 |
realized ifTrue:[ |
340 |
patternField hide. |
|
77 | 341 |
]. |
130 | 342 |
focusSequence := (Array |
343 |
with:enterField |
|
344 |
with:selectionList |
|
345 |
with:okButton |
|
346 |
with:abortButton) |
|
12 | 347 |
] ifFalse:[ |
130 | 348 |
hidePatternField := false. |
59 | 349 |
realized ifTrue:[ |
350 |
patternField realize. |
|
351 |
]. |
|
130 | 352 |
focusSequence := (Array |
353 |
with:patternField |
|
354 |
with:enterField |
|
355 |
with:selectionList |
|
356 |
with:okButton |
|
357 |
with:abortButton) |
|
358 |
]. |
|
359 |
||
360 |
patternField hiddenOnRealize:hidePatternField. |
|
361 |
windowGroup notNil ifTrue:[ |
|
362 |
windowGroup focusSequence:focusSequence |
|
0 | 363 |
]. |
38 | 364 |
! |
0 | 365 |
|
38 | 366 |
matchBlock:aBlock |
367 |
"set the matchBlock (in the selectionList). Only files |
|
368 |
for which the block returns true are shown. |
|
110 | 369 |
The matching is actually done in the fileSelectionList." |
0 | 370 |
|
110 | 371 |
selectionList matchBlock:aBlock |
38 | 372 |
! |
7 | 373 |
|
38 | 374 |
contents |
375 |
"return the current entered value (i.e. the enterFields string). |
|
376 |
redefined to return the full pathname." |
|
377 |
||
378 |
|string sep| |
|
379 |
||
380 |
string := super contents. |
|
381 |
string isNil ifTrue:[ |
|
59 | 382 |
^ selectionList directory pathName |
38 | 383 |
]. |
384 |
sep := Filename separator. |
|
385 |
(string startsWith:sep) ifTrue:[ |
|
59 | 386 |
^ string |
38 | 387 |
]. |
388 |
^ (selectionList directory pathName asFilename construct:string) asString |
|
0 | 389 |
! ! |