FileSelectionList.st
changeset 38 4b9b70b2cc87
parent 24 966098a893f8
child 45 e900c30938c8
equal deleted inserted replaced
37:8dd71a2e79cd 38:4b9b70b2cc87
    11 "
    11 "
    12 
    12 
    13 SelectionInListView subclass:#FileSelectionList
    13 SelectionInListView subclass:#FileSelectionList
    14        instanceVariableNames:'pattern directory timeStamp directoryId
    14        instanceVariableNames:'pattern directory timeStamp directoryId
    15                               directoryContents directoryFileTypes
    15                               directoryContents directoryFileTypes
    16                               realAction fileTypes'
    16                               fileTypes realAction matchBlock'
    17        classVariableNames:''
    17        classVariableNames:''
    18        poolDictionaries:''
    18        poolDictionaries:''
    19        category:'Views-Text'
    19        category:'Views-Text'
    20 !
    20 !
    21 
    21 
    22 FileSelectionList comment:'
    22 FileSelectionList comment:'
    23 
       
    24 COPYRIGHT (c) 1993 by Claus Gittinger
    23 COPYRIGHT (c) 1993 by Claus Gittinger
    25               All Rights Reserved
    24               All Rights Reserved
    26 
    25 
    27 $Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.3 1994-01-13 00:15:25 claus Exp $
    26 $Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.4 1994-08-07 13:21:47 claus Exp $
    28 written Dec 93 by claus
       
    29 '!
    27 '!
    30 
    28 
    31 !FileSelectionList class methodsFor:'documentation'!
    29 !FileSelectionList class methodsFor:'documentation'!
    32 
    30 
       
    31 copyright
       
    32 "
       
    33  COPYRIGHT (c) 1993 by Claus Gittinger
       
    34               All Rights Reserved
       
    35 
       
    36  This software is furnished under a license and may be used
       
    37  only in accordance with the terms of that license and with the
       
    38  inclusion of the above copyright notice.   This software may not
       
    39  be provided or otherwise made available to, or used by, any
       
    40  other person.  No title to or ownership of the software is
       
    41  hereby transferred.
       
    42 "
       
    43 !
       
    44 
       
    45 version
       
    46 "
       
    47 $Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.4 1994-08-07 13:21:47 claus Exp $
       
    48 "
       
    49 !
       
    50 
    33 documentation
    51 documentation
    34 "
    52 "
    35 this class implements file selection lists - its basically a
    53     this class implements file selection lists - its basically a
    36 selection in list, but remembers the previous position when
    54     selection in list, but adds some right-arrows to directories.
    37 changing directories.
    55     (and will soon remember the previous position when changing directories).
    38 Only files matching a pattern (plus directories) are shown.
    56     Only files matching a pattern (plus directories) are shown.
    39 
    57 
    40 Instance variables:
    58     Instance variables:
    41         directoryContents       contents of current directory
    59             pattern                 the matchpattern
    42         directoryFileTypes      file types (symbols) of current directory
    60             directory               the current directory
    43         fileTypes               file types as shown in list 
    61             timeStamp               the time, when directoryContents was last taken
    44                                 (i.e only matching ones)
    62             directoryId             the directories id (inode-nr) when it was taken
       
    63             directoryContents       contents of current directory
       
    64             directoryFileTypes      file types (symbols) of current directory
       
    65             fileTypes               file types as shown in list 
       
    66                                     (i.e only matching ones)
       
    67             realAction              the action to perform when a file is selected
       
    68 
       
    69     Example use:
       
    70         FileSelectionLists are typically used in FileSelectionBoxes,
       
    71         or file-browser-like applications.
    45 "
    72 "
    46 ! !
    73 ! !
    47 
    74 
    48 !FileSelectionList methodsFor:'initialization'!
    75 !FileSelectionList methodsFor:'initialization'!
    49 
    76 
    55 
    82 
    56     "selections in list get forwarded to enterfield if not a directory;
    83     "selections in list get forwarded to enterfield if not a directory;
    57      otherwise directory is changed"
    84      otherwise directory is changed"
    58 
    85 
    59     actionBlock := [:lineNr |
    86     actionBlock := [:lineNr |
    60         |entry|
    87         |entry ok|
    61 
    88 
    62         (self selection isKindOf:Collection) ifFalse:[
    89         (self selection isKindOf:Collection) ifFalse:[
    63             entry := self selectionValue.
    90             entry := self selectionValue.
    64             (entry endsWith:' ...') ifTrue:[
    91             (entry endsWith:' ...') ifTrue:[
    65                 entry := entry copyTo:(entry size - 4).
    92                 entry := entry copyTo:(entry size - 4).
    66             ].
    93             ].
    67             ((directory typeOf:entry) == #directory) ifTrue:[
    94             ((directory typeOf:entry) == #directory) ifTrue:[
       
    95                 ok := false.
    68                 (directory isReadable:entry) ifFalse:[
    96                 (directory isReadable:entry) ifFalse:[
    69                     self warn:(resources string:'not allowed to read directory %1' with:entry)
    97                     self warn:(resources string:'not allowed to read directory %1' with:entry)
    70                 ] ifTrue:[
    98                 ] ifTrue:[
    71                     (directory isExecutable:entry) ifFalse:[
    99                     (directory isExecutable:entry) ifFalse:[
    72                         self warn:(resources string:'not allowed to change to directory %1' with:entry)
   100                         self warn:(resources string:'not allowed to change to directory %1' with:entry)
    73                     ] ifTrue:[
   101                     ] ifTrue:[
    74                         self directory:(directory pathName , Filename separator asString , entry)
   102                         self directory:(directory pathName , Filename separator asString , entry).
       
   103                         ok := true.
    75                     ]
   104                     ]
    76                 ].
   105                 ].
       
   106                 ok ifFalse:[
       
   107                     self deselect
       
   108                 ]
       
   109 
    77             ] ifFalse:[
   110             ] ifFalse:[
    78                 realAction notNil ifTrue:[
   111                 realAction notNil ifTrue:[
    79                     realAction value:lineNr
   112                     realAction value:lineNr
    80                 ]
   113                 ]
    81             ]
   114             ]
    82         ]
   115         ]
    83     ]
   116     ]
    84 
   117 
    85     "FileSelectionList new realize"
   118     "nontypical use ..."
       
   119     "
       
   120      FileSelectionList new open
       
   121      (FileSelectionList new directory:'/etc') open
       
   122      (ScrollableView for:FileSelectionList) open
       
   123      (HVScrollableView for:FileSelectionList) open
       
   124     "
    86 !
   125 !
    87 
   126 
    88 reinitialize
   127 reinitialize
    89     directory := FileDirectory currentDirectory.
   128     directory := FileDirectory currentDirectory.
    90     super reinitialize
   129     super reinitialize
   107 directory:nameOrDirectory
   146 directory:nameOrDirectory
   108     "set the lists contents to the filenames in the directory"
   147     "set the lists contents to the filenames in the directory"
   109 
   148 
   110     |oldPath name|
   149     |oldPath name|
   111 
   150 
   112     (nameOrDirectory isKindOf:String) ifTrue:[
   151     nameOrDirectory isString ifTrue:[
   113         name := nameOrDirectory
   152         name := nameOrDirectory
   114     ] ifFalse:[
   153     ] ifFalse:[
   115         name := nameOrDirectory pathName
   154         name := nameOrDirectory pathName
   116     ].
   155     ].
   117     oldPath := directory pathName.
   156     oldPath := directory pathName.
   128         pattern := aPattern.
   167         pattern := aPattern.
   129         realized ifTrue:[
   168         realized ifTrue:[
   130             self updateList
   169             self updateList
   131         ].
   170         ].
   132     ].
   171     ].
       
   172 !
       
   173 
       
   174 matchBlock:aBlock
       
   175     "set the matchBlock - if non-nil, it controls which
       
   176      names are shown in the list."
       
   177 
       
   178     matchBlock := aBlock
   133 ! !
   179 ! !
   134 
   180 
   135 !FileSelectionList methodsFor:'drawing'!
   181 !FileSelectionList methodsFor:'drawing'!
   136 
   182 
   137 redrawFromVisibleLine:startVisLineNr to:endVisLineNr
   183 redrawFromVisibleLine:startVisLineNr to:endVisLineNr
   193     |oldCursor files newList index|
   239     |oldCursor files newList index|
   194 
   240 
   195     oldCursor := cursor.
   241     oldCursor := cursor.
   196     self cursor:(Cursor read).
   242     self cursor:(Cursor read).
   197 
   243 
       
   244     "
       
   245      if the directory-id changed, MUST update.
       
   246      (can happen after a restart, when a file is no longer
       
   247       there, has moved or is NFS-mounted differently)
       
   248     "
   198     directoryId == directory id ifFalse:[
   249     directoryId == directory id ifFalse:[
   199         timeStamp := directory timeOfLastChange.
   250         timeStamp := directory timeOfLastChange.
   200         directoryId := directory id.
   251         directoryId := directory id.
   201         directoryContents := directory asText sort.
   252         directoryContents := directory asText sort.
   202         directoryFileTypes := OrderedCollection new.
   253         directoryFileTypes := OrderedCollection new.
   206     files := directoryContents.
   257     files := directoryContents.
   207     newList := OrderedCollection new.
   258     newList := OrderedCollection new.
   208     fileTypes := OrderedCollection new.
   259     fileTypes := OrderedCollection new.
   209     index := 1.
   260     index := 1.
   210     files do:[:name |
   261     files do:[:name |
   211         (directoryFileTypes at:index) == #directory ifTrue:[
   262         (matchBlock isNil or:[matchBlock value:name]) ifTrue:[
   212             name = '..' ifTrue:[
   263             (directoryFileTypes at:index) == #directory ifTrue:[
   213                 newList add:name.
   264                 name = '..' ifTrue:[
   214                 fileTypes add:(directoryFileTypes at:index)
   265                     newList add:name.
       
   266                     fileTypes add:(directoryFileTypes at:index)
       
   267                 ] ifFalse:[
       
   268                     name = '.' ifTrue:[
       
   269                         "ignore"
       
   270                     ] ifFalse:[
       
   271                         newList add:(name ", ' ...'").
       
   272                         fileTypes add:(directoryFileTypes at:index)
       
   273                     ]
       
   274                 ]
   215             ] ifFalse:[
   275             ] ifFalse:[
   216                 name = '.' ifTrue:[
   276                 (pattern isNil or:[pattern isEmpty or:[pattern = '*' or:[pattern match:name]]]) ifTrue:[
   217                     "ignore"
   277                     newList add:name.
   218                 ] ifFalse:[
       
   219                     newList add:(name ", ' ...'").
       
   220                     fileTypes add:(directoryFileTypes at:index)
   278                     fileTypes add:(directoryFileTypes at:index)
   221                 ]
   279                 ]
   222             ]
   280             ].
   223         ] ifFalse:[
       
   224             (pattern isEmpty or:[pattern = '*' or:[pattern match:name]]) ifTrue:[
       
   225                 newList add:name.
       
   226                 fileTypes add:(directoryFileTypes at:index)
       
   227             ]
       
   228         ].
   281         ].
   229         index := index + 1
   282         index := index + 1
   230     ].
   283     ].
   231     super list:newList.
   284     super list:newList.
   232     self cursor:oldCursor.
   285     self cursor:oldCursor.