SettingsDialog.st
author Claus Gittinger <cg@exept.de>
Mon, 14 Feb 2011 18:16:30 +0100
changeset 9774 5bde45b1c359
parent 9617 7c53e41f20cf
child 9781 9896a1dff42c
permissions -rw-r--r--
automatically generated by browser

"
 COPYRIGHT (c) 2002 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.
"
"{ Package: 'stx:libtool' }"

ApplicationModel subclass:#SettingsDialog
	instanceVariableNames:'requestor subCanvasApplicationHolder help doReload doSave
		enableReload enableHelp enableOK selectedItem applicationList
		categoryIcons lastSelection whyDisabledInfoHolder'
	classVariableNames:'ApplicationList IconList'
	poolDictionaries:''
	category:'Interface-Smalltalk'
!

HierarchicalList subclass:#HierarchicalApplicationList
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:SettingsDialog
!

HierarchicalItemWithLabelAndIcon subclass:#ApplicationItem
	instanceVariableNames:'application applicationClass nameString'
	classVariableNames:''
	poolDictionaries:''
	privateIn:SettingsDialog::HierarchicalApplicationList
!

!SettingsDialog class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2002 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
"
    This is an application which presents a hierarchy of settings applications.
    It is used (among others) in the launcher's and filebrowser's settings-dialogs.
    However, it is rather generic and can be setup for any applications setting dialog
    See class>>examples for more info.

    [author:]
        Christian Penk, eXept Software AG

    [see also:]
        AbstractLauncherApplication defaultSettingsApplicationList 
"
!

examples

"
  start a Settings Dialog with a language Settings Application under root
                                                                [exBegin]
    | settingsApp |

    settingsApp := self new.
    settingsApp addApplClass:#'AbstractSettingsApplication::LanguageSettingsAppl' 
                withName:'Language'.
    settingsApp open.
                                                                [exEnd]


  start a Settings Dialog with a language Settings Application under root/Test
                                                                [exBegin]
    | settingsApp |

    settingsApp := self new.
    settingsApp addApplClass:#'AbstractSettingsApplication::LanguageSettingsAppl' 
                withName:'Test/Language'.
    settingsApp open.
                                                                [exEnd]

  start a Settings Dialog with a language Settings Application under root/Test
  with an other category test icon
                                                                [exBegin]
    | settingsApp |

    settingsApp := self new.
    settingsApp addApplClass:#'AbstractSettingsApplication::LanguageSettingsAppl' 
                withName:'Test/Language'.
    settingsApp addIcon:ToolbarIconLibrary start22x22Icon forCategory:'Test'.
    settingsApp open.
                                                                [exEnd]

  start a Settings Dialog with a language and memory Settings Application 
  in different categories
                                                                [exBegin]
    | settingsApp |

    settingsApp := self new.
    settingsApp addIcon:ToolbarIconLibrary start22x22Icon forCategory:'Test'.
    settingsApp addApplClass:#'AbstractSettingsApplication::LanguageSettingsAppl' 
                withName:'Test/Language'.
    settingsApp addApplClass:#'AbstractSettingsApplication::MemorySettingsAppl' 
                withName:'Test2/Memory'.
    settingsApp open.
                                                                [exEnd]
"
! !

!SettingsDialog class methodsFor:'initialization'!

initialize
    AbstractSettingsApplication autoload.
! !

!SettingsDialog class methodsFor:'accessing'!

itemClass

    ^ SettingsDialog::HierarchicalApplicationList::ApplicationItem
! !

!SettingsDialog class methodsFor:'application icons'!

defaultSettingsAppIcon

    ^ self settingsIcon
!

settingsIcon
    "This resource specification was automatically generated
     by the ImageEditor of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the ImageEditor may not be able to read the specification."

    "
     self settingsIcon inspect
     ImageEditor openOnClass:self andSelector:#settingsIcon
     Icon flushCachedIcons
    "

    <resource: #image>

    ^Icon
        constantNamed:#'SettingsDialog class settingsIcon'
        ifAbsentPut:[(Depth8Image new) width: 22; height: 22; photometric:(#palette); bitsPerSample:(#(8 )); samplesPerPixel:(1); bits:(ByteArray fromPackedString:'
@@BP#8>O#8>M  @@@@@@@@@@@@@@@@BJ%9&Y&Y&Y%R^B@@@@@@@@@@@@@@BI&J^(*J"(*JZTIIFQ$YFQ$YH@@@@@ JN#(:N#(:N#(9.V%)ZV%)ZKZP@@@HOU
4=OS4=OL3,''I0<B;/]F3*TE?@@BC5MKR4-KM,\^:.MJ:-;21/:1D_@@@ =SR4-KM3<+K/<J01,R20+R+QG0@@HOT4-KR/,^?-\[H1(Q^WW"1*4Q<@@BC5MKR
,-CO1<>/+(X*F1(S,Z1D_@@@ =SR4-J:2,[J0X  TU!!BE+F$GS!!=@HOT,+WK.\J/28\&TV=EKAV1RELG  BC5L7H0-KB/HT TV-CKA8>TG@=_H8M ;[H1+::
-;8\TWECKBD9)T=0]R40AXOE2;F5-;:/G6)EKBD?ZF5KV#ITJ0M>''*B_''ZF]''5,(KA8)X8ENU#XR@ )L^1$YFQ$YFQ$WBA PE@QMVSDON82S@@@@@@@@@@@@
@@A_@F)IU#DQMWH@@@@@@@@@@@@@@@@@@F1JWC\LMJI''^ @@@@@@@@@@@@@@@F9GU28ROGI%+VY4@@@@@@@@@@@@@GIFXC(IL9(@^VR*X''\@@@@@@@@@@@AU
IPXKP@@@@@A3XZ4#@@@@@@@@@@@@# 8AK90@@@@@@GX"T @a') ; colorMapFromArray:#[0 0 0 0 60 60 0 84 84 4 45 45 21 20 15 38 38 38 38 192 192 43 43 43 45 45 45 45 151 151 46 99 99 46 147 147 46 151 151 47 47 47 47 88 88 47 151 151 48 48 48 48 144 144 48 145 145 56 53 38 57 57 49 59 52 40 62 56 43 65 65 53 65 65 65 67 67 54 68 68 68 70 70 70 72 64 47 72 72 35 72 72 72 73 69 48 73 73 73 74 74 74 80 44 10 80 62 43 81 81 24 84 132 132 85 85 76 86 86 36 86 86 86 87 87 49 87 87 77 87 193 193 88 88 88 91 101 101 92 193 193 94 117 117 97 97 97 97 193 193 98 195 195 99 120 120 101 75 31 101 107 89 101 193 193 102 193 193 103 103 103 104 86 69 106 193 193 107 120 120 107 121 121 109 109 109 110 96 70 111 78 42 111 119 119 113 113 60 113 113 113 115 115 115 116 116 56 116 116 116 116 145 145 118 147 147 119 121 85 121 150 150 122 151 151 122 152 148 124 124 124 124 146 139 124 151 149 127 118 78 127 119 78 128 128 128 129 120 112 133 157 157 135 216 216 137 137 137 139 207 207 140 206 206 141 141 141 141 201 201 141 202 202 142 136 75 142 201 201 143 128 88 143 143 88 144 144 144 144 204 204 145 85 24 146 126 100 147 82 13 148 83 17 148 86 24 148 123 91 148 127 98 149 122 89 150 150 150 151 151 151 152 152 152 152 155 155 153 129 100 154 154 154 156 156 156 156 199 199 157 157 157 158 158 158 159 159 159 160 160 160 162 179 179 163 161 159 163 163 162 164 151 101 164 160 156 164 163 161 165 165 150 165 165 165 168 168 168 169 169 62 171 171 171 172 172 71 174 117 58 177 177 177 178 178 89 185 174 113 186 178 115 186 186 115 189 179 116 189 189 115 189 189 132 190 190 139 192 192 142 192 192 192 193 193 154 194 194 194 196 196 155 197 197 159 200 200 168 202 202 174 202 202 202 204 204 102 205 205 97 206 206 149 210 210 144 210 210 149 213 213 103 213 213 213 215 215 164 215 215 215 227 212 124 227 213 140 227 217 124 227 220 124 227 227 124 234 156 79 236 236 197 239 214 144 243 201 148 243 243 146 243 243 155 244 244 147 245 232 157 247 165 83 248 215 149 248 231 149 255 170 86 255 227 156 255 228 156 255 229 156 255 230 156 255 231 156 255 233 168 255 234 156 255 235 156 255 235 193 255 236 156 255 237 156 255 238 156 255 239 156 255 239 168 255 240 156 255 240 168 255 241 156 255 242 156 255 242 168 255 243 156 255 244 156 255 244 168 255 245 156 255 245 193 255 246 156 255 247 156 255 248 156 255 248 168 255 249 156 255 250 156 255 250 168 255 251 156 255 251 168 255 252 156 255 253 156 255 253 168 255 255 156 255 255 168 255 255 193 255 255 200]; mask:((ImageMask new) width: 22; height: 22; photometric:(#blackIs0); bitsPerSample:(#(1 )); samplesPerPixel:(1); bits:(ByteArray fromPackedString:'O<@@_>@@???@??? ???0???0???0???0???0???8???8???<???<???<???<???8@B? @A?0@C?8@G=<@G0<@G0\') ; yourself); yourself]
!

settingsIconOld
    <resource: #programImage>

    ^ self settingsIconOld.
! !

!SettingsDialog class methodsFor:'defaults'!

defaultIcon
    <resource: #programImage>

    ^ ToolbarIconLibrary stxSettings24x24Icon
!

defaultSettingsApplicationList
    ^ #(          
        #('Language'                    #'AbstractSettingsApplication::LanguageSettingsAppl'            )
        #('Keyboard Mappings'           #'AbstractSettingsApplication::KbdMappingSettingsAppl'          )
        #('ViewStyle'                   #'AbstractSettingsApplication::MiscDisplaySettingsAppl'         )
        #('ViewStyle/Style'             #'AbstractSettingsApplication::StyleSettingsAppl'               )
        #('ViewStyle/Other'             #'AbstractSettingsApplication::MiscDisplay2SettingsAppl'        )
        #('ViewStyle/Fonts'             #'AbstractSettingsApplication::FontSettingsAppl'                )
        #('System Messages'             #'AbstractSettingsApplication::SystemMessageSettingsAppl'       )
        #('Memory'                      #'AbstractSettingsApplication::MemorySettingsAppl'              )
        #('Processor & Scheduler'       #'AbstractSettingsApplication::ProcessorSchedulerSettingsAppl'  )
        #('Tools'                       #'AbstractSettingsApplication::ToolsSettingsAppl'               )
        #('Tools/Editor'                #'AbstractSettingsApplication::EditSettingsAppl'                )
        #('Tools/Syntax Color'          #'AbstractSettingsApplication::SyntaxColorSettingsAppl'         )
        #('Tools/Code Format'           #'AbstractSettingsApplication::SourceCodeFormatSettingsAppl'    )
        #('Tools/CodeGenerator'         #'AbstractSettingsApplication::CodeGeneratorSettingsAppl'       )
        #('Tools/System Browser'        #'AbstractSettingsApplication::SystemBrowserSettingsAppl'       )
        #('Tools/Changes'               #'AbstractSettingsApplication::ChangeFileSettingsAppl'          )
        #('Tools/Debugger'              #'AbstractSettingsApplication::DebuggerSettingsAppl'            )
        #('Tools/Compiler'              #'AbstractSettingsApplication::GeneralCompilerSettingsAppl'     )
        #('Tools/Compiler/ByteCode'     #'AbstractSettingsApplication::ByteCodeCompilerSettingsAppl'    )
        #('Tools/Compiler/STC'          #'AbstractSettingsApplication::STCCompilerSettingsAppl'         )
        #('Tools/Compiler/Build'        #'AbstractSettingsApplication::BuildSettingsAppl'         )
        #('Source Code Management'      #'AbstractSettingsApplication::SourceCodeManagementSettingsAppl')
        #('Printer'                     #'AbstractSettingsApplication::PrinterSettingsAppl'             )
        #('Display'                     #'AbstractSettingsApplication::DisplaySettingsAppl'             )
        #('Communication'               nil                                                         #communicationIcon)

        #('Communication/Logging'       #'AbstractSettingsApplication::CommunicationLoggingSettingsAppl')
        #('Communication/RDoIt'         #'AbstractSettingsApplication::RDoItServerSettingsAppl'         )
        #('Communication/OSI Protocol'  #'AbstractSettingsApplication::OsiSettingsAppl'                 )
        #('Communication/Misc'          #'AbstractSettingsApplication::MiscCommunicationSettingsAppl'   )
        #('Communication/SQLServer'     #'AbstractSettingsApplication::SQLServerSettingsAppl'           )
        #('Communication/HTTPServer'    #'AbstractSettingsApplication::HTTPStartServerSettingsApplication'   )
    ) asOrderedCollection

    "Modified: / 16-12-2002 / 18:12:28 / penk"
    "Modified: / 27-10-2010 / 10:17:55 / cg"
!

expandParents

    ^ true
!

resourcePackName
    "return the name which is used as the fileNameBase of my resource file.
     Here, use the same resources as the Launcher"

    ^ AbstractLauncherApplication resourcePackName
! !

!SettingsDialog class methodsFor:'interface specs'!

windowSpec
    "This resource specification was automatically generated
     by the UIPainter of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the UIPainter may not be able to read the specification."

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

    <resource: #canvas>

    ^ 
     #(FullSpec
        name: windowSpec
        window: 
       (WindowSpec
          label: 'Settings Dialog'
          name: 'Settings Dialog'
          min: (Point 10 10)
          bounds: (Rectangle 0 0 789 642)
          menu: mainMenu
          icon: defaultIcon
        )
        component: 
       (SpecCollection
          collection: (
           (VariableHorizontalPanelSpec
              name: 'VariableHorizontalPanel1'
              layout: (LayoutFrame 0 0.0 0 0.0 0 1.0 0 1.0)
              showHandle: true
              snapMode: both
              component: 
             (SpecCollection
                collection: (
                 (ViewSpec
                    name: 'Box1'
                    component: 
                   (SpecCollection
                      collection: (
                       (HierarchicalListViewSpec
                          name: 'HierarchicalListView1'
                          layout: (LayoutFrame 0 0.0 0 0.0 0 1.0 -34 1)
                          model: selectedItem
                          hasHorizontalScrollBar: true
                          hasVerticalScrollBar: true
                          listModel: applicationList
                          useIndex: false
                          highlightMode: label
                          showLeftIndicators: false
                          indicatorSelector: doIndicatorClick:
                        )
                       (HorizontalPanelViewSpec
                          name: 'HorizontalPanel2'
                          layout: (LayoutFrame 0 0.0 -34 1 0 1.0 0 1)
                          visibilityChannel: loadAndSaveSettingsItemVisible
                          horizontalLayout: fitSpace
                          verticalLayout: center
                          horizontalSpace: 3
                          verticalSpace: 3
                          component: 
                         (SpecCollection
                            collection: (
                             (ActionButtonSpec
                                label: 'Save'
                                name: 'Button3'
                                translateLabel: true
                                tabable: true
                                model: saveSettingsWithoutAskingForFile
                                extent: (Point 126 22)
                              )
                             (ActionButtonSpec
                                label: 'Load From...'
                                name: 'Button2'
                                translateLabel: true
                                tabable: true
                                model: loadSettingsFromFile
                                extent: (Point 126 22)
                              )
                             )
                           
                          )
                        )
                       )
                     
                    )
                  )
                 (ViewSpec
                    name: 'Box2'
                    component: 
                   (SpecCollection
                      collection: (
                       (ViewSpec
                          name: 'DisabledSettingsBox'
                          layout: (LayoutFrame 0 0 0 0 0 1 0 1)
                          initiallyInvisible: true
                          component: 
                         (SpecCollection
                            collection: (
                             (LabelSpec
                                label: 'whyDisabledLabel'
                                name: 'whyDisabledLabel'
                                layout: (LayoutFrame 0 0 0 0 0 1 0 1)
                                translateLabel: true
                                labelChannel: whyDisabledInfoHolder
                              )
                             )
                           
                          )
                        )
                       (SubCanvasSpec
                          name: 'SubCanvas'
                          layout: (LayoutFrame 0 0.0 0 0.0 0 1.0 -34 1)
                          level: -1
                          hasHorizontalScrollBar: false
                          hasVerticalScrollBar: false
                          clientHolder: subCanvasApplicationHolder
                          createNewBuilder: false
                          postBuildCallback: postBuildSubCanvas:
                        )
                       (HorizontalPanelViewSpec
                          name: 'HorizontalPanel1'
                          layout: (LayoutFrame 0 0.0 -34 1 0 1.0 0 1)
                          horizontalLayout: fitSpace
                          verticalLayout: center
                          horizontalSpace: 3
                          verticalSpace: 3
                          component: 
                         (SpecCollection
                            collection: (
                             (ActionButtonSpec
                                label: 'Apply'
                                name: 'Apply'
                                translateLabel: true
                                tabable: true
                                model: doSave
                                enableChannel: enableOK
                                extent: (Point 168 22)
                              )
                             (ActionButtonSpec
                                label: 'Discard'
                                name: 'Reload'
                                translateLabel: true
                                tabable: true
                                model: doReload
                                enableChannel: enableReload
                                extent: (Point 169 22)
                              )
                             (ActionButtonSpec
                                label: 'Help'
                                name: 'Help'
                                translateLabel: true
                                tabable: true
                                model: help
                                enableChannel: enableHelp
                                extent: (Point 169 22)
                              )
                             )
                           
                          )
                        )
                       )
                     
                    )
                  )
                 )
               
              )
              handles: (Any 0.3375 1.0)
            )
           )
         
        )
      )
! !

!SettingsDialog class methodsFor:'menu actions'!

askForFileAndSaveSettings
    |fileName resources|

    resources := self classResources.

    fileName := Dialog 
        requestFileNameForSave:(resources string:'Save Settings in File') 
        default:'settings.stx'
        ok:(resources string:'Save') 
        abort:(resources string:'Cancel') 
        pattern:'*.stx'
        fromDirectory:'.'.

    fileName size ~~ 0 ifTrue:[
        "not canceled"
        self saveSettingsTo:fileName.
    ]

    "Modified: / 27-10-2010 / 11:24:54 / cg"
!

saveSettingsAsDefaultSettings
    | file |

    file := (Filename currentDirectory construct:UserPreferences defaultSettingsFilename) asAbsoluteFilename.
    self saveSettingsTo:file.
!

saveSettingsTo:file
    [
        UserPreferences current saveIn:file.
    ] on:StreamError do:[:ex|
        self warn:(self resources 
                stringWithCRs:'Cannot save settings into: %1\(%2)' 
                with:(file asString) 
                with:ex description).
    ]
!

saveSettingsWithoutAskingForFile
    self saveSettingsAsDefaultSettings.
! !

!SettingsDialog class methodsFor:'menu specs'!

mainMenu
    "This resource specification was automatically generated
     by the MenuEditor of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the MenuEditor may not be able to read the specification."

    "
     MenuEditor new openOnClass:SettingsDialog andSelector:#mainMenu
     (Menu new fromLiteralArrayEncoding:(SettingsDialog mainMenu)) startUp
    "

    <resource: #menu>

    ^ 
     #(Menu
        (
         (MenuItem
            label: '&File'
            translateLabel: true
            submenu: 
           (Menu
              (
               (MenuItem
                  label: '&Load From...'
                  itemValue: loadSettingsFromFile
                  translateLabel: true
                  isVisible: loadAndSaveSettingsItemVisible
                )
               (MenuItem
                  label: '-'
                  isVisible: loadAndSaveSettingsItemVisible
                )
               (MenuItem
                  label: 'Save'
                  itemValue: saveSettingsWithoutAskingForFile
                  translateLabel: true
                  isVisible: loadAndSaveSettingsItemVisible
                )
               (MenuItem
                  label: '&Save As...'
                  itemValue: askForFileAndSaveSettings
                  translateLabel: true
                  isVisible: loadAndSaveSettingsItemVisible
                )
               (MenuItem
                  label: '-'
                  isVisible: loadAndSaveSettingsItemVisible
                )
               (MenuItem
                  label: 'E&xit'
                  itemValue: closeRequest
                  translateLabel: true
                )
               )
              nil
              nil
            )
          )
         )
        nil
        nil
      )
! !

!SettingsDialog methodsFor:'accessing'!

requestor
    "return the 'requestor' of the SettingsDialog"

    ^ requestor
!

requestor:something
    requestor := something.
!

rootItem
    ^ self applicationList root.

    "Created: / 03-11-2007 / 14:23:01 / cg"
!

rootItemLabel
    ^ self rootItem label.

    "Modified: / 03-11-2007 / 14:23:18 / cg"
!

rootItemLabel:aString
    self rootItem label:aString.

    "Modified: / 03-11-2007 / 14:23:16 / cg"
!

showRoot:aBoolean
    self applicationList showRoot:aBoolean
! !

!SettingsDialog methodsFor:'actions'!

changeLabelTo:aLabel application:aApplication
    | item |

    item := self getItemByApplication:aApplication.
    item notNil ifTrue:[
        item label:aLabel.
        item changed.
    ].
!

destroyAll
    self applicationList do:[:applItem |
        |application|

        applItem isCategory ifFalse:[
            (application := applItem application) notNil ifTrue:[
                application close.
            ].
        ].
    ].
!

doIndicatorClick:index 
    "handle a click on the indicator"

    (self applicationList at:index) toggleExpand
!

doReload
    |item|

    item := self selectedItem value.
    (item isNil or:[item isCategory or:[item application isNil]]) ifTrue:[
        ^ self
    ].
    item application readSettings
!

doSave
    | item |

    item := self selectedItem value.
    (item isNil or:[item isCategory or:[item application isNil]]) ifTrue:[ ^ self].
    self withWaitCursorDo:[
        item application saveSettingsIfUnsavedChangesArePresent.
    ].
!

expandItemForApplication:aApplication
    | item |

    item := self applicationList detect:[:item | item application = aApplication] ifNone:[nil].
    item notNil ifTrue:[
        item expand
    ].
!

help
    |item |

    item := self selectedItem value.
    (item isNil or:[item isCategory or:[item application isNil]]) ifTrue:[ ^ self].
    item application help.
!

reopenLauncher
    self requestor notNil ifTrue:[
        self requestor reopenLauncher.
    ] ifFalse:[
        NewLauncher current reopenLauncher.
    ].
!

sendLoadRequestToAll
    self applicationList do:[:applItem | 
        (applItem isCategory not and:[applItem application notNil]) ifTrue:[
            applItem application readSettings.
        ]
    ].

    "Modified: / 24-08-2010 / 18:41:36 / sr"
!

sendSaveRequestToAll
    self applicationList do:[ : applItem |
        (applItem isCategory not and:[applItem application notNil]) ifTrue:[
            applItem application saveRequest ifFalse:[
                ^ false
            ].
        ].
    ].
    ^ true
!

sendSaveRequestToCurrent
    | item |

    item := self selectedItem value.
    (item isNil or:[item isCategory or:[item application isNil]]) ifTrue:[ ^ true].
    self withWaitCursorDo:[
        ^ item application saveRequest.
    ].
    ^ true
! !

!SettingsDialog methodsFor:'application list access'!

addApplClass:aClass forApplication:anApplication label:aLabel expand:aBoolean
    "add an application to the settingsTree, take the icon from the application classes defaultIcon method"

    | class |

    class := Smalltalk classNamed:aClass.
    class isNil ifTrue:[
        ('SettingsDialog [warning]:cant find ', aClass asString, ' for register') infoPrintCR.
        ^ nil
    ].
    class autoload.
    ^ self createItemForApplication:anApplication class:aClass label:aLabel icon:class defaultIcon expandParent:aBoolean replaceExisting:false
!

addApplClass:aClass withName:aName
   "add an application to the settingsTree, take the icon from the application classes defaultIcon method"

    ^ self addApplClass:aClass withName:aName icon:nil.
!

addApplClass:aClass withName:aName expand:aBoolean
   "add an application to the settingsTree, take the icon from the application classes defaultIcon method"

    ^ self addApplClass:aClass withName:aName icon:nil expand:aBoolean.
!

addApplClass:aClass withName:aName icon:icon
    ^ self addApplClass:aClass withName:aName icon:icon expand:false
!

addApplClass:aClass withName:aName icon:icon expand:doExpand
    |applicationLabel locIcon locClass|

"/    (self applicationNames includes:aName) ifTrue:[
"/        ^ self getItemForName:aName.
"/    ].
    
    aClass isNil ifTrue:[ 
        self addIcon:icon forCategory:aName.
        ^ self 
    ].
    self addCategoriesFor:aName.

    locIcon := icon.
    locClass := Smalltalk classNamed:aClass.
    locClass isNil ifTrue:[
        ('SettingsDialog [warning]:cant find ', aClass asString, ' for register') infoPrintCR.
        ^ nil
    ].
    locClass autoload.

    (locIcon isNil and:[locClass respondsTo:#defaultIcon]) ifTrue:[ locIcon := locClass defaultIcon].
    applicationLabel := (aName asCollectionOfSubstringsSeparatedBy:$/) last.

    ^ self 
        createItemName:aName 
        label:applicationLabel 
        class:locClass 
        icon:locIcon 
        expandParent:doExpand 
        replaceExisting:false
!

addIcon:aIcon forCategory:aCategoryName
    "add a new category Icon for a category"

    |existingItem|

    self categoryIcons at:aCategoryName put:aIcon.
    existingItem := self getItemForName:aCategoryName.
    existingItem notNil ifTrue:[
        existingItem icon:aIcon
    ].
!

applicationNames
    ^ self applicationList collect:[:item | item nameString].
!

categoryIcons
    categoryIcons isNil ifTrue:[
        categoryIcons := Dictionary new.
    ].
    ^ categoryIcons 
!

childrenApplicationItemFor:aApplication fromItem:anItem
    anItem application = aApplication ifTrue:[
        ^ anItem
    ].
    anItem hasChildren ifTrue:[
        anItem children do:[:aChildItem |
            ^ self childrenApplicationItemFor:aApplication fromItem:aChildItem.
        ].
    ].
    ^ nil
!

getAllChildrenAppsForApplication:aApplication 
    ^ self getAllChildrenAppsForApplication:aApplication matchBlock:nil
!

getAllChildrenAppsForApplication:aApplication childrenClass:aClass 
    |matchBlock|

    matchBlock := [:aSubApplication | aSubApplication class == aClass ].
    ^ self getAllChildrenAppsForApplication:aApplication matchBlock:matchBlock
!

getAllChildrenAppsForApplication:aApplication matchBlock:aMatchBlock
    | children |

    children := self getAllChildrenByApplication:aApplication.
    aMatchBlock notNil ifTrue:[
        children := children select:[:aChild | aMatchBlock value:(aChild application)]
    ].
    ^ children collect:[:item | item application].
!

getAllChildrenByApplication:aApplication
    | item |

    item := self getItemByApplication:aApplication.
    item isNil ifTrue:[ ^ #()].
    ^ item children.
!

getApplicationsByClass:aClass
    | classEntries|

    self applicationList isNil ifTrue:[
        self initialize.
    ].
    classEntries := self applicationList select:[:aEntry | aEntry application class == aClass].
    ^ classEntries collect:[:aItem | aItem application].
!

getItemByApplication:anApplication
    |item|

    self applicationList do:[:anItem |
        item := self childrenApplicationItemFor:anApplication fromItem:anItem.
        item notNil ifTrue:[ ^ item ].
    ].
    ^ nil
!

getNameOfApplication:aApplication
    |item|

    self applicationList do:[:anItem |
        item := self childrenApplicationItemFor:aApplication fromItem:anItem.
        item notNil ifTrue:[ ^ item nameString].
    ].
    ^ ''
!

installSettingsEntries:aCollection
    aCollection do:[ :entry| 
        self installSettingsEntry:entry.
    ].
!

installSettingsEntry:entry
    |applName applClassOrNil iconHolderOrNil|

    applName := entry at:1.
    applClassOrNil := entry at:2.
    iconHolderOrNil := entry at:3 ifAbsent:nil.
    self addApplClass:applClassOrNil withName:applName icon:iconHolderOrNil value
!

parentApplicationFor:aApplication
    |item parent|

    item := (self getItemByApplication:aApplication).
    parent := item parent.
    parent notNil ifTrue:[
        ^ parent application.
    ].
    ^ nil
!

remApplClassByName:aName 
    "remove an application from the settingsTree"

    |item|

    item := self getItemForName:aName.
    self removeItem:item 
!

removeAllApplicationsByClass:aClass
    |apps|

    apps := self getApplicationsByClass:aClass.
    apps do:[:aApp |
        self removeByApplication:aApp.
    ].
!

removeAllSubApplicationsFor:aApp
    |apps|

    apps := self getAllChildrenAppsForApplication:aApp.
    apps do:[:aApp |
        self removeByApplication:aApp.
    ].
!

removeApplicationByClass:aClass
    | classEntries|

    self applicationList isNil ifTrue:[
        self initialize.
    ].
    classEntries := self applicationList select:[:aEntry | aEntry application class == aClass].
    classEntries do:[:aItem |
        self removeItem:aItem.
    ].
!

removeApplicationFromTree:aApplication
    | classEntries|

    self applicationList isNil ifTrue:[
        self initialize.
    ].
    classEntries := self applicationList select:[:aEntry | aEntry application == aApplication].
    classEntries do:[:aItem |
        self removeItem:aItem.
    ].
    self applicationList removeAllFoundIn:classEntries.
!

removeByApplication:aApplication
    | item |

    item := self getItemByApplication:aApplication.
    item notNil ifTrue:[
        self removeItem:item.
    ]
!

sortSubApplicationsFor:anApplication
    |item|

    item := self getItemByApplication:anApplication.
    item sort:[:a :b| a label < b label].
! !

!SettingsDialog methodsFor:'application list access-private'!

addCategoriesFor:aName
    "create all the categories"

    (aName includes:$/) ifFalse:[ ^ self].
    self treeName:aName butLast:1 do:[:currentLabel :currentName| 
        |length icon|

        currentLabel notEmpty ifTrue:[
            icon := self categoryIcons at:currentName ifAbsent:[nil].
            self createItemName:currentName label:currentLabel icon:icon replaceExisting:true.
        ]
    ].
!

createItemForApplication:anApplication class:aClass label:aLabel icon:anIcon expandParent:expandParent replaceExisting:doReplaceExisting
    "create an application item in my application list"

    | newItem parent locIcon item|

    parent := self getItemByApplication:anApplication.
    parent isNil ifTrue:[
        ^ nil
    ].

    doReplaceExisting ifTrue:[
        parent children notNil ifTrue:[
            (item := parent children detect:[ :anItem | anItem application == anApplication] ifNone:[nil]) notNil ifTrue:[
                ^ item.
            ]
        ]
    ].

    newItem := self class itemClass new.  
    "/ newItem label:(resources string:aLabel) "allBold".
    newItem label:aLabel "allBold".
    newItem nameString:(anApplication itemPathName , aLabel).
    newItem applicationClass:aClass.
    locIcon := anIcon.
    locIcon isNil ifTrue:[ locIcon := self class defaultSettingsAppIcon].
    newItem icon:locIcon.
    parent add:newItem.
    expandParent ifTrue:[
        parent expand.
    ].
    ^ newItem
!

createItemName:aNameString label:aLabel class:aClass icon:anIcon
    ^ self createItemName:aNameString label:aLabel class:aClass icon:anIcon expandParent:false
!

createItemName:aNameString label:aLabel class:aClass icon:anIcon expandParent:expandParent
    ^ self
        createItemName:aNameString 
        label:aLabel 
        class:aClass 
        icon:anIcon 
        expandParent:expandParent
        replaceExisting:true
!

createItemName:aNameString label:untranslatedLabel class:aClass icon:anIcon expandParent:expandParent replaceExisting:doReplaceExisting
    "create an application item in my application list"

    |newItem parentsName parent locIcon item|

    (aNameString includes:$/) ifTrue:[
        parentsName := aNameString copyTo:(aNameString lastIndexOf:$/)-1.
        parent := self getItemForName:parentsName.
    ].
    parent isNil ifTrue:[
        parent := self rootItem.
    ].

    doReplaceExisting ifTrue:[
        parent children notNil ifTrue:[
            (item := parent children detect:[ :anItem | anItem nameString = aNameString] ifNone:[nil]) notNil ifTrue:[
                ^ item.
            ]
        ]
    ].

    newItem := self class itemClass new.  
    "/ newItem label:(resources string:aLabel) "allBold".
    newItem label:(resources string:untranslatedLabel) "allBold".
    newItem nameString:aNameString.
    newItem applicationClass:aClass.
    locIcon := anIcon.
    locIcon isNil ifTrue:[ locIcon := self class defaultSettingsAppIcon].
    newItem icon:locIcon.
    parent add:newItem.
    expandParent ifTrue:[
        parent expand.
    ].
    ^ newItem

    "Modified: / 03-11-2007 / 14:23:32 / cg"
!

createItemName:aNameString label:aLabel icon:anIcon
    "create an application item in my application list"

    self createItemName:aNameString label:aLabel class:nil icon:anIcon expandParent:false
!

createItemName:aNameString label:aLabel icon:anIcon replaceExisting:doReplaceExisting
    "create an application item in my application list"

    self createItemName:aNameString label:aLabel class:nil icon:anIcon expandParent:false replaceExisting:doReplaceExisting
!

getItemForName:aName
    | searchItem|

    searchItem := self rootItem.
    self treeName:aName do:[:currentLabel :currentName|
        searchItem children isNil ifTrue:[ ^ nil].
        searchItem := searchItem children detect:[: item | item nameString = currentName] ifNone:[nil].
        searchItem isNil ifTrue:[ ^ nil].
    ].
    ^ searchItem

    "Modified: / 03-11-2007 / 14:23:28 / cg"
!

removeItem:anItem 
    "remove an application from the settingsTree"

    |parent remParent|

    anItem application release.

    parent := anItem parent.
    parent isNil ifTrue:[" already removed " ^ self].
    parent remove:anItem.
    
    [parent isCategory and:[parent children isEmptyOrNil]] whileTrue:[
        remParent := parent.
        parent := remParent parent.
        parent remove:remParent.
    ].
    self applicationList remove:anItem ifAbsent:[nil].
    self selectedItem value:parent.
!

treeName:aName butLast:last do:aBlock 
    | tree  length|

    tree := aName asCollectionOfSubstringsSeparatedBy:$/.
    tree from:1 to:(tree size - last) keysAndValuesDo:[:idx :aCategory| | itemName |
        length := 0.
        tree from:1 to:idx keysAndValuesDo:[:idx2 : el | 
            length := length + el size.
            idx2 > 1 ifTrue:[length := length + 1].
        ].
        itemName := aName copyTo:length.
        aBlock value:aCategory value:itemName.
    ].
!

treeName:aName do:aBlock
    self treeName:aName butLast:0 do:aBlock
! !

!SettingsDialog methodsFor:'aspects'!

applicationList
    applicationList isNil ifTrue:[
        applicationList := SettingsDialog::HierarchicalApplicationList new.
    ].
    ^ applicationList.
!

enableHelp
    enableHelp isNil ifTrue:[
        enableHelp := true asValue.
    ].
    ^ enableHelp.
!

enableOK
    enableOK isNil ifTrue:[
        enableOK := true asValue.
    ].
    ^ enableOK.
!

enableReload
    enableReload isNil ifTrue:[
        enableReload := true asValue.
    ].
    ^ enableReload.
!

loadAndSaveSettingsItemVisible
    "for now, only show these if I have a requestor 
     (i.e. I am a full settings Dialog as opposed to a DebugView, FileBrowser or other
     settingsDialog. I think this has to be reworked..."

    ^ requestor notNil "/ loadAndSaveSettingsItemVisible
!

selectedItem
    selectedItem isNil ifTrue:[
        selectedItem := ValueHolder new.
        selectedItem addDependent:self.
    ].
    ^ selectedItem.
!

subCanvasApplicationHolder
    subCanvasApplicationHolder isNil ifTrue:[
        subCanvasApplicationHolder := ValueHolder new.
    ].
    ^ subCanvasApplicationHolder.
!

whyDisabledInfoHolder
    whyDisabledInfoHolder isNil ifTrue:[
        whyDisabledInfoHolder := '' asValue.
    ].
    ^ whyDisabledInfoHolder.

    "Created: / 25-01-2007 / 17:13:50 / cg"
! !

!SettingsDialog methodsFor:'change & update'!

modifiedChanged
    |changedObject|

    changedObject := self subCanvasApplicationHolder value modifiedChannel.
    self enableOK value:(changedObject value).
    self enableReload value:(changedObject value).
!

selectItemWithClass:aClass
    |entry|

    entry := applicationList root
                recursiveDetect:[:entry | entry applicationClass = aClass].
    entry makeVisible.
    self selectedItem value:entry

    "Created: / 29-10-2010 / 11:54:13 / cg"
!

selectionChanged
    |item oldAppl noApp lbl window app itemLabel|

    self selectedItem value == lastSelection  ifTrue:[ ^ self].

    oldAppl := self subCanvasApplicationHolder value.
    oldAppl notNil ifTrue:[
        oldAppl saveRequest ifFalse:[
            lastSelection notNil ifTrue:[
            self
                enqueueMessage:#value 
                for:[ self selectedItem value:lastSelection ]
                arguments:#().
            ].
            ^ self
        ].
        oldAppl modifiedChannel removeDependent:self.
    ].

    item := self selectedItem value.
    lastSelection := item.
    noApp := (item isNil) ifTrue:true ifFalse:[item isCategory].
    self enableOK value:(noApp not).
    self enableReload value:(noApp not).
    self enableHelp value:(noApp not).

    window := self window.
    window notNil ifTrue:[
        lbl := self rootItemLabel ? 'Settings Dialog'.
        item notNil ifTrue:[
            itemLabel := (item label copyReplaceAll:(Character cr) with:(Character space)).
            itemLabel ~= lbl ifTrue:[
                lbl := lbl , ' [', itemLabel, ']'.
            ]
        ].
        window label:lbl.
    ].

    builder notNil ifTrue:[ |box|
        "hide the DisabledSettingsBox"
        box := builder componentAt:#DisabledSettingsBox.
        box notNil ifTrue:[
            box beInvisible; lower.
        ].
    ].

    noApp ifTrue:[
        self subCanvasApplicationHolder value:nil.
        ^ self
    ].

    app := item application.
    app isNil ifTrue:[
        app := item applicationClass basicNew.
        (app isEnabledInSettingsDialog:self) ifFalse:[
            "raise the DisabledSettingsBox above the settings window"
            self whyDisabledInfoHolder value:('These Settings are currently disabled, because\',app whyDisabledInSettingsDialogInfo) withCRs.
            builder notNil ifTrue:[ |box|
                box := builder componentAt:#DisabledSettingsBox.
                box notNil ifTrue:[
                    box beVisible; raise.
                ].
            ].
            self subCanvasApplicationHolder value:nil.
            ^ self
        ].

        item application:app.
        app basicInitialize.
        app settingsDialog:self.
    ].
    self withWaitCursorDo:[
        app initialize.
        app readSettings.
    ].
    app modifiedChannel addDependent:self.
    self subCanvasApplicationHolder value:app.
    self modifiedChanged.

    "Modified: / 29-10-2010 / 11:51:13 / cg"
!

update:something with:aParameter from:changedObject
    |subCanvasApplication|

    changedObject == self selectedItem ifTrue:[
        super update:something with:aParameter from:changedObject.
        self selectionChanged.
        ^ self.
    ].
    subCanvasApplication := self subCanvasApplicationHolder value.
    (subCanvasApplication notNil 
    and:[changedObject == subCanvasApplication modifiedChannel]) ifTrue:[
        self modifiedChanged.
    ].
    super update:something with:aParameter from:changedObject
! !

!SettingsDialog methodsFor:'initialization & release'!

closeRequest
    "This is a hook method generated by the Browser.
     It will be invoked when your app/dialog-window is about to be
     closed (this method has a chance to suppress the close).
     See also #closeDownViews, which is invoked when the close is really done."

    | req |

    (self sendSaveRequestToCurrent == false) ifTrue:[^ self].
    self destroyAll.

    req := self requestor.
    req notNil ifTrue:[
        req settingsClosed.
    ].
    super closeRequest
!

createRootItem
    |rootItem lbl|

    resources := AbstractLauncherApplication resources.
    rootItem := self class itemClass new.
    lbl := resources string:'Settings'.
    rootItem label:lbl "allBold".
    rootItem nameString:lbl.
    rootItem icon:self class settingsIcon.
    ^ rootItem

    "Created: / 03-11-2007 / 14:22:01 / cg"
!

initialize
    |rootItem|

    rootItem := self createRootItem.
    self applicationList root:rootItem; showRoot:true "false".
    self selectedItem value:rootItem.
    super initialize.

    "Modified: / 03-11-2007 / 14:22:18 / cg"
!

postBuildSubCanvas:aWidget
    aWidget keepClientView:true. 
!

postOpenWith:aBuilder
    self rootItem expand.
    super postOpenWith:aBuilder.

    "Modified: / 03-11-2007 / 14:23:25 / cg"
! !

!SettingsDialog methodsFor:'menu actions'!

askForFileAndSaveSettings
    self class askForFileAndSaveSettings.
!

loadSettingsFromFile
    "restore settings from a settings-file."

    "a temporary kludge - we need a central systemSettings object for this,
     which can be saved/restored with a single store/read."

    |fileName transcript launcher|

    fileName := Dialog 
        requestFileName:(resources string:'Load Settings from File') 
        default:'settings.stx'
        ok:(resources string:'Load') 
        abort:(resources string:'Cancel') 
        pattern:'*.stx'
        fromDirectory:nil.

    (fileName size == 0) ifTrue:[
        "/ canceled
        ^ self
    ].

    self withWaitCursorDo:[
        Smalltalk fileIn:fileName.

        "/ Transcript current topView model reOpen.

        transcript := Transcript current.
        (transcript notNil and:[transcript isExternalStream not]) ifTrue:[
            launcher := transcript application.
            launcher notNil ifTrue:[
                launcher reopenLauncher.
            ]
        ].
        self sendLoadRequestToAll.
    ].

    "Modified: / 08-09-2006 / 19:23:15 / cg"
!

saveSettingsWithoutAskingForFile
    self class saveSettingsWithoutAskingForFile.
! !

!SettingsDialog::HierarchicalApplicationList::ApplicationItem methodsFor:'accessing'!

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

    ^ application
!

application:something
    "set the value of the instance variable 'application' (automatically generated)"

    application := something.
!

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

    ^ applicationClass
!

applicationClass:something
    "set the value of the instance variable 'applicationClass' (automatically generated)"

    applicationClass := something.
!

label:something
    "reset the with in pixels..."
    width := nil.
    label := something.
!

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

    ^ nameString
!

nameString:something
    "set the value of the instance variable 'nameString' (automatically generated)"

    nameString := something.
! !

!SettingsDialog::HierarchicalApplicationList::ApplicationItem methodsFor:'menu'!

middleButtonMenu

    <resource: #programMenu >

    |application|

    (application := self application) isNil ifTrue:[ ^ nil].
    ^ application settingsDialogPopUpMenu
! !

!SettingsDialog::HierarchicalApplicationList::ApplicationItem methodsFor:'printing & storing'!

displayString

    ^ super printString, ' [', (label isText ifTrue:[label string] ifFalse:[label]) ,']'
!

printOn:aStream
    aStream 
        nextPutAll:self class nameWithoutPrefix;
        nextPutAll:' ';
        nextPutAll:self label ? '???'.

    "Created: / 24-08-2010 / 18:36:17 / sr"
!

printString

    ^ super printString, ' [', (label isText ifTrue:[label string] ifFalse:[label]) ,']'
! !

!SettingsDialog::HierarchicalApplicationList::ApplicationItem methodsFor:'queries'!

canCollapse
    "the rootItem is cannot be collapsed"
    ^ self isRootItem not
!

isCategory

    ^ self applicationClass isNil
! !

!SettingsDialog class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libtool/SettingsDialog.st,v 1.90 2010-10-29 10:00:48 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libtool/SettingsDialog.st,v 1.90 2010-10-29 10:00:48 cg Exp $'
! !

SettingsDialog initialize!