Question: In smalltalk / squeak language write a counter that uses the MVC - tutorial . I cannot get it working to save my life. Here

In smalltalk/squeak language write a counter that uses the MVC-tutorial. 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 `Counter`'s value.
Change set for running this demo in Squeak:
```smalltalk
Model subclass: #Counter
instanceVariableNames: 'value '
classVariableNames: ''
poolDictionaries: ''
category: 'MVC-Tutorial'!
MouseMenuController subclass: #CounterController
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MVC-Tutorial'!
View subclass: #CounterView
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MVC-Tutorial'!
!CounterView commentStamp: 'BG 10/27/200315:43' prior: 0!
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 10/27/200312:51'!
value
" Answer the current value of the receiver"
^value! !
!Counter methodsFor: 'accessing' stamp: 'BG 10/27/200312:52'!
value: aNumber
" Initialize the counter to value aNumber"
value := aNumber.
self changed. " to update displayed value "!!
!Counter methodsFor: 'initialize-release' stamp: 'BG 10/27/200312:51'!
initialize
" Set the intitial value to 0."
self value: 0.!!
!Counter methodsFor: 'operations' stamp: 'BG 10/27/200312:52'!
decrement
" subtract 1 from the value of the counter "
self value: value -1.!!
!Counter methodsFor: 'operations' stamp: 'BG 10/27/200312:52'!
increment
" add 1 to the value of the counter "
self value: value +1.!!
!Counter class methodsFor: 'instance creation' stamp: 'BG 10/27/200312:53'!
new
" Answer an initialized instance of the receiver"
^super new initialize " return a new instance"! !
!CounterController methodsFor: 'control defaults' stamp: 'BG 10/27/200313:01'!
isControlActive
^super isControlActive & sensor blueButtonPressed not! !
!CounterController methodsFor: 'initialize-release' stamp: 'BG 10/27/200312:59'!
initialize
"initialize a manue of commands for changing the value of the model"
super initialize.
self redButtonMenu:
(PopUpMenu labels: 'increment\decrement' withCRs)
redButtonMessages: #(increment decrement)
" here we use the red button menu "!!
!CounterController methodsFor: 'menu messages' stamp: 'BG 10/27/200313:00'!
decrement
" subtract 1 from the value of the counter "
self model decrement! !
!CounterController methodsFor: 'menu messages' stamp: 'BG 10/27/200313:00'!
increment
" add 1 to the value of the counter "
self model increment! !
!CounterController methodsFor: 'pluggable menus' stamp: 'BG 10/27/200313:17'!
getPluggableYellowButtonMenu: shiftKeyState
^ nil! !
!CounterView methodsFor: 'controller access' stamp: 'BG 10/27/200313:04'!
defaultControllerClass
" answer the class of a typically useful controller"
^CounterController.
!!
!CounterView methodsFor: 'displaying' stamp: 'BG 10/27/200314:34'!
displayView
| box pos displayText |
box := self insetDisplayBox.
pos := box origin +(4 @ ((box extent y /3) rounded)).
displayText :=('value: ', self model value printString) asDisplayText.
displayText foregroundColor: Color black
backgroundColor: Color white.
displayText displayAt: pos.! !
!CounterView methodsFor: 'updating' stamp: 'BG 10/27/200313:03'!
update: aParameter
"simply redislplay everything"
self display.! !
!CounterView class methodsFor: 'instance creation' stamp: 'BG 10/27/200313:09'!
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: 2.
aCounterView insideColor: Color white.
topView := StandardSystemView new
label: 'Counter'.
topView minimumSize: 80 @ 40;
maximumSize: 120 @ 100.
topView addSubView: aCounterView.
topView controller open
!!
!CounterView class methodsFor: 'instance creation' stamp: 'BG 10/27/200315:41'!
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: 1 right: 2 top: 2 bottom: 2.
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!