0
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
0
|
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 |
ListSelectionBox subclass:#FileSelectionBox
|
3
|
14 |
instanceVariableNames:'patternField directory timeStamp directoryId
|
|
15 |
directoryContents directoryFileTypes'
|
0
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Views-Interactors'
|
|
19 |
!
|
|
20 |
|
|
21 |
FileSelectionBox comment:'
|
|
22 |
|
5
|
23 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
0
|
24 |
All Rights Reserved
|
|
25 |
|
7
|
26 |
$Header: /cvs/stx/stx/libwidg/FileSelectionBox.st,v 1.4 1993-12-11 01:44:10 claus Exp $
|
|
27 |
written Jan 90 by claus
|
|
28 |
'!
|
|
29 |
|
|
30 |
!FileSelectionBox class methodsFor:'documentation'!
|
|
31 |
|
|
32 |
documentation
|
|
33 |
"
|
0
|
34 |
this class implements file selection boxes. They show a list of
|
|
35 |
files, and perform an action block with the selected pathname as
|
|
36 |
argument when ok is clicked.
|
7
|
37 |
There is an optional PatternField, which shows itself when a pattern
|
|
38 |
is defined. If there is such a pattern, only files matching the pattern
|
|
39 |
will be shown in the list (and directories).
|
|
40 |
"
|
|
41 |
! !
|
0
|
42 |
|
|
43 |
!FileSelectionBox methodsFor:'initialization'!
|
|
44 |
|
|
45 |
initialize
|
|
46 |
directory := FileDirectory currentDirectory.
|
|
47 |
super initialize.
|
|
48 |
|
3
|
49 |
labelField extent:(0.7 @ labelField height).
|
|
50 |
labelField label:(resources string:'select a file:').
|
|
51 |
labelField adjust:#left.
|
|
52 |
|
|
53 |
patternField := EditField
|
|
54 |
origin:(0.7 @ labelField origin y)
|
|
55 |
corner:(1.0 @ labelField corner y)
|
|
56 |
in:self.
|
|
57 |
patternField initialText:'*'.
|
|
58 |
patternField leaveAction:[:p | self updateList].
|
|
59 |
patternField hidden:true.
|
|
60 |
|
0
|
61 |
"selections in list get forwarded to enterfield if not a directory;
|
|
62 |
otherwise directory is changed"
|
|
63 |
|
|
64 |
selectionList action:[:lineNr |
|
|
65 |
|entry|
|
|
66 |
|
|
67 |
entry := selectionList selectionValue.
|
3
|
68 |
(entry endsWith:' ...') ifTrue:[
|
|
69 |
entry := entry copyTo:(entry size - 4).
|
|
70 |
].
|
0
|
71 |
((directory typeOf:entry) == #directory) ifTrue:[
|
7
|
72 |
(directory isReadable:entry) ifFalse:[
|
|
73 |
self warn:(resources string:'not allowed to read directory %1' with:entry)
|
|
74 |
] ifTrue:[
|
|
75 |
(directory isExecutable:entry) ifFalse:[
|
|
76 |
self warn:(resources string:'not allowed to change to directory %1' with:entry)
|
|
77 |
] ifTrue:[
|
|
78 |
self directory:(directory pathName , Filename separator asString , entry)
|
|
79 |
]
|
|
80 |
].
|
0
|
81 |
] ifFalse:[
|
|
82 |
enterField contents:entry
|
|
83 |
]
|
|
84 |
]
|
|
85 |
|
|
86 |
"FileSelectionBox new show"
|
|
87 |
!
|
|
88 |
|
|
89 |
reinitialize
|
|
90 |
directory := FileDirectory currentDirectory.
|
|
91 |
super reinitialize
|
|
92 |
! !
|
|
93 |
|
|
94 |
!FileSelectionBox methodsFor:'accessing'!
|
|
95 |
|
|
96 |
directory:nameOrDirectory
|
|
97 |
"set the lists contents to the filenames in the directory name"
|
|
98 |
|
|
99 |
|oldPath name|
|
|
100 |
|
|
101 |
(nameOrDirectory isKindOf:String) ifTrue:[
|
|
102 |
name := nameOrDirectory
|
|
103 |
] ifFalse:[
|
|
104 |
name := nameOrDirectory pathName
|
|
105 |
].
|
|
106 |
oldPath := directory pathName.
|
|
107 |
directory pathName:name.
|
|
108 |
(directory pathName = oldPath) ifFalse:[
|
|
109 |
self updateList
|
|
110 |
]
|
3
|
111 |
!
|
|
112 |
|
|
113 |
pattern:aPattern
|
7
|
114 |
"set the pattern - this enables the PatternField."
|
|
115 |
|
3
|
116 |
patternField initialText:aPattern.
|
|
117 |
patternField hidden:false.
|
|
118 |
realized ifTrue:[
|
|
119 |
patternField realize.
|
|
120 |
self updateList
|
|
121 |
].
|
0
|
122 |
! !
|
|
123 |
|
|
124 |
!FileSelectionBox methodsFor:'private'!
|
|
125 |
|
|
126 |
updateList
|
|
127 |
"set the lists contents to the filenames in the directory"
|
|
128 |
|
3
|
129 |
|oldCursor oldListCursor files pattern newList index|
|
0
|
130 |
|
|
131 |
oldCursor := cursor.
|
|
132 |
oldListCursor := selectionList cursor.
|
|
133 |
self cursor:(Cursor read).
|
|
134 |
selectionList cursor:(Cursor read).
|
3
|
135 |
directoryId == directory id ifFalse:[
|
|
136 |
timeStamp := directory timeOfLastChange.
|
|
137 |
directoryId := directory id.
|
|
138 |
directoryContents := directory asText sort.
|
|
139 |
directoryFileTypes := OrderedCollection new.
|
|
140 |
directoryContents do:[:name | directoryFileTypes add:(directory typeOf:name)].
|
0
|
141 |
].
|
3
|
142 |
files := directoryContents.
|
|
143 |
pattern := patternField contents.
|
|
144 |
newList := OrderedCollection new.
|
|
145 |
index := 1.
|
|
146 |
files do:[:name |
|
|
147 |
(directoryFileTypes at:index) == #directory ifTrue:[
|
|
148 |
name = '..' ifTrue:[
|
|
149 |
newList add:name
|
|
150 |
] ifFalse:[
|
7
|
151 |
name = '.' ifTrue:[
|
|
152 |
] ifFalse:[
|
|
153 |
newList add:(name , ' ...')
|
|
154 |
]
|
3
|
155 |
]
|
|
156 |
] ifFalse:[
|
|
157 |
(pattern isEmpty or:[pattern match:name]) ifTrue:[
|
|
158 |
newList add:name
|
|
159 |
]
|
|
160 |
].
|
|
161 |
index := index + 1
|
|
162 |
].
|
|
163 |
self list:newList.
|
0
|
164 |
self cursor:oldCursor.
|
|
165 |
selectionList cursor:oldListCursor
|
|
166 |
! !
|
|
167 |
|
|
168 |
!FileSelectionBox methodsFor:'events'!
|
|
169 |
|
|
170 |
show
|
|
171 |
"make the box visible; redefined to check if directory is still
|
|
172 |
valid (using timestamp and inode numbers) - reread if not"
|
|
173 |
|
|
174 |
(timeStamp isNil
|
|
175 |
or:[(directory timeOfLastChange > timeStamp)
|
|
176 |
or:[(directoryId isNil)
|
|
177 |
or:[directoryId ~~ directory id]]]) ifTrue:[
|
3
|
178 |
directoryId := nil.
|
0
|
179 |
self updateList
|
|
180 |
].
|
|
181 |
super show
|
|
182 |
! !
|
|
183 |
|
|
184 |
!FileSelectionBox methodsFor:'user interaction'!
|
|
185 |
|
|
186 |
okPressed
|
|
187 |
"redefined, since action will be evaluated with full path as argument
|
|
188 |
(instead of enterfields contents only as inherited by EnterBox"
|
|
189 |
|
7
|
190 |
|absPath|
|
|
191 |
|
0
|
192 |
self hideAndEvaluate:[:string |
|
|
193 |
okAction notNil ifTrue:[
|
7
|
194 |
(string startsWith:(Filename separator)) ifTrue:[
|
|
195 |
absPath := string
|
|
196 |
] ifFalse:[
|
|
197 |
absPath := directory pathName , Filename separator asString , string
|
|
198 |
].
|
|
199 |
okAction value:absPath
|
0
|
200 |
]
|
|
201 |
]
|
|
202 |
! !
|