author | penk |
Wed, 24 Apr 2002 13:35:01 +0200 | |
changeset 2095 | 46120b1fd644 |
parent 1960 | 2363303c1c8c |
child 2104 | 327f2f77ec12 |
permissions | -rw-r--r-- |
1431 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
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 |
||
1780 | 14 |
"{ Package: 'stx:libwidg2' }" |
15 |
||
1390 | 16 |
HierarchicalList subclass:#HierarchicalFileList |
1581 | 17 |
instanceVariableNames:'icons matchBlock showCursor' |
1390 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
1430 | 20 |
category:'Views-Support' |
1390 | 21 |
! |
22 |
||
23 |
HierarchicalItem subclass:#File |
|
24 |
instanceVariableNames:'fileName baseName icon' |
|
25 |
classVariableNames:'' |
|
26 |
poolDictionaries:'' |
|
27 |
privateIn:HierarchicalFileList |
|
28 |
! |
|
29 |
||
30 |
HierarchicalFileList::File subclass:#Directory |
|
31 |
instanceVariableNames:'modificationTime' |
|
32 |
classVariableNames:'' |
|
33 |
poolDictionaries:'' |
|
34 |
privateIn:HierarchicalFileList::File |
|
35 |
! |
|
36 |
||
1431 | 37 |
!HierarchicalFileList class methodsFor:'documentation'! |
38 |
||
39 |
copyright |
|
40 |
" |
|
41 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
42 |
All Rights Reserved |
|
43 |
||
44 |
This software is furnished under a license and may be used |
|
45 |
only in accordance with the terms of that license and with the |
|
46 |
inclusion of the above copyright notice. This software may not |
|
47 |
be provided or otherwise made available to, or used by, any |
|
48 |
other person. No title to or ownership of the software is |
|
49 |
hereby transferred. |
|
50 |
" |
|
51 |
||
52 |
! ! |
|
1390 | 53 |
|
54 |
!HierarchicalFileList class methodsFor:'examples'! |
|
55 |
||
56 |
test |
|
57 |
|top sel list item| |
|
58 |
||
59 |
list := HierarchicalFileList new. |
|
60 |
list directory:(Filename homeDirectory). |
|
61 |
list showRoot:false. |
|
62 |
list matchBlock:[:fn :isDir| |suf rslt| |
|
63 |
(rslt := isDir) ifFalse:[ |
|
64 |
suf := fn suffix. |
|
65 |
||
66 |
suf size ~~ 0 ifTrue:[ |
|
67 |
rslt := ( suf = 'c' |
|
68 |
or:[suf = 'h' |
|
69 |
or:[suf = 'hi']] |
|
70 |
) |
|
71 |
] |
|
72 |
]. |
|
73 |
rslt |
|
74 |
]. |
|
75 |
||
76 |
top := StandardSystemView new; extent:300@300. |
|
77 |
sel := ScrollableView for:HierarchicalListView miniScroller:true |
|
78 |
origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
79 |
||
80 |
sel list:list. |
|
81 |
list root expand. |
|
82 |
||
83 |
sel doubleClickAction:[:i| (list at:i) toggleExpand ]. |
|
84 |
sel indicatorAction:[:i| (list at:i) toggleExpand ]. |
|
85 |
||
86 |
top open. |
|
87 |
||
88 |
||
89 |
! ! |
|
90 |
||
91 |
!HierarchicalFileList class methodsFor:'resources'! |
|
92 |
||
93 |
icons |
|
1780 | 94 |
^ FileBrowser icons. |
1390 | 95 |
|
96 |
||
97 |
||
98 |
||
99 |
! ! |
|
100 |
||
101 |
!HierarchicalFileList methodsFor:'accessing'! |
|
102 |
||
103 |
directory |
|
104 |
"returns the root directory or nil |
|
105 |
" |
|
106 |
^ root notNil ifTrue:[root fileName] ifFalse:[nil] |
|
107 |
||
108 |
! |
|
109 |
||
110 |
directory:aDirectory |
|
111 |
"set the root directory or nil |
|
112 |
" |
|
113 |
|directory| |
|
114 |
||
1960
2363303c1c8c
monitoring must be enebled by user (no longer automatically)
ca
parents:
1867
diff
changeset
|
115 |
"/ must explicitly enabled by user: ca |
2363303c1c8c
monitoring must be enebled by user (no longer automatically)
ca
parents:
1867
diff
changeset
|
116 |
"/ monitoringTaskDelay := 1. |
1390 | 117 |
|
118 |
(aDirectory notNil and:[(directory := aDirectory asFilename) exists]) ifTrue:[ |
|
119 |
directory isDirectory ifFalse:[ |
|
120 |
directory := directory directory |
|
121 |
] |
|
122 |
] ifFalse:[ |
|
123 |
directory := nil |
|
124 |
]. |
|
125 |
||
126 |
directory = self directory ifFalse:[ |
|
127 |
directory notNil ifTrue:[ |
|
128 |
directory := File fileName:directory isDirectory:true |
|
129 |
]. |
|
130 |
self root:directory |
|
131 |
]. |
|
132 |
! ! |
|
133 |
||
134 |
!HierarchicalFileList methodsFor:'actions'! |
|
135 |
||
136 |
matchBlock |
|
137 |
"set the matchBlock - if non-nil, it controls which files are visible. |
|
138 |
" |
|
139 |
^ matchBlock |
|
140 |
||
141 |
! |
|
142 |
||
143 |
matchBlock:aBlock |
|
144 |
"set the matchBlock - if non-nil, it controls which files are visible. |
|
145 |
" |
|
146 |
matchBlock := aBlock. |
|
1866 | 147 |
|
148 |
root notNil ifTrue:[ |
|
1867 | 149 |
self recursionLock critical:[ |
150 |
self stopMonitoringTask. |
|
151 |
root matchBlockChanged. |
|
152 |
]. |
|
153 |
self startMonitoringTask. |
|
1866 | 154 |
]. |
1867 | 155 |
|
1390 | 156 |
! ! |
157 |
||
158 |
!HierarchicalFileList methodsFor:'protocol'! |
|
159 |
||
160 |
childrenFor:anItem |
|
161 |
"returns all visible children derived from the physical |
|
162 |
directory contents. |
|
163 |
" |
|
164 |
|contents list block| |
|
165 |
||
166 |
list := #(). |
|
167 |
||
168 |
anItem isDirectory ifFalse:[ |
|
169 |
^ list |
|
170 |
]. |
|
171 |
||
1581 | 172 |
contents := DirectoryContents directoryNamed:(anItem fileName). |
1390 | 173 |
|
1581 | 174 |
contents notNil ifTrue:[ |
175 |
list := OrderedCollection new. |
|
176 |
block := self matchBlockFor:anItem. |
|
1390 | 177 |
|
1581 | 178 |
block isNil ifTrue:[ |
179 |
contents contentsDo:[:fn :isDir| |
|
180 |
list add:(File fileName:fn isDirectory:isDir) |
|
181 |
] |
|
182 |
] ifFalse:[ |
|
183 |
contents contentsDo:[:fn :isDir| |
|
184 |
(block value:fn value:isDir) ifTrue:[ |
|
1390 | 185 |
list add:(File fileName:fn isDirectory:isDir) |
186 |
] |
|
187 |
] |
|
188 |
] |
|
189 |
]. |
|
190 |
^ list |
|
191 |
||
192 |
||
193 |
||
194 |
||
195 |
! |
|
196 |
||
197 |
hasChildrenFor:anItem |
|
198 |
"returns true if the physical directory contains at least |
|
199 |
one visible item otherwise false. |
|
200 |
" |
|
201 |
|block| |
|
202 |
||
203 |
anItem isDirectory ifFalse:[ |
|
204 |
^ false |
|
205 |
]. |
|
206 |
||
207 |
(block := self matchBlockFor:anItem) isNil ifTrue:[ |
|
208 |
block := [:aFilename :isDirectory| true ] |
|
209 |
]. |
|
210 |
^ DirectoryContents directoryNamed:(anItem fileName) detect:block |
|
211 |
! |
|
212 |
||
213 |
iconFor:anItem |
|
214 |
"returns the icon for an item |
|
215 |
" |
|
1867 | 216 |
|fn key suff mimeType icn| |
1390 | 217 |
|
218 |
fn := anItem fileName. |
|
1867 | 219 |
icons isNil ifTrue:[ icons := self class icons ]. |
1390 | 220 |
|
221 |
fn isDirectory ifTrue:[ |
|
222 |
(fn isReadable and:[fn isExecutable]) ifTrue:[ |
|
223 |
key := fn isSymbolicLink ifTrue:[#directoryLink] |
|
224 |
ifFalse:[#directory] |
|
225 |
] ifFalse:[ |
|
226 |
key := #directoryLocked |
|
1867 | 227 |
]. |
228 |
^ icons at:key ifAbsent:nil |
|
1390 | 229 |
]. |
1867 | 230 |
|
231 |
fn isReadable ifFalse:[ ^ icons at:#fileLocked ifAbsent:nil ]. |
|
232 |
fn isSymbolicLink ifTrue:[ ^ icons at:#fileLink ifAbsent:nil ]. |
|
233 |
||
234 |
suff := fn suffix. |
|
235 |
||
236 |
(suff = 'bak' or:[suff = 'sav']) ifTrue:[ |
|
237 |
suff := fn withoutSuffix suffix. |
|
1390 | 238 |
]. |
239 |
||
1867 | 240 |
suff size > 0 ifTrue:[ |
241 |
( #( 'o' 'so' 'a' ) includes:suff) ifTrue:[ |
|
242 |
^ icons at:#executableFile ifAbsent:nil |
|
243 |
]. |
|
244 |
mimeType := MIMETypes mimeTypeForSuffix:suff. |
|
245 |
||
246 |
mimeType notNil ifTrue:[ |
|
247 |
(icn := icons at:mimeType ifAbsent:nil) notNil ifTrue:[^ icn]. |
|
248 |
||
249 |
(mimeType startsWith:'image/') ifTrue:[ |
|
250 |
^ icons at:#imageFile ifAbsent:nil |
|
251 |
]. |
|
252 |
]. |
|
253 |
]. |
|
254 |
||
255 |
fn isExecutableProgram ifTrue:[ |
|
256 |
^ icons at:#executableFile ifAbsent:nil |
|
257 |
]. |
|
258 |
^ icons at:#file ifAbsent:nil |
|
1390 | 259 |
! |
260 |
||
261 |
matchBlockFor:anItem |
|
262 |
"get the matchBlock - if non-nil, it controls which files are |
|
263 |
visible within the physical directory |
|
264 |
" |
|
265 |
^ matchBlock |
|
2095 | 266 |
! |
267 |
||
268 |
validateIcon:anIcon for:anItem |
|
269 |
^ anIcon |
|
1390 | 270 |
! ! |
271 |
||
1581 | 272 |
!HierarchicalFileList methodsFor:'searching'! |
273 |
||
274 |
detectItemFor:aPathOrFilename |
|
275 |
"make a filename visible |
|
276 |
" |
|
277 |
|file node p1 p2 sz| |
|
278 |
||
1582 | 279 |
aPathOrFilename isNil ifTrue:[^ nil]. |
1581 | 280 |
|
281 |
file := aPathOrFilename asFilename. |
|
282 |
||
283 |
file exists ifFalse:[ |
|
1582 | 284 |
^ nil |
1581 | 285 |
]. |
1582 | 286 |
(node := root) isNil ifTrue:[^ nil]. |
1581 | 287 |
|
288 |
p1 := node pathName. |
|
289 |
p2 := file pathName. |
|
290 |
||
1582 | 291 |
(p2 startsWith:p1) ifFalse:[^ nil]. |
1581 | 292 |
sz := 1 + p1 size. |
293 |
||
294 |
(p1 last == Filename separator) ifFalse:[ |
|
295 |
sz := sz + 1. |
|
296 |
]. |
|
297 |
||
1582 | 298 |
p2 size > sz ifFalse:[^ nil]. |
1581 | 299 |
|
300 |
p2 := Filename components:(p2 copyFrom:sz). |
|
301 |
||
302 |
p2 do:[:bn| |
|
303 |
node := node detect:[:el| el baseName = bn] ifNone:nil. |
|
304 |
node isNil ifTrue:[^ nil ]. |
|
305 |
]. |
|
306 |
^ node |
|
307 |
! ! |
|
308 |
||
1390 | 309 |
!HierarchicalFileList::File class methodsFor:'instance creation'! |
310 |
||
311 |
fileName:aFileName isDirectory:isDirectory |
|
312 |
"instance creation |
|
313 |
" |
|
314 |
|item| |
|
315 |
||
316 |
item := isDirectory ifTrue:[Directory new] ifFalse:[HierarchicalFileList::File new]. |
|
317 |
item fileName:aFileName. |
|
318 |
^ item |
|
319 |
||
320 |
! ! |
|
321 |
||
322 |
!HierarchicalFileList::File methodsFor:'accessing'! |
|
323 |
||
324 |
baseName |
|
325 |
"returns the baseName |
|
326 |
" |
|
327 |
^ baseName |
|
328 |
||
329 |
||
330 |
! |
|
331 |
||
332 |
children |
|
333 |
"always returns an empty list |
|
334 |
" |
|
335 |
^ #() |
|
336 |
! |
|
337 |
||
338 |
fileName |
|
339 |
"returns the fileName |
|
340 |
" |
|
341 |
^ fileName |
|
342 |
||
343 |
||
344 |
! |
|
345 |
||
346 |
fileName:fname |
|
347 |
"instance creation |
|
348 |
" |
|
349 |
fileName := fname. |
|
350 |
baseName := fname baseName. |
|
351 |
! |
|
352 |
||
353 |
icon |
|
354 |
"returns the icon key |
|
355 |
" |
|
356 |
|model| |
|
357 |
||
2095 | 358 |
model := self model. |
359 |
model ifNil:[^ nil]. |
|
360 |
||
1390 | 361 |
icon isNil ifTrue:[ |
2095 | 362 |
icon := model iconFor:self |
1390 | 363 |
]. |
2095 | 364 |
^ model validateIcon:icon for:self |
1390 | 365 |
! |
366 |
||
367 |
label |
|
368 |
"returns the printable name, the baseName |
|
369 |
" |
|
370 |
^ baseName |
|
371 |
||
372 |
||
373 |
! |
|
374 |
||
375 |
pathName |
|
376 |
"returns the pathName |
|
377 |
" |
|
378 |
^ fileName pathName |
|
379 |
! ! |
|
380 |
||
381 |
!HierarchicalFileList::File methodsFor:'accessing hierarchy'! |
|
382 |
||
383 |
recursiveExpand |
|
384 |
"redefined to expand |
|
385 |
" |
|
386 |
self expand |
|
387 |
||
388 |
||
389 |
! ! |
|
390 |
||
391 |
!HierarchicalFileList::File methodsFor:'invalidate'! |
|
392 |
||
393 |
invalidate |
|
394 |
"invalidate the contents |
|
395 |
" |
|
396 |
self invalidateRepairNow:false |
|
397 |
||
398 |
! |
|
399 |
||
400 |
invalidateRepairNow |
|
401 |
"invalidate the contents; repair now |
|
402 |
" |
|
403 |
self invalidateRepairNow:true |
|
404 |
||
405 |
! |
|
406 |
||
407 |
invalidateRepairNow:doRepair |
|
408 |
"invalidate the contents; dependent on the boolean |
|
409 |
do repair immediately |
|
410 |
" |
|
411 |
||
412 |
||
1866 | 413 |
! |
414 |
||
415 |
matchBlockChanged |
|
416 |
"called if the matchBlock changed |
|
417 |
" |
|
418 |
||
1390 | 419 |
! ! |
420 |
||
421 |
!HierarchicalFileList::File methodsFor:'queries'! |
|
422 |
||
423 |
hasChildren |
|
424 |
"always returns false |
|
425 |
" |
|
426 |
^ false |
|
427 |
! |
|
428 |
||
429 |
isDirectory |
|
430 |
"always returns false |
|
431 |
" |
|
432 |
^ false |
|
433 |
||
434 |
! ! |
|
435 |
||
436 |
!HierarchicalFileList::File::Directory methodsFor:'accessing'! |
|
437 |
||
438 |
children |
|
439 |
"returns the list of children |
|
440 |
" |
|
441 |
|model list| |
|
442 |
||
1867 | 443 |
children notNil ifTrue:[ |
444 |
^ children |
|
445 |
]. |
|
1390 | 446 |
|
1867 | 447 |
(model := self model) isNil ifTrue:[ |
448 |
"/ must reread later |
|
449 |
modificationTime := children := nil. |
|
450 |
^ nil |
|
451 |
]. |
|
1390 | 452 |
|
1867 | 453 |
children := #(). |
454 |
modificationTime := fileName modificationTime. |
|
455 |
list := model childrenFor:self. |
|
456 |
||
457 |
list size ~~ 0 ifTrue:[ |
|
458 |
list do:[:aChild| aChild parent:self]. |
|
459 |
children := list. |
|
1390 | 460 |
]. |
461 |
^ children |
|
462 |
! |
|
463 |
||
464 |
icon |
|
465 |
"returns the icon |
|
466 |
" |
|
467 |
(isExpanded and:[children size ~~ 0]) ifTrue:[ |
|
468 |
^ nil |
|
469 |
]. |
|
470 |
^ super icon |
|
471 |
! ! |
|
472 |
||
473 |
!HierarchicalFileList::File::Directory methodsFor:'queries'! |
|
474 |
||
475 |
hasChildren |
|
476 |
"returns true if children exists |
|
477 |
" |
|
478 |
^ children isNil or:[children notEmpty] |
|
479 |
! |
|
480 |
||
481 |
isDirectory |
|
482 |
"always returns true |
|
483 |
" |
|
484 |
^ true |
|
485 |
||
486 |
||
487 |
! ! |
|
488 |
||
489 |
!HierarchicalFileList::File::Directory methodsFor:'validation'! |
|
490 |
||
491 |
invalidateRepairNow:doRepair |
|
492 |
"invalidate contents |
|
493 |
" |
|
494 |
modificationTime := nil. |
|
495 |
||
496 |
doRepair ifTrue:[ |
|
497 |
self monitoringCycle |
|
498 |
] ifFalse:[ |
|
499 |
(isExpanded or:[children size == 0]) ifFalse:[ |
|
500 |
children := nil |
|
501 |
] |
|
502 |
]. |
|
503 |
||
504 |
! |
|
505 |
||
1866 | 506 |
matchBlockChanged |
507 |
"called if the matchBlock changed |
|
508 |
" |
|
509 |
modificationTime := nil. |
|
510 |
||
511 |
isExpanded ifFalse:[ |
|
512 |
children := nil. |
|
1867 | 513 |
] ifTrue:[ |
514 |
self monitoringCycle. |
|
1866 | 515 |
|
1867 | 516 |
children size ~~ 0 ifTrue:[ |
517 |
children do:[:aChild| aChild matchBlockChanged ] |
|
518 |
] |
|
1866 | 519 |
]. |
520 |
! |
|
521 |
||
1390 | 522 |
monitoringCycle |
523 |
"run monitoring cycle |
|
524 |
" |
|
525 |
|list size name modifyTime isNotEmpty wasNotEmpty model| |
|
526 |
||
527 |
modifyTime := fileName modificationTime. |
|
528 |
||
529 |
(modificationTime notNil and:[modifyTime <= modificationTime]) ifTrue:[ |
|
530 |
^ self |
|
531 |
]. |
|
532 |
model := self model. |
|
533 |
modificationTime := modifyTime. |
|
534 |
||
535 |
isExpanded ifFalse:[ |
|
536 |
||
537 |
"/ CHECK WHETHER CHILDREN EXIST( INDICATOR ) |
|
538 |
"/ ========================================= |
|
539 |
||
540 |
isNotEmpty := model hasChildrenFor:self. |
|
541 |
||
542 |
"/ check whether has changed durring evaluation |
|
543 |
(isExpanded or:[modificationTime ~= modifyTime]) ifFalse:[ |
|
544 |
wasNotEmpty := children isNil. |
|
545 |
children := isNotEmpty ifTrue:[nil] ifFalse:[#()]. |
|
546 |
||
547 |
wasNotEmpty ~~ isNotEmpty ifTrue:[ |
|
548 |
self changed |
|
549 |
] |
|
550 |
]. |
|
551 |
^ self |
|
552 |
||
553 |
]. |
|
554 |
||
555 |
"/ START MERGING( CONTENTS IS VISIBLE ) |
|
556 |
"/ ==================================== |
|
557 |
||
558 |
list := model childrenFor:self. |
|
559 |
||
560 |
list size == 0 ifTrue:[ "/ contents becomes empty |
|
561 |
^ self removeAll "/ clear contents |
|
562 |
]. |
|
563 |
(size := children size) == 0 ifTrue:[ "/ old contents was empty |
|
564 |
^ self addAll:list. "/ take over new contents |
|
565 |
]. |
|
566 |
||
567 |
size to:1 by:-1 do:[:anIndex| "/ remove invisible items |
|
568 |
name := (children at:anIndex) baseName. |
|
569 |
||
570 |
(list findFirst:[:i|i baseName = name]) == 0 ifTrue:[ |
|
571 |
self removeIndex:anIndex |
|
572 |
] |
|
573 |
]. |
|
574 |
||
575 |
list keysAndValuesDo:[:anIndex :anItem| "/ add new visible items |
|
576 |
name := anItem baseName. |
|
577 |
||
578 |
(children findFirst:[:i|i baseName = name]) == 0 ifTrue:[ |
|
579 |
self add:anItem beforeIndex:anIndex |
|
580 |
] |
|
581 |
]. |
|
582 |
! ! |
|
583 |
||
584 |
!HierarchicalFileList class methodsFor:'documentation'! |
|
585 |
||
586 |
version |
|
2095 | 587 |
^ '$Header: /cvs/stx/stx/libwidg2/HierarchicalFileList.st,v 1.11 2002-04-24 11:35:01 penk Exp $' |
1390 | 588 |
! ! |