FileSelectionTree.st
changeset 487 d031f77ada55
child 488 bb017dc6df4f
equal deleted inserted replaced
486:d981d312947d 487:d031f77ada55
       
     1 "
       
     2  COPYRIGHT (c) 1997 by eXept Software AG / Claus Gittinger
       
     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 
       
    14 
       
    15 SelectionInTreeView subclass:#FileSelectionTree
       
    16 	instanceVariableNames:'fileIcons itemClass'
       
    17 	classVariableNames:''
       
    18 	poolDictionaries:''
       
    19 	category:'Views-Text'
       
    20 !
       
    21 
       
    22 !FileSelectionTree class methodsFor:'documentation'!
       
    23 
       
    24 copyright
       
    25 "
       
    26  COPYRIGHT (c) 1997 by eXept Software AG / Claus Gittinger
       
    27               All Rights Reserved
       
    28 
       
    29  This software is furnished under a license and may be used
       
    30  only in accordance with the terms of that license and with the
       
    31  inclusion of the above copyright notice.   This software may not
       
    32  be provided or otherwise made available to, or used by, any
       
    33  other person.  No title to or ownership of the software is
       
    34  hereby transferred.
       
    35 "
       
    36 
       
    37 
       
    38 !
       
    39 
       
    40 documentation
       
    41 "
       
    42     somewhat like a FileSelectionInList; but specialized for hierarchical (i.e. tree-like)
       
    43     lists and adds the functions to show/hide subtrees. 
       
    44     Requires SelectionInTree as model and FileSelectionItem (or compatible) list entries.
       
    45 
       
    46     You can define your own TreeItem instead of useing the default class FileSelectionItem
       
    47     by seting the class through to #itemClass:
       
    48 
       
    49     [see also:]
       
    50         FileSelectionItem
       
    51         SelectionInTree
       
    52         SelectionInTreeView
       
    53         SelectionInListView
       
    54 
       
    55     [author:]
       
    56         Claus Atzkern
       
    57 "
       
    58 
       
    59 
       
    60 
       
    61 !
       
    62 
       
    63 examples
       
    64 "
       
    65     open a FileSelectionTree on current directory
       
    66                                                                         [exBegin]
       
    67     |top scr|
       
    68 
       
    69     top := StandardSystemView new label:'select'; extent:300@500.
       
    70     scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
       
    71     scr := scr scrolledView.
       
    72 
       
    73     scr directory:Filename currentDirectory.
       
    74 
       
    75     scr action:[:anIndex| Transcript showCR:anIndex.
       
    76                           Transcript showCR:scr selectedPathname.
       
    77                ].
       
    78     top open
       
    79                                                                         [exEnd]
       
    80 
       
    81 
       
    82 
       
    83     open a FileSelectionTree on root path; show directory
       
    84     indication (open/closed) and no lines
       
    85                                                                         [exBegin]
       
    86     |top scr|
       
    87 
       
    88     top := StandardSystemView new label:'select'; extent:300@500.
       
    89     scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
       
    90     scr := scr scrolledView.
       
    91     scr showDirectoryIndicator:true.
       
    92     scr showLines:false.
       
    93 
       
    94     scr directory:'/'.
       
    95 
       
    96     scr action:[:anIndex| Transcript showCR:anIndex.
       
    97                           Transcript showCR:scr selectedPathname.
       
    98                ].
       
    99     top open
       
   100                                                                         [exEnd]
       
   101 
       
   102 
       
   103 
       
   104     open a FileSelectionTree on root path; showing only files
       
   105                                                                         [exBegin]
       
   106     |top scr|
       
   107 
       
   108     top := StandardSystemView new label:'select'; extent:300@500.
       
   109     scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
       
   110     scr := scr scrolledView.
       
   111     scr itemClass:(FileSelectionItem directoriesOnly).
       
   112     scr directory:'/'.
       
   113 
       
   114     scr action:[:anIndex| Transcript showCR:anIndex.
       
   115                           Transcript showCR:scr selectedPathname.
       
   116                ].
       
   117     top open
       
   118                                                                         [exEnd]
       
   119 
       
   120 
       
   121 
       
   122     open a FileSelectionTree on a specified path
       
   123                                                                         [exBegin]
       
   124     |top scr|
       
   125 
       
   126     top := StandardSystemView new label:'select'; extent:300@500.
       
   127     scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
       
   128     scr := scr scrolledView.
       
   129     scr directory:'/'.
       
   130     scr showLines:false.
       
   131     scr action:[:anIndex| Transcript showCR:scr selectedPathname ].
       
   132     scr doubleClickAction:[:anIndex| Transcript showCR:scr selectedPathname ].
       
   133     top open
       
   134                                                                         [exEnd]
       
   135 
       
   136 
       
   137 
       
   138     open a FileSelectionTree on a specified path; show or hide
       
   139     lines on doubleClick on a file (not a directory)
       
   140                                                                         [exBegin]
       
   141     |top scr|
       
   142 
       
   143     top := StandardSystemView new label:'select'; extent:300@500.
       
   144     scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
       
   145     scr := scr scrolledView.
       
   146     scr directory:'/'.
       
   147 
       
   148     scr action:[:anIndex| Transcript showCR:anIndex.
       
   149                           Transcript showCR:scr selectedPathname.
       
   150                ].
       
   151     scr doubleClickAction:[:anIndex|
       
   152         |fn|
       
   153         fn := scr selectedPathname asFilename.
       
   154         fn isDirectory ifFalse:[
       
   155             scr showLines:(scr showLines not)
       
   156         ]
       
   157     ].
       
   158     top open
       
   159                                                                         [exEnd]
       
   160 
       
   161 
       
   162 
       
   163     a more complex example
       
   164                                                                         [exBegin]
       
   165     |action top tree slvw file tgD tgF button hzPn|
       
   166 
       
   167     top  := StandardSystemView new label:'select'; extent:600@600.
       
   168     tree := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(0.5 @ 1.0) in:top.
       
   169     slvw := HVScrollableView for:FileSelectionTree origin:(0.5 @  30) corner:(1.0 @ 1.0) in:top.
       
   170     hzPn := HorizontalPanelView origin:(0.5 @  0.0) corner:(1.0 @ 30) in:top.
       
   171     tree := tree scrolledView.
       
   172     slvw := slvw scrolledView.
       
   173 
       
   174     tree itemClass:(FileSelectionItem directoriesOnly).
       
   175     tree directory:(Filename homeDirectory).
       
   176 
       
   177     slvw doubleClickSelectionBlock:[:anIndex|
       
   178         (file := slvw selectedPathname asFilename) isDirectory ifTrue:[
       
   179             anIndex == 1 ifTrue:[file := file directory].
       
   180             tree selectPathname:file
       
   181         ].
       
   182         false
       
   183     ].
       
   184     slvw showLines:false.
       
   185 
       
   186     tree action:[:anIndex|
       
   187         slvw directory:(tree selectedPathname)
       
   188     ].
       
   189     action := [:tg||cls|
       
   190         tgF isOn ifTrue:[
       
   191             tgD isOn ifTrue:[cls := FileSelectionItem]
       
   192                     ifFalse:[cls := FileSelectionItem filesOnly]
       
   193         ] ifFalse:[
       
   194             tgD isOn ifTrue:[
       
   195                 cls := FileSelectionItem directoriesOnly
       
   196             ] ifFalse:[
       
   197                 tg turnOn.
       
   198                 tg == tgD ifTrue:[cls := FileSelectionItem directoriesOnly]
       
   199                          ifFalse:[cls := FileSelectionItem filesOnly]
       
   200             ]
       
   201         ].
       
   202         slvw itemClass:cls.
       
   203     ].
       
   204     hzPn verticalLayout:#fitSpace.            
       
   205     hzPn horizontalLayout:#fitSpace.            
       
   206     tgD    := CheckBox label:'directory'    in:hzPn.
       
   207     tgF    := CheckBox label:'file'         in:hzPn.
       
   208     button := Button   label:'File Browser' in:hzPn.
       
   209 
       
   210     button action:[
       
   211         (file := slvw selectedPathname) isNil ifTrue:[
       
   212             file := tree selectedPathname
       
   213         ].
       
   214         file notNil ifTrue:[
       
   215             file asFilename isDirectory ifTrue:[FileBrowser openOn:file]
       
   216                                        ifFalse:[FileBrowser openOnFileNamed:file]
       
   217         ]
       
   218     ].
       
   219 
       
   220     tgD turnOn.
       
   221     tgF turnOn.
       
   222     tgD action:[:v| action value:tgF].
       
   223     tgF action:[:v| action value:tgD].
       
   224 
       
   225     top open
       
   226                                                                         [exEnd]
       
   227 "
       
   228 
       
   229 ! !
       
   230 
       
   231 !FileSelectionTree methodsFor:'accessing'!
       
   232 
       
   233 directory
       
   234     "get the full pathname of the root directory
       
   235     "
       
   236     |root|
       
   237 
       
   238     (root := model root) notNil ifTrue:[
       
   239         ^ root pathName
       
   240     ].
       
   241   ^ nil
       
   242 !
       
   243 
       
   244 directory:aPathname
       
   245     "change the root directory of the selection tree to the full pathName,
       
   246      aPathname. The pathname must be a directory otherwise the root directory
       
   247      is set to nil.
       
   248     "
       
   249     |root|
       
   250 
       
   251     self selection:nil.
       
   252 
       
   253     (aPathname notNil and:[aPathname asFilename isDirectory]) ifFalse:[
       
   254         model root:nil.
       
   255         ^ self
       
   256     ].
       
   257 
       
   258     root := self itemClass pathName:aPathname.
       
   259     model root:root.
       
   260     model expand:root.
       
   261 !
       
   262 
       
   263 itemClass
       
   264     "returns current itemClass used
       
   265     "
       
   266     ^ itemClass
       
   267 !
       
   268 
       
   269 itemClass:anItemClass
       
   270     "set itemClass to be used
       
   271     "
       
   272     |directory|
       
   273 
       
   274     (anItemClass notNil and:[anItemClass ~~ itemClass]) ifTrue:[
       
   275         itemClass := anItemClass.
       
   276         self refetchDeviceResources.
       
   277 
       
   278         (directory := self directory) notNil ifTrue:[
       
   279             self directory:directory
       
   280         ].
       
   281     ].
       
   282 
       
   283 ! !
       
   284 
       
   285 !FileSelectionTree methodsFor:'drawing basics'!
       
   286 
       
   287 figureFor:aNode
       
   288     "get the image used for the node entry.
       
   289     "
       
   290     |keyOrImage|
       
   291 
       
   292     aNode == self selectedNode ifFalse:[keyOrImage := aNode imageUnselected]
       
   293                                 ifTrue:[keyOrImage := aNode imageSelected].
       
   294 
       
   295     keyOrImage isSymbol ifTrue:[^ fileIcons at:keyOrImage].
       
   296   ^ keyOrImage
       
   297 ! !
       
   298 
       
   299 !FileSelectionTree methodsFor:'initialization'!
       
   300 
       
   301 fetchImageResources
       
   302     "initialize heavily used device resources - to avoid rendering
       
   303      images again and again later; returns maximum extent of the images used.
       
   304     "
       
   305     |y x t|
       
   306 
       
   307     fileIcons := self itemClass iconsOn:device.
       
   308 
       
   309     t := super fetchImageResources.
       
   310     y := t y.
       
   311     x := t x.
       
   312 
       
   313     fileIcons do:[:anIcon|
       
   314         (t := anIcon heightOn:self) > y ifTrue:[y := t].
       
   315         (t := anIcon widthOn:self)  > x ifTrue:[x := t].
       
   316     ].
       
   317   ^ x @ y
       
   318 
       
   319 !
       
   320 
       
   321 initialize
       
   322     "setup my model and set the default path to the current directory; on default
       
   323      multiple selection is disabled.
       
   324     "
       
   325     super initialize.
       
   326     itemClass := FileSelectionItem.
       
   327     self model:(SelectionInTree new).
       
   328 
       
   329 ! !
       
   330 
       
   331 !FileSelectionTree methodsFor:'queries'!
       
   332 
       
   333 canSelect:aFilename
       
   334     "returns true if a file is selectable dependant on
       
   335      filter ...
       
   336     "
       
   337     |root|
       
   338 
       
   339     (itemClass isSelectableFile:aFilename) ifTrue:[
       
   340         (root := self directory) notNil ifTrue:[
       
   341             ^ aFilename asString startsWith:root
       
   342         ]
       
   343     ].
       
   344   ^ false
       
   345 ! !
       
   346 
       
   347 !FileSelectionTree methodsFor:'selection'!
       
   348 
       
   349 selectPathname:aPath
       
   350     "set selection to a path
       
   351     "
       
   352     |node comp rdwNd path|
       
   353 
       
   354     (self canSelect:aPath) ifFalse:[
       
   355         ^ self selection:nil
       
   356     ].
       
   357     ((path := aPath asString) = self selectedPathname) ifTrue:[
       
   358         "/ already selected
       
   359         ^ self
       
   360     ].
       
   361 
       
   362     comp := Filename components:(path copyFrom:(self directory size + 1)).
       
   363     node := model root.
       
   364 
       
   365     comp do:[:el||next|
       
   366         next := node detectChild:[:e|e name = el].
       
   367 
       
   368         next notNil ifTrue:[
       
   369             node hidden ifTrue:[
       
   370                 rdwNd isNil ifTrue:[
       
   371                     rdwNd := node.
       
   372                     self selectNode:node.
       
   373                 ].
       
   374                 node expand
       
   375             ].
       
   376             node := next
       
   377         ]
       
   378     ].
       
   379 
       
   380     rdwNd notNil ifTrue:[
       
   381         model updateList.
       
   382         list := self listFromModel.
       
   383         self redrawFromLine:(self indexOfNode:rdwNd).
       
   384         self contentsChanged.
       
   385     ].
       
   386     self selectNode:node.
       
   387 !
       
   388 
       
   389 selectedPathname
       
   390     "if there is a single selection, the full pathname of the selected
       
   391      entry will be returned otherwise nil
       
   392     "
       
   393     |node|
       
   394 
       
   395     (node := self selectedNode) notNil ifTrue:[
       
   396         ^ node pathName
       
   397     ].
       
   398   ^ nil
       
   399 
       
   400 ! !
       
   401 
       
   402 !FileSelectionTree class methodsFor:'documentation'!
       
   403 
       
   404 version
       
   405     ^ '$Header: /cvs/stx/stx/libwidg2/FileSelectionTree.st,v 1.1 1997-08-07 13:15:21 ca Exp $'
       
   406 ! !