author | ca |
Mon, 15 Jan 1996 23:14:28 +0100 | |
changeset 287 | 82691623dd4c |
parent 285 | e1f09d0f2c15 |
child 296 | e1b9431b3aef |
permissions | -rw-r--r-- |
11 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
62 | 3 |
All Rights Reserved |
11 | 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 |
SelectionInListView subclass:#FileSelectionList |
|
287 | 14 |
instanceVariableNames:'pattern directory timeStamp directoryId directoryName |
15 |
directoryContents directoryFileTypes fileTypes realAction |
|
16 |
matchBlock stayInDirectory ignoreParentDirectory markDirectories' |
|
282 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Views-Text' |
|
11 | 20 |
! |
21 |
||
22 |
!FileSelectionList class methodsFor:'documentation'! |
|
23 |
||
38 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
62 | 27 |
All Rights Reserved |
38 | 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 |
||
11 | 38 |
documentation |
39 |
" |
|
38 | 40 |
this class implements file selection lists - its basically a |
41 |
selection in list, but adds some right-arrows to directories. |
|
42 |
(and will soon remember the previous position when changing directories). |
|
59 | 43 |
You can specify an optional filename-pattern (such as '*.st') and an |
44 |
optional matchBlock (such as: [:name | name startsWith:'A']). |
|
45 |
||
46 |
Only files (plus directories) matching the pattern (if present) and |
|
47 |
for which the matchBlock returns true (if present), are shown. |
|
21 | 48 |
|
38 | 49 |
Instance variables: |
62 | 50 |
pattern the matchpattern |
51 |
||
52 |
directory the current directory |
|
59 | 53 |
|
62 | 54 |
timeStamp the time, when directoryContents was last taken |
55 |
directoryId the directories id (inode-nr) when it was taken |
|
285
e1f09d0f2c15
oops - inode is not sufficient for caching dir-contents
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
56 |
directoryName the path when it was taken |
62 | 57 |
directoryContents (cached) contents of current directory |
58 |
directoryFileTypes (cached) file types (symbols) of current directory |
|
59 |
fileTypes file types as shown in list (i.e only matching ones) |
|
60 |
matchBlock if non-nil: block evaluated per full filename; |
|
61 |
only files for which matchBlock returns true are shown. |
|
59 | 62 |
|
62 | 63 |
realAction (internal) the action to perform when a file is selected |
64 |
||
65 |
" |
|
66 |
! |
|
59 | 67 |
|
62 | 68 |
examples |
69 |
" |
|
38 | 70 |
Example use: |
62 | 71 |
FileSelectionLists are typically used in FileSelectionBoxes, |
72 |
or file-browser-like applications. |
|
73 |
Thus, the following examples are a bit untypical. |
|
59 | 74 |
|
75 |
example1 (plain file-list): |
|
62 | 76 |
|list| |
59 | 77 |
|
62 | 78 |
list := FileSelectionList new. |
79 |
list open |
|
59 | 80 |
|
81 |
example2 (scrolled & some action): |
|
62 | 82 |
|top v list| |
59 | 83 |
|
62 | 84 |
top := StandardSystemView new. |
85 |
top extent:(300 @ 200). |
|
86 |
v := ScrollableView for:FileSelectionList in:top. |
|
87 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
88 |
list := v scrolledView. |
|
89 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
90 |
top open |
|
59 | 91 |
|
92 |
example3 (adds a pattern): |
|
62 | 93 |
|top v list| |
59 | 94 |
|
62 | 95 |
top := StandardSystemView new. |
96 |
top extent:(300 @ 200). |
|
97 |
v := ScrollableView for:FileSelectionList in:top. |
|
98 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
99 |
list := v scrolledView. |
|
100 |
list pattern:'*.st'. |
|
101 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
102 |
top open |
|
59 | 103 |
|
104 |
example4 (a more complicated pattern): |
|
62 | 105 |
|top v list| |
59 | 106 |
|
62 | 107 |
top := StandardSystemView new. |
108 |
top extent:(300 @ 200). |
|
109 |
v := ScrollableView for:FileSelectionList in:top. |
|
110 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
111 |
list := v scrolledView. |
|
112 |
list pattern:'[A-D]*.st'. |
|
113 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
114 |
top open |
|
59 | 115 |
|
116 |
example5 (adds a matchblock to show only writable files): |
|
62 | 117 |
|top v list| |
59 | 118 |
|
62 | 119 |
top := StandardSystemView new. |
120 |
top extent:(300 @ 200). |
|
121 |
v := ScrollableView for:FileSelectionList in:top. |
|
122 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
123 |
list := v scrolledView. |
|
124 |
list matchBlock:[:name | |
|
125 |
|fileName| |
|
126 |
fileName := name asFilename. |
|
127 |
fileName isWritable or:[fileName isDirectory] |
|
128 |
]. |
|
129 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
130 |
top open |
|
59 | 131 |
|
132 |
example6 (adds a matchblock to suppress directories): |
|
62 | 133 |
|top v list| |
59 | 134 |
|
62 | 135 |
top := StandardSystemView new. |
136 |
top extent:(300 @ 200). |
|
137 |
v := ScrollableView for:FileSelectionList in:top. |
|
138 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
139 |
list := v scrolledView. |
|
140 |
list matchBlock:[:name | |
|
141 |
name asFilename isDirectory not |
|
142 |
]. |
|
143 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
144 |
top open |
|
59 | 145 |
|
146 |
example7 (adds a matchblock to block moving up (i.e. only allow files here & below): |
|
62 | 147 |
|top v list currentDir| |
59 | 148 |
|
62 | 149 |
currentDir := '.' asFilename pathName. |
59 | 150 |
|
62 | 151 |
top := StandardSystemView new. |
152 |
top extent:(300 @ 200). |
|
153 |
v := ScrollableView for:FileSelectionList in:top. |
|
154 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
155 |
list := v scrolledView. |
|
156 |
list matchBlock:[:name | |
|
157 |
((name endsWith:'/..') and:[list directory pathName = currentDir]) not |
|
158 |
]. |
|
159 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
160 |
top open |
|
59 | 161 |
|
162 |
example8 (block moving up AND show all .rc-files only): |
|
62 | 163 |
|top v list currentDir| |
59 | 164 |
|
62 | 165 |
currentDir := '.' asFilename pathName. |
59 | 166 |
|
62 | 167 |
top := StandardSystemView new. |
168 |
top extent:(300 @ 200). |
|
169 |
v := ScrollableView for:FileSelectionList in:top. |
|
170 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
171 |
list := v scrolledView. |
|
172 |
list pattern:'*.rc'. |
|
173 |
list matchBlock:[:name | |
|
174 |
((name endsWith:'/..') and:[list directory pathName = currentDir]) not |
|
175 |
]. |
|
176 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
177 |
top open |
|
59 | 178 |
|
179 |
example9 (show only .rc-files in current directory): |
|
62 | 180 |
|top v list currentDir| |
59 | 181 |
|
62 | 182 |
currentDir := '.' asFilename pathName. |
59 | 183 |
|
62 | 184 |
top := StandardSystemView new. |
185 |
top extent:(300 @ 200). |
|
186 |
v := ScrollableView for:FileSelectionList in:top. |
|
187 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
188 |
list := v scrolledView. |
|
189 |
list pattern:'*.rc'. |
|
190 |
list matchBlock:[:name | |
|
191 |
name asFilename isDirectory not |
|
192 |
]. |
|
193 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
194 |
top open |
|
59 | 195 |
|
196 |
example10 (show only .rc-files in /etc; dont allow directory changes): |
|
62 | 197 |
|top v list| |
59 | 198 |
|
62 | 199 |
top := StandardSystemView new. |
200 |
top extent:(300 @ 200). |
|
201 |
v := ScrollableView for:FileSelectionList in:top. |
|
202 |
v origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
203 |
list := v scrolledView. |
|
204 |
list directory:'/etc'. |
|
205 |
list pattern:'*.rc'. |
|
206 |
list matchBlock:[:name | name printNL. |
|
207 |
name asFilename isDirectory not |
|
208 |
]. |
|
209 |
list action:[:index | Transcript showCr:'you selected: ' , list selectionValue]. |
|
210 |
top open |
|
11 | 211 |
" |
77 | 212 |
! ! |
213 |
||
214 |
!FileSelectionList methodsFor:'accessing'! |
|
215 |
||
193 | 216 |
action:aBlock |
217 |
"set the action to be performed on a selection" |
|
218 |
||
219 |
realAction := aBlock |
|
220 |
! |
|
221 |
||
222 |
directory |
|
223 |
"return the shown directory" |
|
224 |
||
225 |
^ directory |
|
226 |
! |
|
227 |
||
77 | 228 |
directory:nameOrDirectory |
229 |
"set the lists contents to the filenames in the directory" |
|
230 |
||
231 |
|oldPath name| |
|
232 |
||
233 |
nameOrDirectory isString ifTrue:[ |
|
81 | 234 |
name := nameOrDirectory |
77 | 235 |
] ifFalse:[ |
81 | 236 |
nameOrDirectory isNil ifTrue:[ |
237 |
directory := nil. |
|
238 |
^ self updateList |
|
239 |
]. |
|
240 |
name := nameOrDirectory pathName |
|
77 | 241 |
]. |
242 |
directory isNil ifTrue:[ |
|
81 | 243 |
directory := FileDirectory new. |
244 |
oldPath := nil |
|
77 | 245 |
] ifFalse:[ |
81 | 246 |
oldPath := directory pathName. |
77 | 247 |
]. |
248 |
directory pathName:name. |
|
249 |
realized ifTrue:[ |
|
81 | 250 |
(directory pathName = oldPath) ifFalse:[ |
251 |
self updateList |
|
252 |
] |
|
77 | 253 |
] |
254 |
! |
|
255 |
||
256 |
ignoreParentDirectory:aBoolean |
|
257 |
"set/clear the flag which controls if the parent directory (..) |
|
258 |
is shown in the list. The default is false (i.e. show it)" |
|
259 |
||
260 |
ignoreParentDirectory := aBoolean |
|
261 |
! |
|
262 |
||
282 | 263 |
markDirectories:aBoolean |
264 |
"turn on/off marking of directories with an arrow. |
|
265 |
The default is on" |
|
266 |
||
267 |
markDirectories := aBoolean |
|
268 |
! |
|
269 |
||
193 | 270 |
matchBlock:aBlock |
271 |
"set the matchBlock - if non-nil, it controls which |
|
272 |
names are shown in the list." |
|
273 |
||
274 |
matchBlock := aBlock |
|
275 |
! |
|
276 |
||
77 | 277 |
pattern:aPattern |
278 |
"set the pattern - if it changes, update the list." |
|
21 | 279 |
|
77 | 280 |
pattern ~= aPattern ifTrue:[ |
281 |
pattern := aPattern. |
|
282 |
realized ifTrue:[ |
|
283 |
self updateList |
|
284 |
]. |
|
285 |
]. |
|
286 |
! |
|
287 |
||
288 |
selectedPathname |
|
289 |
"if there is a selection, return its full pathname. |
|
290 |
Of there is no selection, return nil." |
|
291 |
||
292 |
|sel| |
|
293 |
||
294 |
sel := self selectionValue. |
|
295 |
sel isNil ifTrue:[^ nil]. |
|
296 |
^ directory pathName , Filename separator asString , sel. |
|
297 |
||
298 |
! |
|
299 |
||
193 | 300 |
stayInDirectory:aBoolean |
301 |
"set/clear the flag which controls if selecting a directory |
|
302 |
should locally change (if false) or be handled just like |
|
303 |
the selection of a file (if true). |
|
304 |
The default is false (i.e. change and do not tell via action)" |
|
305 |
||
306 |
stayInDirectory := aBoolean |
|
307 |
! ! |
|
308 |
||
309 |
!FileSelectionList methodsFor:'drawing'! |
|
310 |
||
311 |
redrawFromVisibleLine:startVisLineNr to:endVisLineNr |
|
312 |
"redefined to look for directory in every line" |
|
313 |
||
314 |
|l| |
|
315 |
||
316 |
"first, draw chunk of lines" |
|
317 |
super redrawFromVisibleLine:startVisLineNr to:endVisLineNr. |
|
282 | 318 |
markDirectories ifFalse:[^ self]. |
193 | 319 |
|
320 |
"then draw marks" |
|
321 |
startVisLineNr to:endVisLineNr do:[:visLineNr | |
|
282 | 322 |
l := self visibleLineToListLine:visLineNr. |
323 |
l notNil ifTrue:[ |
|
324 |
(fileTypes at:l) == #directory ifTrue:[ |
|
325 |
self drawRightArrowInVisibleLine:visLineNr |
|
326 |
] |
|
327 |
] |
|
193 | 328 |
] |
329 |
! |
|
330 |
||
331 |
redrawVisibleLine:visLineNr |
|
332 |
"if the line is one for a directory, draw a right arrow" |
|
333 |
||
334 |
|l| |
|
335 |
||
336 |
super redrawVisibleLine:visLineNr. |
|
282 | 337 |
markDirectories ifFalse:[^ self]. |
338 |
||
193 | 339 |
l := self visibleLineToListLine:visLineNr. |
340 |
l notNil ifTrue:[ |
|
282 | 341 |
(fileTypes at:l) == #directory ifTrue:[ |
342 |
self drawRightArrowInVisibleLine:visLineNr |
|
343 |
] |
|
193 | 344 |
] |
345 |
! ! |
|
346 |
||
347 |
!FileSelectionList methodsFor:'initialization'! |
|
348 |
||
349 |
initialize |
|
350 |
directory := FileDirectory currentDirectory. |
|
351 |
stayInDirectory := false. |
|
352 |
ignoreParentDirectory := false. |
|
282 | 353 |
markDirectories := true. |
193 | 354 |
super initialize. |
77 | 355 |
|
193 | 356 |
pattern := '*'. |
357 |
self initializeAction. |
|
358 |
||
359 |
"nontypical use ..." |
|
360 |
" |
|
361 |
FileSelectionList new open |
|
362 |
(FileSelectionList new directory:'/etc') open |
|
363 |
(ScrollableView for:FileSelectionList) open |
|
364 |
(HVScrollableView for:FileSelectionList) open |
|
365 |
" |
|
366 |
! |
|
367 |
||
368 |
initializeAction |
|
369 |
"setup action as: selections in list get forwarded to enterfield if not |
|
370 |
a directory; otherwise directory is changed" |
|
371 |
||
372 |
actionBlock := [:lineNr | |
|
282 | 373 |
|entry ok| |
193 | 374 |
|
282 | 375 |
self selection isCollection ifFalse:[ |
376 |
entry := self selectionValue. |
|
377 |
(entry endsWith:' ...') ifTrue:[ |
|
378 |
entry := entry copyWithoutLast:4 "copyTo:(entry size - 4)". |
|
379 |
]. |
|
380 |
(stayInDirectory not |
|
381 |
and:[(directory typeOf:entry) == #directory]) ifTrue:[ |
|
382 |
ok := false. |
|
383 |
(directory isReadable:entry) ifFalse:[ |
|
384 |
self warn:(resources string:'not allowed to read directory %1' with:entry) |
|
385 |
] ifTrue:[ |
|
386 |
(directory isExecutable:entry) ifFalse:[ |
|
387 |
self warn:(resources string:'not allowed to change to directory %1' with:entry) |
|
388 |
] ifTrue:[ |
|
389 |
self directory:(directory pathName , Filename separator asString , entry). |
|
390 |
ok := true. |
|
391 |
] |
|
392 |
]. |
|
393 |
ok ifFalse:[ |
|
394 |
self deselect |
|
395 |
] |
|
193 | 396 |
|
282 | 397 |
] ifFalse:[ |
398 |
realAction notNil ifTrue:[ |
|
399 |
realAction value:lineNr |
|
400 |
] |
|
401 |
] |
|
402 |
] |
|
193 | 403 |
] |
404 |
! |
|
405 |
||
406 |
reinitialize |
|
407 |
directory := FileDirectory currentDirectory. |
|
408 |
super reinitialize |
|
21 | 409 |
! ! |
410 |
||
11 | 411 |
!FileSelectionList methodsFor:'private'! |
412 |
||
77 | 413 |
updateList |
414 |
"set the lists contents to the filenames in the directory" |
|
415 |
||
287 | 416 |
|oldCursor files newList index path obsolete| |
77 | 417 |
|
418 |
directory isNil ifTrue:[ |
|
287 | 419 |
super list:nil. |
420 |
files := newList := fileTypes := nil. |
|
421 |
^ self |
|
77 | 422 |
]. |
423 |
||
424 |
oldCursor := cursor. |
|
425 |
self cursor:(Cursor read). |
|
426 |
||
427 |
" |
|
428 |
if the directory-id changed, MUST update. |
|
429 |
(can happen after a restart, when a file is no longer |
|
430 |
there, has moved or is NFS-mounted differently) |
|
431 |
" |
|
287 | 432 |
obsolete := directoryId ~~ directory id |
433 |
or:[directoryName ~= directory pathName |
|
434 |
or:[timeStamp notNil |
|
435 |
and:[directory timeOfLastChange > timeStamp]]]. |
|
436 |
||
437 |
obsolete ifTrue:[ |
|
438 |
timeStamp := directory timeOfLastChange. |
|
439 |
directoryId := directory id. |
|
440 |
directoryName := directory pathName. |
|
441 |
directoryContents := directory asStringCollection sort. |
|
442 |
directoryFileTypes := OrderedCollection new. |
|
443 |
directoryContents do:[:name | directoryFileTypes add:(directory typeOf:name)]. |
|
77 | 444 |
]. |
445 |
||
446 |
files := directoryContents. |
|
447 |
newList := OrderedCollection new. |
|
448 |
fileTypes := OrderedCollection new. |
|
449 |
index := 1. |
|
450 |
||
95 | 451 |
path := directory pathName , Filename separator asString. |
452 |
files do:[:name | |
|
287 | 453 |
|type| |
77 | 454 |
|
287 | 455 |
(matchBlock isNil or:[matchBlock value:(path , name)]) ifTrue:[ |
456 |
type := directoryFileTypes at:index. |
|
457 |
type == #directory ifTrue:[ |
|
458 |
name = '..' ifTrue:[ |
|
459 |
ignoreParentDirectory ifFalse:[ |
|
460 |
newList add:name. |
|
461 |
fileTypes add:type |
|
462 |
] |
|
463 |
] ifFalse:[ |
|
464 |
name = '.' ifTrue:[ |
|
465 |
"ignore" |
|
466 |
] ifFalse:[ |
|
467 |
newList add:(name ", ' ...'"). |
|
468 |
fileTypes add:type |
|
469 |
] |
|
470 |
] |
|
471 |
] ifFalse:[ |
|
472 |
(pattern isNil |
|
473 |
or:[pattern isEmpty |
|
474 |
or:[pattern = '*' |
|
475 |
or:[pattern match:name]]]) ifTrue:[ |
|
476 |
newList add:name. |
|
477 |
fileTypes add:type |
|
478 |
] |
|
479 |
]. |
|
480 |
]. |
|
481 |
index := index + 1 |
|
77 | 482 |
]. |
483 |
super list:newList. |
|
95 | 484 |
|
77 | 485 |
self cursor:oldCursor. |
287 | 486 |
|
77 | 487 |
! |
488 |
||
21 | 489 |
visibleLineNeedsSpecialCare:visLineNr |
490 |
|l| |
|
491 |
||
492 |
l := self visibleLineToListLine:visLineNr. |
|
493 |
l notNil ifTrue:[ |
|
62 | 494 |
(fileTypes at:l) == #directory ifTrue:[^ true]. |
495 |
^ super visibleLineNeedsSpecialCare:visLineNr |
|
21 | 496 |
]. |
497 |
^ false |
|
498 |
! |
|
499 |
||
193 | 500 |
widthForScrollBetween:firstLine and:lastLine |
501 |
"return the width in pixels for a scroll between firstLine and lastLine |
|
502 |
- return full width here since there might be directory marks" |
|
11 | 503 |
|
193 | 504 |
^ (width - margin - margin) |
11 | 505 |
! ! |
506 |
||
507 |
!FileSelectionList methodsFor:'realization'! |
|
508 |
||
509 |
realize |
|
510 |
"make the view visible; redefined to check if directory is still |
|
511 |
valid (using timestamp and inode numbers) - reread if not" |
|
512 |
||
513 |
(timeStamp isNil |
|
514 |
or:[(directory timeOfLastChange > timeStamp) |
|
515 |
or:[(directoryId isNil) |
|
516 |
or:[directoryId ~~ directory id]]]) ifTrue:[ |
|
62 | 517 |
directoryId := nil. |
518 |
self updateList |
|
11 | 519 |
]. |
520 |
super realize |
|
521 |
! ! |
|
77 | 522 |
|
282 | 523 |
!FileSelectionList class methodsFor:'documentation'! |
524 |
||
525 |
version |
|
287 | 526 |
^ '$Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.19 1996-01-15 22:14:28 ca Exp $' |
282 | 527 |
! ! |