5. Events in OS/2 and Java


Event-Driven Programming

  • GUI is started
  • GUI components wait in an "event loop" until something happens
  • The system sends an event (aka message) to the GUI
  • GUI wakes up, examines the message and performs processing
  • Communication is _indirect_
    • Tell event queue that you want to send a message
    • It sends the message when it can
    • Target component reads events from a queue

OS/2 Messages

  • After GUI has been set up, code enters event loop
  • Uses WinGetMsg() to grab the next event sent to it
  • Needs explicit event loop

Java Events

  • Not very different
  • Event loop hidden
  • After GUI is set up, setVisible() is called
    • Event loop runs "under the covers"
  • Two models
    • "Old-style" events (JDK 1.0.x)
    • "Delegation-model" events (JDK 1.1.x)
    • VisualAge generates Delegation-model events

Old-Style Events

  • System sends event to a component
    • Could be mouseclick on a component, keypress, etc
  • If component wants to handle it, it does
    • Usually the component "consumes" the event
  • If component doesn't care about the event, it passes it up to it's parent Container

Delegation-Model Events

  • Objects known as event sources can post events
  • Objects can be registered as Listeners
    • Lets system know they care about a specific event on a specific component
    • Normally, several objects can listen to events
  • When an event source decides to post an event, it posts it to all registered Listeners
  • The Listener handles the event by providing specificly-named routines that the system event loop calls.

VisualAge Event Setup

  • VisualAge implements delegation-model events
  • The Visual Composition Editor helps set up event handlers by drawing connections
  • Connections:
    • Property-to-Property
    • Event-to-Property
    • Event-to-Method
    • Event-to-Script
    • Property-to-Method
    • Property-to-Script
    • Parameter

Property-to-Property Connections

  • Used to keep two object properties in synch
  • One of the properties must be writable
  • If the selected target property is not writable and the selected source is, VisualAge automatically reverses the connection
  • If the source is bound (property change event will be posted)
    • The target value will be initialized and updated when the source changes
  • If the source is not bound
    • The target will be initialized, but not updated when the source changes

Event-to-Property Connections

  • Used to update a property when an event occurs
  • The property must be writable
  • Dotted line means more information (a parameter) is needed.

Event-to-Method Connections

  • Used to call a method when an event occurs
  • The method must be part of the bean interface
  • If a method doesn't do everything you need, use an Event-to-Script connection
  • Dotted line means more information (a parameter) is needed.

Event-to-Script Connections

  • Used to call a method when a property has changed
  • The method is not part of the public interface of the bean (usually private or protected)
  • VisualAge provides a place to define these methods as you need them
  • Dotted line means more information (a parameter) is needed.

Property-to-Method Connections

  • Used to call a method when an event occurs
  • The source property must be bound
  • Dotted line means more information (a parameter) is needed.

Parameter Connections

  • Used to provide parameters for other connections
  • The target value can be a property or a method. Note that a method may need other properties.
  • Dotted line means more information (a parameter) is needed.

Constant Parameters

  • If the value of a parameter to any connection should be a constant, you may set it.
  • Bring up the property sheet for the connection and press "Set parameters..."

Multiple Connections

  • A Component may have multiple connections for a given event (or property change)
  • Order is very important!
  • Use the "Reorder connections..." option in the component's pop-up menu to see and possible reorder its connections.

Editing the VisualAge-Generated Code

  • VisualAge re-generates the connection and GUI setup code often.
  • If you must edit the code, stay within the "User code" comments!
  • Instead of editing code, try using Event-to-Script connections where possible

Editing the VisualAge-Generated Code (continued)

  • Use the Visual Composition Editor (VCE) to help document your design.
    • Other programmers can look at connections to see what you had intended.
  • Use Interchange-Format for exporting code that others will work on.
    • Exporting by .java or .class file will lose VCE connection information.

A Few Tricks

  • Factoring Connections
  • Tear-off properties
  • Casting Properties

Factoring Connections

  • Use this strategy if several components need to perform the same actions
  • Create a "trigger" object that has a bound property (most likely a reference to the component that was activated)
  • Connect the "actionPerformed" event of the components to set the property of the trigger class
  • Then, connect the property change event for that property to the whatever connections you need.
    • Careful -- if the target is a property, make sure you go into "all features" and explicitly select the event for the property.

Tear-Off Properties

  • If you need to access features of a property of a bean, you can "tear-off" that property.
  • This creates a variable that is bound to the property with a Property-to-Property connection.
  • The variable is kept in synch with the property.
  • You can connect to the variable as though it were a bean of the property's type.

Casting Properties

  • What if the property is of a parent class type?
  • For example, Containers have a layout property, but you may know it's a CardLayout and need to access its next() method.
  • Create a new variable bean of the type you want. (CardLayout, for example)
  • Form a Property-to-Property connection between the variable and the property.
  • Make your connections to the variable bean and the propery essentially acts as though it were that type.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.