DictionaryInspectorView.st
author Claus Gittinger <cg@exept.de>
Sat, 26 Jun 1999 17:50:00 +0200
changeset 2213 d6e6c41db3ef
parent 2212 10903bf15c4b
child 2215 7797faf73e01
permissions -rw-r--r--
keep scroll-offset when removing a key

"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      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.
"

InspectorView subclass:#DictionaryInspectorView
	instanceVariableNames:'keys'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Inspector'
!

!DictionaryInspectorView class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      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
"
    a modified Inspector for Dictionaries

    [author:]
        Claus Gittinger
"
! !

!DictionaryInspectorView methodsFor:'menu'!

fieldMenu
    <resource: #programMenu >

    |items m idx|

    inspectedObject == Smalltalk ifTrue:[
        items := #(
                       ('copy key'             doCopyKey      )
                       ('-')
                       ('inspect'              doInspect      )
                       ('inspect key'          doInspectKey   )
                       ('basicInspect'         doBasicInspect )
                       ('inspect hierarchical' doNewInspect   )
                       ('-')
                       ('ref chains'           showReferences )
                       ('references to key'    showKeyReferences )
                       ('-')
                       ('add key'              doAddKey       )
                       ('remove key'           doRemoveKey    )
                   ).
    ] ifFalse:[
        items := #(
                       ('copy key'             doCopyKey      )
                       ('-')
                       ('inspect'              doInspect      )
                       ('inspect key'          doInspectKey   )
                       ('basicInspect'         doBasicInspect )
                       ('inspect hierarchical' doNewInspect   )
                       ('-')
                       ('ref chains'           showReferences )
                       ('-')
                       ('add key'              doAddKey       )
                       ('remove key'           doRemoveKey    )
                   ).
    ].

    monitorProcess isNil ifTrue:[
        items := items , #(
                       ('-')
                       ('start monitor'    doStartMonitor )
                          ).
    ] ifFalse:[
        items := items , #(
                       ('-')
                       ('stop monitor'     doStopMonitor  )
                          ).
    ].

    m := PopUpMenu itemList:items resources:resources.

    selectedLine isNil ifTrue:[
        m disableAll:#(doInspect doInspectKey doBasicInspect doNewInspect
                       doRemoveKey doStartMonitor doStopMonitor doCopyKey
                      )
    ] ifFalse:[
        (self keyIndexForLine:selectedLine) isNil ifTrue:[
            m disableAll:#(doInspectKey doRemoveKey doCopyKey)
        ]
    ].
    ^ m.

    "Modified: / 21.5.1998 / 13:25:10 / cg"
! !

!DictionaryInspectorView methodsFor:'menu actions'!

doAddKey
    "add a key"

    |keyName key l|

    keyName := Dialog request:'key to add:' initialAnswer:''.
    keyName notEmpty ifTrue:[
        key := keyName asSymbol.
        (inspectedObject includesKey:key) ifFalse:[
            inspectedObject at:key put:nil.
            selectedLine := nil.
            inspectedObject changed.
            l := listView firstLineShown.
            self reinspect. "force list update"
            listView scrollToLine:l
        ]
    ]
!

doInspectKey
    "inspect selected items key"

    |idx|

    idx := self keyIndexForLine:selectedLine.
    idx notNil ifTrue:[
        (keys at:idx) inspect
    ]
!

doReferences
    "show users of selected key (i.e. global).
     Only useful when inspecting smalltalk"

    |idx|

    idx := self keyIndexForLine:selectedLine.
    idx notNil ifTrue:[
        SystemBrowser browseReferendsOf:(keys at:idx) asSymbol.
        ^ self
    ].
!

doRemoveKey
    "remove selected item from keys"

    |idx key l|

    idx := self keyIndexForLine:selectedLine.
    idx notNil ifTrue:[
        key := keys at:idx.
        (inspectedObject includesKey:key) ifTrue:[
            listView cursor:(Cursor wait).
            inspectedObject removeKey:key.
            keys := nil.
            selectedLine := nil.
            inspectedObject changed.
            listView cursor:(Cursor hand).
            l := listView firstLineShown.
            self reinspect. "force list update"
            listView scrollToLine:l.
        ].
    ]
!

showKeyReferences
    "show users of selected key (i.e. global).
     Only useful when inspecting smalltalk"

    |idx key|

    idx := self keyIndexForLine:selectedLine.
    idx notNil ifTrue:[
        self withWaitCursorDo:[
            SystemBrowser browseReferendsOf:(keys at:idx) asSymbol
        ].
    ]

! !

!DictionaryInspectorView methodsFor:'private'!

baseInspectedObjectClass
    ^ Dictionary
!

defaultLabel
    ^ 'keys'

    "Created: 28.6.1996 / 19:46:51 / cg"
!

fieldList 
    "return a list of names for the selectionlist."

    |aList l|

    inspectedObject isNil ifTrue:[^ #()].

    self topView withWaitCursorDo:[
        inspectedObject == Smalltalk ifTrue:[
            "/ kludge ...
            aList := OrderedCollection new
        ] ifFalse:[
            aList := inspectedObject class allInstVarNames asOrderedCollection.
            "/ hide Dictionary stuff
            aList := aList copyFrom:(Dictionary instSize + 1).
        ].
        hideReceiver ifFalse:[aList addFirst:'self'].

        keys := inspectedObject keys asSortedCollection:[:a :b | a displayString < b displayString].
        l := keys collect:[:k | k isSymbol ifTrue:[
                                                    k printString
                                                   ] ifFalse:[
                                                    k displayString
                                                   ]
                                             ].
        aList addAll:l.
    ].
    ^ aList

    "Modified: 8.5.1996 / 14:16:35 / stefan"
    "Modified: 14.2.1997 / 18:26:29 / cg"
!

release 
    "release inspected object"

    keys := nil.
    super release
! !

!DictionaryInspectorView methodsFor:'user interaction'!

doAccept:theText
    "accept value for selected item"

    |value idx key|

    value := Compiler evaluate:theText receiver:inspectedObject notifying:workspace.

    idx := self keyIndexForLine:selectedLine.
    idx notNil ifTrue:[
        key := keys at:idx.
        inspectedObject at:key put:value.
    ] ifFalse:[
        idx := self instVarIndexForLine:selectedLine.
        idx notNil ifTrue:[
            inspectedObject instVarAt:idx put:value.
        ] ifFalse:[
            ^ self "/ self selected
        ]
    ].

    inspectedObject changed.
    self reinspect.
!

valueAtLine:lineNr
    "helper - return the value of the selected entry"

    |key idx|

    idx := self keyIndexForLine:lineNr.
    idx notNil ifTrue:[
        key := keys at:idx.
        ^ inspectedObject at:key ifAbsent:nil.
    ].
    idx := self instVarIndexForLine:lineNr.
    idx notNil ifTrue:[
        ^ inspectedObject instVarAt:idx.
    ].

    hideReceiver ifFalse:[
        (lineNr == 1 or:[lineNr isNil]) ifTrue:[
            ^ inspectedObject
        ].
    ].

    ^ nil

! !

!DictionaryInspectorView class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libtool/DictionaryInspectorView.st,v 1.38 1999-06-26 15:50:00 cg Exp $'
! !