'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 11 May 2008 at 11:51 pm'!
!classDefinition: #Person category: #'LightWidgets-Examples'!
Object subclass: #Person
	instanceVariableNames: 'name birthday '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Examples'!

!Person commentStamp: '<historical>' prior: 0!
Just a sample model for an example of LightWidgets programming!

!classDefinition: #PersonView category: #'LightWidgets-Examples'!
CompositeLW subclass: #PersonView
	instanceVariableNames: 'nameLW birthdayLW ageLW'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Examples'!

!PersonView commentStamp: '<historical>' prior: 0!
An example of LightWidgets programming!


!LightWidget methodsFor: 'view of a model or target' stamp: 'jmv 5/11/2008 23:12'!
aspect: aSymbol adaptor: aSymbol2 action: aSymbol3 adaptor: aSymbol4
	"The aspect is what part of the model (or target widget) we are concerned with. 
	It is a message sent to the target to get the object to show to the user."
	"The aspect adaptor is a message sent to the aspect to adapt it for viewing. 
	For example, the aspect could be a numeric value, but it is shown as a String."
	"The action is the message to send to the model (or target widget) 
	when the user enters some value or invokes an action."
	"The actionAdaptor is a message that is sent to the value that the user entered 
	to adapt it to be sent to the model (or target widget), as an argument in the action."
	
	aspect _ aSymbol.
	aspectAdaptor _ aSymbol2.
	action _ aSymbol3.
	actionAdaptor _ aSymbol4.
	self modelChanged! !

!LightWidget methodsFor: 'view of a model or target' stamp: 'jmv 5/11/2008 23:35'!
performActionWith: data

	| argument |
	action isNil ifTrue: [
		^self].
	argument _ actionAdaptor isNil
		ifTrue: [data]
		ifFalse: [data perform: actionAdaptor].
	target perform: action with: argument! !


!NameEntryLW class methodsFor: 'examples' stamp: 'jmv 5/11/2008 23:04'!
openExample
	| model view |
	model _ Person new.
	view _ self new.
	view beMainViewOn: model.
	view openInWorld! !


!Person methodsFor: 'accessing' stamp: 'jmv 5/11/2008 22:49'!
age
	^((Date today subtractDate: birthday) / 365.25) truncated! !

!Person methodsFor: 'accessing' stamp: 'jmv 5/11/2008 23:27'!
age: years
	birthday _ Date
		newDay: birthday dayOfMonth 
		month: birthday monthIndex 
		year: Date today year - years.
	self triggerEvent: #selfChanged ! !

!Person methodsFor: 'accessing' stamp: 'jmv 5/11/2008 22:34'!
birthday
	^birthday! !

!Person methodsFor: 'accessing' stamp: 'jmv 5/11/2008 23:38'!
birthday: aDate
	birthday _ aDate ifNil: [Date today].
	self triggerEvent: #selfChanged ! !

!Person methodsFor: 'initialize-release' stamp: 'jmv 5/11/2008 23:16'!
initialize
	super initialize.
	name _ 'Unnamed'.
	birthday _ Date today! !


!PersonView methodsFor: 'drawing' stamp: 'jmv 5/11/2008 22:56'!
drawOn: aCanvas

	aCanvas fillRectangle: bounds color: Color blue muchLighter! !

!PersonView methodsFor: 'initialization' stamp: 'jmv 5/11/2008 23:41'!
initialize
	"initialize the state of the receiver"
	super initialize.
	bounds _ 10@10 extent: 400@300.
	
	nameLW _ TextEntryFieldLW new
		aspect: #name;
		action: #name:;
		bounds: (30@100 extent: 200@40).
	
	birthdayLW _ TextEntryFieldLW new
		aspect: #birthday adaptor: #asString action: #birthday: adaptor: #asDate;
		bounds: (30@150 extent: 200@40).
	
	ageLW _ TextEntryFieldLW new
		aspect: #age adaptor: #asString action: #age: adaptor: #asNumber;
		bounds: (30@200 extent: 200@40).
	
	self addMorphBack: nameLW; addMorphBack: birthdayLW; addMorphBack: ageLW.
	
	ActiveHand navigationFocus: nameLW! !

!PersonView methodsFor: 'updating' stamp: 'jmv 5/11/2008 23:15'!
updateView

	nameLW target: target.
	birthdayLW target: target.
	ageLW target: target! !


!PersonView class methodsFor: 'examples' stamp: 'jmv 5/11/2008 23:04'!
openExample
	| model view |
	model _ Person new.
	view _ self new.
	view beMainViewOn: model.
	view openInWorld! !


!StackedPageSample1LW class methodsFor: 'examples' stamp: 'jmv 5/11/2008 23:42'!
openExample
	self new openInWorld! !


!TextEntryFieldLW methodsFor: 'user actions' stamp: 'jmv 5/11/2008 23:37'!
backspaceEntered
	| h t string |
	string _ self value.
	(cursorPosition = 1 or:[string isEmpty]) ifTrue:[^self].
	self hasSelection 
		ifTrue:[ 
			h _ string copyFrom: 1 to: selection first -1.
			t _ string copyFrom: selection last + 1 to: string size.
			cursorPosition _  h size + 1.
			self clearSelection]
		ifFalse:[
			h _ string copyFrom: 1 to: cursorPosition - 2.
			t _  string copyFrom: cursorPosition to: string size.
			self moveCursorBack].
	string _ h , t.
	self performActionWith: string! !

!TextEntryFieldLW methodsFor: 'user actions' stamp: 'jmv 5/11/2008 23:37'!
characterEntered:  char
	| string h t newValue valid |
	string _ self value.
	self hasSelection
		ifTrue:[ h _string copyFrom: 1 to: selection first -1. 
				t _ string copyFrom: selection last to: string size. 
				h _ h , (Array with: char)]
		ifFalse:[	h _ string. t _ Array with: char].
	newValue _ h , t.
	entryMask notNil
				ifTrue:[valid _ entryMask validate: newValue.  valid ifFalse:[ ^ Beeper beep]].
	cursorPosition _ cursorPosition + 1.
	self performActionWith: newValue
	
	

	 
	
	! !


!VolumeSliderLW class methodsFor: 'instance creation' stamp: 'jmv 5/11/2008 23:05'!
openExample
	| model view |
	model _ VolumeControl new.
	view _ self new.
	view beMainViewOn: model.
	^ view openInWorld
! !


!StackedPageSample1LW class reorganize!
('examples' openExample)
!


!PersonView class reorganize!
('examples' openExample)
!


!PersonView reorganize!
('drawing' drawOn:)
('initialization' initialize)
('updating' updateView)
!

!classDefinition: #Person category: #'LightWidgets-Examples'!
Object subclass: #Person
	instanceVariableNames: 'name birthday'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Examples'!
