DataSetView.st
author ca
Wed, 28 Jan 1998 17:19:04 +0100
changeset 700 57e96fcbc6f6
parent 654 957008fbd9c5
child 714 79580d4ddf73
permissions -rw-r--r--
bugfix: level:

"
 COPYRIGHT (c) 1997 by Claus Gittinger / eXept Software AG
              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.
"




View subclass:#DataSetView
	instanceVariableNames:'labelView columnView listHolder useIndex labels scrolledView'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-DataSet'
!

!DataSetView class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by Claus Gittinger / eXept Software AG
              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 a selection list view based on rows and columns.
    It allows for the dynamic editing of this information.

    [author:]
        Claus Atzkern

    [see also:]
        DataSetColumnSpec
        DataSetColumn
        DSVColumnView
"
!

examples
"
    example 1: list with valid rows of type Array
                                                                                [exBegin]
    |top scr columns rows bool rdWtSel|

    top  := StandardSystemView new label:'select'; extent:700@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    columns := OrderedCollection new.
    rows    := OrderedCollection new.
    bool    := true.
    rdWtSel := #( #at: #at:put: ).

    1 to:1000 do:[:i||n|
        n := i printString.
        rows add:(Array with:('text: ', n) with:('input: ', n) with:bool).
        bool := bool not.
    ].

    columns add:(DataSetColumnSpec label:'Text'   editorType:#None        selector:rdWtSel).
    columns add:(DataSetColumnSpec label:'Input'  editorType:#InputField  selector:rdWtSel).
    columns add:(DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:rdWtSel).

    scr columnDescriptors:columns.
    scr list:rows.
    top open.
                                                                                [exEnd]



    example 2: list with none valid rows; defining #rowIfAbsent: block
                                                                                [exBegin]
    |top scr columns bool rdWtSel|

    top  := StandardSystemView new label:'select'; extent:700@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    columns := OrderedCollection new.
    bool    := true.
    rdWtSel := #( #at: #at:put: ).

    columns add:(DataSetColumnSpec label:'Text'   editorType:#None        selector:rdWtSel).
    columns add:(DataSetColumnSpec label:'Input'  editorType:#InputField  selector:rdWtSel).
    columns add:(DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:rdWtSel).

    scr rowIfAbsent:[:i|
        bool := bool not.
        Array with:('text: ', i printString) with:('input: ') with:bool
    ].

    scr columnDescriptors:columns.
    scr list:(Array new:1000).
    top open.
                                                                                [exEnd]



    example 3: list with valid rows of type Structure
                                                                                [exBegin]
    |top scr clDc rows slct list idx bool|

    top  := StandardSystemView new label:'select'; extent:700@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    clDc := OrderedCollection new.
    rows := OrderedCollection new.
    list := #( 'Text ' 'Field ' 'C-Box ' 'C-List ' true    #( 'foo' 'bar' 'baz' ) ).
    slct := #( #text   #field   #cbox    #clist    #toggle #choices               ).
    idx  := 11.
    bool := true.

    20 timesRepeat:[ |values|
        values := list collect:[:n|
            n isString ifTrue:[n, idx printString]
                      ifFalse:[n == true ifTrue:[bool] ifFalse:[n]]
        ].
        rows add:(Structure newWith:slct values:values).
        bool := bool not.
        idx  := idx + 1.
    ].
    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Field'  editorType:#InputField  selector:#field ).
    clDc add:( DataSetColumnSpec label:'C-Box'  editorType:#ComboBox    selector:#cbox ).
    clDc last choices:#choices.
    clDc add:( DataSetColumnSpec label:'C-List' editorType:#ComboList   selector:#clist ).
    clDc last choices:#choices.
    clDc add:( DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:#toggle ).

    scr has3Dseparators:true.
    scr columnDescriptors:clDc.
    scr list:rows.
    top open.
                                                                                [exEnd]




    example 4: table includes a row selector and multiple select is enabled
                                                                                [exBegin]
    |top scr clDc rows slct list idx bool|

    top  := StandardSystemView new label:'select'; extent:700@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    clDc := OrderedCollection new.
    rows := OrderedCollection new.
    list := #( 'Text ' 'Field ' 'C-Box ' 'C-List ' true    #( 'foo' 'bar' 'baz' ) ).
    slct := #( #text   #field   #cbox    #clist    #toggle #choices               ).
    idx  := 11.
    bool := true.

    20 timesRepeat:[ |values|
        values := list collect:[:n|
            n isString ifTrue:[n, idx printString]
                      ifFalse:[n == true ifTrue:[bool] ifFalse:[n]]
        ].
        rows add:(Structure newWith:slct values:values).
        bool := bool not.
        idx  := idx + 1.
    ].
    clDc add:( DataSetColumnSpec rowSelector ).
    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Field'  editorType:#InputField  selector:#field ).
    clDc add:( DataSetColumnSpec label:'C-Box'  editorType:#ComboBox    selector:#cbox ).
    clDc last choices:#choices.
    clDc add:( DataSetColumnSpec label:'C-List' editorType:#ComboList   selector:#clist ).
    clDc last choices:#choices.
    clDc add:( DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:#toggle ).

    scr has3Dseparators:true.
    scr columnDescriptors:clDc.
    scr multipleSelectOk:true.
    scr list:rows.
    top open.
                                                                                [exEnd]




    example 5: performance test; with valid rows
                                                                                [exBegin]
    |t1 top scr clDc rows slct list bool tmArr listModel|

    top  := StandardSystemView new label:'select'; extent:600@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    clDc := OrderedCollection new.
    rows := OrderedCollection new.
    list := #( 'Text ' 'Field ' 'C-Box ' 'C-List ' true ).
    slct := #( #text   #field   #cbox    #clist    #toggle ).
    bool := true.

    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Field'  editorType:#InputField  selector:#field ).
    clDc add:( DataSetColumnSpec label:'C-Box'  editorType:#ComboBox    selector:#cbox ).
    clDc add:( DataSetColumnSpec label:'C-List' editorType:#ComboList   selector:#clist ).
    clDc add:( DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:#toggle ).

    scr columnDescriptors:clDc.
    scr beDependentOfRows:false.
    scr has3Dseparators:false.

    top openAndWait.
    tmArr := Array new:4.
    listModel := List new.
    scr listHolder:listModel.

    (1 to:tmArr size) do:[:i|
        listModel removeAll.

        t1 := Time millisecondsToRun:[
            1 to:200 do:[:i| |values|
                values := list collect:[:n|
                    n isString ifTrue:[n, i printString]
                              ifFalse:[n == true ifTrue:[bool] ifFalse:[n]]
                ].
                listModel add:(Structure newWith:slct values:values).
                bool := bool not.

                i even ifTrue:[
                    listModel removeFirst
                ]       
            ].
        ].
        tmArr at:i put:t1
    ].
    t1 := 0.

    Transcript showCR:'----------'.
    tmArr do:[:t|
        t1 := t1 + t.
        Transcript showCR:'TIME : ', t printString.
    ].
    Transcript showCR:'----------'.
    Transcript showCR:'DIFF : ', (t1 // tmArr size) printString.
                                                                                [exEnd]



    example 6: performance test; with invalid rows: defining #rowIfAbsent:.
                                                                                [exBegin]
    |t1 top scr clDc rows slct list bool tmArr listModel ctr|

    top  := StandardSystemView new label:'select'; extent:600@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    clDc := OrderedCollection new.
    rows := OrderedCollection new.
    list := #( 'Text ' 'Field ' 'C-Box ' 'C-List ' true ).
    slct := #( #text   #field   #cbox    #clist    #toggle ).
    bool := true.

    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Field'  editorType:#InputField  selector:#field ).
    clDc add:( DataSetColumnSpec label:'C-Box'  editorType:#ComboBox    selector:#cbox ).
    clDc add:( DataSetColumnSpec label:'C-List' editorType:#ComboList   selector:#clist ).
    clDc add:( DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:#toggle ).
    ctr := 0.

    scr rowIfAbsent:[:i||values|
        bool := bool not.
        ctr := ctr + 1.
        values := list collect:[:n|
            n isString ifTrue:[n, ctr printString]
                      ifFalse:[n == true ifTrue:[bool] ifFalse:[n]]
        ].
        Structure newWith:slct values:values
    ].

    scr columnDescriptors:clDc.
    scr beDependentOfRows:false.
    scr has3Dseparators:true.

    top openAndWait.
    tmArr := Array new:4.
    listModel := List new.
    scr listHolder:listModel.

    (1 to:tmArr size) do:[:i|
        listModel removeAll.

        t1 := Time millisecondsToRun:[
            1 to:200 do:[:i| |values|
                  listModel add:nil.
                i even ifTrue:[
                    listModel removeFirst
                ]       
            ]
        ].
        tmArr at:i put:t1
    ].
    t1 := 0.

    Transcript showCR:'----------'.
    tmArr do:[:t|
        t1 := t1 + t.
        Transcript showCR:'TIME : ', t printString.
    ].
    Transcript showCR:'----------'.
    Transcript showCR:'DIFF : ', (t1 // tmArr size) printString.
                                                                                [exEnd]



    example 7: images as label
                                                                                [exBegin]

    |lbl top scr clDc rows slct list idx bool|

    top  := StandardSystemView new label:'select'; extent:400@440.
    scr  := DataSetView origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:top.

    clDc := OrderedCollection new.
    rows := OrderedCollection new.
    list := #( 'Text ' 'Field ' 'C-Box ' 'C-List ' true    #( 'foo' 'bar' 'baz' ) ).
    slct := #( #text   #field   #cbox    #clist    #toggle #choices               ).
    idx  := 11.
    bool := true.

    20 timesRepeat:[ |values|
        values := list collect:[:n|
            n isString ifTrue:[n, idx printString]
                      ifFalse:[n == true ifTrue:[bool] ifFalse:[n]]
        ].
        rows add:(Structure newWith:slct values:values).
        bool := bool not.
        idx  := idx + 1.
    ].
    lbl := Image fromFile:('gifImages/nexthand.gif' ).

    clDc add:( DataSetColumnSpec label:lbl      editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Text'   editorType:#None        selector:#text ).
    clDc add:( DataSetColumnSpec label:'Field'  editorType:#InputField  selector:#field ).

    lbl := Image fromFile:('xpmBitmaps/misc_tools/box_full.xpm' ).
    clDc add:( DataSetColumnSpec label:lbl      editorType:#ComboBox    selector:#cbox ).
    clDc last choices:#choices.
    clDc add:( DataSetColumnSpec label:'C-List' editorType:#ComboList   selector:#clist ).
    clDc last choices:#choices.
    clDc add:( DataSetColumnSpec label:'Toggle' editorType:#CheckToggle selector:#toggle ).

    scr has3Dseparators:true.
    scr columnDescriptors:clDc.
    scr list:rows.
    top open.
                                                                                [exEnd]



"
! !

!DataSetView methodsFor:'accessing'!

add:aRow
    "add a row; reimplemented caused by add in base class
    "
    ^ columnView add:aRow
!

useIndex
    "specify, if the selected components value or its index in the
     list should be sent to the model. The default is its index.
    "
    ^ useIndex
!

useIndex:aBool
    "specify, if the selected components value or its index in the
     list should be sent to the model. The default is its index.
    "
    useIndex := aBool
! !

!DataSetView methodsFor:'accessing look'!

font:aFont
    "set the font for all rows and labels.
    "
    columnView font:aFont
!

horizontalMini:aBool
    "control the horizontal scrollBar to be either a miniScroller,
     or a full scrollBar.
    "
    scrolledView horizontalMini:aBool.
    self recomputeLabelView.
!

horizontalScrollable:aBool
    "enable/disable horizontal scrollability.
     If disabled, the horizontal scrollBar is made invisible.
    "
    scrolledView horizontalScrollable:aBool.
    self recomputeLabelView.
!

isHorizontalScrollable
    "returns true if horizontal scrollable
    "
    ^ scrolledView isHorizontalScrollable
!

isVerticalScrollable
    "returns true if vertical scrollable
    "
    ^ scrolledView isVerticalScrollable
!

verticalMini:aBool
    "control the vertical scrollBar to be either a miniScroller,
     or a full scrollBar.
    "
    scrolledView verticalMini:aBool.
    self recomputeLabelView.

!

verticalScrollable:aBool
    "enable/disable vertical scrollability.
     If disabled, the horizontal scrollBar is made invisible.
    "
    scrolledView verticalScrollable:aBool.
    self recomputeLabelView
!

viewBackground
    "get the background color of the rows and labels
    "
    ^ columnView backgroundColor.


!

viewBackground:aColor
    "set the background color of the rows and labels
    "
    ^ columnView backgroundColor:aColor.


! !

!DataSetView methodsFor:'accessing menus'!

menuHolder:anObject
    "set the menu holder in the column view too
    "
    super menuHolder:anObject.
    columnView menuHolder:anObject.

!

menuMessage:aSymbol
    "set the menu message in the column view too
    "
    super menuMessage:aSymbol.
    columnView menuMessage:aSymbol.

!

menuPerformer:anObject
    "set the performer in the column view too
    "
    super menuPerformer:anObject.
    columnView menuPerformer:anObject.

! !

!DataSetView methodsFor:'accessing mvc'!

listHolder:aListHolder
    "set the valueHolder which holds the list of rows
    "
    listHolder ~~ aListHolder ifTrue:[
        listHolder notNil ifTrue:[
            listHolder removeDependent:self
        ].

        (listHolder := aListHolder) notNil ifTrue:[
            listHolder addDependent:self
        ]
    ].

    columnView list:(listHolder value)
!

model:aModel
    "set the valueHolder which holds the selection and maybe the list of rows
    "
    (model respondsTo:#list) ifTrue:[
        (model list == listHolder) ifTrue:[
            self listHolder:nil
        ]
    ].
    super model:aModel.

    aModel notNil ifTrue:[
        (aModel respondsTo:#list) ifTrue:[
            self listHolder:model list
        ]
    ]
! !

!DataSetView methodsFor:'change & update'!

recomputeLabelView
    "recompute label view
    "
    |p x|

    p := columnView styleSheet at:'scrollBar.position' default:#left.
    x := labelView width - columnView width.

    p == #left ifTrue:[labelView  leftInset:(columnView left)]
              ifFalse:[labelView rightInset:(columnView right - columnView width)].
!

recomputeLabels
    "labels changed; recompute labels
    "
    |hgt lbl prv lst|

    labels size ~~ 0 ifTrue:[ labels do:[:b| b destroy] ].
    labels := OrderedCollection new.

    columnView numberOfColumns == 0 ifTrue:[
        ^ self
    ].

    hgt := 0.
    prv := nil.
    lst := columnView lastColumn.

    columnView columnsDo:[:aCol||l|
        (aCol showColSeparator or:[aCol == lst]) ifTrue:[
            lbl := DSVLabelView column:(prv ? aCol) in:labelView.
            prv := nil.
            hgt := (lbl preferredExtent y) max:hgt.
            labels add:lbl.
        ] ifFalse:[
            (prv isNil and:[aCol label notNil]) ifTrue:[prv := aCol]
        ]
    ].
    hgt := hgt + 10.

    scrolledView topInset:hgt.
    labelView bottomInset:(hgt negated).

    realized ifTrue:[
        self updateLabels.
        labels do:[:l| l realize ].
    ]

!

update:what with:aPara from:chgObj
    "one of my models changed
    "
    |val|

    chgObj == columnView ifTrue:[
        what == #selection ifTrue:[
            model notNil ifTrue:[
                val := columnView selectedRowIndex.

                (model respondsTo:#selectionIndex:) ifTrue:[
                    model selectionIndex:val
                ] ifFalse:[
                    useIndex ifFalse:[ model value:(columnView selectedRow) ]
                              ifTrue:[ model value:val ]
                ]
            ].
            ^ self
        ].

        what == #sizeOfColumns ifTrue:[ ^ self recomputeLabels ].
        what == #columnsLayout ifTrue:[ ^ self updateLabels ].

        shown ifTrue:[
            what == #originOfContents ifTrue:[
                (labels size ~~ 0 and:[(val := aPara x) ~~ 0]) ifTrue:[
                    labels do:[:b| b origin:((b origin) - (val @ 0)) ]
                ].
            ]
        ].
        ^ self
    ].

    chgObj == model ifTrue:[
        (what == #selectionIndex or:[what == #selection]) ifTrue:[
            ^ columnView selectRowIndex:(model selectionIndex)
        ].
        what == #list ifTrue:[
            ^ self listHolder:model list
        ].
        model == listHolder ifFalse:[
            what == #value ifTrue:[
                columnView selectRowIndex:model value
            ].
            ^ self
        ].
    ].

    "/ listHolder and model could be the same

    chgObj == listHolder ifTrue:[
        what == #at: ifTrue:[
            columnView at:aPara put:(listHolder at:aPara)
        ] ifFalse:[
            (what isNil or:[what == #list or:[what == #size]]) ifTrue:[
                ^ self listHolder:listHolder        "/ reread list from model
            ].
            self perform:what with:aPara
        ]
    ].
!

updateLabels
    "layout of labels changed; recompute layout
    "
    |x0 x1 dX id lst|

    labels size == columnView numberOfColumns ifFalse:[
        id := columnView lastColumn showColSeparator ifTrue:[0] ifFalse:[1].

        columnView columnsDo:[:aCol|
            aCol showColSeparator ifTrue:[id := id + 1]
        ].
        labels size ~~ id ifTrue:[
            ^ self recomputeLabels
        ].
    ].
    labels size == 0 ifTrue:[ ^ self ].

    x0  := columnView margin - columnView xOriginOfContents.
    dX  := columnView separatorSize - 1.
    lst := columnView lastColumn.
    id  := 1.
    x1  := 0.

    columnView columnsDo:[:aCol|
        x1 := x1 + aCol width.

        (aCol showColSeparator or:[aCol == lst]) ifTrue:[
            (labels at:id) origin:(x0 @ 0.0) extent:(x1 - dX @ 1.0).
            x0 := x0 + x1.
            x1 := 0.
            id := id + 1.
        ]
    ]


! !

!DataSetView methodsFor:'change & update list'!

insert:anIndex
    "raised from listHolder: insert row derived from listHolder at anIndex
    "
    |list|

    list := listHolder value.
    columnView add:(list at:anIndex) beforeIndex:anIndex


!

insertCollection:anArray
    "raised from listHolder: insert collection of rows derived from listHolder
     from start (anArray at:1) to stop (anArray at:2).
    "
    |start stop size list|

    list  := listHolder value.
    start := anArray first.
    size  := anArray last.

    size ~~ 0 ifTrue:[
        stop := start + size - 1.
        columnView addAll:(list copyFrom:start to:stop) beforeIndex:start
    ]
!

remove:anIndex
    "raised from listHolder: remove row at anIndex
    "
    columnView removeIndex:anIndex


!

removeFrom:anArray
    "raised from listHolder: remove rows from start (anArray at:1) 
     to stop (anArray at:2).
    "
    |start stop|

    start := anArray first.
    stop  := anArray last.

    listHolder value size == 0 ifTrue:[
        columnView list:nil
    ] ifFalse:[
        (stop - start + 1) timesRepeat:[
            columnView removeIndex:start
        ]
    ]
!

replace:anArray
    "raised from listHolder: replace collection of rows derived from listHolder
     from start (anArray at:1) to stop (anArray at:2).
    "
    |start stop list|

    start := anArray first.
    stop  := anArray last.
    list  := listHolder value.

    start to:stop do:[:anIndex|
        columnView at:anIndex put:(list at:anIndex)
    ].

! !

!DataSetView methodsFor:'error handling'!

doesNotUnderstand:aMessage
    "does not understand message; delegate to column view
    "
    ^ aMessage sendTo:columnView
! !

!DataSetView methodsFor:'initialize'!

destroy
    "remove dependencies
    "
    listHolder notNil ifTrue:[
        listHolder removeDependent:self
    ].
    columnView removeDependent:self.

    super destroy



!

initialize
    "set column area
    "
    super initialize.

    labelView    := View origin:(0.0 @ 0.0) corner:(1.0 @ 0.0) in:self.

    scrolledView := HVScrollableView for:DSVColumnView 
                         miniScrollerH:true
                         miniScrollerV:false
                                origin:(0.0 @ 0.0)
                                corner:(1.0 @ 1.0)
                                     in:self.

    useIndex   := true.
    labels     := OrderedCollection new.
    columnView := scrolledView scrolledView.

    columnView borderWidth:0.
    columnView addDependent:self.

!

realize
    "realize view; update labels
    "
    super realize.
    self  recomputeLabelView.
    self  updateLabels.
! !

!DataSetView methodsFor:'queries'!

specClass
    "returns my spec class
    "
    ^ DataSetSpec
! !

!DataSetView class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/DataSetView.st,v 1.12 1998-01-28 16:19:04 ca Exp $'
! !