11
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 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 |
"
|
|
12 |
|
|
13 |
SelectionInListView subclass:#FileSelectionList
|
|
14 |
instanceVariableNames:'pattern directory timeStamp directoryId
|
|
15 |
directoryContents directoryFileTypes
|
38
|
16 |
fileTypes realAction matchBlock'
|
11
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Views-Text'
|
|
20 |
!
|
|
21 |
|
|
22 |
FileSelectionList comment:'
|
|
23 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
24 |
All Rights Reserved
|
|
25 |
|
38
|
26 |
$Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.4 1994-08-07 13:21:47 claus Exp $
|
11
|
27 |
'!
|
|
28 |
|
|
29 |
!FileSelectionList class methodsFor:'documentation'!
|
|
30 |
|
38
|
31 |
copyright
|
|
32 |
"
|
|
33 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
34 |
All Rights Reserved
|
|
35 |
|
|
36 |
This software is furnished under a license and may be used
|
|
37 |
only in accordance with the terms of that license and with the
|
|
38 |
inclusion of the above copyright notice. This software may not
|
|
39 |
be provided or otherwise made available to, or used by, any
|
|
40 |
other person. No title to or ownership of the software is
|
|
41 |
hereby transferred.
|
|
42 |
"
|
|
43 |
!
|
|
44 |
|
|
45 |
version
|
|
46 |
"
|
|
47 |
$Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.4 1994-08-07 13:21:47 claus Exp $
|
|
48 |
"
|
|
49 |
!
|
|
50 |
|
11
|
51 |
documentation
|
|
52 |
"
|
38
|
53 |
this class implements file selection lists - its basically a
|
|
54 |
selection in list, but adds some right-arrows to directories.
|
|
55 |
(and will soon remember the previous position when changing directories).
|
|
56 |
Only files matching a pattern (plus directories) are shown.
|
21
|
57 |
|
38
|
58 |
Instance variables:
|
|
59 |
pattern the matchpattern
|
|
60 |
directory the current directory
|
|
61 |
timeStamp the time, when directoryContents was last taken
|
|
62 |
directoryId the directories id (inode-nr) when it was taken
|
|
63 |
directoryContents contents of current directory
|
|
64 |
directoryFileTypes file types (symbols) of current directory
|
|
65 |
fileTypes file types as shown in list
|
|
66 |
(i.e only matching ones)
|
|
67 |
realAction the action to perform when a file is selected
|
|
68 |
|
|
69 |
Example use:
|
|
70 |
FileSelectionLists are typically used in FileSelectionBoxes,
|
|
71 |
or file-browser-like applications.
|
11
|
72 |
"
|
|
73 |
! !
|
|
74 |
|
|
75 |
!FileSelectionList methodsFor:'initialization'!
|
|
76 |
|
|
77 |
initialize
|
|
78 |
directory := FileDirectory currentDirectory.
|
|
79 |
super initialize.
|
|
80 |
|
|
81 |
pattern := '*'.
|
|
82 |
|
|
83 |
"selections in list get forwarded to enterfield if not a directory;
|
|
84 |
otherwise directory is changed"
|
|
85 |
|
|
86 |
actionBlock := [:lineNr |
|
38
|
87 |
|entry ok|
|
11
|
88 |
|
|
89 |
(self selection isKindOf:Collection) ifFalse:[
|
|
90 |
entry := self selectionValue.
|
|
91 |
(entry endsWith:' ...') ifTrue:[
|
|
92 |
entry := entry copyTo:(entry size - 4).
|
|
93 |
].
|
|
94 |
((directory typeOf:entry) == #directory) ifTrue:[
|
38
|
95 |
ok := false.
|
11
|
96 |
(directory isReadable:entry) ifFalse:[
|
|
97 |
self warn:(resources string:'not allowed to read directory %1' with:entry)
|
|
98 |
] ifTrue:[
|
|
99 |
(directory isExecutable:entry) ifFalse:[
|
|
100 |
self warn:(resources string:'not allowed to change to directory %1' with:entry)
|
|
101 |
] ifTrue:[
|
38
|
102 |
self directory:(directory pathName , Filename separator asString , entry).
|
|
103 |
ok := true.
|
11
|
104 |
]
|
|
105 |
].
|
38
|
106 |
ok ifFalse:[
|
|
107 |
self deselect
|
|
108 |
]
|
|
109 |
|
11
|
110 |
] ifFalse:[
|
|
111 |
realAction notNil ifTrue:[
|
|
112 |
realAction value:lineNr
|
|
113 |
]
|
|
114 |
]
|
|
115 |
]
|
|
116 |
]
|
|
117 |
|
38
|
118 |
"nontypical use ..."
|
|
119 |
"
|
|
120 |
FileSelectionList new open
|
|
121 |
(FileSelectionList new directory:'/etc') open
|
|
122 |
(ScrollableView for:FileSelectionList) open
|
|
123 |
(HVScrollableView for:FileSelectionList) open
|
|
124 |
"
|
11
|
125 |
!
|
|
126 |
|
|
127 |
reinitialize
|
|
128 |
directory := FileDirectory currentDirectory.
|
|
129 |
super reinitialize
|
|
130 |
! !
|
|
131 |
|
|
132 |
!FileSelectionList methodsFor:'accessing'!
|
|
133 |
|
|
134 |
action:aBlock
|
|
135 |
"set the action to be performed on a selection"
|
|
136 |
|
|
137 |
realAction := aBlock
|
|
138 |
!
|
|
139 |
|
|
140 |
directory
|
|
141 |
"return the shown directory"
|
|
142 |
|
|
143 |
^ directory
|
|
144 |
!
|
|
145 |
|
|
146 |
directory:nameOrDirectory
|
|
147 |
"set the lists contents to the filenames in the directory"
|
|
148 |
|
|
149 |
|oldPath name|
|
|
150 |
|
38
|
151 |
nameOrDirectory isString ifTrue:[
|
11
|
152 |
name := nameOrDirectory
|
|
153 |
] ifFalse:[
|
|
154 |
name := nameOrDirectory pathName
|
|
155 |
].
|
|
156 |
oldPath := directory pathName.
|
|
157 |
directory pathName:name.
|
|
158 |
(directory pathName = oldPath) ifFalse:[
|
|
159 |
self updateList
|
|
160 |
]
|
|
161 |
!
|
|
162 |
|
|
163 |
pattern:aPattern
|
|
164 |
"set the pattern - if it changes, update the list."
|
|
165 |
|
|
166 |
pattern ~= aPattern ifTrue:[
|
|
167 |
pattern := aPattern.
|
|
168 |
realized ifTrue:[
|
|
169 |
self updateList
|
|
170 |
].
|
|
171 |
].
|
38
|
172 |
!
|
|
173 |
|
|
174 |
matchBlock:aBlock
|
|
175 |
"set the matchBlock - if non-nil, it controls which
|
|
176 |
names are shown in the list."
|
|
177 |
|
|
178 |
matchBlock := aBlock
|
11
|
179 |
! !
|
|
180 |
|
21
|
181 |
!FileSelectionList methodsFor:'drawing'!
|
|
182 |
|
|
183 |
redrawFromVisibleLine:startVisLineNr to:endVisLineNr
|
|
184 |
"redefined to look for directory in every line"
|
|
185 |
|
24
|
186 |
|l|
|
|
187 |
|
|
188 |
"first, draw chunk of lines"
|
|
189 |
super redrawFromVisibleLine:startVisLineNr to:endVisLineNr.
|
|
190 |
|
|
191 |
"then draw marks"
|
|
192 |
startVisLineNr to:endVisLineNr do:[:visLineNr |
|
|
193 |
l := self visibleLineToListLine:visLineNr.
|
|
194 |
l notNil ifTrue:[
|
|
195 |
(fileTypes at:l) == #directory ifTrue:[
|
|
196 |
self drawRightArrowInVisibleLine:visLineNr
|
|
197 |
]
|
|
198 |
]
|
21
|
199 |
]
|
|
200 |
!
|
|
201 |
|
|
202 |
redrawVisibleLine:visLineNr
|
|
203 |
"if the line is one for a directory, draw a right arrow"
|
|
204 |
|
|
205 |
|l|
|
|
206 |
|
|
207 |
super redrawVisibleLine:visLineNr.
|
|
208 |
l := self visibleLineToListLine:visLineNr.
|
|
209 |
l notNil ifTrue:[
|
|
210 |
(fileTypes at:l) == #directory ifTrue:[
|
|
211 |
self drawRightArrowInVisibleLine:visLineNr
|
|
212 |
]
|
|
213 |
]
|
|
214 |
! !
|
|
215 |
|
11
|
216 |
!FileSelectionList methodsFor:'private'!
|
|
217 |
|
21
|
218 |
visibleLineNeedsSpecialCare:visLineNr
|
|
219 |
|l|
|
|
220 |
|
|
221 |
l := self visibleLineToListLine:visLineNr.
|
|
222 |
l notNil ifTrue:[
|
|
223 |
(fileTypes at:l) == #directory ifTrue:[^ true].
|
|
224 |
^ super visibleLineNeedsSpecialCare:visLineNr
|
|
225 |
].
|
|
226 |
^ false
|
|
227 |
!
|
|
228 |
|
|
229 |
widthForScrollBetween:firstLine and:lastLine
|
|
230 |
"return the width in pixels for a scroll between firstLine and lastLine
|
|
231 |
- return full width here since there might be directory marks"
|
|
232 |
|
|
233 |
^ (width - margin - margin)
|
|
234 |
!
|
|
235 |
|
11
|
236 |
updateList
|
|
237 |
"set the lists contents to the filenames in the directory"
|
|
238 |
|
|
239 |
|oldCursor files newList index|
|
|
240 |
|
|
241 |
oldCursor := cursor.
|
|
242 |
self cursor:(Cursor read).
|
|
243 |
|
38
|
244 |
"
|
|
245 |
if the directory-id changed, MUST update.
|
|
246 |
(can happen after a restart, when a file is no longer
|
|
247 |
there, has moved or is NFS-mounted differently)
|
|
248 |
"
|
11
|
249 |
directoryId == directory id ifFalse:[
|
|
250 |
timeStamp := directory timeOfLastChange.
|
|
251 |
directoryId := directory id.
|
|
252 |
directoryContents := directory asText sort.
|
|
253 |
directoryFileTypes := OrderedCollection new.
|
|
254 |
directoryContents do:[:name | directoryFileTypes add:(directory typeOf:name)].
|
|
255 |
].
|
|
256 |
|
|
257 |
files := directoryContents.
|
|
258 |
newList := OrderedCollection new.
|
21
|
259 |
fileTypes := OrderedCollection new.
|
11
|
260 |
index := 1.
|
|
261 |
files do:[:name |
|
38
|
262 |
(matchBlock isNil or:[matchBlock value:name]) ifTrue:[
|
|
263 |
(directoryFileTypes at:index) == #directory ifTrue:[
|
|
264 |
name = '..' ifTrue:[
|
|
265 |
newList add:name.
|
|
266 |
fileTypes add:(directoryFileTypes at:index)
|
|
267 |
] ifFalse:[
|
|
268 |
name = '.' ifTrue:[
|
|
269 |
"ignore"
|
|
270 |
] ifFalse:[
|
|
271 |
newList add:(name ", ' ...'").
|
|
272 |
fileTypes add:(directoryFileTypes at:index)
|
|
273 |
]
|
|
274 |
]
|
11
|
275 |
] ifFalse:[
|
38
|
276 |
(pattern isNil or:[pattern isEmpty or:[pattern = '*' or:[pattern match:name]]]) ifTrue:[
|
|
277 |
newList add:name.
|
21
|
278 |
fileTypes add:(directoryFileTypes at:index)
|
11
|
279 |
]
|
38
|
280 |
].
|
11
|
281 |
].
|
|
282 |
index := index + 1
|
|
283 |
].
|
|
284 |
super list:newList.
|
|
285 |
self cursor:oldCursor.
|
|
286 |
! !
|
|
287 |
|
|
288 |
!FileSelectionList methodsFor:'realization'!
|
|
289 |
|
|
290 |
realize
|
|
291 |
"make the view visible; redefined to check if directory is still
|
|
292 |
valid (using timestamp and inode numbers) - reread if not"
|
|
293 |
|
|
294 |
(timeStamp isNil
|
|
295 |
or:[(directory timeOfLastChange > timeStamp)
|
|
296 |
or:[(directoryId isNil)
|
|
297 |
or:[directoryId ~~ directory id]]]) ifTrue:[
|
|
298 |
directoryId := nil.
|
|
299 |
self updateList
|
|
300 |
].
|
|
301 |
super realize
|
|
302 |
! !
|