FileSelectionBox.st
author claus
Fri, 16 Jul 1993 11:44:44 +0200
changeset 0 e6a541c1c0eb
child 3 9d7eefb5e69f
permissions -rw-r--r--
Initial revision

"
 COPYRIGHT (c) 1990-93 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

ListSelectionBox subclass:#FileSelectionBox
       instanceVariableNames:'patternField directory timeStamp directoryId'
       classVariableNames:''
       poolDictionaries:''
       category:'Views-Interactors'
!

FileSelectionBox comment:'

COPYRIGHT (c) 1990-93 by Claus Gittinger
              All Rights Reserved

this class implements file selection boxes. They show a list of
files, and perform an action block with the selected pathname as
argument when ok is clicked.

%W% %E%
written Jan 90 by claus
'!

!FileSelectionBox methodsFor:'initialization'!

initialize
    directory := FileDirectory currentDirectory.
    super initialize.

    "selections in list get forwarded to enterfield if not a directory;
     otherwise directory is changed"

    selectionList action:[:lineNr |
        |entry|

        entry := selectionList selectionValue.
        ((directory typeOf:entry) == #directory) ifTrue:[
            self directory:(directory pathName , '/' , entry)
        ] ifFalse:[
            enterField contents:entry
        ]
    ]

    "FileSelectionBox new show"
!

reinitialize
    directory := FileDirectory currentDirectory.
    super reinitialize
! !

!FileSelectionBox methodsFor:'accessing'!

directory:nameOrDirectory
    "set the lists contents to the filenames in the directory name"

    |oldPath name|

    (nameOrDirectory isKindOf:String) ifTrue:[
        name := nameOrDirectory
    ] ifFalse:[
        name := nameOrDirectory pathName
    ].
    oldPath := directory pathName.
    directory pathName:name.
    (directory pathName = oldPath) ifFalse:[
        self updateList
    ]
! !

!FileSelectionBox methodsFor:'private'!

updateList
    "set the lists contents to the filenames in the directory"

    |oldCursor oldListCursor files|

    oldCursor := cursor.
    oldListCursor := selectionList cursor.
    self cursor:(Cursor read).
    selectionList cursor:(Cursor read).
    timeStamp := directory timeOfLastChange.
    directoryId := directory id.
    files := directory asText sort.
    ((files at:1) = '.') ifTrue:[
        files removeIndex:1
    ].
    self list:files.
    self cursor:oldCursor.
    selectionList cursor:oldListCursor
! !

!FileSelectionBox methodsFor:'events'!

show
    "make the box visible; redefined to check if directory is still 
     valid (using timestamp and inode numbers) - reread if not"

    (timeStamp isNil 
     or:[(directory timeOfLastChange > timeStamp) 
     or:[(directoryId isNil)
     or:[directoryId ~~ directory id]]]) ifTrue:[
        self updateList
    ].
    super show
! !

!FileSelectionBox methodsFor:'user interaction'!

okPressed
    "redefined, since action will be evaluated with full path as argument
     (instead of enterfields contents only as inherited by EnterBox"

    self hideAndEvaluate:[:string |
        okAction notNil ifTrue:[
            okAction value:(directory pathName , '/' , string)
        ]
    ]
! !