toc.prn - squeak

678
The joy of Smalltalk

Upload: others

Post on 05-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: toc.prn - Squeak

The joy of SmalltalkAn introduction to Smalltalk

© Ivan Tomek, 2000

Page 2: toc.prn - Squeak
Page 3: toc.prn - Squeak
Page 4: toc.prn - Squeak

Introduction to Smalltalk, VisualWorks - Table of contents Ivan Tomek 9/18/00

3

5.4 Using numbers for iteration - ‘repeat n times’5.5 Repeating a block for all numbers between a start and a stop value5.5 Repeating a block with a specified step5.7 Measuring the speed of arithmetic and other operations5.8 Declaring a new class: Currency

Page 5: toc.prn - Squeak
Page 6: toc.prn - Squeak

Introduction to Smalltalk, VisualWorks - Table of contents Ivan Tomek 9/18/00

5

Page 7: toc.prn - Squeak
Page 8: toc.prn - Squeak
Page 9: toc.prn - Squeak
Page 10: toc.prn - Squeak
Page 11: toc.prn - Squeak
Page 12: toc.prn - Squeak
Page 13: toc.prn - Squeak
Page 14: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

4

285 + (37 squared)

Smalltalk now asks 37 to execute message squared. This returns object 1369 and the code noweffectively becomes

285 + 1369

Smalltalk now asks object 285 to execute message + with argument 1369. This returns the finalresult 1654. Note that this works because all Smalltalk messages return objects, and messages can thus becombined.

The examples that you have just seen cover all possible forms of Smalltalk messages and if theygive you the impression that Smalltalk is essentially simple, you are quite right - Smalltalk is based on veryfew very powerful ideas. Unfortunately, the ease of reading and writing pieces of Smalltalk programs doesnot mean that writing significant Smalltalk programs such as a word processors or spreadsheets is very easy.Difficult task are difficult even in Smalltalk - but not as difficult as in most other languages as you will seelater.

At this point, you would probably like to proceed to other Smalltalk programs, and if you really

Page 15: toc.prn - Squeak
Page 16: toc.prn - Squeak
Page 17: toc.prn - Squeak
Page 18: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

8

1. I press the Power button, which can be interpreted as sending message ‘Initiate cooking time setting

Page 19: toc.prn - Squeak
Page 20: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

10

Figure 1.7. The opening window of Farm Launcher. Use it to select the first version of the Farm program.Farm

Page 21: toc.prn - Squeak
Page 22: toc.prn - Squeak
Page 23: toc.prn - Squeak
Page 24: toc.prn - Squeak
Page 25: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

15

3 + 5

must be treated as a message + to object 3 to do addition with object 5 as in

‘Object 3, do

Page 26: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

16

and executes it with the do it command. This sends the open message to Farm and Smalltalk starts bylooking for the definition of the

Page 27: toc.prn - Squeak
Page 28: toc.prn - Squeak
Page 29: toc.prn - Squeak
Page 30: toc.prn - Squeak
Page 31: toc.prn - Squeak
Page 32: toc.prn - Squeak
Page 33: toc.prn - Squeak
Page 34: toc.prn - Squeak
Page 35: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts

Page 36: toc.prn - Squeak
Page 37: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

27

Page 38: toc.prn - Squeak
Page 39: toc.prn - Squeak
Page 40: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

30

Figure 1.30. A very small part of VisualWorks class hierarchy tree. Concrete classes are shown in boldface,all other classes are abstract.

To find out about Smalltalk’s class hierarchy, use the System Browser. After selecting a class suchas Magnitude, select the hierarchy command in the class view <operate> menu and the text view willdisplay as in Figure 1.31, showing subclasses and superclasses and all instance variables.

Figure 1.31. System Browser showing a part of the hierarchy of class Magnitude.

The hierarchy shown in Figure 1.31 shows both abstract and concrete classes. Class Magnitude isabstract and factors out properties needed for comparison, and its subclasses include numbers, printablecharacters, date objects, and time objects. Magnitude has no instances because there is no need for such

Page 41: toc.prn - Squeak
Page 42: toc.prn - Squeak
Page 43: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

33

Exercises

1. Find and count all definitions (‘implementations’) of the following methods.a. displayOn:b. squaredc. <d. =

2. Give an example of polymorphism from the physical world.3. The three animals in the Farm world share the following messages: color, name, eat, isHungry, run,

walk. Some of them are implemented in the same way in each animal, others are not and the programexecutes them polymorphically. List each group on the basis of your intuition.

Conclusion

Page 44: toc.prn - Squeak
Page 45: toc.prn - Squeak
Page 46: toc.prn - Squeak
Page 47: toc.prn - Squeak

Introduction to Smalltalk - Chapter 1 - Object-oriented programming - essential concepts Ivan Tomek 9/17/00

Page 48: toc.prn - Squeak
Page 49: toc.prn - Squeak
Page 50: toc.prn - Squeak
Page 51: toc.prn - Squeak
Page 52: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

42

2. Repeat Exercise 1 but assign each classes instead of pieces. Each card contains the name of the class,its brief description, and a list of its functionalities. Replay the scenarios.

3.

Page 53: toc.prn - Squeak
Page 54: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

44

section. We then expand the high-level conversations from Step 1 into class-level conversationsinvolving the identified classes.As we develop the conversations, we record the responsibilities that the classes must have to implement

Page 55: toc.prn - Squeak
Page 56: toc.prn - Squeak
Page 57: toc.prn - Squeak
Page 58: toc.prn - Squeak
Page 59: toc.prn - Squeak
Page 60: toc.prn - Squeak
Page 61: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

51

We will now evaluate the desirability of the selected nouns as classes:

• Address. If the address included the name of the city, postal code, province or state, and otherinformation, we would implement it as a class. As it is, an address is just a string of alphabetic andnumeric characters and strings are already in the library. We thus remove

Page 62: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

52

1. Assign each class to an individual or a small group that will be responsible for maintaining allinformation about the class.

2. Each group prepares a CRC card (Figure 2.8) for its class. Use 5 x 8” paper index cards.3.

Page 63: toc.prn - Squeak
Page 64: toc.prn - Squeak
Page 65: toc.prn - Squeak
Page 66: toc.prn - Squeak
Page 67: toc.prn - Squeak
Page 68: toc.prn - Squeak
Page 69: toc.prn - Squeak
Page 70: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

60

3. User clicks Open.4. System opens Farm 1 user interface.

Scenario 4: User sends ‘meow’ to the cat.High-level conversation:1. User clicks cat in the Animals list.2. System displays commands understood by cat and updates the rightmost part of the window.3. User clicks meow.4.

Page 71: toc.prn - Squeak
Page 72: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

62

Identify class responsibilities

We will now examine our scenarios and expand high-level conversations into class-level conversations.

Scenario 1: User starts Farm.High-level conversation:1.

Page 73: toc.prn - Squeak
Page 74: toc.prn - Squeak
Page 75: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

65

Components:• list of commands understood• list of commands forbidden in Farm1Responsibilities •

Page 76: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

66

• Opening•

Page 77: toc.prn - Squeak
Page 78: toc.prn - Squeak
Page 79: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

69

Cow: Simulated cow. Knows the commands it understands and how to respond to them. Knows commandsthat it should not understand in Farm1. A domain object.Superclass: ObjectComponents

Page 80: toc.prn - Squeak
Page 81: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

71

Exercises

1. Repeat the example, completing all CRC cards, writing all missing scenarios, descriptions, andperforming all required tests.

2.

Page 82: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects Ivan Tomek 9/17/00

72

• Construct a Context Diagram showing external actors (outside the scope of current project) and majorparts of the system to be developed.

• Construct a Glossary of terms. This Glossary will be continuously updated during the process.

Preliminary Design:

Page 83: toc.prn - Squeak

Introduction to Smalltalk - Chapter 2 - Finding objects

Page 84: toc.prn - Squeak
Page 85: toc.prn - Squeak
Page 86: toc.prn - Squeak
Page 87: toc.prn - Squeak
Page 88: toc.prn - Squeak
Page 89: toc.prn - Squeak
Page 90: toc.prn - Squeak
Page 91: toc.prn - Squeak
Page 92: toc.prn - Squeak

Introduction to Smalltalk - Chapter 3 - Principles of Smalltalk Ivan Tomek 9/17/00

82

Dealing with simpler errors

Sooner or later, you will type and attempt to execute incorrect code. Code can be incorrect in morethan one way:

Page 93: toc.prn - Squeak
Page 94: toc.prn - Squeak
Page 95: toc.prn - Squeak
Page 96: toc.prn - Squeak
Page 97: toc.prn - Squeak
Page 98: toc.prn - Squeak
Page 99: toc.prn - Squeak

Introduction to Smalltalk - Chapter 3 - Principles of Smalltalk Ivan Tomek 9/17/00

89

'a cat' spellAgainst: 'a car' “Returns 80 - degree of similarity between the two strings.”

Whereas one-keyword messages don’t seem to cause any problems, keyword messages with multiplekeywords may be more difficult to read. Beginners often don’t understand that the multiple keywords form

Page 100: toc.prn - Squeak
Page 101: toc.prn - Squeak
Page 102: toc.prn - Squeak
Page 103: toc.prn - Squeak
Page 104: toc.prn - Squeak
Page 105: toc.prn - Squeak
Page 106: toc.prn - Squeak
Page 107: toc.prn - Squeak
Page 108: toc.prn - Squeak

Introduction to Smalltalk - Chapter 3 - Principles of Smalltalk Ivan Tomek 9/17/00

98

Figure 3.13. Debugger window with two most recently invoked methods shown on top.

To see the code of a message and the point reached in its execution, click the appropriate line inthe call stack. Since a code fragment is always referred to as

Page 109: toc.prn - Squeak
Page 110: toc.prn - Squeak
Page 111: toc.prn - Squeak
Page 112: toc.prn - Squeak
Page 113: toc.prn - Squeak
Page 114: toc.prn - Squeak
Page 115: toc.prn - Squeak
Page 116: toc.prn - Squeak
Page 117: toc.prn - Squeak
Page 118: toc.prn - Squeak
Page 119: toc.prn - Squeak
Page 120: toc.prn - Squeak
Page 121: toc.prn - Squeak
Page 122: toc.prn - Squeak
Page 123: toc.prn - Squeak
Page 124: toc.prn - Squeak
Page 125: toc.prn - Squeak
Page 126: toc.prn - Squeak
Page 127: toc.prn - Squeak
Page 128: toc.prn - Squeak
Page 129: toc.prn - Squeak
Page 130: toc.prn - Squeak
Page 131: toc.prn - Squeak
Page 132: toc.prn - Squeak
Page 133: toc.prn - Squeak
Page 134: toc.prn - Squeak
Page 135: toc.prn - Squeak
Page 136: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

126

class. To do this, open a Browser, and add a new category called for example Tests using the add …T e s t N a m e 2 2 . 0 8 4 n a m e 1 2 / F 0 9 . 9 6 T f - 0 . 0 3 4 3 T c

Page 137: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

127

This class has not yet been commented. The comment should state the purpose of the class, whatmessages are subclassResponsibility, and the type and purpose of each instance and class variable. Thecomment should also explain any unobvious aspects of the implementation.

Select the whole text and replace it with a comment. There are two styles for writing the comment.One is very antropomorphic and uses the first person as in ‘I represent a name ..’. Its advantage is that itmakes you think in terms of the class which always helps. The other style is less personal and uses the thirdperson as in ‘This class represents a name …’. This style seems less extravagant. Choose the style that youlike better and use it consistently. We will use the first style because it will force us to think as if we werethe class which seems a good idea at this point of the book. Following the template, we write

I represent a personal name consisting of a first name, a middle name, and a last name.

Instance variables

firstName <String> first namemiddleName <String> middle namelastName <String> last name

The comment seems almost unnecessary because it seems very obvious but it is an essentialpractice to use class comments. As an example, the indication that the values of variables should be String

Page 138: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

128

Figure 4.12. Browser with method template.

Let’s start with the get method for the firstName instance variable. A get message simply returnsthe value of a variable and it is standard practice that it is named with the name of the instance variable. Thedefinition is thus

firstName^ firstName

Page 139: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

129

should return ‘John’. Type this expression into a workspace and execute with print it to see that it indeeddoes.

Now that we think about it, it seems that our approach could cause a problem. A programmermight, for example, create a Name object and give it a first and last name but no middle name. If a programthen tried to print the name, it would fail on the middle name because its value would be nil and nil does notunderstand String messages. It will thus be a good idea to modify the instance creation message so that itinitializes all instance variable to empty strings with no characters in them. This is, of course, rather uselessbut at least it will not cause a crash if a variable is left unassigned.

There are several possible solutions to our problem and the simplest one is to define a newinitialization method that forces the user to enter some strings for each of the instance variables as in

Name first: ‘John’ middle: ‘Mathew’ last: ‘Smith’

This new message is obviously a class message because the receiver is the class which creates theobject. We will thus define it on the class side and put it into a new protocol called instance creation . Its

Page 140: toc.prn - Squeak
Page 141: toc.prn - Squeak
Page 142: toc.prn - Squeak
Page 143: toc.prn - Squeak
Page 144: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

134

[self <= max]

to the true object. This returns true or false. The Boolean result is then returned. Note that all subclasses ofMagnitude including Date,

Page 145: toc.prn - Squeak
Page 146: toc.prn - Squeak
Page 147: toc.prn - Squeak
Page 148: toc.prn - Squeak
Page 149: toc.prn - Squeak
Page 150: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

140

Figure 4.19. y := x := ‘John Smith’ binds x and y to the same object and both x=y and x==y are true.

As another example, consider that class True and False each have only one instance. As aconsequence, equivalence and equality of Boolean objects mean the same thing.

The check of equivalence is faster because it only compares the memory addresses of the internal

Page 151: toc.prn - Squeak
Page 152: toc.prn - Squeak
Page 153: toc.prn - Squeak
Page 154: toc.prn - Squeak
Page 155: toc.prn - Squeak
Page 156: toc.prn - Squeak
Page 157: toc.prn - Squeak
Page 158: toc.prn - Squeak
Page 159: toc.prn - Squeak
Page 160: toc.prn - Squeak
Page 161: toc.prn - Squeak
Page 162: toc.prn - Squeak

Introduction to Smalltalk - Chapter 4 - True and False objects, blocks, selection and iteration Ivan Tomek 9/17/00

152

singleton - the single instance of a class that does not allow multiple instancestruth table - a table defining a Boolean operation by listing all combinations of true and false operand

Page 163: toc.prn - Squeak

Introduction to Smalltalk - Chapter 5 - Numbers Ivan Tomek 9/17/00

153

Chapter 5 - Numbers

Overview

VisualWorks library contains many classes representing numbers of various kinds, mainly becausecomputers internally represent and process different kinds of numbers differently. In addition, computerprograms often require facilities that go beyond basic hardware capabilities and Smalltalk number classessatisfy these additional needs.

Page 164: toc.prn - Squeak
Page 165: toc.prn - Squeak
Page 166: toc.prn - Squeak
Page 167: toc.prn - Squeak
Page 168: toc.prn - Squeak
Page 169: toc.prn - Squeak
Page 170: toc.prn - Squeak
Page 171: toc.prn - Squeak
Page 172: toc.prn - Squeak
Page 173: toc.prn - Squeak
Page 174: toc.prn - Squeak
Page 175: toc.prn - Squeak
Page 176: toc.prn - Squeak
Page 177: toc.prn - Squeak
Page 178: toc.prn - Squeak
Page 179: toc.prn - Squeak
Page 180: toc.prn - Squeak
Page 181: toc.prn - Squeak
Page 182: toc.prn - Squeak
Page 183: toc.prn - Squeak
Page 184: toc.prn - Squeak
Page 185: toc.prn - Squeak
Page 186: toc.prn - Squeak
Page 187: toc.prn - Squeak
Page 188: toc.prn - Squeak

Introduction to Smalltalk - Chapter 5 - Numbers Ivan Tomek 9/17/00

178

Design

The behaviors that we expect of Currency include creation (create a new Currency object),arithmetic (add or subtract two Currency objects), comparison (equal, not equal, less than, and so on), andprinting (for testing and inspecting). Each of these behaviors will be implemented as a protocol, and thenotion of dollars and cents suggests the use of two instance variables called dollars and cents.

We have now decided all the major features of Currency except where to put it in the classhierarchy. Currency CMagnitde Tj -338948 011.76 TD -F0 9.96 Tf -0.0101 Tc 0.0176 Tw (nbrnce of thesclass) tree. Sice vcrrencyis calndol smplem rithmetic ,we emustcomnsdedrwherhesrthescsuperlass

C u r r e n c y Currency

Page 189: toc.prn - Squeak
Page 190: toc.prn - Squeak
Page 191: toc.prn - Squeak
Page 192: toc.prn - Squeak
Page 193: toc.prn - Squeak

Introduction to Smalltalk - Chapter 5 - Numbers Ivan Tomek 9/17/00

183

Currency dollars: 100 cents: 3412

creates a Currency object with 100 dollars and 3412 cents but we would prefer a Currency object with 134dollars and 12 cents. Similarly,

Currency dollars: 100 cents: -34

Page 194: toc.prn - Squeak

Introduction to Smalltalk - Chapter 5 - Numbers Ivan Tomek 9/17/00

184

cents:

Page 195: toc.prn - Squeak

Introduction to Smalltalk - Chapter 5 - Numbers Ivan Tomek 9/17/00

185

4. Currency

Page 196: toc.prn - Squeak
Page 197: toc.prn - Squeak

Introduction to Smalltalk - Chapter 5 - Numbers Ivan Tomek 9/17/00

187

origin printOn: aStream.aStream nextPutAll: ' corner: '.corner printOn: aStream

and extend it as follows:

printOn: aStream"Append to the argument aStream a sequence of characters that identifies the receiver. The general formatis originPoint corner: cornerPoint angle: angle."

super printOn: aStream.aStream nextPutAll: ' angle: '.angle printOn: aStream

In this definition, super is a special identifier that allows us to access an identically named method

Page 198: toc.prn - Squeak
Page 199: toc.prn - Squeak
Page 200: toc.prn - Squeak
Page 201: toc.prn - Squeak
Page 202: toc.prn - Squeak
Page 203: toc.prn - Squeak
Page 204: toc.prn - Squeak
Page 205: toc.prn - Squeak
Page 206: toc.prn - Squeak
Page 207: toc.prn - Squeak
Page 208: toc.prn - Squeak
Page 209: toc.prn - Squeak
Page 210: toc.prn - Squeak

Introduction to Smalltalk - Chapter 6 - Design of applications with graphical user interfaces

Page 211: toc.prn - Squeak
Page 212: toc.prn - Squeak
Page 213: toc.prn - Squeak
Page 214: toc.prn - Squeak
Page 215: toc.prn - Squeak
Page 216: toc.prn - Squeak
Page 217: toc.prn - Squeak
Page 218: toc.prn - Squeak
Page 219: toc.prn - Squeak
Page 220: toc.prn - Squeak
Page 221: toc.prn - Squeak
Page 222: toc.prn - Squeak
Page 223: toc.prn - Squeak
Page 224: toc.prn - Squeak
Page 225: toc.prn - Squeak
Page 226: toc.prn - Squeak
Page 227: toc.prn - Squeak
Page 228: toc.prn - Squeak
Page 229: toc.prn - Squeak
Page 230: toc.prn - Squeak
Page 231: toc.prn - Squeak
Page 232: toc.prn - Squeak
Page 233: toc.prn - Squeak
Page 234: toc.prn - Squeak
Page 235: toc.prn - Squeak
Page 236: toc.prn - Squeak
Page 237: toc.prn - Squeak
Page 238: toc.prn - Squeak
Page 239: toc.prn - Squeak
Page 240: toc.prn - Squeak
Page 241: toc.prn - Squeak
Page 242: toc.prn - Squeak
Page 243: toc.prn - Squeak
Page 244: toc.prn - Squeak
Page 245: toc.prn - Squeak
Page 246: toc.prn - Squeak
Page 247: toc.prn - Squeak
Page 248: toc.prn - Squeak
Page 249: toc.prn - Squeak
Page 250: toc.prn - Squeak

Introduction to Smalltalk - Chapter 7 - Introduction to Collections Ivan Tomek 9/17/00

240

#(1 2 3 4 5 3 4 5 3 4 5) do: [:el| Trancript cr; show: el printString] exceptFor: 3

We found that all tests worked but we should, of course, test the method for other kinds of

Page 251: toc.prn - Squeak

Introduction to Smalltalk - Chapter 7 - Introduction to Collections Ivan Tomek 9/17/00

241

1. Test conversion messages asSet, asSortedCollection, asr Tf rderllection

Page 252: toc.prn - Squeak
Page 253: toc.prn - Squeak
Page 254: toc.prn - Squeak
Page 255: toc.prn - Squeak
Page 256: toc.prn - Squeak
Page 257: toc.prn - Squeak
Page 258: toc.prn - Squeak
Page 259: toc.prn - Squeak
Page 260: toc.prn - Squeak
Page 261: toc.prn - Squeak
Page 262: toc.prn - Squeak

Introduction to Smalltalk - Chapter 7 - Introduction to Collections Ivan Tomek 9/17/00

252

TwoDList columns: 3 rows: 5

returns TwoDList (nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil).

Accessing a TwoDList

Just like Array, TwoDList is accessed by at: and at:put:. The difference is that the at: argument isnormally a Point because a position in a table requires two coordinates. The x part of the point is the columnnumber, and the y part is the row number. The following example illustrates the principle:

| table |table := TwoDList on: #(12 14 16 21 42 24) columns: 3 rows: 2.Transcript clear; cr; show: (table at: 1@2) printString. "Prints 21 – element in column 1, row 2."table at: 2@2 put: 77.able at16 232TD 0.036 T4c 0.872"talnglust Tw uctt: 7774

Page 263: toc.prn - Squeak
Page 264: toc.prn - Squeak
Page 265: toc.prn - Squeak
Page 266: toc.prn - Squeak
Page 267: toc.prn - Squeak
Page 268: toc.prn - Squeak
Page 269: toc.prn - Squeak
Page 270: toc.prn - Squeak
Page 271: toc.prn - Squeak
Page 272: toc.prn - Squeak
Page 273: toc.prn - Squeak
Page 274: toc.prn - Squeak
Page 275: toc.prn - Squeak
Page 276: toc.prn - Squeak
Page 277: toc.prn - Squeak
Page 278: toc.prn - Squeak
Page 279: toc.prn - Squeak

Introduction to Smalltalk - Chapter 8 - More Sequenceable Collections, List widgets Ivan Tomek 9/17/00

270

never redefine it in any subclass. Method basicAt: has exactly this role. If everybody plays by the rules,basicAt: thus has a guaranteed single definition that we can always rely on. When you examine the library,you will find that there are quite a few

Page 280: toc.prn - Squeak
Page 281: toc.prn - Squeak
Page 282: toc.prn - Squeak
Page 283: toc.prn - Squeak

Introduction to Smalltalk - Chapter 8 - More Sequenceable Collections, List widgets Ivan Tomek 9/17/00

274

(items inject: (Currency cents: 0)into: [:total :item | total + item price]) displayString

Note the use of inject:into: with Currency objects, and the displayString message - you may haveexpected printString. displayString

Page 284: toc.prn - Squeak
Page 285: toc.prn - Squeak
Page 286: toc.prn - Squeak
Page 287: toc.prn - Squeak
Page 288: toc.prn - Squeak
Page 289: toc.prn - Squeak
Page 290: toc.prn - Squeak
Page 291: toc.prn - Squeak
Page 292: toc.prn - Squeak
Page 293: toc.prn - Squeak
Page 294: toc.prn - Squeak
Page 295: toc.prn - Squeak
Page 296: toc.prn - Squeak
Page 297: toc.prn - Squeak
Page 298: toc.prn - Squeak
Page 299: toc.prn - Squeak

Introduction to Smalltalk - Chapter 8 - More Sequenceable Collections, List widgets Ivan Tomek 9/17/00

290

“Initialize items to a suitably sized sorted collection. Sort alphabetically by name.”items := SortedCollection sortBlock: [:item1 :item2| (item1 name) < (item2 name)].self execute

Exercises

1. Find and examine all references to sortBlock: and determine which of them are instance messages: andwhy they are used.

2. Study and describe how SortedCollection adds a new element.3. Browse SortedCollectionWithPolicy and explain how it differs from SortedCollection.4. Formulate the sort block for storing entries of the library catalog from the Section 8.3 in a

SortedCollection using the alphabetical order of author names. Make up any necessary accessingmethods.

5. Write a code fragment to create ten random rectangles and sort them in the order of increasing area.6. ( I t e m s e q u e e S l a g e t M a k , o r t t i f S l a g e t M a k f r e q u e r t b l M a k e r t n e e S f i r g e t M a k . E x n d o d e r t b p r e v i o u s t e ) S t u 5 4 1 T 1 5 ( ) T j t h e a l p h a b e T j a s e r t b p r i m t a l o g o m p a r i s f S l 7 3 t b l M a k e r t n e e S f i r g e t M a r t b l M 3 t b c i r c u m / F 3 9 f S l 7 3 t b s e c o n d n e e S f i r g e t M a r 6 .

Page 300: toc.prn - Squeak
Page 301: toc.prn - Squeak
Page 302: toc.prn - Squeak
Page 303: toc.prn - Squeak
Page 304: toc.prn - Squeak
Page 305: toc.prn - Squeak
Page 306: toc.prn - Squeak
Page 307: toc.prn - Squeak
Page 308: toc.prn - Squeak
Page 309: toc.prn - Squeak
Page 310: toc.prn - Squeak
Page 311: toc.prn - Squeak

Introduction to Smalltalk - Chapter 8 - More Sequenceable Collections, List widgets Ivan Tomek 9/17/00

302

contentsFromUser"Answer an Image with the contents of a user-specified area on my screen."

^self completeContentsOfArea: Rectangle fromUser

This shows that if we have a window rectangle (call it windowRectangle) we can obtain its rectangle by

Screen default completeContentsOfArea: windowRectangle

Page 312: toc.prn - Squeak
Page 313: toc.prn - Squeak
Page 314: toc.prn - Squeak
Page 315: toc.prn - Squeak
Page 316: toc.prn - Squeak
Page 317: toc.prn - Squeak
Page 318: toc.prn - Squeak
Page 319: toc.prn - Squeak
Page 320: toc.prn - Squeak
Page 321: toc.prn - Squeak
Page 322: toc.prn - Squeak
Page 323: toc.prn - Squeak
Page 324: toc.prn - Squeak
Page 325: toc.prn - Squeak
Page 326: toc.prn - Squeak
Page 327: toc.prn - Squeak
Page 328: toc.prn - Squeak
Page 329: toc.prn - Squeak
Page 330: toc.prn - Squeak
Page 331: toc.prn - Squeak
Page 332: toc.prn - Squeak
Page 333: toc.prn - Squeak
Page 334: toc.prn - Squeak
Page 335: toc.prn - Squeak
Page 336: toc.prn - Squeak

Introduction to Smalltalk - Chapter 9 - Sets, bags, and dictionaries Ivan Tomek 9/17/00

327

the second association replaces the first one because its key is equal to the first key.IdentityDictionary is an important subclass of Dictionary that uses equivalence instead of equality,

and is internally represented as a collection of keys and a collection of values rather than as a set ofassociations. An IdentityDictionary is often used as a kind of a knapsack to carry around a variety ofidentifiable objects that could not be predicted when a class was designed and consequently could not bedeclared as instance variables. To put this differently, an IdentityDictionary

Page 337: toc.prn - Squeak
Page 338: toc.prn - Squeak
Page 339: toc.prn - Squeak
Page 340: toc.prn - Squeak
Page 341: toc.prn - Squeak
Page 342: toc.prn - Squeak
Page 343: toc.prn - Squeak
Page 344: toc.prn - Squeak
Page 345: toc.prn - Squeak

Introduction to Smalltalk - Chapter 9 - Sets, bags, and dictionaries Ivan Tomek 9/17/00

336

Deletion will be implemented by instance method language1Delete: aString (and a symmetric methodlanguage2Delete: aString). The method deletes the whole language1 association with key aString, andpropagates the change to language2. Since this and similar messages are sent by the pop up menu after theuser has made a selection in the user interface, we can assume that aString is present in the set of keys oflanguage1. We can thus use removeKey: and similar messages without worrying about the ifAbsent: part.The algorithm for responding to pop up command delete when a word in language 1 is selected is asfollows:

1.

Page 346: toc.prn - Squeak
Page 347: toc.prn - Squeak
Page 348: toc.prn - Squeak
Page 349: toc.prn - Squeak
Page 350: toc.prn - Squeak

Introduction to Smalltalk - Chapter 9 - Sets, bags, and dictionaries Ivan Tomek 9/17/00

341

Figure 9.13. State transition table of a JK flip-flop.

Page 351: toc.prn - Squeak
Page 352: toc.prn - Squeak
Page 353: toc.prn - Squeak
Page 354: toc.prn - Squeak
Page 355: toc.prn - Squeak
Page 356: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

348

There are methods that only work with external streams, methods that can be used with read streams but notwith write streams, and so on. Most of these limitations are obvious and natural.

Creation

Internal streams are usually created with class methods on: with:

Page 357: toc.prn - Squeak
Page 358: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

350

upTo: anObject - repeats sending next until it reaches the first occurrence of anObject or the readLimit. Itreturns a collection whose elements2 are the elements retrieved by the consecutive next messagesfrom the start of the iteration up to but not including anObject. The pointer is left pointing atanObject so that the next next

Page 359: toc.prn - Squeak
Page 360: toc.prn - Squeak
Page 361: toc.prn - Squeak
Page 362: toc.prn - Squeak
Page 363: toc.prn - Squeak
Page 364: toc.prn - Squeak
Page 365: toc.prn - Squeak
Page 366: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

358

2. Repeat for each position of the input stream beginning from the start:a. For each element of the dictionary do:

i. Increment current position holder for match string.ii. Compare input string and match character.

1. If no match, reset current position holder for match string to 0.2. If match, check if this is the last character to match.

Page 367: toc.prn - Squeak
Page 368: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

360

1. Extend TextFilter to accept blocks as replacement arguments as stated in the specification.

10.5 Example: Circular Buffer

In computing terminology, a buffer is a memory area that accepts data from one process and emitsit to another process; the two processes work at their own speeds. An example of the use of a buffer isreading a block of data from a file into memory where it is processed one byte at a time by a program.Another example is a computing node on a network that accepts parcels of data arriving in unpredictableamounts and at unpredictable times, processes them one byte at-a-time, and possibly sends the data on toanother network node.

The hardware implementation of buffers often has the form of a special memory chip with a fixednumber of memory locations with pointers to the first byte to be retrieved, and to the location where thenext byte is to be stored as in Figure 10.8. When a new byte arrives, it is stored at the next availablelocation and the pointer is incremented, and when a byte is required from storage, it is removed from thelocation pointed to and the pointer incremented.

Figure 10.8. Buffer as a fixed size array with pointers to the next available byte and the next availablelocation.

In reality, of course, a byte read from the buffer is not ‘removed’ and only the changed value of thepointer indicates that the byte has been used. Similarly, an ‘empty’ position is not really empty but the new

lomuctr Buarh pmeonc the fomenadic of medingsn of bue pointion isbthe chze ar bue poffer i Tj 36 -11.76 TD -00056 Tc 0.4192.6Tw (The haffer isdoenot rof course, a haveo) Tj T*0Tc 0.0366Tw ( ) Tj 10.646 -3 TD -0.0161 Tw 0.4199 Tw (In stspecial merdware imip wia )an Fifactit isusuly em not r.Tj -208.06 -35.76 TD -0.0122 Tc 1.6722 Tc (In rstd fit isc � stjust memory arrngi to)t themtr t them ccular Baffer isea t. lementatg terh procend anccular Baffer isstrtioe 10ithem purintar bue stoecon.

Ivalementat comcular Baffer isbad one fixed s-ze array w. ratenssef the

spoca, a haveo

the nqu st onenot rof courionandeeme161 wfacaeinef thelelt roaal merdware im,ugtwlsfunstomhalitbushhemess and

Page 369: toc.prn - Squeak
Page 370: toc.prn - Squeak
Page 371: toc.prn - Squeak
Page 372: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

364

Figure 10.10. Main classes used in file processing.

The cookbook procedure for processing data stored in a file is as follows:

1.

Page 373: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

365

Class Filename is an abstract class and its concrete subclasses (MaxFilename, PCFilename and itssubclasses, and UnixFilename) implement the platform-specific behavior needed on your machine, such asparsing platform-specific syntax of file names. However, you never need to deal with these concretesubclasses because Filename automatically sends all platform-dependent messages to the subclassrepresenting your platform. This is done via Filename’s class variable DefaultClass which holds the nameof the appropriate Filename class. Removing explicit dependence on one platform makes it possible to writeprograms that will run on different platforms. This arrangement is similar to the implementation of strings.

External streams perform data transfer operations. Instances of external streams are never createdby class messages to external stream classes but by messages to the Filename

Page 374: toc.prn - Squeak
Page 375: toc.prn - Squeak
Page 376: toc.prn - Squeak
Page 377: toc.prn - Squeak
Page 378: toc.prn - Squeak
Page 379: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

371

Example 3: Let user delete a file from a listProblem: Implement a method to display the file names in the current directory in a multiple choice dialog,and allow the user to delete a fileSolution: This problem does not require a specific Filename

Page 380: toc.prn - Squeak
Page 381: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

373

This explains how the limited number of external stream classes (Figure 10.8) can provide such avariety of accessing modes - the type of access is controlled by an instance of FileConnection. The otherstream creation messages are similar.

Since a file and its mode of access are two separate things, a file initially accessed via one kind of

Page 382: toc.prn - Squeak
Page 383: toc.prn - Squeak
Page 384: toc.prn - Squeak
Page 385: toc.prn - Squeak
Page 386: toc.prn - Squeak
Page 387: toc.prn - Squeak
Page 388: toc.prn - Squeak

Introduction to Smalltalk - Chapter 10 - Streams, files, and BOSS Ivan Tomek 9/17/00

380

The basic definition of storeOn: in Object simply generates messages to create a new instance ofthe receiver and further messages to initialize its variables. The interesting part of the definition is that it

Page 389: toc.prn - Squeak
Page 390: toc.prn - Squeak
Page 391: toc.prn - Squeak
Page 392: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

384

messagecontext

messagecontext

Page 393: toc.prn - Squeak
Page 394: toc.prn - Squeak
Page 395: toc.prn - Squeak
Page 396: toc.prn - Squeak
Page 397: toc.prn - Squeak
Page 398: toc.prn - Squeak
Page 399: toc.prn - Squeak
Page 400: toc.prn - Squeak
Page 401: toc.prn - Squeak
Page 402: toc.prn - Squeak
Page 403: toc.prn - Squeak
Page 404: toc.prn - Squeak
Page 405: toc.prn - Squeak
Page 406: toc.prn - Squeak
Page 407: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek

Page 408: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

400

queue length. The output will, of course, be left to the application model (class BankSimulation) to display.This way, if we want to change the user interface (output of results) or make our simulation a part of alarger scheme, the domain objects can stay the same and only the application model must be changed.

Page 409: toc.prn - Squeak
Page 410: toc.prn - Squeak
Page 411: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

403

• lowerLimit: anInteger upperLimit: anInteger.• Accessing - implemented by

• next. Returns random integer.

Page 412: toc.prn - Squeak
Page 413: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

405

4. No complete match, copy character from string to result, increment position in string and result.5. Compare position 2 in pair1 ($b) with first character of string - match.6. Compare position 1 in pair2 ($a) with first character of string key; no match.7. We have a complete match. Select the longest replacement possible, changing result to ‘xx’.8. Continue in this way until the last character in string. At this point, there are two matches and two

possible replacements. The longer one is selected giving result = ‘xxcdyy’ as expected.

This scenario works as expected. But wait - the behavior in the following scenario does not quitemeet our expectations.

Scenario 2: Filtering string = ‘abcd’ with match/replacement pairs pair1 = ‘bc’->‘xx’, pair2 = ‘abcd’->‘yy’Assume string = ‘abcd’ and match/replacement pairs pair1 = ‘bc’->‘xx’string = abcd’STw (key; no maTj 10.12 0 TD722F0 9.96aTf -0. Tfobtaed. Butmonue T Tfsed and match/Tj 69yyent pairs) �s selected giving string = Assume as expec 0.sfinishe2 0 TD36.3beD14-220..211F0 9.96 Tf 0.0256 Tc 0.0044 Tw ( to ) Tj 12.84 0 TDng = ‘ t o t B n c t o f - 2 u e u 0 n T j 1 T * d r T f w 4 0 T D n 4 2 7 T f - 0 . 0 W h e 0 6 i t D - 0 . , u s e 5 2 i t D s c t i n o n g - 2 1 d 1 1 8 T p r a t e l 9 T w ( ) T . a r i o 2 : u e T T f s e d a n d 2 1 8 8 0 3 T f l l 2 4 8 2 a i t e w B 0 2 T c e 6 2 m u l t e l 9 - 2 1 d l g i g i t h m c 0 T T f 2 5 1 . 9 6 D 0 . 0 4 2 q m 0 T T 5 3 s c ( S T w ( a n d 0 7 6 2 0 3 T f l l 3 5 6 2 0 3 n 9 r i g c a n r e f u l l y w e 6 8 5 u t k o n g e 9 l o o k 6 t T j - 2 o b j 0 . s / F 2 a c t o c 0 . 0 1 1 8 s e d i 0 t e e 2 T j k 4 s r T f 5 2 2 2 - > ª 3 1 1 - 1 1 . 7 s u b p p o d w i n t n g I n a d 9 i . 0 . 0 6 s c t 4 2 e a m e . 0 9 0 3 T c ( 2 8 T j - 2 i 6 s c t k e e p t D i k o f - 2 i g i t B w ( 4 2 T - > . 0 9 T w 2 1 ( 7 4 w e T j 1 1 D 0 t e e 2 T j k 4 r T f 8 7 0 T D n 4 2 1 T D 0 . 0 0 a l s o / F 2 a T j 1 1 D 0 2 4 2 0 o 3 e r - 1 a s e d i T c 1 9 8 4 2 1 6 0 T D 0 . 0 4 2 2 - 6 7 T j 1 1 c t e d > ‘ y s t r i n t o ) T j D / F 2 9 0 8 T f - 0 . 0 0 8 3 T . 2 1 4 1 . 4 3 3 3 T w ( C o n t i n u e i n 1 3 1 8 0 3 T f 4 n 6 4 7 T f - 0 . 0 A > u e u 0 w h o e 5 2 e 6 r o d s . 2 - 1 0 9 l l 0 . 0 . 0 w o f p l d w i a y 6 T D d , e t c o c i 0 2 9 5 - 1 1 8 5 u c a \ i . T j 1 1 D 0 t e e 2 T j k 4 s t r i n g = ) T j 1 3 - T i T D 0 . 0 4 2 M n g s e s I n 4 0 T . 1 7 4 T c ( W a i j T - 4 0 7 T - 4 6 . 4 3 3 3 T w ( C o n t i n u e i n 1 f 9 7 0 T D n m 3 T - T i n 9 r > . 0 9 i f w o u ) T . 0 i 5 2 0 0 c l e t c v a r a t b l 0 n T j 1 T - 9 n g s 0 T - 4 6 . 3 e r - 1 a s e d i T c 1 9 8 4 2 1 6 0 T D 0 . 0 4 2 2 - 6 7 T j 1 1 c t e d > ‘ y s t r i n t o ) T j D / F 2 9 0 8 T f - 0 . 0 0 8 3 T . 2 1 4 1 . 4 3 3 3 T w ( C o n t i n u e i n 1 0 8 o u l d w i n g 8 9 . 9 6 0 . 0 A > 0 9 l l 0 . 0 . 0 6 o f a u e t c o c i 0 2 9 5 ) T j w 2 - 1 f u l l y 6 T D d , e 0 9 � d y ’ ) 2 1 T w ( ) T . n g ) i s c 8 5 u b e 0 0 c l e t c T j 1 1 D 0 t e e 2 T j k 4 r T 1 2 f i n i s h e T 1 7 2 = � v a r a t b l 0 u c a e T c 0 6 1 . 0 8 - > ‘ y s t r i n g = ) T j 2 4 8 0 3 T f 0 = � R 2 2 d y A s e R 1 T w ( ) T . T j 1 1 � . 0 0 4 4 T w ( t o ) T j 1 2 2 2 2 - > ( n T j 1 T - 1 7 T - 0 8 - - 2 3 2 2 2 - s e d a n d 2 1 2 2 2 - > ¬ 9 f - 0 . 0 W . 0 9 o u l d w b D i k g r T j d w e c a n 0 0 f u l l y 6 d e s c r i b e g i v i n g s c t e 8 g c 2 e d u r e 0 ) T j - s : j 1 1 D 0 2 4 2 2 2 - s e d a 5 4 9 i n i s h e 2 0 T 2 1 T j 1 T - 7 e d > ‘ y s t r i n t o

Page 414: toc.prn - Squeak
Page 415: toc.prn - Squeak
Page 416: toc.prn - Squeak
Page 417: toc.prn - Squeak
Page 418: toc.prn - Squeak
Page 419: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

411

SortedLinkedList (LinkNode 3 LinkNode 13 LinkNode 33)SortedLinkedList (LinkNode 0 LinkNode 3 LinkNode 13 LinkNode 33)

Finally, we must test that SortedLinkedList works even with non-default sort blocks and we thusdefine a new SortedLinkedList creation method called sortBlock: and modify our test program to create anew list with the same elements sorted in descending order:

| sll |Transcript clear.sll := SortedLinkedList sortBlock: [:x :y | x > y].Transcript show: sll printString; cr.sll add: 3.Transcript show: sll printString; cr.sll add: 33.Transcript show: sll printString; cr.sll add: 13.Transcript show: sll printString; cr.sll add: 0.Transcript show: sll printString

The test produces

SortedLinkedList ()SortedLinkedList (LinkNode LinkNode 3)SortedLinkedList (LinkNode LinkNode 33 LinkNode LinkNode 3)SortedLinkedList (LinkNode LinkNode 33 LinkNode LinkNode 13 LinkNode LinkNode 3)SortedLinkedList (LinkNode LinkNode 33 LinkNode LinkNode 13 LinkNode LinkNode 3 LinkNode LinkNode0)

in the Transcript 0064 agan tconfirmsthe Tcorrectness ofour timpementstion

Man tlessons ear.ned

Page 420: toc.prn - Squeak
Page 421: toc.prn - Squeak
Page 422: toc.prn - Squeak
Page 423: toc.prn - Squeak
Page 424: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

416

i) Add root

Page 425: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

417

add: 17;add: 49;add: 32;do: [:node | Transcript show: node value printString; cr]

which produces

8142117363249

Main lessons learned:

Page 426: toc.prn - Squeak
Page 427: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs

Page 428: toc.prn - Squeak
Page 429: toc.prn - Squeak

Introduction to Smalltalk - Chapter 11 - Stacks, queues, linked lists, trees, and graphs Ivan Tomek 9/17/00

421

1 <D8 03> push 33 <D8 03> push 35 <DF 00> no-check send +7 <66> pop8 <D8 05> push 510 <CC 00> no-check send factorial12 <45> pop; push self13 <65> return

In conclusion, let’s note that both Parser and Scanner are in the library and you can subclasses

Page 430: toc.prn - Squeak
Page 431: toc.prn - Squeak
Page 432: toc.prn - Squeak
Page 433: toc.prn - Squeak
Page 434: toc.prn - Squeak
Page 435: toc.prn - Squeak
Page 436: toc.prn - Squeak
Page 437: toc.prn - Squeak
Page 438: toc.prn - Squeak

Introduction to Smalltalk - Chapter 12 - Developing user interfaces Ivan Tomek 9/17/00

431

Chapter 12 - Developing user interfaces

Overview

The group of classes supporting graphical user interfaces (GUIs) is one of the most complex partsof VisualWorks and its detailed coverage is beyond the scope of this book. Fortunately, to be able to use thetools for routine applications and to create interesting extensions of the widget library, you only need tounderstand a limited number of concepts, and specialized books are available for more advanced uses.

The first requirement for creating a user interface is to be able to draw objects such as lines,widgets, and text on the screen. VisualWorks perspective is that drawing requires a surface to draw on (suchas a computer screen or printer paper), the graphical objects being drawn, and an object that does thedrawing and holds the parameters that describe the drawing context (such as fonts, colors, and line widths).

Being able to draw an object on the screen is, however, only a part of a typical GUI. Unless thedisplayed windows are completely passive, the user must also interact with them via the mouse and thekeyboard. A user interface must also ensure that the display reflects changes of the displayed data model,and that damage caused, for example, by resizing a window is automatically repaired.

To implement automatic response to model changes and user interaction, Smalltalk uses themodel-view-controller paradigm - MVC for short. The model part of the MVC triad holds data and provides

GUIs-ay reflece to drulical objsxt (suculine wonrks n Tj -36 -1.28 TD /F0 9.96 Tf -0.002 Tc70.057 Tw222The firsomaseto Smaiileddns a-36 -166348 0.0557 T010.8575 130(and threateramet paciplthe diWorks perspenterfaces (GUIsovides) Tj 0 backof cnd114.6 -29rep8.S24f -0.03.804c 0.887remenrfaomiz T32 metplayed wiilontrir 164( aeo)w onrectInols for routine appli, noces lecmmcrejhment autom0 -11.76 T -0.0132 c05.044involvir morese ag contexuteanheeTw0et but 6 Tw (undir ml partse agr of cteaniWofule)interactscr36 -11.76 T6 -0.0009588c72.042Dces (supporting graphical oobject on the sc Tw (asst ctaskhangesiffatenrklodeuael changodenctract wcalt-36 -11.76 09 -0.01328715 1.4631 ffatenrkdnd ogrametction, Smaoutroae outesiffatenrlynnd prs andmet paciplthe dective is that drawing rejhment1 T8 0.0281 Tc a (such) Tj 0 -11.76 f -0.001.925c 1.4631 that dUIs-ay ,aphical oace isof the die that the display realynnetharibe the drawing and hothe paramedel,

Page 439: toc.prn - Squeak
Page 440: toc.prn - Squeak
Page 441: toc.prn - Squeak
Page 442: toc.prn - Squeak
Page 443: toc.prn - Squeak
Page 444: toc.prn - Squeak

Introduction to Smalltalk - Chapter 12 - Developing user interfaces Ivan Tomek 9/17/00

437

• moveTo: is triggered by the Move button via the move method (below). It displaysprompts to select the window to be moved and the destination desk, unmaps the window,and assigns the number of the specified desk to this window in the registry.

• openBrowser and openWorkspace buttons open the Browser and the Workspace.• rename requests a new name for the currently selected desk button and assigns it to the

button as its label.• move asks the user to select a window and click the destination desk button. The internal

assignment of the desk is performed by updateDesk:.• Private support methods.

• updateDesk: is trigga tosupp

Page 445: toc.prn - Squeak
Page 446: toc.prn - Squeak

Introduction to Smalltalk - Chapter 12 - Developing user interfaces Ivan Tomek 9/17/00

439

| window |(waitingApps at: currentDesk) remove: anApplication.cg816 Tw ( Iow |) Tj -4.92 -1:= TD 0.0557 Tc 0 Tw 3 4.92(curre162ation.) Tj -15

Page 447: toc.prn - Squeak
Page 448: toc.prn - Squeak
Page 449: toc.prn - Squeak
Page 450: toc.prn - Squeak
Page 451: toc.prn - Squeak

Introduction to Smalltalk - Chapter 12 - Developing user interfaces Ivan Tomek 9/17/00

444

gc paint: paint.startx := (Dialog request: 'Upper left x' initialAnswer: '30') asNumber.starty := (Dialog request: 'Upper left y' initialAnswer: '30') asNumber.side := (Dialog request: 'Side' initialAnswer: '20') asNumber.“Disply.”^gc displayRectangle: (startx @ starty extent: side @ side)

Main lessons learned:

• All display within a window is performed by an instance of GraphicsContext.• GraphicsContext is central to everything related to display of visual components.• In addition to being the drawing engine, a GraphicsContext also holds display parameters such as line

width, paint, and fault.• Display parameters stored in the graphics context can be controlled programmatically.

Page 452: toc.prn - Squeak
Page 453: toc.prn - Squeak
Page 454: toc.prn - Squeak
Page 455: toc.prn - Squeak
Page 456: toc.prn - Squeak
Page 457: toc.prn - Squeak
Page 458: toc.prn - Squeak
Page 459: toc.prn - Squeak
Page 460: toc.prn - Squeak
Page 461: toc.prn - Squeak
Page 462: toc.prn - Squeak
Page 463: toc.prn - Squeak
Page 464: toc.prn - Squeak

Introduction to Smalltalk - Chapter 12 - Developing user interfaces Ivan Tomek 9/17/00

457

width := aGraphicsContext widthOfString: string.aGraphicsContext displayString at: (center x ( width/2)) @ 50)]

ifFalse: “Display currently selected image.”[aGraphicsContext displayImage: image at: 0@0]

The program is now fully functional. Note that unlike our previous programs it automaticallyrepairs window damage due to collapsing, reshaping, and other window events.

Improvements

We know that it is better to centralize shared behavior and in our case both image

Page 465: toc.prn - Squeak
Page 466: toc.prn - Squeak
Page 467: toc.prn - Squeak
Page 468: toc.prn - Squeak
Page 469: toc.prn - Squeak
Page 470: toc.prn - Squeak
Page 471: toc.prn - Squeak
Page 472: toc.prn - Squeak
Page 473: toc.prn - Squeak
Page 474: toc.prn - Squeak

Introduction to Smalltalk - Chapter 12 - Developing user interfaces Ivan Tomek 9/17/00

467

its components with the clipped GraphicsContext. Each component then redraws that part of itself whichfalls within the clipping rectangle, using the supplied default graphics context parameters or redefining themtemporarily.

Older versions of VisualWorks used polling controllers which repeatedly queried the sensor for UI

Page 475: toc.prn - Squeak
Page 476: toc.prn - Squeak
Page 477: toc.prn - Squeak
Page 478: toc.prn - Squeak
Page 479: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

473

Figure 13.2. Simplified behavior of processes: Stopwatch runs until stopped by Delay. At this pointanother process can start execution and the stopwatch resumes when the one second delay expires.

The Stop button stops the infinite iteration process started by the Start button by sending it theterminate message:

stop"Terminate the process implementing continuous stopwatch operation."

process terminate

stop

Start

Page 480: toc.prn - Squeak
Page 481: toc.prn - Squeak
Page 482: toc.prn - Squeak
Page 483: toc.prn - Squeak
Page 484: toc.prn - Squeak
Page 485: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

479

2. Program opens form.3. User enters information and clicks Accept.4. Program closes form, inserts new alarm in the list, and displays the updated list in the main window.

Scenario 2: User edits an alarm in the list1. User selects an alarm and clicks edit

3. UserAccept.3. Program User eects an alarm and clicks .

Page 486: toc.prn - Squeak
Page 487: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

481

Design Refinement

Class Hierarchy

AlarmTool is an application model, and its superclass is therefore ApplicationModel. Alarm does not

Page 488: toc.prn - Squeak
Page 489: toc.prn - Squeak
Page 490: toc.prn - Squeak
Page 491: toc.prn - Squeak
Page 492: toc.prn - Squeak
Page 493: toc.prn - Squeak
Page 494: toc.prn - Squeak
Page 495: toc.prn - Squeak
Page 496: toc.prn - Squeak
Page 497: toc.prn - Squeak
Page 498: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

492

truck process will use the Truck class in a similar way. Process objects thus use domain objects but areseparate from them. With this background, we can now expand Scenario 1.

Scenario 1: Starting or restarting simulation1. User clicks Go.2. TrainSimulation creates a train process and a truck process.3. TrainSimulation starts the train process and the vehicle processes automatically start taking turns

executing single step motion while respecting the semaphore at the crossing.

Scenario 2: Stopping simulation1. User clicks Stop.2. TrainSimulation terminates the two processes.

The immediate question is whether clicking Stop can actually freeze simulation. The point is that User clicks vehicl9.96 Tf 0.0428 Tc 0 Tw (S fr1 TD eingeabe s u.843er) Tj 19.32 P4.1ougF0 9.924oc 9/mov3t T TD r the 28.2 0 tartotion incrosxpl Tjr t -0.01tinsemap4n li.0ti2 -1),greenappli thcow24 12 354. Chang BT 9peed T2545 88 .imu 1h. 74 /F49y fretigle s.024 18 TD eingeabe ss u.843er

Page 499: toc.prn - Squeak
Page 500: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

494

Figure 13.10. Class Hierarchy Diagram with classes to be designed shown as heavy rectangles.

Refined class descriptions

We now understand enough of the behaviors of our classes to be able to start refining classdescriptions:

Domain classes

Vehicle. Abstract superclass of domain classes Train and Truck. Factors out shared behavior and knowledgesuch as vehicle position and direction of motion, calculation of the next position, testing for the end of thetrack or crossing, and turning.Superclass: ObjectComponents: limit (distance from crossing to lights), position (distance of vehicle from start of track),direction

Page 501: toc.prn - Squeak
Page 502: toc.prn - Squeak
Page 503: toc.prn - Squeak
Page 504: toc.prn - Squeak
Page 505: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

499

"Return true if I have just reached the start of the crossing zone."^(direction == #incrementing and: [position = (crossing - limit - vehicleLength)])

or: [direction == #decrementing and: [position = (crossing + limit)]]

and

endOfTrack"Return true if this is the end of the track. Remember that vehicle might have turned."

^(direction = #decrementing and: [position = 0])or: [direction == #incrementing and: [position = (trackLength - vehicleLength)]]

The method that triggers redisplay of semaphore lights uses the same principle as moveOneStepand asks TrainSimulation to display semaphores controlled by the vehicle in the specified color:

semaphores:

Page 506: toc.prn - Squeak
Page 507: toc.prn - Squeak
Page 508: toc.prn - Squeak
Page 509: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

503

7. Add an animation of a crash of the two vehicles.8. Extend the simulation program to several parallel train and truck tracks with a single shared crossing.

Each track has its own vehicle running at its own speed and each vehicle’s speed is controlledindependently.

Page 510: toc.prn - Squeak

Introduction to Smalltalk - Chapter 13 - Processes and their coordination, additional UI topics Ivan Tomek 9/17/00

504

5. User releases the mouse button.6. Program stops dragging the track.

Scenario 3: User drags the left end of the horizontal track to a new position1. User moves the cursor into the hot area surrounding the left end of the horizontal track.2. Program changes cursor to the shape shown on the left of Figure 13.12.3. User presses the <operate> button and moves the mouse while holding the button down.4. Program

Page 511: toc.prn - Squeak
Page 512: toc.prn - Squeak
Page 513: toc.prn - Squeak
Page 514: toc.prn - Squeak
Page 515: toc.prn - Squeak
Page 516: toc.prn - Squeak
Page 517: toc.prn - Squeak
Page 518: toc.prn - Squeak
Page 519: toc.prn - Squeak
Page 520: toc.prn - Squeak
Page 521: toc.prn - Squeak

The Joy of Smalltalk - Glossary Ivan Tomek 9/18/00

515

File out. Command available in file browsing tools and used to store a method, a protocol, a class, or acategory in a file, using a special form expected by the file in command. Used for transporting source codefrom one computer to another. The command is available in various Smalltalk browsers.Finalization.

Page 522: toc.prn - Squeak

The Joy of Smalltalk - Glossary Ivan Tomek 9/18/00

516

Message.Metaclass.Method.Model.Mouse buttons.Multiple inheritance.MVC paradigm.Object.Object file.Optimization.Order of evaluation.Ordered collection.Overloading.Palette.Pane.Parameter.Persostent object.Pointer.Pool dictionary.Po-up menu.Polymorphism.Private method.Primitive method.Private method.Protocol.Pseudovariable.Radio button.Receiver.Return object.Reusability.Selector.self.Semantics.Set.Shortcut key.Signature.Simulation.Single inheritance.

ReSourceolld

S i g t a .

Sigta.ic binng.S i g t r e a

PaSubass.P a S u p e r a s s .seluper Tj T* 0.0082 Tc 0.04523Tw (Theempory.) riable.S i T e x t T j T * 0 . 0 1 9 T c 0 . 0 1 1 T w ( R e T e x t e d i r . ) f w i d g .

Page 523: toc.prn - Squeak

The Joy of Smalltalk - Glossary Ivan Tomek 9/18/00

517

Text file.Transcript.UI.User interface.Unary message.Undeclared variable.Value holder.Variable.View.Widget.Wildcard.Window.Workspace.

Page 524: toc.prn - Squeak
Page 525: toc.prn - Squeak
Page 526: toc.prn - Squeak
Page 527: toc.prn - Squeak
Page 528: toc.prn - Squeak
Page 529: toc.prn - Squeak
Page 530: toc.prn - Squeak
Page 531: toc.prn - Squeak
Page 532: toc.prn - Squeak
Page 533: toc.prn - Squeak
Page 534: toc.prn - Squeak

Introduction to Smalltalk - Appendix 1 - Check Boxes, Radio Buttons, Input Fields, and their applications Ivan Tomek 04/06/2001

10

Exercises

1. Modify our example 1 to display ‘Input text’ in the input field and ‘Output text’

Page 535: toc.prn - Squeak
Page 536: toc.prn - Squeak
Page 537: toc.prn - Squeak
Page 538: toc.prn - Squeak

Introduction to Smalltalk - Appendix 1 - Check Boxes, Radio Buttons, Input Fields, and their applications Ivan Tomek 04/06/2001

14

(builder componentAt: #mozzarella) enable.(componentAt: #figs) enable.(builder componentAt: #pineapple) enable.(builder componentAt: #mozzarellaPrice) enable.(builder componentAt: #figsPrice) enable.

Page 539: toc.prn - Squeak
Page 540: toc.prn - Squeak

Introduction to Smalltalk - Appendix 1 - Check Boxes, Radio Buttons, Input Fields, and their applications

Page 541: toc.prn - Squeak

Introduction to Smalltalk - Appendix 1 - Check Boxes, Radio Buttons, Input Fields, and their applications Ivan Tomek 04/06/2001

17

Figure A.1.14. Builder’s bindings dictionary.

Once the

Page 542: toc.prn - Squeak
Page 543: toc.prn - Squeak
Page 544: toc.prn - Squeak
Page 545: toc.prn - Squeak
Page 546: toc.prn - Squeak
Page 547: toc.prn - Squeak
Page 548: toc.prn - Squeak
Page 549: toc.prn - Squeak

Introduction to Smalltalk - Appendix 1 - Check Boxes, Radio Buttons, Input Fields, and their applications Ivan Tomek 04/06/2001

25

printAndClose

Page 550: toc.prn - Squeak
Page 551: toc.prn - Squeak
Page 552: toc.prn - Squeak
Page 553: toc.prn - Squeak
Page 554: toc.prn - Squeak
Page 555: toc.prn - Squeak
Page 556: toc.prn - Squeak
Page 557: toc.prn - Squeak
Page 558: toc.prn - Squeak
Page 559: toc.prn - Squeak
Page 560: toc.prn - Squeak
Page 561: toc.prn - Squeak
Page 562: toc.prn - Squeak
Page 563: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus Ivan Tomek 04/06/2001

7

Figure A.2.6. The main canvas (left), and the two subcanvases. The subcanvas component of the main

Page 564: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus Ivan Tomek 04/06/2001

8

Page 565: toc.prn - Squeak
Page 566: toc.prn - Squeak
Page 567: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus

Page 568: toc.prn - Squeak
Page 569: toc.prn - Squeak
Page 570: toc.prn - Squeak
Page 571: toc.prn - Squeak
Page 572: toc.prn - Squeak
Page 573: toc.prn - Squeak
Page 574: toc.prn - Squeak
Page 575: toc.prn - Squeak
Page 576: toc.prn - Squeak
Page 577: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus Ivan Tomek 04/06/2001

21

dirString isEmpty | (dirName := dirString asFilename) isDirectory notifTrue: [^Dialog warn: dirName asString , ' is not a valid directory.']

Page 578: toc.prn - Squeak
Page 579: toc.prn - Squeak
Page 580: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus

Page 581: toc.prn - Squeak
Page 582: toc.prn - Squeak
Page 583: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus Ivan Tomek 04/06/2001

27

Figure A.2.17. Passive (left) and activated (right) menu button. The passive view may display the currentchoice in the menu. The menu bar at the top of the window has one label.

The distinguishing characteristics of popup menus, menu bars, and menu buttons are summarizedin the following table:

Page 584: toc.prn - Squeak
Page 585: toc.prn - Squeak
Page 586: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus Ivan Tomek 04/06/2001

30

Figure A.2.21. Dialog for installing a menu.

Page 587: toc.prn - Squeak
Page 588: toc.prn - Squeak
Page 589: toc.prn - Squeak
Page 590: toc.prn - Squeak

Introduction to Smalltalk - Appendix 2 - Dataset, Subcanvas, Notebook, Dialog Window, Menus Ivan Tomek 04/06/2001

34

3. Use the Menu Editor to color the background of the delete

Page 591: toc.prn - Squeak
Page 592: toc.prn - Squeak
Page 593: toc.prn - Squeak
Page 594: toc.prn - Squeak
Page 595: toc.prn - Squeak
Page 596: toc.prn - Squeak

Appendix 3 – Chess board – a custom user interface Ivan Tomek 06/04/01

3

Context Diagram

The major components of the system are the chess board with its pieces and user interface, and theplayers. The players (including a computer program player) are outside of the scope of the task. TheContext Diagram is shown in Figure A.3.2.

Page 597: toc.prn - Squeak
Page 598: toc.prn - Squeak
Page 599: toc.prn - Squeak
Page 600: toc.prn - Squeak
mailto:16@16
Page 601: toc.prn - Squeak
Page 602: toc.prn - Squeak
Page 603: toc.prn - Squeak
Page 604: toc.prn - Squeak
Page 605: toc.prn - Squeak
Page 606: toc.prn - Squeak
Page 607: toc.prn - Squeak
Page 608: toc.prn - Squeak
Page 609: toc.prn - Squeak
Page 610: toc.prn - Squeak

Appendix 3 – Chess board – a custom user interface Ivan Tomek 06/04/01

17

iii. How many moves are required for a knight to reach a given square from a given startingsquare?

iv. What is the maximum number of moves for a knight to reach any position on the board from agiven starting square?

Page 611: toc.prn - Squeak
Page 612: toc.prn - Squeak
Page 613: toc.prn - Squeak
Page 614: toc.prn - Squeak
Page 615: toc.prn - Squeak
Page 616: toc.prn - Squeak
Page 617: toc.prn - Squeak
Page 618: toc.prn - Squeak
Page 619: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Classes, Metaclasses, and Metaprogramming Ivan Tomek 04/06/2001

9

String subclasses “Returns #(ByteEncodedString TwoByteString Symbol GapString).”

To find which of all existing arrays has the largest size, evaluate

| size |size := 0.Array allInstancesDo: [:anArray| size := size max: anArray size].size

or more simplyArray allInstances inject: 0 into: [:max :anArray| max max: anArray size]

To find all superclasses of a class, execute allSuperclasses as inSet allSuperclasses “OrderedCollection (Collection Object).”

Instance variable format contains a code that describes what kind of class this is. The protocolbased on format makes it possible to ask a class whether it has fixed size (classes that have only namedvariables) or variable size (collections represented internally as indexed elements) and, in the case of avariable size class, whether its variables are stored as eight-bit or 16-bit quantities. As an example, allvariable size classes can be obtained by

Object withAllSubclasses select: [:aClass| aClass isVariable]

which returns

OrderedCollection (AnnotatedMethod Array BinaryStorageBytes BOSSBytes BOSSReaderMap ByteArray

Page 620: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Classes, Metaclasses, and Metaprogramming Ivan Tomek 04/06/2001

10

Functionality based on access to class hierarchy and method dictionary

Behavior provides several methods for adding new selectors to the method dictionary or removing

Page 621: toc.prn - Squeak
Page 622: toc.prn - Squeak
Page 623: toc.prn - Squeak
Page 624: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Classes, Metaclasses, and Metaprogramming Ivan Tomek 04/06/2001

14

classVariableNames: ''poolDictionaries: ''category: 'Graphics-Support'

Page 625: toc.prn - Squeak
Page 626: toc.prn - Squeak
Page 627: toc.prn - Squeak
Page 628: toc.prn - Squeak
Page 629: toc.prn - Squeak
Page 630: toc.prn - Squeak
Page 631: toc.prn - Squeak
Page 632: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Classes, Metaclasses, and Metaprogramming Ivan Tomek 04/06/2001

22

Proxy to insert a programmer action and pass the original message to x

Page 633: toc.prn - Squeak
Page 634: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Classes, Metaclasses, and Metaprogramming Ivan Tomek 04/06/2001

24

TestOfProxy newWithProxy var: 13; var: 15

Page 635: toc.prn - Squeak
Page 636: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Classes, Metaclasses, and Metaprogramming Ivan Tomek 04/06/2001

26

Important classes introduced in this chapter

Page 637: toc.prn - Squeak
Page 638: toc.prn - Squeak
Page 639: toc.prn - Squeak

Introduction to Smalltalk - Appendix 5 – Style Recommendations Ivan Tomek 04/06/2001

3

A 5.4 Formatting

Guideline F.1: Use automatic formatting. Since the VisualWorks formatter exhibits some undesirable

Page 640: toc.prn - Squeak
Page 641: toc.prn - Squeak
Page 642: toc.prn - Squeak
Page 643: toc.prn - Squeak
Page 644: toc.prn - Squeak
Page 645: toc.prn - Squeak
Page 646: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Projects Ivan Tomek 04/06/2001

2

We want to be able to perform the following tasks:

• Patient record• Add•

Page 647: toc.prn - Squeak
Page 648: toc.prn - Squeak
Page 649: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Projects

Page 650: toc.prn - Squeak
Page 651: toc.prn - Squeak
Page 652: toc.prn - Squeak

Introduction to Smalltalk - Appendix 4 - Projects Ivan Tomek 04/06/2001

8

High level conversation:1. User selects borrower in the catalog and clicks Open.2. System opens a book form with current information about the book.3. User edits the form and indicates end of task by clicking Accept in.

Page 653: toc.prn - Squeak
Page 654: toc.prn - Squeak
Page 655: toc.prn - Squeak
Page 656: toc.prn - Squeak
Page 657: toc.prn - Squeak
Page 658: toc.prn - Squeak
Page 659: toc.prn - Squeak
Page 660: toc.prn - Squeak
Page 661: toc.prn - Squeak

Introduction to Smalltalk - Appendix 8 - Tidbits Ivan Tomek 04/06/2001

2

added to a stream and displayed in the text view, whereas non-normal characters are ‘dispatched’ for further

Page 662: toc.prn - Squeak

Introduction to Smalltalk - Appendix 8 - Tidbits Ivan Tomek 04/06/2001

3

global interest), or class variables, or keys in a pool dictionary1. It turns out that they are keys in the pooldictionary TextConstants because they are used by several other classes as well. When you inspect thisdictionary, you will find that Ctrlt ,Ctrlf, Cut, Paste, and BS are among many characters that are assigned

Page 663: toc.prn - Squeak
Page 664: toc.prn - Squeak
Page 665: toc.prn - Squeak

Introduction to Smalltalk - Appendix 8 - Tidbits Ivan Tomek 04/06/2001

6

A ComposedText contains Text and has access to a dictionary of text styles via its TextAttributescomponent (Figure A.8.2). The TextAttributes object does not itself contain the style dictionary but hasaccess to a

Page 666: toc.prn - Squeak
Page 669: toc.prn - Squeak

Introduction to Smalltalk - Appendix 8 - Tidbits Ivan Tomek 04/06/2001

10

Page 670: toc.prn - Squeak
Page 671: toc.prn - Squeak

Introduction to Smalltalk - Appendix 8 - Tidbits Ivan Tomek 04/06/2001

12

(fontSize := Dialog choose: 'Which font size do you want?'

Page 672: toc.prn - Squeak
Page 673: toc.prn - Squeak
Page 674: toc.prn - Squeak
Page 675: toc.prn - Squeak
Page 676: toc.prn - Squeak
Page 677: toc.prn - Squeak
Page 678: toc.prn - Squeak