FileSelectionBox.st
author Claus Gittinger <cg@exept.de>
Thu, 29 Feb 1996 23:04:54 +0100
changeset 438 8df6f72d1569
parent 351 24a527f86c7b
child 548 c418699a90c0
permissions -rw-r--r--
removed wierd example

"
 COPYRIGHT (c) 1990 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 selectingDirectory'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-DialogBoxes'
!

!FileSelectionBox class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1990 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
"
    this class implements file selection boxes. Instances show a list of
    files, and perform an action block with the selected pathname as
    argument when ok is clicked. It is also possible, to open the box
    without action and ask it afterward if it has been left with ok
    (i.e. the ST-80 way, asking 'aBox accepted ifTrue:[...]')

    There is an optional PatternField, which shows itself when a pattern
    is defined (i.e. if there is no pattern, it is hidden). 
    If there is a pattern, only files matching the pattern are shown in 
    the list. Directories are not affected by the patternField.

    In addition, there is an optional matchBlock (actually this is defined 
    in the FileSelectionList). Only names for which this matchblock
    returns true will be presented. The matchBlock affects both regular files
    and names of directories. The argument passed to the matchBlock is the full
    pathname.

    All of the actual work is done in the fileList; see the documentation
    of FileSelectionList for more options 
    (you can access a boxes fileList via 'aBox>>listView' and get access to all
     of those fancy settings)
    For example, by accessing the list, it is possible to hide all directories 
    ('aBox listView ignoreDirectories:true'), to hide the parentDirectory alone 
    ('aBox listView ignoreParentDirectory') and to turn off the marking 
    of subdirectories ('aBox listView markSubdirectories:false').
"
!

examples 
"
  simple standard queries

    very simple:
        |name|

        name := FileSelectionBox requestFilename.
        Transcript showCr:name


    simple:
        |name|

        name := FileSelectionBox requestFilename:'which file ?'.
        Transcript showCr:name


    with initial selection:
        |name|

        name := FileSelectionBox requestFilename:'which file ?' default:'Make.proto'.
        Transcript showCr:name



  more detailed setup

    setting title:

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box open.
        box accepted ifTrue:[
            Transcript showCr:'you selected: ' , box
        ]

    setting a matchpattern:

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box pattern:'*.rc'.
        box open

    setting multiple patterns:

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box pattern:'*.rc;*.st'.
        box open

    setting a matchblock:

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box directory:'/etc'.
        box pattern:'*'.
        box matchBlock:[:name | name asFilename baseName first between:$a and:$z].
        box open

    both pattern and matchBlock:

        |box|
        box := FileSelectionBox new.
        box title:'Which directory ?'.
        box selectingDirectory:true.
        box pattern:'l*'.
        box matchBlock:[:name | OperatingSystem isDirectory:name].
        box action:[:fn | Transcript showCr:fn].
        box open

    dont show the parent directory:

        |box|
        box := FileSelectionBox new.
        box title:'Which directory ?'.
        box listView ignoreParentDirectory:true.
        box open

    dont show any directory:

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box listView ignoreDirectories:true.
        box open

    dont show any directory or hidden file:
    (notice the basename extraction - we are not interrested in the full pathName)

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box listView ignoreDirectories:true.
        box matchBlock:[:name | (name asFilename baseName startsWith:'.') not].
        box open

    dont allow direct filename entry:
    (i.e. avoid the user choosing files from other directories)

        |box|
        box := FileSelectionBox new.
        box title:'Which one ?'.
        box enterField beInvisible.
        box open.
        box accepted ifTrue:[
            Transcript showCr:'path is ' , box pathName
        ].

    combined with above directory ignoring,
    this limits selection of files from a single directory:

        |box|
        box := FileSelectionBox new.
        box title:'Which file ?'.
        box enterField beInvisible.
        box listView ignoreDirectories:true.
        box open.
        box accepted ifTrue:[
            Transcript showCr:'path is ' , box pathName
        ].

    finally, an action:

        |box|
        box := FileSelectionBox new.
        box title:'Which directory ?'.
        box pattern:'l*'.
        box matchBlock:[:name | OperatingSystem isDirectory:name].
        box action:[:name | Transcript showCr:name].
        box open

  concrete examples:

    only show files beginning with lowercase characters
    and ending in '.c':

        |box|
        box := FileSelectionBox new.
        box title:'Which directory ?'.
        box matchBlock:[:name |
                            box pathName asFilename isDirectory
                            or:[name first isLowercase
                                and:[name endsWith:'.c']]
                       ].
        box open.
        box accepted ifTrue:[
            Transcript showCr:'full path:  ' , box pathName.
            Transcript showCr:'files name: ' , box fileName.
            Transcript showCr:'directory : ' , box directory.
        ]
"
! !

!FileSelectionBox class methodsFor:'defaults'!

requestFilename
    ^ self requestFilename:'filename:'

    "
     FileSelectionBox requestFilename
    "
!

requestFilename:title
    |fileBox|

    fileBox := self
		    title:title
		    okText:'ok'
		    abortText:'cancel'
		    action:[:fileName | ^ fileName].

    fileBox showAtPointer.
    ^ nil

    "
     FileSelectionBox requestFilename:'which file ?'
    "
!

requestFilename:title default:aFileName
    |fileBox|

    fileBox := self
		    title:title
		    okText:'ok'
		    abortText:'cancel'
		    action:[:fileName | ^ fileName].

    fileBox initialText:aFileName.
    fileBox showAtPointer.
    ^ nil

    "
     FileSelectionBox requestFilename:'which file ?' default:'Makefile'
    "
!

requestFilename:title  fromDirectory: aDirectory

    |fileBox|

    fileBox := self
		    title:title
		    okText:'ok'
		    abortText:'cancel'
		    action:[:fileName | ^ fileName].

    fileBox directory: aDirectory.
    fileBox showAtPointer.
    ^ nil

    "
     FileSelectionBox requestFilename:'which file ?' fromDirectory:'/etc'
    "

    "Modified: 12.10.1995 / 11:47:27 / markus"
! !

!FileSelectionBox class methodsFor:'requests'!

listViewType
    "return the type of listView - using a FileSelectionList here"

    ^ FileSelectionList
! !

!FileSelectionBox methodsFor:'accessing'!

contents
    "return the current entered value (i.e. the enterFields string).
     redefined to return the full pathname."

    |string sep|

    string := super contents.
    string isNil ifTrue:[
	^ selectionList directory pathName
    ].
    sep := Filename separator.
    (string startsWith:sep) ifTrue:[
	^ string
    ].
    ^ (selectionList directory pathName asFilename construct:string) asString
!

directory
    "return the directory which is currently shown"

    ^ selectionList directory
!

directory:directoryName
    "change the directory shown in the list."

    selectionList directory:directoryName
!

fileName
    "if some filename has been entered, return it (without the directory path)
     otherwise, return nil"

    |string|

    string := super contents.
    string isNil ifTrue:[^ nil].
    ^ self pathName
!

matchBlock:aBlock
    "set the matchBlock (in the selectionList). Only files
     for which the block returns true are shown.
     The matching is actually done in the fileSelectionList."

    selectionList matchBlock:aBlock 
!

openOn:aPath
    "open the box showing files in aPath.
     This is only a shortcut message - no new functionality."

    self directory:aPath.
    self showAtPointer
!

pathName
    "same as contents - return the full pathname of the selected file,
     or the pathname of the directory if nothing has been entered"

    ^ self contents
!

pattern:aPattern
    "set the pattern - this also enables the PatternField
     (if the pattern is non-nil) or hides it (if nil)."

    |hidePatternField focusSequence|

    patternField initialText:aPattern.
    selectionList pattern:aPattern.
    aPattern isNil ifTrue:[
	hidePatternField := true.
	realized ifTrue:[
	    patternField hide.
	].
	focusSequence := (Array 
			     with:enterField 
			     with:selectionList 
			     with:okButton 
			     with:abortButton)
    ] ifFalse:[
	hidePatternField := false.
	realized ifTrue:[
	    patternField realize.
	].
	focusSequence := (Array 
			     with:patternField 
			     with:enterField 
			     with:selectionList 
			     with:okButton 
			     with:abortButton)
    ].

    patternField hiddenOnRealize:hidePatternField.
    windowGroup notNil ifTrue:[
	windowGroup focusSequence:focusSequence
    ].
!

selectingDirectory:aBoolean
    selectingDirectory := aBoolean
! !

!FileSelectionBox methodsFor:'change & update'!

update:something with:argument from:changedObject
    |commonName index s|

    something == #directory ifTrue:[
	"
	 sent by fileNameEnterField, if a filename
	 completion was not possible due to multiple
	 matches.
	"
	selectionList directory:argument.
	s := enterField contents.
	s notNil ifTrue:[
	    commonName := s asFilename baseName.
	    commonName size > 0 ifTrue:[
		index := selectionList list findFirst:[:entry | entry startsWith:commonName].
		index ~~ 0 ifTrue:[
		    selectionList makeLineVisible:index
		]
	    ]
	]
    ]
! !

!FileSelectionBox methodsFor:'initialization'!

createEnterField
    "if the (optional) class FilenameEditField is present, use
     it, since it provides filename completion. Otherwise, we have
     to live with the dump (default) field ...
    "
    FilenameEditField notNil ifTrue:[
	^ FilenameEditField new.
    ].
    ^ super createEnterField
!

focusSequence
    patternField shown ifTrue:[
	^ Array 
	    with:patternField 
	    with:enterField 
	    with:selectionList 
	    with:abortButton
	    with:okButton 
    ].
    ^ super focusSequence
!

initialize
    |corner|

    super initialize.
    selectingDirectory := false.

    label := resources string:'File dialog'.

    labelField extent:(0.7 @ labelField height).
    labelField label:(resources string:'select a file:').
    labelField adjust:#left.

    patternField := EditField in:self.
    self is3D ifTrue:[
        corner := (1.0 @ (labelField origin y+patternField heightIncludingBorder)).
    ] ifFalse:[
        corner := [(width - ViewSpacing - (patternField borderWidth * 2)) @ (labelField origin y+patternField height"IncludingBorder")].
    ].
    patternField origin:(0.7 @ labelField origin y) corner:corner.
    patternField rightInset:ViewSpacing.
    patternField initialText:'*'.
    patternField leaveAction:[:reason | 
        selectionList pattern:patternField contents. 
        self updateList
    ].
    patternField hiddenOnRealize:true. "delay showing, until a pattern is defined"

    enterField addDependent:self.

    "
     FileSelectionBox open
     FileSelectionBox new show
    "
!

realize
    "if some default is present in the enterField,
     scroll to make this one visible"

    |contents|

    super realize.
    (contents := enterField contents) notNil ifTrue:[
	contents notEmpty ifTrue:[
	    selectionList makeVisible:contents
	]
    ]
! !

!FileSelectionBox methodsFor:'private'!

updateList
    selectionList updateList
! !

!FileSelectionBox methodsFor:'queries'!

preferredExtent
    "return my preferred extent - thats the minimum size 
     to make everything visible"

    |wWanted hWanted|

    wWanted := ViewSpacing + 
	       labelField preferredExtent x + 
	       (ViewSpacing * 2) + 
	       patternField preferredExtent x + 
	       ViewSpacing.
    (wWanted < width) ifTrue:[
	wWanted := width
    ].
    hWanted := ViewSpacing + labelField height +
	       ViewSpacing + enterField height +
	       ViewSpacing + selectionList height +
	       ViewSpacing + buttonPanel preferredExtent y +
	       ViewSpacing.

    (hWanted < height) ifTrue:[
	hWanted := height
    ].
    ^ (wWanted @ hWanted)
! !

!FileSelectionBox methodsFor:'user actions'!

doubleClick
    |entry|

    entry := selectionList selectionValue.
    entry notNil ifTrue:[
	((selectionList directory typeOf:entry) == #directory) ifFalse:[
	    enterField contents:entry.
	    self okPressed
	]
    ].
!

okPressed
    "called for both on ok-press and on return-key"

    |dir string fname|

    string := enterField contents.
    (string notNil and:[string notEmpty]) ifTrue:[
        string := string withoutSeparators.
        string asFilename isAbsolute ifTrue:[
            fname := string asFilename
        ] ifFalse:[
            dir := selectionList directory pathName asFilename.
            fname := dir construct:string
        ].
        fname isDirectory ifTrue:[
            selectingDirectory ifFalse:[
                selectionList directory:fname asString.
                self updateList.
                ^ self
            ]    
        ]
    ] ifFalse:[
        selectingDirectory ifTrue:[
            enterField contents:(selectionList directory pathName).
        ].
    ].

    super okPressed

!

selectionChanged
    |entry|

    entry := selectionList selectionValue.
    enterField contents:entry
! !

!FileSelectionBox class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg/FileSelectionBox.st,v 1.29 1996-02-29 22:04:54 cg Exp $'
! !