29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

inst<strong>an</strong>ceVariableNames: ''<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: ‘BoxDraw’<br />

Note that I have put this class in a category called BoxDraw. Also note that I have not had to define<br />

<strong>an</strong>y new inst<strong>an</strong>ce or class variables - it is not necessary as I i nherit all I need from DrawingEditor. In<br />

fact I am not even going to define <strong>an</strong>y inst<strong>an</strong>ce methods for this class. So why have a BoxDrawEditor<br />

class at all? Because I w<strong>an</strong>t a different set of tools in the toolPalette.<br />

21.4.2 Define the set of tools to use<br />

The next s tep is therefore to define the set of tools available to a user of BoxDraw. This is done by<br />

defining a method called defaultTools on the class side. This method is used to create the<br />

ToolPaletteView. This method returns <strong>an</strong> OrderedCollection of creationTools. Every<br />

type of figure will provide a creation tool for example:<br />

LineFigure creationTool.<br />

We are going to use the default set of tools, with the addition of just one figure, called<br />

BoxDrawFigure. We c<strong>an</strong> therefore use the defaultTools method defined in t he superclass of<br />

BoxDrawEditor <strong><strong>an</strong>d</strong> just add a creation tool for BoxDrawFigure to the end of this<br />

OrderedCollection. The code for this method is listed below:<br />

defaultTools<br />

"Answer <strong>an</strong> OrderedCollection of the tools I c<strong>an</strong> use"<br />

^(super defaultTools) add: (BoxDrawFigure creationTool); yourself.<br />

This method should be defined in a class method category called “defaults”.<br />

21.4.3 An example class method<br />

Finally, for the BoxDrawEditor class we shall define a class method protocol called “examples”. In<br />

this protocol we shall define a single method called example1. This method will send the message<br />

open to the BoxDrawEditor which will create a new inst<strong>an</strong>ce of the BoxDrawEditor <strong><strong>an</strong>d</strong> will<br />

cause the graphic editor we are constructing to be displayed.<br />

example1<br />

BoxDrawEditor open.<br />

21.5 BoxDrawFigure<br />

21.5.1 Defining the figure<br />

We now have a drawing editor for BoxDraw with everything we need defined - except what a<br />

BoxDrawFigure is. This is the next step.<br />

We could make a BoxDrawFigure a subclass of a simple figure such as Rect<strong>an</strong>gle, Ellipse<br />

or PolylineFigure. However, we w<strong>an</strong>t to have a text field in the middle of BoxDrawFigure<br />

which will represent the name of the figure. This me<strong>an</strong>s that BoxDrawFigure will have at least one<br />

subfigure, we therefore w<strong>an</strong>t to make BoxDrawFigure a subclass of CompositeFigure.<br />

A CompositeFigure is a figure which possesses subfigures, who determine part or all of the<br />

look of the figure. CompositeFigures are also often used if it is necessary to give a user access to<br />

the subfigures (for example to ch<strong>an</strong>ge the text in the text field of the BoxDrawFigure). The<br />

definition for BoxDrawFigure is given below:<br />

CompositeFigure subclass: #BoxDrawFigure<br />

inst<strong>an</strong>ceVariableNames: ''<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'BoxDraw'<br />

177

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!