📄 defaulteventhandler.java
字号:
/* * @(#)DefaultEventHandler.java 1.77 02/09/16 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package com.sun.midp.lcdui;import java.io.*;import javax.microedition.io.*;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Item;import javax.microedition.media.PlayerListener;import com.sun.midp.main.Configuration;import com.sun.mmedia.BasicPlayer;/** * This is the default event handler for the LCDUI. */public class DefaultEventHandler implements EventHandler {// ------- Private Data Members ------- // /** * The eventThread is a single thread which processes * all the events originating in both the VM and profile layers. */ private Thread eventThread; /** * 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 VMEventHandler vmEventHandler; /** * 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 QueuedEventHandler queuedEventHandler; /** * 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 EventQueue eventQueue; /** * The DisplayManager object handling the display events. */ private DisplayManager displayManager; /** * This is a special lock object for synchronizing event * deliveries between the different event threads */ protected Object eventLock; // These are for handling Abstract Command menus. /** * This field is true if the current display is the menu screen. */ private boolean inMenu; // = false;// ------- Public Methods -------- // /** * 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 int getKeyCode(int gameAction); /** * 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 int getGameAction(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 */ public native int getSystemKey(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 String getKeyName(int keyCode); /** * 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 */ public native void updateCommandSet(Command[] itemCommands, int numItemCommands, Command[] commands, int numCommands); /** * Returns true if the current thread is the EventHandler's dispatch * thread. * * @return true if the current thread is this EventHandler's * dispatch thread */ public boolean isDispatchThread() { return (Thread.currentThread() == eventThread); } /** * Called to force the event handler to clear whatever system screen * has interrupted the current Displayable and allow the foreground * Display to resume painting. */ public void clearSystemScreen() { inMenu = false; dismissMenu(); } /** * 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 */ public void scheduleScreenChange(Display parent, Displayable d) { eventQueue.push(parent, d); } /** * 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 */ public void scheduleRepaint(int x, int y, int w, int h, Object target) { eventQueue.push(x, y, w, h, target); } /** * Called to schedule a serial callback of a Runnable object passed * into Display's callSerially() method. */ public void scheduleCallSerially() { eventQueue.push(); } /** * Called to schedule an invalidation of a Form * * @param src the Item which may have caused the invalidation */ public void scheduleInvalidate(Item src) { eventQueue.push(src, true); } /** * Called to schedule a call to an ItemStateChangeListener * * @param src the Item which has changed */ public void scheduleItemStateChanged(Item src) { eventQueue.push(src, false); } /** * Called to service any pending repaint operations */ public void serviceRepaints() { try { eventQueue.serviceRepaints(); } catch (Throwable t) { t.printStackTrace(); } }// -------- Constructor --------- // /** * The constructor for the default event handler for LCDUI. */ public DefaultEventHandler() { 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(); } }// ------- Package Private Event Handling Routines -------- //// These will be utilized by the Automated Event Handler subclass // /** * 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 */ void keyEvent(int type, String str, int code) { 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); } } /** * 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 */ void pointerEvent(int type, int x, int y) { 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); } } /** * Process a command event * * @param type The type of Command event to process */ void commandEvent(int type) { 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); } } /** * Process a system event * * @param type The type of system event to process, such as * suspend, pause, kill, exit */ void systemEvent(int type) { 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); } } /** * Process a multimedia event * * @param playerID The player indentity * @param curMTime The current time in milliseconds */ void multiMediaEvent(int playerID, int curMTime) { try { BasicPlayer p = BasicPlayer.get(playerID); if (p != null) { p.sendEvent(PlayerListener.END_OF_MEDIA, new Long(curMTime)); } } catch (Throwable t) { handleThrowable(t); } } /** * Process a screen change event * * @param parent parent Display of the Displayable * @param d The Displayable to make current */ void screenChangeEvent(Display parent, Displayable d) { try { synchronized (eventLock) { displayManager.screenChange(parent, d); } } catch (Throwable t) { handleThrowable(t); } } /** * 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 */ void repaintScreenEvent(int x1, int y1, int x2, int y2, Object target) { try { synchronized (eventLock) { displayManager.repaint(x1, y1, x2, y2, target); } } catch (Throwable t) { handleThrowable(t); } } /** * Process all pending call serially's */ void callSeriallyEvent() { try { synchronized (eventLock) { displayManager.callSerially(); } } catch (Throwable t) { handleThrowable(t); } } /** * Process a Form invalidation * * @param src the Item which may have caused the invalidation */ void validateEvent(Item src) { try { synchronized (eventLock) { displayManager.callInvalidate(src); } } catch (Throwable t) { handleThrowable(t); } } /** * Process an item state change * * @param src the Item which has changed */ void itemStateChangedEvent(Item src) { try { synchronized (eventLock) { displayManager.callItemStateChanged(src); } } catch (Throwable t) { handleThrowable(t); } } /** * 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 */ void unknownVMEvent(int event, Events queue) { System.err.println("Unknown VM Event: " + event); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -