FileDocCategorySizeDatePackage
DefaultEventHandler.javaAPI DocJ2ME MIDP 2.029746Thu Nov 07 12:02:22 GMT 2002com.sun.midp.lcdui

DefaultEventHandler

public class DefaultEventHandler extends Object implements EventHandler
This is the default event handler for the LCDUI.

Fields Summary
private Thread
eventThread
The eventThread is a single thread which processes all the events originating in both the VM and profile layers.
private VMEventHandler
vmEventHandler
The low level vm event handler routine. This handler reads event types off the event stream and marshals the arguments to the proper event routines.
private QueuedEventHandler
queuedEventHandler
The high level queued event handler routine. This handler processes all the events in the event queue and marshals the arguments to the proper event routines.
private EventQueue
eventQueue
Queue which contains all the events of the system. Both those which originate from the VM as well as those which originate from application or MIDP code.
private DisplayManager
displayManager
The DisplayManager object handling the display events.
protected Object
eventLock
This is a special lock object for synchronizing event deliveries between the different event threads
private boolean
inMenu
This field is true if the current display is the menu screen.
Constructors Summary
public DefaultEventHandler()
The constructor for the default event handler for LCDUI.

        displayManager = DisplayManagerFactory.getDisplayManager();
        eventLock = new Object();

        // FIRE UP THE HIGH LEVEL EVENT THREAD
        try {
            queuedEventHandler = new QueuedEventHandler();
            eventThread = new Thread(queuedEventHandler);
            eventThread.start();
        } catch (Throwable t) {
            t.printStackTrace();
        }

        // FIRE UP THE LOW LEVEL VM EVENT READER
        try {
            vmEventHandler = new VMEventHandler();
            (new Thread(vmEventHandler)).start();
        } catch (Throwable t) {
            t.printStackTrace();
        }

    
Methods Summary
voidcallSeriallyEvent()
Process all pending call serially's

        try {
            synchronized (eventLock) {
                displayManager.callSerially();
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
public voidclearSystemScreen()
Called to force the event handler to clear whatever system screen has interrupted the current Displayable and allow the foreground Display to resume painting.

        inMenu = false;
        dismissMenu();
    
voidcommandEvent(int type)
Process a command event

param
type The type of Command event to process

        try {
            synchronized (eventLock) {
                if (type == MENU_REQUESTED) {
                    displayManager.suspendPainting();
                    paintMenu();
                    inMenu = true;
                } else {
                    if (inMenu) {
                        displayManager.resumePainting();
                    }
                    inMenu = false;
                    if (type >= 0) {
                        displayManager.commandAction(type);
                    }
                }
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
native voiddismissMenu()
Native method to dismiss the current menu in the case of setCurrent() being called while the Display is suspended by a system screen.

public native intgetGameAction(int keyCode)
Get the abstract gameAction corresponding to the given keyCode.

param
keyCode A system-specific keyCode
return
gameAction The abstract game action associated with the keyCode

public native intgetKeyCode(int gameAction)
Get the system-specific key code corresponding to the given gameAction.

param
gameAction A game action
return
The keyCode associated with that action

public native java.lang.StringgetKeyName(int keyCode)
Get the informative key string corresponding to the given keyCode.

param
keyCode A system-specific keyCode
return
a string name for the key, or null if no name is available

public native intgetSystemKey(int keyCode)
Get the abstract system key that corresponds to keyCode.

param
keyCode A system-specific keyCode
return
The SYSTEM_KEY_ constant for this keyCode, or 0 if none

static voidhandleThrowable(java.lang.Throwable t)
Handle an unexpected exception while processing an event

param
t the Throwable to handle

        if (t != null) {
            System.err.println("\nError occurred while dispatching event:");
            t.printStackTrace();
        }
    
native voidincomingCall()
Simulate an incoming phone call in the RI. This method would be removed in an actual port. It exists solely to simulate one possible behavior of a system whereby MIDP needs to be suspended for the device to handle some task.

public booleanisDispatchThread()
Returns true if the current thread is the EventHandler's dispatch thread.

return
true if the current thread is this EventHandler's dispatch thread

        return (Thread.currentThread() == eventThread);
    
voiditemStateChangedEvent(javax.microedition.lcdui.Item src)
Process an item state change

param
src the Item which has changed

        try {
            synchronized (eventLock) {
                displayManager.callItemStateChanged(src);
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
voidkeyEvent(int type, java.lang.String str, int code)
Process a key event

param
type The type of key event
param
str The String associated with an input method event
param
code The keycode of the key event

        try {
            if (type == IME) {
                synchronized (eventLock) {
                    displayManager.inputMethodEvent(str);
                }
            } else if (getSystemKey(code) == SYSTEM_KEY_END &&
                    type == RELEASED) {
                synchronized (eventLock) {
                    displayManager.killCurrent();
                }
            } else if (inMenu) {
                // native method, no need to synchronize
                inMenu = menuKeyEvent(type, code);
            } else {
                synchronized (eventLock) {
                    displayManager.keyEvent(type, code);
                }
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
native booleanmenuKeyEvent(int type, int code)
Handle the key event when the menu is the current display.

param
type one of PRESSED, RELEASED, or REPEATED
param
code the key code of the key event
return
true if the event is the current display is the menu screen.

native booleanmenuPointerEvent(int type, int x, int y)
Handle the pointer event when the menu is the current display.

param
type one of PRESSED, RELEASE, or DRAGGED
param
x the x co-ordinate of the pointer event
param
y the y co-ordinate of the pointer event
return
true if the event is the current display is the menu screen.

voidmultiMediaEvent(int playerID, int curMTime)
Process a multimedia event

param
playerID The player indentity
param
curMTime The current time in milliseconds

        try {
            BasicPlayer p = BasicPlayer.get(playerID);
            if (p != null) {
                p.sendEvent(PlayerListener.END_OF_MEDIA, new Long(curMTime));
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
native voidpaintMenu()
Native method to draw the command menu on the screen

voidpointerEvent(int type, int x, int y)
Process a pointer event

param
type The type of pointer event
param
x The x coordinate location of the event
param
y The y coordinate location of the event

        try {
            if (inMenu) {
                // native method, no need to synchronize
                inMenu = menuPointerEvent(type, x, y);
            } else {
                synchronized (eventLock) {
                    displayManager.pointerEvent(type, x, y);
                }
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
voidrepaintScreenEvent(int x1, int y1, int x2, int y2, java.lang.Object target)
Process a repaint event

param
x1 The x origin coordinate
param
y1 The y origin coordinate
param
x2 The lower right x coordinate
param
y2 The lower right y coordinate
param
target The optional paint target

        try {
            synchronized (eventLock) {
                displayManager.repaint(x1, y1, x2, y2, target);
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
public voidscheduleCallSerially()
Called to schedule a serial callback of a Runnable object passed into Display's callSerially() method.

        eventQueue.push();
    
public voidscheduleInvalidate(javax.microedition.lcdui.Item src)
Called to schedule an invalidation of a Form

param
src the Item which may have caused the invalidation

        eventQueue.push(src, true);
    
public voidscheduleItemStateChanged(javax.microedition.lcdui.Item src)
Called to schedule a call to an ItemStateChangeListener

param
src the Item which has changed

        eventQueue.push(src, false);
    
public voidscheduleRepaint(int x, int y, int w, int h, java.lang.Object target)
Called to schedule a repaint of the current Displayable as soon as possible

param
x The x coordinate of the origin of the repaint rectangle
param
y The y coordinate of the origin of the repaint rectangle
param
w The width of the repaint rectangle
param
h The height of the repaint rectangle
param
target An optional target Object, which may have been the original requestor for the repaint

        eventQueue.push(x, y, w, h, target);
    
public voidscheduleScreenChange(javax.microedition.lcdui.Display parent, javax.microedition.lcdui.Displayable d)
Called to schedule a screen change to the given Displayable as soon as possible

param
parent parent Display of the Displayable
param
d The Displayable to change to

        eventQueue.push(parent, d);
    
voidscreenChangeEvent(javax.microedition.lcdui.Display parent, javax.microedition.lcdui.Displayable d)
Process a screen change event

param
parent parent Display of the Displayable
param
d The Displayable to make current

        try {
            synchronized (eventLock) {
                displayManager.screenChange(parent, d);
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
public voidserviceRepaints()
Called to service any pending repaint operations

        try {
            eventQueue.serviceRepaints();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    
voidsystemEvent(int type)
Process a system event

param
type The type of system event to process, such as suspend, pause, kill, exit

        try {
            synchronized (eventLock) {
                switch (type) {
                    case SUSPEND_ALL:
                    displayManager.suspendAll();
                    // NOTE: A real port would not use 'incomingCall()'
                    // and the line below would be removed.
                    // It exists solely in the RI to simulate the fact
                    // that the system is doing something which has
                    // suspended MIDP
                    incomingCall();
                    break;
                    case RESUME_ALL:
                    displayManager.resumeAll();
                    break;
                    case SHUTDOWN:
                    displayManager.shutdown();
                    case SUSPEND_CURRENT:
                    displayManager.suspendCurrent();
                    break;
                    case RESUME_PREVIOUS:
                    displayManager.resumePrevious();
                    break;
                    case KILL_CURRENT:
                    displayManager.killCurrent();
                    break;
                    default:
                    break;
                }
            } // synchronized
        } catch (Throwable t) {
            handleThrowable(t);
        }
    
voidunknownVMEvent(int event, Events queue)
Handle an undefined VM Event. Designed for subclasses to easily override and process new event types.

param
event The unkown event type
param
queue The event queue to read subsequent values from pertaining to the event

        System.err.println("Unknown VM Event: " + event);
    
public native voidupdateCommandSet(javax.microedition.lcdui.Command[] itemCommands, int numItemCommands, javax.microedition.lcdui.Command[] commands, int numCommands)
Set the current set of active Abstract Commands.

param
itemCommands The list of Item Commands that should be active
param
numItemCommands The number of Item commands in the list
param
commands The list of Commands that should be active
param
numCommands The number of commands in the list

voidvalidateEvent(javax.microedition.lcdui.Item src)
Process a Form invalidation

param
src the Item which may have caused the invalidation

        try {
            synchronized (eventLock) {
                displayManager.callInvalidate(src);
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }