FilenameEditField.st
author Claus Gittinger <cg@exept.de>
Wed, 09 Jun 2004 17:19:52 +0200
changeset 2717 f5738b80c8ca
parent 2584 5b3da940cb71
child 3195 39debbeac7d5
permissions -rw-r--r--
entryCompletionBlockHolder

"
 COPYRIGHT (c) 1994 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.
"

"{ Package: 'stx:libwidg2' }"

EditField subclass:#FilenameEditField
	instanceVariableNames:'directoriesOnly filesOnly directory acceptOnExpand'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Text'
!

!FilenameEditField class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1994 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.
"
!

documentation
"
    like a normal editField, but does filename-completion on the last word of
    the contents, when TAB is pressed.
    Filename completion ignores regular files if directoriesOnly is true,
    and ignores directories, if filesOnly is true. Both default to false.

    [author:]
        Claus Gittinger
"
! !

!FilenameEditField methodsFor:'accessing'!

acceptOnExpand
    "return the autoAccept on filename expansion flag.
     The default is true, which means that an expand accepts"

    ^ acceptOnExpand
!

acceptOnExpand:aBoolean
    "set/clear autoAccept on filename expansion.
     The default is true, which means that an expand accepts"

    acceptOnExpand := aBoolean
!

contents:someText
    "redefined to add a trailing file-separator if the displayed
     filename is a directory"

    ^ self
        contents:someText 
        addSeparatorToDirectories:true
!

contents:someText addSeparatorToDirectories:doAddSeparator
    "optionally add a trailing file-separator if the displayed
     filename is a directory."

    |newText f nm|

    newText := someText.
    doAddSeparator ifTrue:[
        directoriesOnly ifFalse:[
            someText notNil ifTrue:[
                f := someText asFilename.
                (f exists and:[f "asAbsoluteFilename" isDirectory]) ifTrue:[
                    nm := f name.
                    (nm endsWith:f separator) ifFalse:[
                        newText := nm copyWith:f separator
                    ]
                ]
            ].
        ].
    ].
    super contents:newText.
    self cursorToEndOfLine.
!

directoriesOnly
    "set to expand names for directories only"

    directoriesOnly := true.
!

directory
    ^ directory

    "Modified: 7.9.1995 / 10:12:40 / claus"
!

directory:aFilename
    aFilename isNil ifTrue:[
        directory := Filename currentDirectory
    ] ifFalse:[
        directory := aFilename asFilename
    ]
    "Modified: 7.9.1995 / 10:12:55 / claus"
!

filesOnly
    "set to expand names for files only"

    filesOnly := true.
!

initialText:aString selected:aBoolean
    "move the cursor to the end - thats the most interesting part of
     a filename
    "

    super initialText:aString selected:aBoolean.
    self cursorToEndOfLine.
!

showsDirectoriesOnly
    "return if expanding names for directories only"

    ^ directoriesOnly

    "Modified: 6.9.1995 / 20:35:30 / claus"
!

showsFilesOnly
    "return if expanding names for files only"

    ^ filesOnly

    "Modified: 6.9.1995 / 20:34:57 / claus"
! !

!FilenameEditField methodsFor:'event handling'!

keyPress:key x:x y:y
    "handle tab for filename completion.
     Bug: it completes the last word; it should complete the
          word before the cursor."

    <resource: #keyboard ( #Tab #FilenameCompletion ) >

    |oldContents newContents|

    enabled ifTrue:[
        ((key == entryCompletionCharacter)
        or:[key == #FilenameCompletion]) ifTrue:[
            oldContents := self contents.
            oldContents isNil ifTrue:[
                oldContents := ''
            ] ifFalse:[
                oldContents := oldContents asString
            ].

            self entryCompletionBlock value:oldContents.

            newContents := self contents.
            newContents isNil ifTrue:[
                newContents := ''
            ] ifFalse:[
                newContents := newContents asString
            ].
            newContents ~= oldContents ifTrue:[
                self textChanged.
                acceptOnExpand ifTrue:[
                    self accept 
                ]
            ].
            ^ self
        ]
    ].
    ^ super keyPress:key x:x y:y.

    "Modified: 7.3.1996 / 13:16:49 / cg"
! !

!FilenameEditField methodsFor:'initialization'!

initialize
    super initialize.

    acceptOnExpand := true.
    directoriesOnly := filesOnly := false.
    directory := Filename currentDirectory.

    self 
        entryCompletionBlock:[:contents |
            |newString isMultiMatch canonContents|

            isMultiMatch := false.
            canonContents := Filename canonicalize:contents.
            newString := Filename 
                            filenameCompletionFor:canonContents 
                            directory:directory 
                            directoriesOnly:directoriesOnly 
                            filesOnly:filesOnly 
                            ifMultiple:[:dir |
"/                                dir asFilename isDirectory ifTrue:[
"/                                    self changed:#directory with:dir.
"/                                ].
                                isMultiMatch := true.
                                self flash.
                            ].
            newString asFilename pathName = canonContents ifTrue:[
                self flash.
            ].

            self contents:newString addSeparatorToDirectories:isMultiMatch not.
            self cursorToEndOfLine.
        ].

    "Modified: 7.9.1995 / 10:20:46 / claus"
    "Modified: 7.9.1997 / 23:51:47 / cg"
!

mapped
    "move the cursor to the end - thats the most interesting part of
     a filename
    "
    super mapped.
"/    self cursorToEndOfLine.
    self makeCursorVisible.
! !

!FilenameEditField class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/FilenameEditField.st,v 1.32 2004-06-09 15:19:52 cg Exp $'
! !