SettingsDialog.st
author penk
Thu, 10 Jul 2003 15:03:38 +0200
changeset 5043 4ea8ed8336f4
parent 5026 0eae1f1848ba
child 5050 566eab79b16b
permissions -rw-r--r--
checkin from browser

"{ Package: 'stx:libtool' }"

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

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

HierarchicalItem subclass:#ApplicationItem
	instanceVariableNames:'application applicationClass icon label nameString'
	classVariableNames:''
	poolDictionaries:''
	privateIn:SettingsDialog::HierarchicalApplicationList
!

!SettingsDialog class methodsFor:'documentation'!

documentation

"
    This is an application which presents a hierarchy of settings applications 

    [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:'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:#'AbstractLauncherApplication::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]
! !

!SettingsDialog class methodsFor:'defaults'!

expandParents

    ^ true
! !

!SettingsDialog class methodsFor:'initialize'!

initialize
    AbstractSettingsApplication autoload.
! !

!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)
          #max: #(#Point 1024 768)
          #bounds: #(#Rectangle 16 42 816 662)
          #menu: #mainMenu
        )
        #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)
                          #horizontalLayout: #fitSpace
                          #verticalLayout: #center
                          #horizontalSpace: 3
                          #verticalSpace: 3
                          #component: 
                         #(#SpecCollection
                            #collection: #(
                             #(#ActionButtonSpec
                                #label: 'Save'
                                #name: 'Button3'
                                #translateLabel: true
                                #model: #saveSettingsWithoutAskForFile
                                #extent: #(#Point 84 22)
                              )
                             #(#ActionButtonSpec
                                #label: 'Save as...'
                                #name: 'Button1'
                                #translateLabel: true
                                #model: #saveSettingsAndAskForFile
                                #extent: #(#Point 84 22)
                              )
                             #(#ActionButtonSpec
                                #label: 'Load from...'
                                #name: 'Button2'
                                #translateLabel: true
                                #model: #loadSettingsFromFile
                                #extent: #(#Point 85 22)
                              )
                             )
                           
                          )
                        )
                       )
                     
                    )
                  )
                 #(#ViewSpec
                    #name: 'Box2'
                    #component: 
                   #(#SpecCollection
                      #collection: #(
                       #(#SubCanvasSpec
                          #name: 'SubCanvas'
                          #layout: #(#LayoutFrame 0 0.0 0 0.0 0 1.0 -34 1)
                          #level: -1
                          #hasHorizontalScrollBar: false
                          #hasVerticalScrollBar: false
                          #clientHolder: #subCanvasApplicationHolder
                          #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
                                #model: #doSave
                                #enableChannel: #enableOK
                                #extent: #(#Point 171 22)
                              )
                             #(#ActionButtonSpec
                                #label: 'Discard'
                                #name: 'Reload'
                                #translateLabel: true
                                #model: #doReload
                                #enableChannel: #enableReload
                                #extent: #(#Point 171 22)
                              )
                             #(#ActionButtonSpec
                                #label: 'Help'
                                #name: 'Help'
                                #translateLabel: true
                                #model: #help
                                #enableChannel: #enableHelp
                                #extent: #(#Point 171 22)
                              )
                             )
                           
                          )
                        )
                       )
                     
                    )
                  )
                 )
               
              )
              #handles: #(#Any 0.3375 1.0)
            )
           )
         
        )
      )
! !

!SettingsDialog class methodsFor:'menu actions'!

saveSettingsAndAskForFile
    |fileName resources|

    resources := self classResources.

    fileName := Dialog 
        requestFileName:(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.
    ]
!

saveSettingsTo:file

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

saveSettingsWithoutAskForFile

    | file |
    file := (Filename currentDirectory construct:'settings.stx') asAbsoluteFilename.
    self saveSettingsTo:file.
! !

!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
                )
               #(#MenuItem
                  #label: 'Save As...'
                  #itemValue: #saveSettingsIntoFile
                  #translateLabel: true
                )
               #(#MenuItem
                  #label: '-'
                )
               #(#MenuItem
                  #label: 'Exit'
                  #itemValue: #closeRequest
                  #translateLabel: true
                )
               )
              nil
              nil
            )
          )
         )
        nil
        nil
      )
! !

!SettingsDialog methodsFor:'accessing'!

requestor
    "return the 'requestor' of the SettingsDialog"

    ^ requestor
!

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

    requestor := something.
! !

!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 |
        (applItem isCategory not and:[applItem application notNil]) ifTrue:[
            applItem application close.
        ].
    ].
    ^ true
!

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
        ]
    ].
    ^ true
!

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 a application in the to the settingsTree, take the icon from the application class 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 a application in the to the settingsTree, take the icon from the application class defaultIcon method"

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

addApplClass:aClass withName:aName expand:aBoolean
" add a application in the to the settingsTree, take the icon from the application class 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 categorie "

    |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
!

remApplClassByName:aName 
" remove a 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.
    ].
!

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.
    ]



! !

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

addCategoriesFor:aName

    " create all the categories before application itself
    "

    (aName includes:$/) not ifTrue:[ ^ 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:aIcon 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:[ :aItem | aItem 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 := aIcon.
    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:aIcon

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

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

createItemName:aNameString label:aLabel class:aClass icon:aIcon expandParent:expandParent replaceExisting:doReplaceExisting

    " create an application item in my application list 
    "
    | newItem parent locIcon item|

    aNameString ~= aLabel ifTrue:[
        parent := self getItemForName:(aNameString copyTo:(aNameString size - aLabel size - 1)).
    ].
    parent isNil ifTrue:[
        parent := self applicationList root.
    ].

    doReplaceExisting ifTrue:[
        parent children notNil ifTrue:[
            (item := parent children detect:[ :aItem | aItem label = aLabel] ifNone:[nil]) notNil ifTrue:[
                ^ item.
            ]
        ]
    ].

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

createItemName:aNameString label:aLabel icon:aIcon

    " create an application item in my application list 
    "
    self createItemName:aNameString label:aLabel class:nil icon:aIcon expandParent:false
!

createItemName:aNameString label:aLabel icon:aIcon replaceExisting:doReplaceExisting

    " create an application item in my application list 
    "
    self createItemName:aNameString label:aLabel class:nil icon:aIcon expandParent:false replaceExisting:doReplaceExisting
!

getItemForName:aName

    | searchItem|

    searchItem := self applicationList root.
    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
!

removeItem:aItem 
" remove a application from the settingsTree"

    |parent remParent|

    aItem application release.

    parent := aItem parent.
    parent isNil ifTrue:[" already removed " ^ self].
    parent remove:aItem.
    
    [parent isCategory and:[parent children isEmptyOrNil]] whileTrue:[
        remParent := parent.
        parent := remParent parent.
        parent remove:remParent.
    ].
    self applicationList remove:aItem 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.
!

selectedItem

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

subCanvasApplicationHolder

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

!SettingsDialog methodsFor:'change & update'!

modifiedChanged

    |changedObject|

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

selectionChanged

    | item oldAppl noApp lbl window app|

    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.
    item isNil ifTrue:[ noApp := true] ifFalse:[noApp := item isCategory].
    self enableOK value:(noApp not).
    self enableReload value:(noApp not).
    self enableHelp value:(noApp not).
    window := self window.
    window notNil ifTrue:[
        lbl := 'Settings Dialog'.
        item notNil ifTrue:[
            lbl := lbl , ' [', (item label copyReplaceAll:(Character cr) with:(Character space)), ']'.
        ].
        window label:lbl.
    ].
    noApp ifTrue:[
        self subCanvasApplicationHolder value:nil.
        ^ self
    ].
    (app := item application) isNil ifTrue:[
        item application:(app := item applicationClass basicNew).
        app basicInitialize.
        app settingsDialog:self.
    ].
    app initialize.
    app readSettings.
    app modifiedChannel addDependent:self.
    self subCanvasApplicationHolder value:app.
    self modifiedChanged.
!

update:something with:aParameter from:changedObject
    "Invoked when an object that I depend upon sends a change notification."

    "stub code automatically generated - please change as required"

    changedObject == self selectedItem ifTrue:[
        super update:something with:aParameter from:changedObject.
        self selectionChanged.
        ^ self.
    ].
    self subCanvasApplicationHolder value notNil ifTrue:[
        changedObject == self subCanvasApplicationHolder value 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
!

initialize

    |rootItem|

    resources := AbstractLauncherApplication resources.
    rootItem := self class itemClass new.
    rootItem label:(resources string:'Settings') "allBold".
    rootItem nameString:'Settings'.
    rootItem icon:self class settingsIcon.
    self applicationList root:rootItem.
    self applicationList showRoot:true.
    self selectedItem value:rootItem.
    super initialize.
!

postBuildSubCanvas:aWidget

    aWidget keepClientView:true. 
!

postOpenWith:aBuilder

    self applicationList root expand.
    super postOpenWith:aBuilder.
! !

!SettingsDialog methodsFor:'menu actions'!

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|

    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.
        self sendLoadRequestToAll.
    ].
!

saveSettingsAndAskForFile
    self class saveSettingsAndAskForFile.
!

saveSettingsWithoutAskForFile
    self class saveSettingsWithoutAskForFile.
! !

!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.
!

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

    ^ icon
!

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

    icon := something.
!

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

    ^ label
!

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

    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:'printing & storing'!

displayString

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

printString

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

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

isCategory

    ^ self applicationClass isNil
! !

!SettingsDialog class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libtool/SettingsDialog.st,v 1.32 2003-07-10 13:03:38 penk Exp $'
! !

SettingsDialog initialize!