SelectionInTree.st
changeset 458 e4b907c8f0ce
child 482 7f922ead2b06
equal deleted inserted replaced
457:c0f108c6d05f 458:e4b907c8f0ce
       
     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 Model subclass:#SelectionInTree
       
    16 	instanceVariableNames:'root list selection'
       
    17 	classVariableNames:''
       
    18 	poolDictionaries:''
       
    19 	category:'Interface-Support-Models'
       
    20 !
       
    21 
       
    22 !SelectionInTree 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     list and selection holder for hierarchical list structures. Used
       
    43     to buildup file-trees, class tress etc.
       
    44 
       
    45     Especially suited for use with SelectionInTreeView.
       
    46 
       
    47     [See also:]
       
    48         TreeItem
       
    49         SelectionInTreeView
       
    50 
       
    51     [Author:]
       
    52         W. Olberding
       
    53         Claus Atzkern
       
    54 "
       
    55 ! !
       
    56 
       
    57 !SelectionInTree methodsFor:'accessing'!
       
    58 
       
    59 list
       
    60     "get list oc currently shown objects
       
    61     "
       
    62     ^ list
       
    63 !
       
    64 
       
    65 root
       
    66     "get the root node
       
    67     "
       
    68     ^ root
       
    69 !
       
    70 
       
    71 root:aRootNode
       
    72     "set a new root and recompute list
       
    73     "
       
    74     root := aRootNode.
       
    75     self recomputeList.
       
    76 ! !
       
    77 
       
    78 !SelectionInTree methodsFor:'accessing hierarchy'!
       
    79 
       
    80 collapse:something 
       
    81     "collapse a node or collection of nodes
       
    82     "
       
    83     |invalidate|
       
    84 
       
    85     self each:something do:[:aNode|
       
    86         aNode isCollapsable ifTrue:[
       
    87             aNode collapse.
       
    88             invalidate := true
       
    89         ]
       
    90     ].
       
    91     invalidate == true ifTrue:[self recomputeList]
       
    92 !
       
    93 
       
    94 expand:something 
       
    95     "expand a node or collection of nodes
       
    96     "
       
    97     |invalidate|
       
    98 
       
    99     self each:something do:[:aNode|
       
   100         aNode isExpandable ifTrue:[
       
   101             aNode expand.
       
   102             invalidate := true
       
   103         ]
       
   104     ].
       
   105     invalidate == true ifTrue:[self recomputeList]
       
   106 ! !
       
   107 
       
   108 !SelectionInTree methodsFor:'adding & removing'!
       
   109 
       
   110 add:something after:aChild
       
   111     "add a node or collection of nodes to parent after a child
       
   112     "
       
   113     "add a node or collection of nodes to after a child
       
   114     "
       
   115     aChild notNil ifTrue:[
       
   116         aChild parent add:something after:aChild.
       
   117         self recomputeList
       
   118     ]
       
   119 !
       
   120 
       
   121 add:something afterIndex:anIndex below:aParent
       
   122     "add a node or collection of nodes to parent after an index
       
   123     "
       
   124     aParent add:something afterIndex:anIndex.
       
   125     self recomputeList
       
   126 
       
   127 !
       
   128 
       
   129 add:something before:aChild
       
   130     "add a node or collection of nodes to before a child
       
   131     "
       
   132     aChild notNil ifTrue:[
       
   133         aChild parent add:something before:aChild.
       
   134         self recomputeList
       
   135     ]
       
   136 !
       
   137 
       
   138 add:something beforeIndex:anIndex below:aParent
       
   139     "add a node or collection of nodes to parent before an index
       
   140     "
       
   141     aParent add:something beforeIndex:anIndex.
       
   142     self recomputeList
       
   143 
       
   144 !
       
   145 
       
   146 add:something below:aParent
       
   147     "add a node or collection of nodes to parent
       
   148     "
       
   149     aParent add:something.
       
   150     self recomputeList
       
   151 
       
   152 !
       
   153 
       
   154 remove:something
       
   155     "remove a node or collection of nodes
       
   156     "
       
   157     |invalidate|
       
   158 
       
   159     self each:something do:[:aNode|
       
   160         aNode parent notNil ifTrue:[
       
   161             aNode parent remove:aNode.
       
   162             invalidate := true
       
   163         ]
       
   164     ].
       
   165     invalidate == true ifTrue:[self recomputeList].
       
   166   ^ something
       
   167 !
       
   168 
       
   169 removeIndex:something
       
   170     "remove a node at index or collection of indexed nodes
       
   171     "
       
   172     |invalidate node|
       
   173 
       
   174     something isCollection ifFalse:[
       
   175         (something isNil or:[something == 0]) ifFalse:[
       
   176             ^ self remove:(list at:something)
       
   177         ].
       
   178       ^ nil
       
   179     ].
       
   180 
       
   181     (SortedCollection withAll:something) reverseDo:[:anIndex|
       
   182         node := list at:anIndex.
       
   183 
       
   184         node parent notNil ifTrue:[
       
   185             node parent remove:node.
       
   186             invalidate := true
       
   187         ]
       
   188     ].
       
   189     invalidate == true ifTrue:[self recomputeList].
       
   190 !
       
   191 
       
   192 removeSelection
       
   193     "remove selected nodes
       
   194     "
       
   195     self removeIndex:(self selectionIndex).
       
   196     self selectionIndex:nil
       
   197 ! !
       
   198 
       
   199 !SelectionInTree methodsFor:'enumerating'!
       
   200 
       
   201 each:something do:aBlock
       
   202     "evaluate a block for something or in case of a collection for each
       
   203      element in the collection
       
   204     "
       
   205     something notNil ifTrue:[
       
   206         something isCollection ifTrue:[something do:[:el|aBlock value:el]]
       
   207                               ifFalse:[aBlock value:something]
       
   208     ]
       
   209 
       
   210 ! !
       
   211 
       
   212 !SelectionInTree methodsFor:'private'!
       
   213 
       
   214 recomputeList
       
   215     "Travers the tree and build a new list."
       
   216 
       
   217     list := OrderedCollection new.
       
   218 
       
   219     root notNil ifTrue:[
       
   220         list add:root.
       
   221         root recomputeList:list
       
   222     ].
       
   223     self changed:#list.
       
   224 ! !
       
   225 
       
   226 !SelectionInTree methodsFor:'selection'!
       
   227 
       
   228 selection
       
   229     "get the selection or nil
       
   230     "
       
   231     ^ self selectionIndex
       
   232 !
       
   233 
       
   234 selection:indexesOrNil
       
   235     "set the selection
       
   236     "
       
   237     self selectionIndex:indexesOrNil
       
   238 !
       
   239 
       
   240 selectionIndex
       
   241     "get the selection or nil
       
   242     "
       
   243     ^ selection
       
   244 !
       
   245 
       
   246 selectionIndex:indexesOrNil
       
   247     "set the selection
       
   248     "
       
   249     |indexes|
       
   250 
       
   251     indexes := indexesOrNil.
       
   252 
       
   253     indexes size == 0 ifTrue:[
       
   254         (indexes isCollection or:[indexes == 0]) ifTrue:[
       
   255             indexes := nil
       
   256         ]
       
   257     ].
       
   258 
       
   259     indexes = selection ifFalse:[
       
   260         selection := indexes.
       
   261         self changed:#selection
       
   262     ]
       
   263 ! !
       
   264 
       
   265 !SelectionInTree class methodsFor:'documentation'!
       
   266 
       
   267 version
       
   268     ^ '$Header: /cvs/stx/stx/libwidg2/SelectionInTree.st,v 1.1 1997-07-05 12:59:13 ca Exp $'
       
   269 ! !