Object subclass: #LSContainerModel instanceVariableNames: 'stack queue history operations lastResult capacity' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-StacksQueues'! Morph subclass: #LSContainerApp instanceVariableNames: 'model window panel info detail buttons nextValue' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-StacksQueues'! Morph subclass: #LSContainerPanel instanceVariableNames: 'app' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-StacksQueues'! SystemWindow subclass: #LSContainerWindow instanceVariableNames: 'fixedExtent' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-StacksQueues'! !LSContainerModel methodsFor: 'tutorial'! initialize super initialize. stack := OrderedCollection new. queue := OrderedCollection new. history := OrderedCollection new. operations := 0. capacity := 6. lastResult := 'Ready: both containers are empty' ! ! !LSContainerModel methodsFor: 'tutorial'! seedExample capacity < 3 ifTrue: [self record: 'example rejected: capacity is below 3'. ^ false]. stack := OrderedCollection withAll: #(1 2 3). queue := OrderedCollection withAll: #(1 2 3). self record: 'Loaded identical arrivals: 1, 2, 3'. ^ true ! ! !LSContainerModel methodsFor: 'tutorial'! stackItems ^ stack copy ! ! !LSContainerModel methodsFor: 'tutorial'! queueItems ^ queue copy ! ! !LSContainerModel methodsFor: 'tutorial'! stackSize ^ stack size ! ! !LSContainerModel methodsFor: 'tutorial'! queueSize ^ queue size ! ! !LSContainerModel methodsFor: 'tutorial'! history ^ history collect: [:line | line copy] ! ! !LSContainerModel methodsFor: 'tutorial'! operations ^ operations ! ! !LSContainerModel methodsFor: 'tutorial'! capacity ^ capacity ! ! !LSContainerModel methodsFor: 'tutorial'! lastResult ^ lastResult copy ! ! !LSContainerModel methodsFor: 'tutorial'! record: text operations := operations + 1. lastResult := text copy. history addLast: operations printString, ' ', text. history size > 6 ifTrue: [history removeFirst] ! ! !LSContainerApp methodsFor: 'tutorial'! initialize super initialize. self extent: 960@675; color: (Color fromString: '#EEF3F8'). model := LSContainerModel new. nextValue := 1. buttons := Dictionary new. self addLabel: 'Container Lab' at: 24@12 size: 26. self addLabel: ('Checkpoint ', self stage printString, ' / 8 - ', self stageTitle) at: 26@49 size: 14. self addLabel: 'Same arrivals. Different departures. Two collection contracts.' at: 26@77 size: 12. self buildToolbar. panel := LSContainerPanel new app: self; position: self position + (20@185). self addMorph: panel. info := self addLabel: '' at: 25@620 size: 14. detail := self addLabel: '' at: 25@647 size: 11. self refresh ! ! !LSContainerApp methodsFor: 'tutorial'! addLabel: text at: point size: size | label | label := StringMorph contents: text font: (TextStyle default fontOfSize: size). label color: (Color fromString: '#20364A'); position: self position + point. self addMorph: label. ^ label ! ! !LSContainerApp methodsFor: 'tutorial'! button: text at: point action: selector arguments: args | button | button := SimpleButtonMorph new label: text; target: self; actionSelector: selector; arguments: args; color: Color white; yourself. button position: self position + point. self addMorph: button. buttons at: text put: button. ^ button ! ! !LSContainerApp methodsFor: 'tutorial'! model ^ model ! ! !LSContainerApp methodsFor: 'tutorial'! window ^ window ! ! !LSContainerApp methodsFor: 'tutorial'! panel ^ panel ! ! !LSContainerApp methodsFor: 'tutorial'! nextValue ^ nextValue ! ! !LSContainerApp methodsFor: 'tutorial'! buttonNamed: text ^ buttons at: text ! ! !LSContainerApp methodsFor: 'tutorial'! open window ifNotNil: [window comeToFront. ^ self]. window := LSContainerWindow new model: self; setLabel: 'Container Lab - LearnSqueak'. window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)). window fixedExtent: 970@708. window openInWorld. self refresh. ^ self ! ! !LSContainerApp methodsFor: 'tutorial'! okToClose ^ true ! ! !LSContainerApp methodsFor: 'tutorial'! release ^ self ! ! !LSContainerApp methodsFor: 'tutorial'! windowIsClosing window := nil ! ! !LSContainerApp methodsFor: 'tutorial'! buildToolbar self button: 'Push next' at: 27@112 action: #pushNext arguments: #(). self button: 'Pop' at: 146@112 action: #pop arguments: #(). self button: 'Enqueue next' at: 291@112 action: #enqueueNext arguments: #(). self button: 'Dequeue' at: 438@112 action: #dequeue arguments: #(). self button: 'Capacity 3' at: 590@112 action: #capacity: arguments: #(3). self button: 'Capacity 6' at: 718@112 action: #capacity: arguments: #(6). self addLabel: 'A full container rejects insertions. Reducing capacity never discards items.' at: 27@151 size: 12 ! ! !LSContainerApp methodsFor: 'tutorial'! refresh info contents: model lastResult. detail contents: ('Stack: ', model stackSize printString, ' items | Queue: ', model queueSize printString, ' items', (self stage >= 5 ifTrue: [' | Next value: ', nextValue printString] ifFalse: ['']), (self stage >= 7 ifTrue: [' | Capacity each: ', model capacity printString] ifFalse: [' | View shows up to six items per container'])). panel changed. self changed ! ! !LSContainerApp class methodsFor: 'tutorial'! open ^ self new open ! ! !LSContainerWindow methodsFor: 'tutorial'! fixedExtent: point fixedExtent := point. super extent: point ! ! !LSContainerWindow methodsFor: 'tutorial'! extent: point super extent: (fixedExtent ifNil: [point]) ! ! !LSContainerPanel methodsFor: 'tutorial'! initialize super initialize. self extent: 920@420; color: Color white ! ! !LSContainerPanel methodsFor: 'tutorial'! app: anApp app := anApp ! ! !LSContainerPanel methodsFor: 'tutorial'! text: text on: canvas at: point size: size color: shade canvas drawString: text at: self position + point font: (TextStyle default fontOfSize: size) color: shade ! ! !LSContainerPanel methodsFor: 'tutorial'! drawOn: canvas | model ink muted values shown y x | super drawOn: canvas. model := app model. ink := Color fromString: '#20364A'. muted := Color fromString: '#667C90'. self text: 'STACK / LIFO' on: canvas at: 26@17 size: 19 color: ink. self text: 'Last in, first out' on: canvas at: 26@47 size: 12 color: muted. self text: 'QUEUE / FIFO' on: canvas at: 290@17 size: 19 color: ink. self text: 'First in, first out' on: canvas at: 290@47 size: 12 color: muted. canvas fillRectangle: ((self position + (24@89)) extent: 218@273) color: (Color fromString: '#F0EDF9'). values := model stackItems. shown := values last: (values size min: 6). shown withIndexDo: [:value :index | y := 322 - ((index - 1) * 38). canvas fillRectangle: ((self position + (38@y)) extent: 158@33) color: (Color fromString: '#7762B9'). self text: value printString on: canvas at: 105@(y + 6) size: 16 color: Color white. index = shown size ifTrue: [self text: '< top' on: canvas at: 199@(y + 7) size: 10 color: ink]]. values isEmpty ifTrue: [self text: '(empty)' on: canvas at: 85@215 size: 15 color: muted]. self text: 'push / pop use the top' on: canvas at: 26@377 size: 12 color: muted. canvas fillRectangle: ((self position + (286@96)) extent: 600@81) color: (Color fromString: '#E5F4F0'). values := model queueItems. shown := values first: (values size min: 6). shown withIndexDo: [:value :index | x := 299 + ((index - 1) * 96). canvas fillRectangle: ((self position + (x@110)) extent: 85@50) color: (Color fromString: '#198E77'). self text: value printString on: canvas at: (x + 34)@125 size: 16 color: Color white]. values isEmpty ifTrue: [self text: '(empty)' on: canvas at: 532@126 size: 15 color: muted]. self text: 'FRONT / remove' on: canvas at: 289@76 size: 10 color: ink. self text: 'REAR / add' on: canvas at: 797@76 size: 10 color: ink. self text: 'dequeue <- front rear <- enqueue' on: canvas at: 290@185 size: 12 color: muted. app stage >= 6 ifTrue: [self drawHistoryOn: canvas] ifFalse: [self text: 'A view asks questions; the model changes collections.' on: canvas at: 290@257 size: 14 color: ink. self text: 'Stack: addLast: / removeLast' on: canvas at: 290@301 size: 14 color: muted. self text: 'Queue: addLast: / removeFirst' on: canvas at: 290@334 size: 14 color: muted]. app stage >= 7 ifTrue: [self text: ('CAPACITY ', model capacity printString, ' / container') on: canvas at: 26@68 size: 10 color: muted] ! ! !LSContainerApp methodsFor: 'tutorial'! stage ^ 7 ! ! !LSContainerApp methodsFor: 'tutorial'! stageTitle ^ 'Enforce a capacity before mutating' ! ! !LSContainerModel methodsFor: 'tutorial'! push: value value isInteger ifFalse: [self record: 'push rejected: use an integer'. ^ false]. stack size >= capacity ifTrue: [self record: 'push rejected: full'. ^ false]. stack addLast: value. self record: 'push ', value printString, ' -> accepted'. ^ true ! ! !LSContainerModel methodsFor: 'tutorial'! pop | value | stack isEmpty ifTrue: [self record: 'pop -> empty (nil)'. ^ nil]. value := stack removeLast. self record: 'pop -> ', value printString. ^ value ! ! !LSContainerModel methodsFor: 'tutorial'! enqueue: value value isInteger ifFalse: [self record: 'enqueue rejected: use an integer'. ^ false]. queue size >= capacity ifTrue: [self record: 'enqueue rejected: full'. ^ false]. queue addLast: value. self record: 'enqueue ', value printString, ' -> accepted'. ^ true ! ! !LSContainerModel methodsFor: 'tutorial'! dequeue | value | queue isEmpty ifTrue: [self record: 'dequeue -> empty (nil)'. ^ nil]. value := queue removeFirst. self record: 'dequeue -> ', value printString. ^ value ! ! !LSContainerApp methodsFor: 'tutorial'! pushNext (model push: nextValue) ifTrue: [nextValue := nextValue + 1]. self refresh ! ! !LSContainerApp methodsFor: 'tutorial'! pop model pop. self refresh ! ! !LSContainerApp methodsFor: 'tutorial'! enqueueNext (model enqueue: nextValue) ifTrue: [nextValue := nextValue + 1]. self refresh ! ! !LSContainerApp methodsFor: 'tutorial'! dequeue model dequeue. self refresh ! ! !LSContainerPanel methodsFor: 'tutorial'! drawHistoryOn: canvas | ink | ink := Color fromString: '#20364A'. self text: ('OPERATION TRACE / newest six / ', app model operations printString, ' total') on: canvas at: 290@224 size: 12 color: ink. app model history withIndexDo: [:line :index | self text: line on: canvas at: 290@(245 + ((index - 1) * 25)) size: 12 color: ink] ! ! !LSContainerModel methodsFor: 'tutorial'! capacity: anInteger (anInteger isInteger and: [anInteger between: 1 and: 6]) ifFalse: [self record: 'capacity rejected: choose 1 through 6'. ^ false]. (anInteger < (stack size max: queue size)) ifTrue: [self record: 'capacity rejected: remove items first'. ^ false]. capacity := anInteger. self record: 'capacity -> ', capacity printString. ^ true ! ! !LSContainerApp methodsFor: 'tutorial'! capacity: count model capacity: count. self refresh ! !