move functionality to base class
authorca
Thu, 04 Sep 1997 14:15:45 +0200
changeset 513 fe060a9ba204
parent 512 97beb6e8acbe
child 514 c25e078ed84c
move functionality to base class
FileSelectionTree.st
--- a/FileSelectionTree.st	Tue Sep 02 19:47:21 1997 +0200
+++ b/FileSelectionTree.st	Thu Sep 04 14:15:45 1997 +0200
@@ -79,6 +79,43 @@
                                                                         [exEnd]
 
 
+    open a FileSelectionTree useing a model
+                                                                        [exBegin]
+    |top scr model|
+
+    top := StandardSystemView new label:'select'; extent:300@500.
+    scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
+    scr := scr scrolledView.
+    model := (Filename currentDirectory asString) asValue.
+    scr rootHolder:model.
+    model inspect.
+
+    scr action:[:anIndex| Transcript showCR:anIndex.
+                          Transcript showCR:scr selectedPathname.
+               ].
+    top open
+                                                                        [exEnd]
+
+
+                                                                        [exBegin]
+    |top scr|
+
+    top := StandardSystemView new label:'select'; extent:300@500.
+    scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
+    scr := scr scrolledView.
+
+    scr directory:Filename currentDirectory.
+    scr selectionHolder:nil asValue.
+    scr selectionHolder inspect.
+    scr multipleSelectOk:true.
+
+    scr action:[:anIndex| Transcript showCR:anIndex.
+                          Transcript showCR:scr selectedPathname.
+               ].
+    top open
+                                                                        [exEnd]
+
+
 
     open a FileSelectionTree on root path; show directory
     indication (open/closed), no lines and not the root
@@ -248,6 +285,24 @@
                                                                         [exEnd]
 "
 
+!
+
+test
+    |top scr|
+
+    top := StandardSystemView new label:'select'; extent:300@500.
+    scr := HVScrollableView for:FileSelectionTree origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.
+    scr := scr scrolledView.
+
+    scr directory:Filename currentDirectory.
+    scr selectionHolder:nil asValue.
+    scr selectionHolder inspect.
+    scr multipleSelectOk:true.
+
+    scr action:[:anIndex| Transcript showCR:anIndex.
+                          Transcript showCR:scr selectedPathname.
+               ].
+    top open
 ! !
 
 !FileSelectionTree methodsFor:'accessing'!
@@ -410,8 +465,147 @@
     "
     super initialize.
     itemClass := FileSelectionItem.
-    self model:(SelectionInTree new).
+
+! !
+
+!FileSelectionTree methodsFor:'model'!
+
+rootFromModel
+    "update hierarchical list from root model
+    "
+    |oldPath newPath|
+
+    (newPath := rootHolder value) notNil ifTrue:[
+        newPath := newPath asString.
+    ].
+
+    self directory = newPath ifFalse:[
+        self changeDirectory:newPath
+    ]
+!
+
+selectionFromModel
+    "set the selection derived from the selectionHolder
+    "
+    |selection value shown|
+
+    selectionHolder isNil ifTrue:[
+        ^ self
+    ].
+
+    value := selectionHolder value.
+
+    multipleSelectOk ifFalse:[
+        value isNil ifTrue:[
+            self deselect
+        ] ifFalse:[
+            value asFilename exists ifFalse:[
+                selectionHolder value:nil
+            ] ifTrue:[
+                self selectPathname:value
+            ]
+        ].
+        ^ self
+    ].
+
+    value size == 0 ifTrue:[
+        ^ self deselect
+    ].
+
+    selection := value select:[:aPath| aPath asFilename exists ].
+
+    selection size ~~ value size ifTrue:[
+        selection size ~~ 0 ifTrue:[selectionHolder value:selection]
+                     ifFalse:[selectionHolder value:nil].
+        ^ self
+    ].
+
+    selection size == 1 ifTrue:[
+        ^ self selectPathname:(selection first)
+    ].
+    shown := true.
+
+    selection do:[:el|(self showFile:el) ifFalse:[shown := false]].
+
+    shown ifFalse:[
+        model recomputeList.
+    ].
+
+    selection := selection collect:[:el|
+        listOfNodes findFirst:[:n| n pathName = el]
+    ].
+    super selection:selection.
 
+!
+
+selectionToModel
+    "write selection to selection holder
+    "
+    |old new|
+
+    old := selectionHolder value.
+
+    multipleSelectOk ifFalse:[
+        (new := self pathnameAtIndex:selection) = old ifFalse:[
+            selectionHolder value:new
+        ]
+    ] ifTrue:[
+        self numberOfSelections == 0 ifTrue:[
+            old size ~~ 0 ifTrue:[
+                selectionHolder value:nil
+            ]
+        ] ifFalse:[
+            new := selection collect:[:i|(listOfNodes at:i) pathName].
+
+            new size ~~ old size ifTrue:[
+                selectionHolder value:new
+            ] ifFalse:[
+                new do:[:pN|
+                    (old findFirst:[:pO| pO = pN]) == 0 ifTrue:[
+                        ^ selectionHolder value:new
+                    ]
+                ]
+            ]
+        ]
+    ]
+
+! !
+
+!FileSelectionTree methodsFor:'private'!
+
+showFile:aPathname
+    "show a file
+    "
+    |components path root node shown|
+
+    path  := aPathname asString.
+    root  := self directory.
+    shown := true.
+
+    (path startsWith:root) ifFalse:[
+        self error.
+        ^ shown
+    ].
+
+    path size <= (root size + 2) ifTrue:[^ shown].
+    path := path copyFrom:(root size + 2).
+
+    components := Filename components:path.
+    node := model root.
+
+    components do:[:el||next|
+        next := node detectChild:[:e|e name = el].
+        next isNil ifTrue:[
+            self error.
+            ^ shown
+        ].
+        node hidden ifTrue:[
+            node expand.
+            shown := false.
+        ].
+        node := next.
+    ].
+    ^ shown
 ! !
 
 !FileSelectionTree methodsFor:'selection'!
@@ -446,5 +640,5 @@
 !FileSelectionTree class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/FileSelectionTree.st,v 1.4 1997-08-15 09:25:43 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/FileSelectionTree.st,v 1.5 1997-09-04 12:15:45 ca Exp $'
 ! !