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