// works on Preview and EAv0.1 versions

import com.sun.kjava.*;

/** An example showing event handling options for your basic Spotlet.
 * @author Robert Evans, rbevans@akane.jhuapl.edu
 */ 
public class EventHandling extends Spotlet {
    
    // First some header strings to be used later on
    final String eventHeader = "Event:  ";
    final String locationHeader = "Located at ";
    final String keyHeader = "Key pressed was [";

    // we will have one line of text showing the event
    String eventLine = eventHeader + "<None>";
    // and one line of text showing info about the event
    String infoLine = locationHeader + "<N/A>";
    // I use a flag to keep track if we are continuing a penMove
    // event...this lets me update the graphics quicker
    boolean penMoving = false;

    // Where I start the event and info lines
    int leftMargin = 15;
    // spacing between the two lines
    int line = 15;
    // the Y-coord of the first (eventLine) line
    int firstLine = 60;

    // The EXIT GUI button
    Button exitButton;

    // handle on the singleton Graphics object
    static Graphics g = Graphics.getGraphics();
    
    /**
     * The main method simply creates a Scribble spotlet and
     * registers its event handlers.
     */
    public static void main(String[] args) {
        EventHandling eh = new EventHandling();
  	eh.register(WANT_SYSTEM_KEYS);
//  	eh.register(NO_EVENT_OPTIONS);	    
    }
    
    /**
     * Default constructor creates the GUI components and draws them.
     */
    public EventHandling() {
	exitButton = new Button("Exit",139,145);
	// paint the screen
	paint();
    }
    
    /**
     * Draw the screen
     */
    private void paint() {
	g.clearScreen();
	g.drawString(" EventHandling Demo ", 34, 0, Graphics.INVERT);
	g.drawString(eventLine, leftMargin, firstLine);
	g.drawString(infoLine, leftMargin, firstLine + line);
	exitButton.paint();
    }

    /**
     * Handle a pen down event. If it is in our exit button, we will
     * exit the application, otherwise we will print the data to the screen
     */
    public void penDown(int x, int y) {
	if (exitButton.pressed(x,y)) {
	    // The following is used with the KVM_EA1 version
	    System.exit(0);
	    // The following is used with the KVM_J1 version
	    // Runtime.exit();
	} else {
	    eventLine = eventHeader + "penDown";
	    infoLine = locationHeader + "(" + x + "," + y + ")";
	    paint();
	}
    }

    /**
     * Handle a pen up event.
     */
    public void penUp(int x, int y) {
	penMoving = false;
	eventLine = eventHeader + "penUp";
	infoLine = locationHeader + "(" + x + "," + y + ")";
	paint();
    }

    /**
     * Handle a pen move event.
     */
    public void penMove(int x, int y) {
	eventLine = eventHeader + "penMove";
	// if the pen is still moving, we only need to update the
	// location line
	if (penMoving) {
	    // note the trailing spaces, this "clears" extra
	    // characters from the last write operation.  Not elegant,
	    // but it works, and it is fast and flicker free...
	    infoLine = locationHeader + "(" + x + "," + y + ")    ";
	    g.drawString(infoLine, leftMargin, firstLine + line);
	} else {
	    // if this is a new event, we want a full paint/clear cycle
	    infoLine = locationHeader + "(" + x + "," + y + ")";
	    paint();
	    penMoving = true;
	}
	
    }

    /**
     * <p>Handle a key down event. A keyDown event *may* involve
     * <ul>
     * <li>Text Entry using Graffiti
     * <li>Pushing the hard buttons at the bottom of the unit
     * <li>Touching the four icons to the sides of the Graffiti area
     * or touching the "abc" or "123" areas within the Graffiti area
     * </ul></p>
     * <P>If the Spotlet was registered with
     * <code>WANT_SYSTEM_KEYS</code>, all three of the above key down
     * events are trapped and handled by the application.</P>
     * 
     *<p>If the Spotlet was registered with the
     * <code>NO_EVENT_OPTIONS</code>, the only keys the application
     * will react to are:
     * <ul>
     * <li>Text Entry using Graffiti
     * <li>Tapping the Menu Icon
     * </ul>
     * All other keys will perform their normal PalmOS function, with
     * the exception of the "abc" and "123" areas in the Graffiti area,
     * which now have no effect whatsoever.</p>
     */
    public void keyDown(int keyCode) {
	eventLine = eventHeader + "keyDown";
	switch(keyCode) {
	case KEY_HARD1:
	    infoLine = keyHeader + "Hard Key 1]";	    
	    break;
	case KEY_HARD2:
	    infoLine = keyHeader + "Hard Key 2]";	    
	    break;
	case KEY_HARD3:
	    infoLine = keyHeader + "Hard Key 3]";	    
	    break;
	case KEY_HARD4:
	    infoLine = keyHeader + "Hard Key 4]";	    
	    break;
	case CALCICON:
	    infoLine = keyHeader + "Calculator Icon]";	    
	    break;
	case PAGEDOWN:
	    infoLine = keyHeader + "Page Down Key]";	    
	    break;
	case PAGEUP:
	    infoLine = keyHeader + "Page Up Key]";	    
	    break;
	case MENUICON:
	    infoLine = keyHeader + "Menu Icon]";	    
	    break;
	case 264:
	    infoLine = keyHeader + "Home Icon]";	    
	    break;
	case 266:
	    infoLine = keyHeader + "Find Icon]";	    
	    break;
	case 272:
	    infoLine = keyHeader + "abc Area]";	    
	    break;
	case 273:
	    infoLine = keyHeader + "123 Area]";	    
	    break;
	default:
	    char tmpChar = (char)keyCode;
	    infoLine = keyHeader + tmpChar + "]";
	}
	paint();
    }
    
}


