Question: In smalltalk / squeak language write a counter that uses the MVC - tutorial . I cannot get it working to save my life. Here
In smalltalksqueak language write a counter that uses the MVCtutorial I cannot get it working to save my life. Here is the tutorial Case Study: The Counter View
Here's a simple example where:
The model an instance of Counter is a simple numeric value
The view an instance of CounterView shows the value of what is
represented by Counter
And the controller an instance of CounterController implements a
menu allowing one to increment or decrement the Counters value.
Change set for running this demo in Squeak:
smalltalk
Model subclass: #Counter
instanceVariableNames: 'value
classVariableNames:
poolDictionaries:
category: MVCTutorial'!
MouseMenuController subclass: #CounterController
instanceVariableNames:
classVariableNames:
poolDictionaries:
category: MVCTutorial'!
View subclass: #CounterView
instanceVariableNames:
classVariableNames:
poolDictionaries:
category: MVCTutorial'!
CounterView commentStamp: BG : prior:
This is the view for the first example from the MVC tutorial from Kranser and Pope.
To open the simple counter window, execute this statement:
CounterView open
The menu of the counter is called with the left mouse button.
To open the enhanced couter window, execute this statement:
CounterView openWithGraphicalButtons!
Counter methodsFor: 'accessing' stamp: BG :
value
Answer the current value of the receiver"
value!
Counter methodsFor: 'accessing' stamp: BG :
value: aNumber
Initialize the counter to value aNumber"
value : aNumber.
self changed. to update displayed value
Counter methodsFor: 'initializerelease' stamp: BG :
initialize
Set the intitial value to
self value:
Counter methodsFor: 'operations' stamp: BG :
decrement
subtract from the value of the counter
self value: value
Counter methodsFor: 'operations' stamp: BG :
increment
add to the value of the counter
self value: value
Counter class methodsFor: 'instance creation' stamp: BG :
new
Answer an initialized instance of the receiver"
super new initialize return a new instance"!
CounterController methodsFor: 'control defaults' stamp: BG :
isControlActive
super isControlActive & sensor blueButtonPressed not!
CounterController methodsFor: 'initializerelease' stamp: BG :
initialize
"initialize a manue of commands for changing the value of the model"
super initialize.
self redButtonMenu:
PopUpMenu labels: 'incrementdecrement withCRs
redButtonMessages: #increment decrement
here we use the red button menu
CounterController methodsFor: 'menu messages' stamp: BG :
decrement
subtract from the value of the counter
self model decrement!
CounterController methodsFor: 'menu messages' stamp: BG :
increment
add to the value of the counter
self model increment!
CounterController methodsFor: 'pluggable menus' stamp: BG :
getPluggableYellowButtonMenu: shiftKeyState
nil!
CounterView methodsFor: 'controller access' stamp: BG :
defaultControllerClass
answer the class of a typically useful controller"
CounterController.
CounterView methodsFor: 'displaying' stamp: BG :
displayView
box pos displayText
box : self insetDisplayBox.
pos : box origin @ box extent y rounded
displayText :value: self model value printString asDisplayText.
displayText foregroundColor: Color black
backgroundColor: Color white.
displayText displayAt: pos.!
CounterView methodsFor: 'updating' stamp: BG :
update: aParameter
"simply redislplay everything"
self display.!
CounterView class methodsFor: 'instance creation' stamp: BG :
open
open a view for a new counter"
select and execute this comment to test this method:"
"CounterView open"
aCounterView topView
aCounterView : CounterView new
model: Counter new.
aCounterView borderWidth:
aCounterView insideColor: Color white.
topView : StandardSystemView new
label: 'Counter'.
topView minimumSize: @ ;
maximumSize: @
topView addSubView: aCounterView.
topView controller open
CounterView class methodsFor: 'instance creation' stamp: BG :
openWithGraphicalButtons
open a view for a new counter"
select and execute this comment to test this method:"
"CounterView openWithGraphicalButtons"
aCounterView topView incrButton decrButton
incrSwitchView decrSwitchView dt textStyle
aCounterView : CounterView new
model: Counter new.
aCounterView borderWidthLeft: right: top: bottom:
aCounterView insideColor: Color white.
topView : ColorSystemView new
label: 'Counter'.
topView minimumSize
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
