//======================================================================//  PROJECT:            _programming.java_//  FILE:               GalaEvents.java//  PURPOSE:            Chapter 6 lablet, first of two//  VERSION:            2.1//  TARGET:             Java v1.1//  UPDATE HISTORY:     2.1     10/14/98    put VERBOSE flag back in//                      2.0     4/21/98     1.1 event model//                      1.1     6/26/97     removed VERBOSE event reporting//                      1.0     11/21/96    the version in _p.j_//======================================================================//------------------------------- NOTES --------------------------------/*    This class extends (literally) our Chapter 3 lablet, Gigobite, to report    on a variety of user interface events as they happen.  Notice that     GalaEvents is an Applet by virtue of the fact that it extends another    Applet, Gigobite.        NOTE: We should warn you that when running this program you shouldn't be    surprised if some of the events are not reported.  As of this writing,    it's still a sad fact of life that some Java environments aren't as diligent    as they should be in generating and catching events. *///------------------------------ IMPORTS -------------------------------import java.awt.*;import java.awt.event.*;import java.io.*;//========================= GalaEvents CLASS ===========================/** * We use all of Gigobite's methods for initializing and painting, so we * don't have to specify them here; what we're adding are the functions for * recognizing user interface events.  These methods allow the applet to * respond to mouse and keyboard events within it.  In each case, the * "response" is simply to display a message. * <BR> * In the Java 1.1 event model, we need to register a listener for each * category of event we wish to handle.  In this program, the applet itself * is the listener, so we use "this" to indicate that the GalaEvents * object will implement all the methods of the five interfaces. */public class GalaEvents extends     Gigobite                        implements  MouseListener,                                    MouseMotionListener,                                    KeyListener,                                    ActionListener,                                    ItemListener{    /*       Because a typical Component generates a *lot* of MouseEvents,       we give you the opportunity to turn mouse reporting off.       Set VERBOSE to true to get reports on all mouse events       and set it to false to turn reporting off (by not registering       any listener for mouse events).    */    private final boolean VERBOSE = true;        /**     * The only thing we need to do to construct a GalaEvents object     * is register the listeners for all the events.  Notice that the     * MouseListener and the MouseMotionListener both receive any     * events generated by the applet, while, for instance, the ActionListener     * receives events only from the order button and the sandwiches, drinks,     * and sides lists.     */    public GalaEvents()    {    	if (VERBOSE)    // Turn mouse reporting on or off.    	{            addMouseListener(this);            addMouseMotionListener(this);        }        addKeyListener(this);                order.addActionListener(this);        sandwiches.addActionListener(this);        drinks.addActionListener(this);        sides.addActionListener(this);                superSize.addItemListener(this);        sizes.addItemListener(this);        sandwiches.addItemListener(this);        drinks.addItemListener(this);        sides.addItemListener(this);    }	    //--------------------- MouseListener METHODS ----------------------        public void mouseClicked(MouseEvent e)    {        System.out.println("MOUSE_CLICKED at " + e.getX() + ", " + e.getY());    }        public void mouseEntered(MouseEvent e)    {        System.out.println("MOUSE_ENTERED at " + e.getX() + ", " + e.getY());    }        public void mouseExited(MouseEvent e)    {        System.out.println("MOUSE_EXITED at " + e.getX() + ", " + e.getY());    }        public void mousePressed(MouseEvent e)    {        System.out.println("MOUSE_PRESSED at " + e.getX() + ", " + e.getY());    }        public void mouseReleased(MouseEvent e)    {        System.out.println("MOUSE_RELEASED at " + e.getX() + ", " + e.getY());    }	    //------------------ MouseMotionListener METHODS -------------------        public void mouseDragged(MouseEvent e)    {        System.out.println("MOUSE_DRAGGED to " + e.getX() + ", " + e.getY());    }        public void mouseMoved(MouseEvent e)    {        System.out.println("MOUSE_MOVED to " + e.getX() + ", " + e.getY());    }	    //---------------------- KeyListener METHODS -----------------------        /*       There are three methods in the KeyListener interface.  Just to do       things differently, we're only responding when a KEY_TYPED event       occurs.  However, we still have to provide implementations of all       the methods in any interface, and we do so here by providing       empty method bodies for the unused methods.  See the description       in the SketchPad applet for a different way of not handling events.    */        public void keyPressed(KeyEvent e)    { }        public void keyReleased(KeyEvent e)    { }        public void keyTyped(KeyEvent e)    {        System.out.println("KEY_TYPED: " + e.getKeyChar());    }        //-------------------- ActionListener METHODS ----------------------        /*       There's only one method in the ActionListener interface,       actionPerformed().  An ActionEvent is generated when a button is       clicked, a List item is double-clicked, a menu item is chosen,       or the user presses the Return key while in a TextField.  In our       implementation of this handler, we're only interested in the first two       of these events, namely a click on the order button or a double-click       on an item in the sandwich, drink, or sides Lists.    */        public void actionPerformed(ActionEvent e)    {        Object source = e.getSource();        if (source == order)        {            System.out.println("ACTION: order button clicked");        }        else if (source == sandwiches)        {            System.out.println("ACTION: Sandwich chosen: " + sandwiches.getSelectedItem());        }        else if (source == drinks)        {            System.out.println("ACTION: Drink chosen: " + drinks.getSelectedItem());        }        else if (source == sides)        {            System.out.println("ACTION: Side order chosen: " + sides.getSelectedItem());        }    }        //--------------------- ItemListener METHODS -----------------------        /*       As with ActionListener, there's only one method in the ItemListener       interface, itemStateChanged().  An ItemEvent is triggered when the       user selects or deselects a Checkbox, a Choice item, a List item,       or a CheckboxMenu item.  In this program, we're interested in an       event that came from the superSize Checkbox, the size Choice,       or one of the sandwiches, drinks, or sides lists.    */        public void itemStateChanged(ItemEvent e)    {        Object source = e.getSource();        if (source == superSize)           // We're in the Checkbox        {            if (e.getStateChange() == ItemEvent.SELECTED)            {                System.out.println("ITEM: Supersize selected");            }            else            {                System.out.println("ITEM: Supersize deselected");            }        }        else if (source == sizes)          // We're in the Choice        {            System.out.println("Size choice made: " + e.getItem());        }        else                               // We're in one of the Lists        {            // First, find out which list generated the event            String message = "ITEM: ";            if (source == sandwiches)            {                message += "Sandwich ";            }            else if (source == drinks)            {                message += "Drink ";            }            else if (source == sides)            {                message += "Side order ";            }                        // Then, get the item chosen            message += getItemString(e);     // use the function defined below                        // Finally, determine if the item was selected or deselected.            if (e.getStateChange() == ItemEvent.SELECTED)            {                message += " selected";            }            else            {                message += " deselected";            }            System.out.println(message);        }    }        /**     * Don't even try to figure out what's going on here.  This highly     * arcane code is just so we can get the String representation of     * an item chosen in a List in the function above.     */    private String getItemString(ItemEvent e)    {        String itemStr = "" + e.getItem();        // item Object --> String        Integer value = Integer.valueOf(itemStr); // String --> Integer        int index = value.intValue();             // Integer --> int        List src = (List)(e.getSource());         // source Object --> List        return src.getItem(index);                // int-th item in List --> String    }}