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