'From Cuis 6.0 [latest update: #5116] on 28 April 2022 at 7:53:07 am'!
!classDefinition: #MyPref category: #'System-Support'!
Object subclass: #MyPref
	instanceVariableNames: 'name description value setter informer'
	classVariableNames: 'PrefDict'
	poolDictionaries: ''
	category: 'System-Support'!
!MyPref commentStamp: '<historical>' prior: 0!
I describe and hold a Preference.

name: a Symbol
description: a String
setter: a two-argument closure, 	
	e.g.  [ :pref :newValue | 
		  (newValue isKindOf: Boolean)
			ifTrue: [ prev value: newValue
			ifFalse: [ self inform: pref name , 'needs a Boolean value']. ]
informer: a one-argument closure,
	e.g. [ :pref | pref triggerEvent: #valueChange with: pref ]

E.g. 
  (MyPref name: #DoYouLikeMe description: 'Like me? true or false') inspect.
In inspector, try 
  self setter value: self value: 3.
	!


!MyPref methodsFor: 'accessing' stamp: 'KenD 4/28/2022 07:44:15'!
informer
	
	^ informer ! !

!MyPref methodsFor: 'accessing' stamp: 'KenD 4/28/2022 07:47:46'!
name
	
	^ name! !

!MyPref methodsFor: 'accessing' stamp: 'KenD 4/28/2022 07:44:02'!
setter
	
	^ setter ! !

!MyPref methodsFor: 'accessing' stamp: 'KenD 4/28/2022 07:44:24'!
value
	
	^ value ! !


!MyPref methodsFor: 'initialization' stamp: 'KenD 4/28/2022 07:47:35'!
name: aSymbol  description: aString value: initialValue setter: twoArgProc informer: oneArgProc

	(twoArgProc isKindOf: BlockClosure)
		ifFalse:  [ self error: 'Expected a closure for setting ', twoArgProc printString ].
	(twoArgProc numArgs = 2)
		ifFalse: [ self error:
					'Expected setter closure w 2 args [ :property :newValue | ..] ', 
					twoArgProc printString ].
				
	(oneArgProc isKindOf: BlockClosure)
		ifFalse:  [ self error: 'Expected a closure for informing of value change ', oneArgProc printString ].
	(oneArgProc numArgs = 1)
		ifFalse: [ self error:
					'Expected informet closure w 1 arg [ :property | ..] ', 
					oneArgProc printString ].
				
	name := aSymbol asSymbol. "allow a string"
	description := aString.
	value := initialValue.
	setter := twoArgProc.
	informer := oneArgProc.
	
	self class preferencesDictionary at: name asSymbol put: self.
	! !


!MyPref methodsFor: 'private' stamp: 'KenD 4/28/2022 07:45:35'!
value: newValue
	
	"Note Bene: unchecked and uninformed.  Use my setter!!"
	value := newValue ! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'MyPref class' category: #'System-Support'!
MyPref class
	instanceVariableNames: ''!

!MyPref class methodsFor: 'instance creation' stamp: 'KenD 4/28/2022 07:36:14'!
name: aSymbol description: aString 

	^ self new 
		name: aSymbol 
		description: aString value: true 
		setter:  [ :pref :newValue | 
		  		(newValue isKindOf: Boolean)
					ifTrue: [ pref value: newValue . pref informer value: pref ]
					ifFalse: [ pref inform: pref name , ' needs a Boolean value']
			]
		informer: [ :pref | pref triggerEvent: #valueChange with: pref ]! !

!MyPref class methodsFor: 'instance creation' stamp: 'KenD 4/28/2022 07:33:26'!
name: aSymbol description: aString value: initialValue setter: twoArgProc informer: oneArgProc

	^ self new 
		name: aSymbol 
		description: aString 
		value: initialValue 
		setter: twoArgProc 
		informer: oneArgProc! !


!MyPref class methodsFor: 'accessing' stamp: 'KenD 4/28/2022 07:27:21'!
preferencesDictionary

 	PrefDict ifNil: [ PrefDict := Dictionary new ].
	
	^ PrefDict! !
