The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Fun WIth CheckBoxes

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Fun WIth CheckBoxes Posted: Mar 30, 2007 1:13 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Fun WIth CheckBoxes
Feed Title: Pollock
Feed URL: http://www.cincomsmalltalk.com/rssBlog/pollock-rss.xml
Feed Description: Pollock - the next VW GUI
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Pollock

Advertisement

Check(box) Mate

Today we're going to use CheckBoxes, and show how to use some general features of Widgetry along the way.

What we want to do is create a user interface with 4 CheckBoxes. They will be labeled "Disable", "Hide", "Turn On" and "Example". We'll then hook up "Disable" to disable/enable the Example CheckBox, "Hide" to hide/show the Example CheckBox and "Turn On" to check/un-check the Example CheckBox.

We'll start off with a UserInterface subclass, we'll name CheckBoxWork, and give it four instance variables, one each for each CheckBox:

	Smalltalk defineClass: #CheckBoxWork
		superclass: #{Widgetry.UserInterface}
		indexedType: #none
		private: false
		instanceVariableNames: 'disable hide turnOn example '
		classInstanceVariableNames: ''
		imports: 'private Widgetry.* '
		category: '(none)'

We'll start off with a simple #createInterface, which creates and adds each CheckBox and puts them in the window:

	createInterface

		disable := CheckBox new.
		disable addComponent: (DisplayLabel string: '&Disable').
		disable frame inset: 10 @ 10.
		disable frame extent: 100 @ 30.
		self addComponent: disable.
		hide := CheckBox new.
		hide addComponent: (DisplayLabel string: '&Hide').
		hide frame inset: 10 @ 50.
		hide frame extent: 100 @ 30.
		self addComponent: hide.
		turnOn := CheckBox new.
		turnOn addComponent: (DisplayLabel string: '&Turn On').
		turnOn frame inset: 10 @ 90.
		turnOn frame extent: 100 @ 30.
		self addComponent: turnOn.
		example := CheckBox new.
		example addComponent: (DisplayLabel string: '&Example').
		example frame inset: 10 @ 130.
		example frame extent: 100 @ 30.
		self addComponent: example.

And this is what our window looks like:

The above is pretty boring in that it repeats the same basic code four times. However, there are a few notes. Like with a Button, we use #addComponent: to add a label to a CheckBox. This is really a bit of polymorphism sugar, in that unlike with windows you really don't add any pane to a CheckBox, just a DisplayLabel. Also note that just like all UI frameworks, adding a "&" to a DisplayLabel's text puts an underline on the next character in that string.

Some standard behavior we see: When the window starts up, the first widget added to the window is in focus. Because we added "&" to the labels, if we press "Alt-D" or H or T or E, it will change focus to the appropriate CheckBox, and it toggles it. Finally, pressing Tab or BackTab cycles through the widgets.

Dippity-Do

Now, we'll create a #hookupInterface method, to add the behavior for actually affecting our Example CheckBox:

	hookupInterface

		disable when: ValueChanged send: #changeEnablement to: self.
		hide when: ValueChanged send: #changeVisiblity to: self.
		turnOn when: ValueChanged send: #changeCheck to: self.

A CheckBox broadcasts the following Announcements: ValueAboutToChange, ValueChanging, ValueChanged, TurnedOn and TurnedOff.

When a check box's check is changed, it first broadcasts ValueAboutToChange, before the change takes place, and that is vetoable. Then, it broadcasts ValueChanging before the change, then ValueChanged after the change, and finally, TurnedOn or TurnedOff, depending on the final state of the CheckBox.

In our case, we only really care about after the change has taken place.

If we re-open our CheckBoxWork, and try to click any of the first three check boxes, we get errors, saying in effect we haven't created the #change.... method(s). So, here they are. Style note: I usually put these methods into a protocol named 'announcement handlers':

	changeEnablement

		example beEnabled: disable isChecked not.

	changeVisiblity

		example beVisible: hide isChecked not.

	changeCheck

		example beChecked: turnOn isChecked.

As you see, we like Hamlet. A lot of the simple toggle APIs in Widgetry have to be / not to be naming. Here, we use the isChecked test of each of the CheckBoxes, to change the state of the example CheckBox.

Here's a look at it with some of the checks checked:

You'll notice that even though the example CheckBox is disabled, you can turn it on or off programmatically, but of course, not with the mouse or via the Alt-E shortcut. You can even hide the CheckBox, and update it's state from the others.

Extra Credit

Let's make sure that if example is checked via the keyboard or the mouse, that Turn On is updated to match:

	hookupInterface

		disable when: ValueChanged send: #changeEnablement to: self.
		hide when: ValueChanged send: #changeVisiblity to: self.
		turnOn when: ValueChanged send: #changeCheck to: self.
		example when: ValueChanged send: #updateTurnOn to: self
		
	updateTurnOn
	
		turnOn beChecked: example isChecked.
And So It Goes
Sames

Read: Fun WIth CheckBoxes

Topic: Authenticated RSS feeds Previous Topic   Next Topic Topic: More pictures from Tuesday at SPA

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use