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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

21.5.5 Running the editor<br />

You have now done enough to open a BoxDrawEditor. Do this by evaluating:<br />

BoxDrawEditor open.<br />

in a Workspace window.<br />

The BoxDrawFigure is the last icon in the ToolPalette. However, all you c<strong>an</strong> do is place a<br />

BoxDrawFigure in a drawing <strong><strong>an</strong>d</strong> ch<strong>an</strong>ge the label. Note to ch<strong>an</strong>ge the label, select the Text tool (the<br />

big ‘T’) <strong><strong>an</strong>d</strong> hold down the shift button while selecting the label with the left mouse button.<br />

21.5.6 Adding h<strong><strong>an</strong>d</strong>les<br />

To be able to link a BoxDrawFigure to <strong>an</strong>other figure we need to add <strong>an</strong>other h<strong><strong>an</strong>d</strong>le. H<strong><strong>an</strong>d</strong>les<br />

provide a way of telling a figure that so me operation should be performed. The most common h<strong><strong>an</strong>d</strong>les<br />

are for shrinking or growing a figure. For example, h<strong><strong>an</strong>d</strong>les on the four corners of a figure. We w<strong>an</strong>t to<br />

add <strong>an</strong>other h<strong><strong>an</strong>d</strong>le which will allow us to link one BoxDrawFigure to <strong>an</strong>other BoxDrawFigure.<br />

By default we have inherited the h<strong><strong>an</strong>d</strong>les defined in the figure. These h<strong><strong>an</strong>d</strong>les are on the four corners<br />

of our figure <strong><strong>an</strong>d</strong> allow the user to resize the figure. The method h<strong><strong>an</strong>d</strong>les in the protocol “accessing”<br />

on the class Figure provides for this. We w<strong>an</strong>t to add <strong>an</strong> additional h<strong><strong>an</strong>d</strong>le which allows links to be<br />

created. We shall select the center point. However we do not w<strong>an</strong>t to lose the ability to resize our<br />

figures, we shall therefore define a new h<strong><strong>an</strong>d</strong>les method in the “accessing” protocol of the<br />

BoxDrawFigure which uses super to obtain the original h<strong><strong>an</strong>d</strong>les <strong><strong>an</strong>d</strong> then add our h<strong><strong>an</strong>d</strong>le to this<br />

collection of h<strong><strong>an</strong>d</strong>les:<br />

h<strong><strong>an</strong>d</strong>les<br />

"Add a h<strong><strong>an</strong>d</strong>le at my center that draws connections to other figures"<br />

^(super h<strong><strong>an</strong>d</strong>les) add: (Comm<strong><strong>an</strong>d</strong>H<strong><strong>an</strong>d</strong>le<br />

connectionFor: self<br />

at: #center:<br />

class: LineFigure); yourself<br />

We now have a fully functional BoxDraw application. Open the BoxDrawEditor again <strong><strong>an</strong>d</strong> place<br />

some BoxDrawFigures on the drawing area. Now select the “arrow” icon on the icon menu. This is<br />

the top icon. Next select one of the BoxDrawFigures. You should see a small square (a h<strong><strong>an</strong>d</strong>le) in<br />

the center of the figure. Click on this <strong><strong>an</strong>d</strong> drag the resulting line to <strong>an</strong>other BoxDrawFigure. This should<br />

connect the two figures together. Now move the figures around. You should find that the lin k between<br />

the two figures moves with you.<br />

21.5.7 Ch<strong>an</strong>ging a figure menu<br />

To complete this application we shall ch<strong>an</strong>ge the menu available when clicking on a BoxDrawFigure<br />

using the middle mouse button. At present the user has the option to show visible area, hide v isible area<br />

<strong><strong>an</strong>d</strong> reset visible area as well as the st<strong><strong>an</strong>d</strong>ard cut, copy <strong><strong>an</strong>d</strong> paste options. We only w<strong>an</strong>t the user to have<br />

access to the st<strong><strong>an</strong>d</strong>ard cut, copy <strong><strong>an</strong>d</strong> paste options. To do this we redefine the menu method inherited<br />

from CompositeFigure. This method is defined within the accessing protocol:<br />

menu<br />

"Define a pop up Menu for use with BoxDrawFigure"<br />

^Menu labels: 'cut\copy\paste' withCRs<br />

values: #(#cut #copy #paste )<br />

21.5.8 The (pattern) solution statement<br />

The ability to specify h<strong><strong>an</strong>d</strong>les which perform speci fic functions allows operations to be performed<br />

through operation of the mouse. The use of a Comm<strong><strong>an</strong>d</strong>H<strong><strong>an</strong>d</strong>le with a LineFigure c<strong>an</strong> be used to link<br />

two figures together.<br />

179

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

Saved successfully!

Ooh no, something went wrong!