HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Implementing Callback-Style Support for Java's AWT"
Vol. 3, Issue 4, p. 54

	

Listing 1.
  
/**  
 * Copyright (c) 1997 Daniel Dee  
 * Description:  
   Package contains common classes for the ViviGraphics Widget  
   Toolkit - a callback-based toolkit.  
   Originally, part of the Eva Toolkit - the prototype  
   implementation.  
 */  
package com.wigitek.vivigraphics.widget.common;  

import java.awt.Event;  

/**  
 * This class implements the callback mechanism that is required  
   to support the Widget interface. You will typically subclass  
   this to implement your own activate method in order to perform  
   functions specific to events that trigger the callback.  
 * @version $Revision$  
 * @author Originally written by Daniel Dee, 3/17/97  
 * @author Last updated by $Author$, $Date$  
 */  
public abstract class Callbackable extends Object  
{  
    /**  
     * Performs functions specific to the event that triggers this  
       callback. By default, it prints information about the activating  
       object when this callback was registered and the activating event.   
       You should normally override this method to perform function   
       specific to your application.  
     * @param evt        the event that triggers this callback  
     */  
    public abstract boolean activate( Event e );  
}  

Listing 2.
  
/**  
 * Copyright (c) 1997 Daniel Dee  
 * Description:  
   Package contains common classes for the Vivigraphics Widget  
   Toolkit - a callback-based callback-based toolkit.  
   Originally, part of the Eva Toolkit - the prototype  
   implementation.  
 */  
package com.wigitek.vivigraphics.widget.gui;  

import java.awt.Event;  
import java.util.Vector;  
import java.io.IOException;  
import com.wigitek.vivigraphics.widget.common.Callbackable;  

/**  
 * This class chains callbacks associated with a single callback type  
   together.  
 * @version $Revision$  
 * @author Originally written by Daniel Dee, 3/17/97  
 * @author Last updated by $Author$, $Date$  
 */  
public class CallbackList extends Vector  
{  
    /**  
     * Constructs a CallbackList. Creates a Vector with 100 initial elements  
       and a increment size of 100.  
     */  
    public CallbackList()  
    {  
        super(100, 100);  
    }  

    /**  
     * Removes a Callbackable from the CallbackList.  
     * @param cb the callback  
     */  
    public boolean remove( Callbackable cb )  
    {  
        boolean returnValue = false;  

        for( int i=0; i < size(); i++ )  
        {  
            Callbackable _cb = (Callbackable)elementAt(i);  
            if( _cb == cb )  
            {  
                removeElementAt(i);  
                returnValue = true;  
            }  
        }  
        return returnValue;  
    }  

    /**  
     * Removes a CallbackClientPair at the indexed position.  
     * @param index the position of the CallbackClientPair in the  
                    CallbackList.  
     */  
    public boolean removeAt( int index )  
    {  
        try  
        {  
            removeElementAt( index );  
            return true;  
        }  
        catch( ArrayIndexOutOfBoundsException e )  
        {  
            return false;  
        }  
    }  

    /**  
     * Adds a callback to a chain.  
     * @param callback     the callback  
     * @return the Callbackable  
     */  
    public Callbackable add( Callbackable cb )  
    {  
     if( cb == null )  
      cb = defaultCB;  
        addElement( cb );  
        return cb;  
    }  

    /**  
     * Calls all the callbacks in this chain.  
     * @param evt the event that triggers this callbacklist  
     * @return true if event has been processed by the callback  
               and no further processing is necessary; false  
               if further processing from the activating object  
               is required.  
     */  
    public boolean activate( Event e )  
    {  
        boolean returnValue = true;  

        for( int i=0; i < size(); i++ )  
        {  
            Callbackable cb = (Callbackable)elementAt(i);  
            returnValue = cb.activate( e );  
        }  

        return returnValue;  
    }  
      
    private CallbackListDefaultCallbackable defaultCB =   
       new CallbackListDefaultCallbackable();  
}  

/**  
 * Default Callbackable.  
 */  
class CallbackListDefaultCallbackable extends Callbackable  
{  
    /**  
     * Prints information about the activating  
       object.  
     * @param evt        the event that triggers this callback  
     */  
    public boolean activate( Event evt )  
    {  
        System.out.println( "Calling object is " + evt.target.toString() + "." );  
        System.out.println( "Activating event is " + evt.toString() + "." );  
        return true;  
    }  
}  

Listing 3.
  
/**  
 * Copyright (c) 1997 Daniel Dee  
 * Description:  
   Package contains common classes for the Vivigraphics Widget  
   Toolkit - a callback-based callback-based toolkit.  
   Originally, part of the Eva Toolkit - the prototype  
   implementation.  
 */  
package com.wigitek.vivigraphics.widget.gui;  

import java.awt.Event;  
import java.awt.AWTEvent;  
import java.util.Vector;  
import java.io.IOException;  
import java.awt.event.*;  
import com.wigitek.vivigraphics.widget.common.Callbackable;  

/**  
 * This class chains callbacks associated with a single callback type  
   together.  
 * @version $Revision$  
 * @author Originally written by Daniel Dee, 3/17/97  
 * @author Last updated by $Author$, $Date$  
 */  
public class CallbackList extends Vector  
                     implements ActionListener,  
                                AdjustmentListener,  
                                ComponentListener,  
                                ContainerListener,  
                                FocusListener,  
                                ItemListener,  
                                KeyListener,  
                                MouseListener,  
                                MouseMotionListener,  
                                TextListener,  
                                WindowListener  
{  
    /**  
     * Constructs a CallbackList. Creates a Vector with 100 initial elements  
       and a increment size of 100.  
     */  
    public CallbackList()  
    {  
        super(100, 100);  
    }  

    /**  
     * This method is called when an action event occurs inside the  
       source object. The corresponding callbacks added  
       by the user are activated.  
     * @param evt the event  
     */  
    public void actionPerformed(ActionEvent evt)  
    {  
     Event e = new Event( evt.getSource(), evt.getID(), evt );  
        activate(e);  
    }  

 // TO BE IMPLEMENTED  
    public void adjustmentValueChanged(AdjustmentEvent evt) {}  
    public void componentResized(ComponentEvent evt) {}  
    public void componentMoved(ComponentEvent evt) {}  
    public void componentShown(ComponentEvent evt) {}  
    public void componentHidden(ComponentEvent evt) {}  
    public void componentAdded(ContainerEvent evt) {}  
    public void componentRemoved(ContainerEvent evt) {}  
    public void focusGained(FocusEvent evt) {}  
    public void focusLost(FocusEvent evt) {}  
    public void itemStateChanged(ItemEvent evt) {}  
    public void keyTyped(KeyEvent evt) {}  
    public void keyPressed(KeyEvent evt) {}  
    public void keyReleased(KeyEvent evt) {}  
    public void mouseClicked(MouseEvent evt) {}  
    public void mousePressed(MouseEvent evt) {}  
    public void mouseReleased(MouseEvent evt) {}  
    public void mouseEntered(MouseEvent evt) {}  
    public void mouseExited(MouseEvent evt) {}  
    public void mouseDragged(MouseEvent evt) {}  
    public void mouseMoved(MouseEvent evt) {}  
    public void textValueChanged(TextEvent evt) {}  
    public void windowOpened(WindowEvent evt) {}  
    public void windowClosing(WindowEvent evt) {}  
    public void windowClosed(WindowEvent evt) {}  
    public void windowIconified(WindowEvent evt) {}  
    public void windowDeiconified(WindowEvent evt) {}  
    public void windowActivated(WindowEvent evt) {}  
    public void windowDeactivated(WindowEvent evt) {}  

    /**  
     * Removes a Callbackable from the CallbackList.  
     * @param cb the callback  
     */  
    public boolean remove( Callbackable cb )  
    {  
        boolean returnValue = false;  

        for( int i=0; i < size(); i++ )  
        {  
            Callbackable _cb = (Callbackable)elementAt(i);  
            if( _cb == cb )  
            {  
                removeElementAt(i);  
                returnValue = true;  
            }  
        }  
        return returnValue;  
    }  

    /**  
     * Removes a CallbackClientPair at the indexed position.  
     * @param index the position of the CallbackClientPair in the  
                    CallbackList.  
     */  
    public boolean removeAt( int index )  
    {  
        try  
        {  
            removeElementAt( index );  
            return true;  
        }  
        catch( ArrayIndexOutOfBoundsException e )  
        {  
            return false;  
        }  
    }  

    /**  
     * Adds a callback to a chain.  
     * @param callback     the callback  
     * @return the Callbackable  
     */  
    public Callbackable add( Callbackable cb )  
    {  
     if( cb == null )  
      cb = defaultCB;  
        addElement( cb );  
        return cb;  
    }  

    /**  
     * Calls all the callbacks in this chain.  
     * @param evt the event that triggers this callbacklist  
     * @return true if event has been processed by the callback  
               and no further processing is necessary; false  
               if further processing from the activating object  
               is required.  
     */  
    public boolean activate( Event e )  
    {  
        boolean returnValue = true;  

        for( int i=0; i < size(); i++ )  
        {  
            Callbackable cb = (Callbackable)elementAt(i);  
            returnValue = cb.activate( e );  
        }  

        return returnValue;  
    }  
      
    private CallbackListDefaultCallbackable defaultCB =   
       new CallbackListDefaultCallbackable();  
}  

/**  
 * Default Callbackable.  
 */  
class CallbackListDefaultCallbackable extends Callbackable  
{  
    /**  
     * Prints information about the activating  
       object.  
     * @param evt        the event that triggers this callback  
     */  
    public boolean activate( Event evt )  
    {  
        System.out.println( "Calling object is " + evt.target.toString() + "." );  
        System.out.println( "Activating event is " + evt.toString() + "." );  
        return true;  
    }  
} 
 
Listing 4.
  
/**  
 * Copyright (c) 1997 Daniel Dee  
 * Description:  
   Package contains common classes for the ViviGraphics Widget  
   Toolkit - a callback-based toolkit.  
   Originally, part of the Eva Toolkit - the prototype  
   implementation.  
 */  
package com.wigitek.vivigraphics.widget.gui;  

import java.awt.Event;  
import java.lang.String;  
import com.wigitek.vivigraphics.widget.common.Callbackable;  
import com.wigitek.vivigraphics.widget.gui.CallbackList;  

/**  
 * This class extends the java.awt.Button class to support  
   the callback mechanism by implementing the Widget interface.  
 * @version $Revision$  
 * @author Originally written by Daniel Dee, 2/21/97  
 * @author Last updated by $Author$, $Date$  
 * @see Widget  
 */  
public class Button extends java.awt.Button  
{  
    /**  
     * Constructs a Button with no label and with name "unnamed".  
     */  
    public Button()  
    {  
        this( "", "unnamed" );  
    }  

    /**  
     * Constructs a Button with a string label and with name "unnamed".  
     * @param label the specified label  
     */  
    public Button( String label )  
    {  
        this(label, "unnamed");  
    }  

    /**  
     * Constructs a Button with a string label and with given name.  
     * @param label the specified label  
     * @param name  the specified name  
     */  
    public Button( String label, String name )  
    {  
        super(label);  
        this.name = name;  
    }  

    /**  
     * Returns a String that represents the value of this Object.  
       Overrides the method in java.lang.Object.  
     * @return a String  
     * @see Object  
     */  
    public String toString()  
    {  
        String string = super.toString();  
        int length = string.length();  
        return string.substring( 0, length-1 ) + ",name=" + name + "]";  
    }  

     /**  
     * Registers a callback for the top level frame. Top level frame  
       currently recognizes only ACTIVATE_CALLBACK when the particular  
       instance of the button is pushed. Unknown callbacks  
       are ignored.  
     * @param callbackName the type of callback to register  
     * @param callback     the object of the Callbackable class that will activated  
                           when trigger by the callback type given by callbackName  
     * @see Callbackable  
     */  
    public void addCallback( String callbackName, Callbackable callback )  
    {  
        if( callbackName.compareTo(ACTIVATE_CALLBACK) == 0 )  
            activateCallbackList.add( callback );  
    }  

    /**  
     * This method is called when an action event occurs inside this  
       Button. Depending on the event, the corresponding callbacks added  
       by the user are activated.  
     * The method returns true to indicate that it has successfully  
       handled the action; or false if the event that triggered  
       the action should be passed up to the component's parent.  
     * @param evt the event  
     * @return true if the event has been handled and no further  
               action is necessary; false if the event is to be  
               given to the component's parent.  
     */  
    public boolean action(Event evt, Object arg)  
    {  
        activateCallbackList.activate( evt );  
        return true;  
    }  

    // Name of instance of this class.  
    private String name;  

    // Callback when button is pushed.  
    private CallbackList activateCallbackList = new CallbackList();  
    public static final String ACTIVATE_CALLBACK = "activateCallback";  
}  

Listing 5.
  
/**  
 * Copyright (c) 1997 Daniel Dee  
 * Description:  
   Package contains common classes for the ViviGraphics Widget  
   Toolkit - a callback-based toolkit.  
   Originally, part of the Eva Toolkit - the prototype  
   implementation.  
 */  
package com.wigitek.vivigraphics.widget.gui;  

import java.awt.Event;  
import java.lang.String;  
import com.wigitek.vivigraphics.widget.common.Callbackable;  
import com.wigitek.vivigraphics.widget.gui.CallbackList;  

/**  
 * This class extends the java.awt.Button class to support  
   the callback mechanism by implementing the Widget interface.  
 * @version $Revision$  
 * @author Originally written by Daniel Dee, 2/21/97  
 * @author Last updated by $Author$, $Date$  
 * @see Widget  
 */  
public class Button extends java.awt.Button  
{  
    /**  
     * Constructs a Button with no label and with name "unnamed".  
     */  
    public Button()  
    {  
        this( "", "unnamed" );  
    }  

    /**  
     * Constructs a Button with a string label and with name "unnamed".  
     * @param label the specified label  
     */  
    public Button( String label )  
    {  
        this(label, "unnamed");  
    }  

    /**  
     * Constructs a Button with a string label and with given name.  
     * @param label the specified label  
     * @param name  the specified name  
     */  
    public Button( String label, String name )  
    {  
        super(label);  
        this.name = name;  
        addActionListener(activateCallbackList);  
    }  

    /**  
     * Returns a String that represents the value of this Object.  
       Overrides the method in java.lang.Object.  
     * @return a String  
     * @see Object  
     */  
    public String toString()  
    {  
        String string = super.toString();  
        int length = string.length();  
        return string.substring( 0, length-1 ) + ",name=" + name + "]";  
    }  

    /**  
     * Registers a callback for the top level frame. Top level frame  
       currently recognizes only ACTIVATE_CALLBACK when the particular  
       instance of the button is pushed. Unknown callbacks  
       are ignored.  
     * @param callbackName the type of callback to register  
     * @param callback     the object of the Callbackable class that will be activated  
       when triggered by the callback type given by callbackName  
     * @see Callbackable  
     */  
    public void addCallback( String callbackName, Callbackable callback )  
    {  
        if( callbackName.compareTo(ACTIVATE_CALLBACK) == 0 )  
            activateCallbackList.add( callback );  
    }  

    // Name of instance of this class.  
    private String name;  

    // Callback when button is pushed.  
    private CallbackList activateCallbackList = new CallbackList();  
    public static final String ACTIVATE_CALLBACK = "activateCallback";  
}  

Listing 6
  
import java.awt.Frame;  
import com.wigitek.vivigraphics.widget.common.*;  
import com.wigitek.vivigraphics.widget.gui.*;  
      
public class ButtonTest extends Frame  
{  
 public ButtonTest( String title )  
 {  
  super(title);  
 }  
 /**  
 * This is the main program to test the Button class.  
 * @param args unused  
 */  
 public static void main( String args[] )  
 {  
   ButtonTest applic = new ButtonTest( "Eva Toolkit: Button Test" );  
   Button button = new Button( "Click me" );  
   button.addCallback( Button.ACTIVATE_CALLBACK, null );  
   applic.add( "Center", button );  
     
   // Note that one can easily make this application completely   
   // Java 1.0 & Java 1.1-compliant by using setSize and   
   // setVisible here and implementing them in terms of resize and    // show/hide in Java 1.0 with a callback enabled subclass of      // Frame.  
   applic.resize(300,75);  
   applic.show();  
 }  
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.