InspectorView.st
author claus
Sun, 16 Jan 1994 05:02:19 +0100
changeset 23 3363884b8e9f
parent 17 58c360f199be
child 29 8a72e10043f6
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1989 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.
"

View subclass:#InspectorView
       instanceVariableNames:'listView workspace 
                              inspectedObject selectedLine
                              inspectedValues nShown menu1 menu2'
       classVariableNames:''
       poolDictionaries:''
       category:'Interface-Inspector'
!

InspectorView comment:'

COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved

This class implements an graphical inspector.
Inspecting can be done on an object -
(where its instvarnames/values are inspected)
or a list of objects (where a namearray/valuesarray is inspected).
The later is used by the debugger to inspect method variables/args.

The system calls the inspector through the global variable "Inspector"
which is bound to this class (but could be redefined).

$Header: /cvs/stx/stx/libtool/InspectorView.st,v 1.7 1994-01-16 04:01:57 claus Exp $
written winter 89 by claus
'!

!InspectorView class methodsFor:'instance creation'!

for:anObject
    "create and launch a new inspector for anObject"

    ^ self openOn:anObject
!

inspect:anObject
    "create and launch a new inspector for anObject"

    ^ self openOn:anObject
!

openOn:anObject
    "create and launch a new inspector for anObject"

    |topView inspectorView|

    topView := StandardSystemView
                    label:('Inspector on: ' , anObject classNameWithArticle)
                     icon:(Form fromFile:'Inspector.xbm' resolution:100)
                minExtent:(100 @ 100).

    topView extent:(Display width // 3) @ (Display height // 3).

    inspectorView := self origin:(0.0 @ 0.0)
                          corner:(1.0 @ 1.0)
                             in:topView.

    "kludge: must realize first, to be able to set menu again"
    topView open.
    inspectorView inspect:anObject.

    "InspectorView openOn:(5 @ 7)"
    "DictionaryInspectorView openOn:(IdentityDictionary new)"
! !

!InspectorView methodsFor:'initialization'!

initialize
    |v panel|

    super initialize.

    panel := VariableHorizontalPanel origin:(0.0 @ 0.0)
                                     corner:(1.0 @ 1.0)
                                         in:self.

    v := ScrollableView for:SelectionInListView in:panel.
    v origin:(0.0 @ 0.0) corner:(0.3 @ 1.0).
    listView := v scrolledView.
    listView action:[:lineNr | self showSelection:lineNr].
    listView doubleClickAction:[:lineNr | self doInspect].
    listView ignoreReselect:false.

    workspace := CodeView origin:(0.3 @ 0.0) corner:(1.0 @ 1.0) in:panel.
    nShown := 100
!

initEvents
    ^ self enableKeyEvents
!

create
    super create.
    self initializeListViewMiddleButtonMenu
!

initializeListViewMiddleButtonMenu
    menu1 := PopUpMenu
                  labels:(resources array:#('inspect'))
               selectors:#doInspect
                receiver:self
                     for:listView.
    menu2 := PopUpMenu
                  labels:(resources array:#(
                                            'inspect'
                                            '-'
                                            'show more'
                                           ))
               selectors:#(doInspect nil showMore)
                receiver:self
                     for:listView.
    listView setMiddleButtonMenu:menu1.
    workspace acceptAction:[:theText | self doAccept:theText]
! !

!InspectorView methodsFor:'accessing'!

noChoice
    "clear name and value views"

"
    inspectedObject notNil ifTrue:[
        inspectedObject removeDependent:self
    ].
"
    inspectedObject := nil.
    inspectedValues := nil.
    workspace contents:nil.
    listView contents:nil
!

listOfNames
    "return a list of names to show in the selectionList"

    |aList n cut|

    aList := Text new.
    aList add:'self'.
    (inspectedObject class allInstVarNames) do:[:instVarName |
        aList add:instVarName
    ].
    (inspectedObject class isVariable) ifTrue:[
        n := inspectedObject basicSize.
        (n > nShown) ifTrue:[
            n := nShown.
            cut := true.
            listView setMiddleButtonMenu:menu2.
        ] ifFalse:[
            cut := false.
            listView setMiddleButtonMenu:menu1.
        ].
        1 to:n do:[:index |
            aList add:(index printString)
        ].
        cut ifTrue:[
            aList add:' ... '
        ]
    ].
    ^ aList
!

showMore
    nShown := nShown * 2.
    self inspect:inspectedObject
!

inspect:anObject
    "define the object to be inspected"

    |aList sameObject|

    sameObject := anObject == inspectedObject.
"
    sameObject ifFalse:[
        inspectedObject notNil ifTrue:[
            inspectedObject removeDependent:self
        ]
    ].
"
    inspectedObject := anObject.

    aList := self listOfNames.
    sameObject ifTrue:[
        listView setContents:aList
    ] ifFalse:[
        listView contents:aList
    ].

    workspace contents:nil.
    workspace doItAction:[:theCode |
        inspectedObject class compiler evaluate:theCode
                                       receiver:inspectedObject 
                                      notifying:workspace
    ].

"
    sameObject ifFalse:[
        inspectedObject notNil ifTrue:[
            inspectedObject addDependent:self
        ]
    ].
"
    inspectedValues := nil
!

inspect:anObject values:valueArray names:nameArray
    listView contents:nameArray.
    workspace contents:nil.
"
    inspectedObject notNil ifTrue:[
        inspectedObject removeDependent:self
    ].
"
    inspectedObject := anObject.
"
    inspectedObject notNil ifTrue:[
        inspectedObject addDependent:self
    ].
"
    inspectedValues := valueArray
!

inspectValues:valueArray names:nameArray
    listView contents:nameArray.
    workspace contents:nil.
"
    inspectedObject notNil ifTrue:[
        inspectedObject removeDependent:self
    ].
"
    inspectedObject := nil.
    inspectedValues := valueArray
! !

!InspectorView methodsFor:'user interaction'!

keyPress:aKey x:x y:y
    "all my input is passed on to the workspace-field"

    workspace keyPress:aKey x:0 y:0
!

update:something
    "handle updates from other inspectors"

    |oldSelection|

    something == inspectedObject ifTrue:[
        oldSelection := listView selection.
        self inspect:inspectedObject.
        oldSelection notNil ifTrue:[
            self showSelection:oldSelection
        ]
    ]
!

destroy
    inspectedObject notNil ifTrue:[
"
        inspectedObject removeDependent:self.
"
        inspectedObject := nil
    ].
    menu1 notNil ifTrue:[
        menu1 destroy. menu1 := nil.
    ].
    menu2 notNil ifTrue:[
        menu2 destroy. menu2 := nil.
    ].
    inspectedValues := nil.
    super destroy
!

showSelection:lineNr
    "user clicked on an instvar - show value in workspace"

    |val string index|

    workspace contents:nil.
    inspectedValues isNil ifTrue:[
        lineNr == 1 ifTrue:[
            val := inspectedObject
        ] ifFalse:[
            index := lineNr - 1.
            (inspectedObject class isVariable) ifFalse:[
                val := inspectedObject instVarAt:index
            ] ifTrue:[
                index <= (inspectedObject class instSize) ifTrue:[
                    val := inspectedObject instVarAt:index
                ] ifFalse:[
                    index := index - inspectedObject class instSize.
                    val := inspectedObject basicAt:index
                ]
            ]
        ]
    ] ifFalse:[
        val := inspectedValues at:lineNr
    ].
    string := val displayString.
    workspace cursorToTop.
"
    workspace show:string.
"
    workspace paste:string.

    selectedLine := lineNr
!

doAccept:theText
    |value index|

    value := Compiler evaluate:theText
                      receiver:inspectedObject 
                     notifying:workspace.
    inspectedValues isNil ifTrue:[
        selectedLine notNil ifTrue:[
            selectedLine == 1 ifFalse:[
                index := selectedLine - 1.
                (inspectedObject class isVariable) ifFalse:[
                    inspectedObject instVarAt:index put:value
                ] ifTrue:[
                    index <= (inspectedObject class instSize) ifTrue:[
                        inspectedObject instVarAt:index put:value
                    ] ifFalse:[
                        index := index - inspectedObject class instSize.
                        inspectedObject basicAt:index put:value
                    ]
                ]
            ]
        ]
    ] ifFalse:[
        selectedLine notNil ifTrue:[
            inspectedValues at:selectedLine put:value
        ]
    ].
    inspectedObject changed
!

doInspect
    "user selected inspect-menu entry"
    |index objectToInspect|

    selectedLine notNil ifTrue:[
        inspectedValues isNil ifTrue:[
            (selectedLine == 1) ifTrue:[
                objectToInspect := inspectedObject
            ] ifFalse:[
                index := selectedLine - 1.
                (inspectedObject class isVariable) ifFalse:[
                    objectToInspect := inspectedObject instVarAt:index
                ] ifTrue:[
                    index <= (inspectedObject class instSize) ifTrue:[
                        objectToInspect := inspectedObject instVarAt:index
                    ] ifFalse:[
                        index := index - inspectedObject class instSize.
                        objectToInspect := inspectedObject basicAt:index
                    ]
                ]
            ]
        ] ifFalse:[
            objectToInspect := inspectedValues at:selectedLine
        ].
        objectToInspect inspect
    ]
! !