UIHelpTool.st
author tz
Fri, 09 Jan 1998 17:48:57 +0100
changeset 401 30ecf82e3ae7
parent 393 a824a3a9b834
child 454 8b016ffd83ce
permissions -rw-r--r--
checkin from browser

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



ApplicationModel subclass:#UIHelpTool
	instanceVariableNames:'isModified specClass dictionary listSelection maxCharsPerLine
		modifiedHolder'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-UIPainter'
!

!UIHelpTool class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 by 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
"
    used by the UIPainter to add help text to any component which will be shown
    during runing an application with enabled activeHelp mode.

    [author:]
        Claus Atzkern
"
! !

!UIHelpTool class methodsFor:'instance creation'!

openOnClass:aClass
    "
     UIHelpTool openOnClass:UIPainter
    "
    |helpTool|

    helpTool := UIHelpTool new.
    helpTool openInterface:#standAloneSpec.
    helpTool helpSpecFrom:aClass


! !

!UIHelpTool class methodsFor:'constants'!

label
    ^ 'Help'
! !

!UIHelpTool class methodsFor:'help specs'!

helpSpec
    "return a dictionary filled with helpKey -> helptext associations.
     These are used by the activeHelp tool."

    "
    UIHelpTool openOnClass:UIHelpTool    
    "

  ^ super helpSpec addPairsFrom:#(

#activeHelpAccessKey
'This ID is used to access an active help text for the selected component'

)
! !

!UIHelpTool class methodsFor:'interface specs'!

keyMenu
    "this window spec was automatically generated by the ST/X MenuEditor"

    "do not manually edit this - the builder may not be able to
     handle the specification if its corrupted."

    "
     MenuEditor new openOnClass:UIHelpTool andSelector:#keyMenu
     (Menu new fromLiteralArrayEncoding:(UIHelpTool keyMenu)) startUp
    "

    <resource: #menu>

    ^
     
       #(#Menu
          
           #(
             #(#MenuItem
                #'label:' 'save as ...'
                #'value:' #saveAs
            )
             #(#MenuItem
                #'label:' '-'
            )
             #(#MenuItem
                #'label:' 'remove'
                #'value:' #removeKey
            )
          ) nil
          nil
      )

    "Created: / 27.10.1997 / 00:55:20 / cg"
!

menuPanelStandAlone
    "this window spec was automatically generated by the ST/X MenuEditor"

    "do not manually edit this - the builder may not be able to
     handle the specification if its corrupted."

    "
     MenuEditor new openOnClass:UIHelpTool andSelector:#menuPanelStandAlone
     (Menu new fromLiteralArrayEncoding:(UIHelpTool menuPanelStandAlone)) startUp
    "

    <resource: #menu>

    ^
     
       #(#Menu
          
           #(
             #(#MenuItem
                #'label:' 'file'
                #'submenu:' 
                 #(#Menu
                    
                     #(
                       #(#MenuItem
                          #'label:' 'reload'
                          #'value:' #doReload
                      )
                       #(#MenuItem
                          #'label:' '-'
                      )
                       #(#MenuItem
                          #'label:' 'quit'
                          #'value:' #closeRequest
                      )
                    ) nil
                    nil
                )
            )
             #(#MenuItem
                #'label:' 'code'
                #'submenu:' 
                 #(#Menu
                    
                     #(
                       #(#MenuItem
                          #'label:' 'class'
                          #'value:' #doFromClass
                      )
                       #(#MenuItem
                          #'label:' '-'
                      )
                       #(#MenuItem
                          #'label:' 'install help spec.'
                          #'value:' #doInstallHelpSpec
                      )
                    ) nil
                    nil
                )
            )
          ) nil
          nil
      )
!

standAloneSpec
    "this window spec was automatically generated by the ST/X UIPainter"

    "do not manually edit this - the painter/builder may not be able to
     handle the specification if its corrupted."

    "
     UIPainter new openOnClass:UIHelpTool andSelector:#standAloneSpec
     UIHelpTool new openInterface:#standAloneSpec
    "

    <resource: #canvas>

    ^
     
       #(#FullSpec
          #'window:' 
           #(#WindowSpec
              #'name:' 'HelpTool'
              #'layout:' #(#LayoutFrame 219 0 193 0 814 0 690 0)
              #'label:' 'HelpTool'
              #'min:' #(#Point 10 10)
              #'max:' #(#Point 1160 870)
              #'bounds:' #(#Rectangle 219 193 815 691)
              #'usePreferredExtent:' false
          )
          #'component:' 
           #(#SpecCollection
              #'collection:' 
               #(
                 #(#MenuPanelSpec
                    #'name:' 'menuPanelStandAlone'
                    #'layout:' #(#LayoutFrame 0 0.0 0 0.0 0 1.0 25 0)
                    #'menu:' #menuPanelStandAlone
                )
                 #(#VariableHorizontalPanelSpec
                    #'name:' 'variableHorizontalPanel1'
                    #'layout:' #(#LayoutFrame 0 0.0 25 0.0 0 1.0 0 1.0)
                    #'component:' 
                     #(#SpecCollection
                        #'collection:' 
                         #(
                           #(#TextEditorSpec
                              #'name:' 'textView'
                              #'hasHorizontalScrollBar:' true
                              #'hasVerticalScrollBar:' true
                              #'miniScrollerHorizontal:' true
                              #'miniScrollerVertical:' true
                          )
                           #(#SequenceViewSpec
                              #'name:' 'selectionList'
                              #'model:' #listModel
                              #'menu:' #keyMenu
                              #'hasHorizontalScrollBar:' true
                              #'hasVerticalScrollBar:' true
                              #'miniScrollerHorizontal:' true
                              #'miniScrollerVertical:' true
                              #'useIndex:' false
                              #'sequenceList:' #listChannel
                          )
                        )
                    )
                    #'handles:' #(#Any 0.647651 1.0)
                )
              )
          )
      )
!

windowSpec
    "this window spec was automatically generated by the ST/X UIPainter"

    "do not manually edit this - the painter/builder may not be able to
     handle the specification if its corrupted."

    "
     UIPainter new openOnClass:UIHelpTool andSelector:#windowSpec
     UIHelpTool new openInterface:#windowSpec
    "
    "UIHelpTool open"

    <resource: #canvas>

    ^
     
       #(#FullSpec
          #'window:' 
           #(#WindowSpec
              #'name:' 'HelpTool'
              #'layout:' #(#LayoutFrame 199 0 167 0 484 0 437 0)
              #'label:' 'unnamed canvas'
              #'min:' #(#Point 10 10)
              #'max:' #(#Point 1160 870)
              #'bounds:' #(#Rectangle 199 167 485 438)
          )
          #'component:' 
           #(#SpecCollection
              #'collection:' 
               #(
                 #(#LabelSpec
                    #'name:' 'keyLabel'
                    #'layout:' #(#AlignmentOrigin 3 0.0 42 0.0 0 1)
                    #'label:' 'Key:'
                    #'adjust:' #left
                    #'resizeForLabel:' true
                )
                 #(#ComboBoxSpec
                    #'name:' 'comboBox'
                    #'layout:' #(#LayoutFrame 48 0 22 0 -3 1.0 44 0)
                    #'activeHelpKey:' #activeHelpAccessKey
                    #'tabable:' true
                    #'model:' #listModel
                    #'type:' #symbolOrNil
                    #'comboList:' #listChannel
                )
                 #(#TextEditorSpec
                    #'name:' 'textView'
                    #'layout:' #(#LayoutFrame 3 0.0 45 0.0 -3 1.0 -3 1.0)
                    #'hasHorizontalScrollBar:' true
                    #'hasVerticalScrollBar:' true
                    #'miniScrollerHorizontal:' true
                    #'miniScrollerVertical:' true
                )
              )
          )
      )
! !

!UIHelpTool methodsFor:'accessing'!

dictionary
    "return the value of the instance variable 'dictionary' (automatically generated)"

    ^ dictionary!

dictionary:aDictionary
    "set the value of the instance variable 'dictionary' (automatically generated)"

    (dictionary := aDictionary) isNil ifTrue:[
        dictionary := IdentityDictionary new.
    ].
    self updateList
!

helpKey
    listSelection size ~~ 0 ifTrue:[
        ^ listSelection asSymbol
    ].
    ^ nil
!

helpKey:aKey
    |key|

    aKey size ~~ 0 ifTrue:[
        key := aKey asString
    ].
    self listModel value:key
!

helpSpecFrom:aClass
    "read help text from an application associated with the class
    "
    |help|

    specClass  := self applicationClassAssociatedWith:aClass.
    isModified := false.

    (specClass respondsTo:#helpSpec) ifTrue:[
        help := specClass helpSpec
    ] ifFalse:[
        specClass := nil
    ].
    self dictionary:help
!

modifiedHolder:aValueHolder
    "set the value holder set to true in case of modifying attributes
    "
    modifiedHolder notNil ifTrue:[
        modifiedHolder removeDependent:self. 
    ].

    (modifiedHolder := aValueHolder) notNil ifTrue:[
        modifiedHolder addDependent:self.
    ].


!

updateList
    "update list from dictionary
    "
    self listChannel value:(dictionary keys asSortedCollection)
! !

!UIHelpTool methodsFor:'accessing menu'!

keyMenu
    "this window spec was automatically generated by the UI Builder"

    ^ self class keyMenu

    "Created: / 27.10.1997 / 00:55:20 / cg"
! !

!UIHelpTool methodsFor:'actions'!

accept
    "accept the text
    "
    |view key txt list listChgd|

    (listSelection size == 0 or:[(view := self editTextView) isNil]) ifFalse:[
        txt    := view contents asString.
        key    := listSelection asSymbol.
        list   := self listChannel value.

        (listChgd := (dictionary at:key ifAbsent:nil) isNil) ifTrue:[
            list add:key.
        ].
        dictionary at:key put:txt.
        isModified := true.

        listChgd ifTrue:[
            self updateList
        ]
    ]
!

installHelpSpecInto:aClass
    "install help text
    "
    |cls src order|

    cls := self applicationClassAssociatedWith:aClass.

    cls isNil ifTrue:[
        ^ self information:'no application class defined'.
    ].
    dictionary isEmpty ifTrue:[
        ^ self information:'no help text defined'
    ].
    src  := '' writeStream.

    src nextPutAll:

'helpSpec
    "return a dictionary filled with helpKey -> helptext associations.
     These are used by the activeHelp tool."

    "
    UIHelpTool openOnClass:', cls name asString ,'    
    "

  ^ super helpSpec addPairsFrom:#(

'.

    order := dictionary keys asSortedCollection.
    order do:[:key |
        |txt t|

        txt := dictionary at:key.
        src nextPutLine:key storeString.

        t := txt asString replaceAll:(Character cr) with:(Character space).

        (t endsWith:Character space) ifTrue:[
            t := t copyWithoutLast:1
        ].
        src nextPutLine:t storeString; cr.
    ].
    src nextPutLine:')'.

    Compiler compile:(src contents)
            forClass:cls class 
          inCategory:'help specs'.

    isModified := false.

! !

!UIHelpTool methodsFor:'aspects'!

listChannel
    "automatically generated by UIPainter ..."

    |holder|

    (holder := builder bindingAt:#listChannel) isNil ifTrue:[
        builder aspectAt:#listChannel put:(holder :=  OrderedCollection new asValue).
    ].
    ^ holder

    "Modified: / 27.10.1997 / 01:50:33 / cg"
!

listModel
    "automatically generated by UIPainter ..."

    |holder|

    (holder := builder bindingAt:#listModel) isNil ifTrue:[
        holder := AspectAdaptor new subject:self; forAspect:#listSelection.
        builder aspectAt:#listModel put:holder.
    ].
    ^ holder
! !

!UIHelpTool methodsFor:'initialization'!

initialize
    "setup instance attributes
    "
    super initialize.
    dictionary := IdentityDictionary new.
    isModified := false.

! !

!UIHelpTool methodsFor:'menu actions'!

removeKey
    |k sym|

    k := Dialog request:'name of key to remove:' initialAnswer:listSelection.
    k size > 0 ifTrue:[
        ((dictionary includesKey:k)
        or:[(sym := k asSymbolIfInterned) notNil
            and:[dictionary includesKey:sym]]) 
        ifFalse:[
            ^ self warn:'key is not present.'.
        ].
        sym notNil ifTrue:[
            dictionary removeKey:sym ifAbsent:nil.
        ].
        dictionary removeKey:k ifAbsent:nil.
        isModified := true.
        self updateList
     ].

    "Created: / 27.10.1997 / 01:48:48 / cg"
    "Modified: / 27.10.1997 / 02:25:30 / cg"
!

saveAs
    |key new|

    key := Dialog request:'save under key:' initialAnswer:(listSelection ? '').
    key := key withoutSeparators.

    key size ~~ 0 ifTrue:[
        key := key asSymbol.
        new := (dictionary at:key ifAbsent:nil) isNil.

        dictionary at:key put:(self editTextView contents asString).

        new ifTrue:[
            self updateList
        ].
        isModified := true.
        self listModel value:key.
     ].
! !

!UIHelpTool methodsFor:'private'!

editTextView
    "get the editTextView or nil.
    "
    |view|

    (view := builder componentAt:#textView) notNil ifTrue:[
        view := view scrolledView.

        view acceptAction isNil ifTrue:[
            view acceptAction:[:aList| self accept ].
        ].

        view left ~~ 0 ifTrue:[
            (maxCharsPerLine := view extent x // view font width) < 10 ifTrue:[
                maxCharsPerLine := nil
            ]
        ].
    ].
    ^ view
! !

!UIHelpTool methodsFor:'queries'!

applicationClassAssociatedWith:aClass
    "get application class keeping the associated help text or nil
    "
    |cls|

    cls := self resolveName:aClass.

    (cls notNil and:[cls includesBehavior:UISpecification]) ifTrue:[
        ^ UISpecificationTool
    ].
  ^ cls


!

isModified
    ^ isModified
! !

!UIHelpTool methodsFor:'selection'!

listSelection
    "returns current selection
    "
    ^ listSelection
!

listSelection:aSelection
    "current selection changed
    "
    |txt view sel|

    aSelection isNumber ifTrue:[
        aSelection ~~ 0 ifTrue:[
            sel := self listChannel value at:aSelection
        ]
    ] ifFalse:[
        aSelection size ~~ 0 ifTrue:[
            sel := aSelection withoutSeparators.
            sel size == 0 ifTrue:[
                sel := nil
            ]
        ]
    ].

    listSelection = sel ifFalse:[
        listSelection := sel.

        modifiedHolder notNil ifTrue:[
            modifiedHolder value:true.
        ].

        (view := self editTextView) notNil ifTrue:[
            listSelection notNil ifTrue:[
                txt := dictionary at:(listSelection asSymbol) ifAbsent:nil.

                (txt isNil or:[maxCharsPerLine isNil]) ifFalse:[
                    txt := UIPainter convertString:(txt asString) maxLineSize:maxCharsPerLine skipLineFeed:false.
                ]
            ].
            view contents:txt.    
        ]
    ]

! !

!UIHelpTool methodsFor:'startup / release'!

closeRequest

    (isModified and:[self masterApplication isNil]) ifTrue:[
        (self confirm:'quit without without saving your modifications ?') ifFalse:[
            ^ self
        ]
    ].
    ^ super closeRequest.
! !

!UIHelpTool methodsFor:'user interactions'!

doFromClass
    "setup new specification from a class accessed through to a dialog
    "
    |cls accepted|

    specClass notNil ifTrue:[cls := specClass name asValue]
                    ifFalse:[cls := '' asValue].

    [true] whileTrue:[
        accepted :=
            (DialogBox new
                addTextLabel:'Classes name:';
                addInputFieldOn:cls; 
                addAbortButton; 
                addOkButton; 
                open
            ) accepted.

        accepted ifFalse:[^ self].
        cls := self applicationClassAssociatedWith:cls value.

        cls notNil ifTrue:[
            ^ self helpSpecFrom:cls
        ].
        self warn:'no such class'.
    ]
!

doInstallHelpSpec
    "install help spec
    "
    self installHelpSpecInto:specClass
!

doReload
    "reload specification
    "
    |oldSel model|

    model  := self listModel.
    oldSel := model value.
    model value:nil.
    self helpSpecFrom:specClass.
    model value:oldSel.
    isModified := false.

! !

!UIHelpTool class methodsFor:'documentation'!

version
    ^ '$Header$'
! !