📄 defaulteventhandler.java
字号:
/** * Handle an unexpected exception while processing an event * * @param t the Throwable to handle */ static void handleThrowable(Throwable t) { if (t != null) { System.err.println("\nError occurred while dispatching event:"); t.printStackTrace(); } }// -------- Native Command Menu Handling Routines --------- // /** * Native method to draw the command menu on the screen */ native void paintMenu(); /** * Native method to dismiss the current menu in the case of setCurrent() * being called while the Display is suspended by a system screen. */ native void dismissMenu(); /** * 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 boolean menuKeyEvent(int type, int code); /** * 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. */ native boolean menuPointerEvent(int type, int x, int y); /** * 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. */ native void incomingCall();// --------- Low Level VM Event Handler ------- // /** * The VMEventHandler opens a private stream from the * VM and marshals the events from the stream to the * event queue. */ class VMEventHandler implements Runnable { /** The private stream of events from the VM */ Events queue; /** * Signal this handler to continue on after waiting * for the event queue to process a pending event */ public synchronized void proceed() { try { notify(); } catch (Throwable t) { t.printStackTrace(); } } /** * Process events from the VM event stream */ public synchronized void run() { try { queue = new Events(); // connect to the native event queue queue.open(); } catch (Throwable t) { t.printStackTrace(); } for (;;) { try { // What this does is push each new event type // to the queue and then stops. When the queue // is processed, the remaining options associated // with the event are read from the stream and // then this thread is resumed to read the next // new event type. eventQueue.push(queue.readInt()); try { wait(); } catch (Throwable t) { // TO DO: Do something more useful with this t.printStackTrace(); } } catch (Throwable t) { t.printStackTrace(); } } // for } // run() } // VMEventHandler// -------- High Level Profile Event Handler -------- // /** * The QueuedEventHandler processes all events, * those from the underlying VM as well as those * coming from the application and MIDP layers. */ class QueuedEventHandler implements Runnable { /** * Create a new default QueuedEventHandler */ public QueuedEventHandler() { eventQueue = new EventQueue(); } /** * Signal this handler there is a need to process * a pending event. */ public synchronized void process() { try { notify(); } catch (Throwable t) { // TO DO: Do something more useful with this t.printStackTrace(); } } /** * Waits for an event if there are no events pending. */ public synchronized void tryToSleep() { if (eventQueue.nextScreen == null && eventQueue.paintX1 == -1 && !eventQueue.callSeriallyPending && !eventQueue.invalidatePending && eventQueue.changedItem == null && eventQueue.vmEvent == 0) { try { wait(); } catch (Throwable t) { // TO DO: Do something more useful with this t.printStackTrace(); } } } /** * Process events from the EventQueue */ public void run() { int x1, y1, x2, y2, type = 0; Display parentOfNextScreen = null; Displayable nextScreen = null; boolean call = false; boolean validate = false; Item validateItem = null; Item changedItem = null; x1 = y1 = x2 = y2 = -1; Object target = null; for (;;) { try { tryToSleep(); if (eventQueue.vmEvent != 0) { switch (eventQueue.vmEvent) { case KEY_EVENT: type = vmEventHandler.queue.readInt(); if (type == IME) { keyEvent(type, vmEventHandler.queue.readUTF(), 0); } else { keyEvent(type, null, vmEventHandler.queue.readInt()); } break; case PEN_EVENT: pointerEvent(vmEventHandler.queue.readInt(), vmEventHandler.queue.readInt(), vmEventHandler.queue.readInt()); break; case COMMAND_EVENT: commandEvent(vmEventHandler.queue.readInt()); break; case SYSTEM_EVENT: systemEvent(vmEventHandler.queue.readInt()); break; case MM_EOM_EVENT: multiMediaEvent(vmEventHandler.queue.readInt(), vmEventHandler.queue.readInt()); break; default: unknownVMEvent(eventQueue.vmEvent, vmEventHandler.queue); } eventQueue.vmEvent = 0; vmEventHandler.proceed(); } synchronized (eventQueue.qLock) { // We first check for a screen change. if (eventQueue.nextScreen != null) { parentOfNextScreen = eventQueue.parentOfNextScreen; nextScreen = eventQueue.nextScreen; eventQueue.nextScreen = null; // On a screen change, we flush any pending // repaints, since changing the screen will // repaint the entire thing eventQueue.paintX1 = eventQueue.paintY1 = eventQueue.paintX2 = eventQueue.paintY2 = -1; eventQueue.paintTarget = null; } else if (eventQueue.paintX1 != -1) { // We then check for a repaint x1 = eventQueue.paintX1; x2 = eventQueue.paintX2; y1 = eventQueue.paintY1; y2 = eventQueue.paintY2; target = eventQueue.paintTarget; eventQueue.paintX1 = eventQueue.paintY1 = eventQueue.paintX2 = eventQueue.paintY2 = -1; eventQueue.paintTarget = null; } // We last check for a callSerially() if (eventQueue.callSeriallyPending) { call = true; eventQueue.callSeriallyPending = false; } if (eventQueue.invalidatePending) { validate = true; validateItem = eventQueue.invalidItem; eventQueue.invalidItem = null; eventQueue.invalidatePending = false; } if (eventQueue.changedItem != null) { changedItem = eventQueue.changedItem; eventQueue.changedItem = null; } } if (nextScreen != null) { screenChangeEvent(parentOfNextScreen, nextScreen); parentOfNextScreen = null; nextScreen = null; } if (x1 != -1) { repaintScreenEvent(x1, y1, x2, y2, target); x1 = y1 = x2 = y2 = -1; target = null; } if (call) { callSeriallyEvent(); call = false; } if (validate) { validateEvent(validateItem); validateItem = null; validate = false; } if (changedItem != null) { itemStateChangedEvent(changedItem); changedItem = null; } } catch (Throwable t) { t.printStackTrace(); } } // for (;;) } // run() } // QueuedEventHandler() /** * The EventQueue queues all event-related data and notifies * the QueuedEventHandler when events need processing. */ class EventQueue { /** The latest vm event */ int vmEvent; /** The parent Display of the next Displayable. */ Display parentOfNextScreen; /** The next displayable to show */ Displayable nextScreen; /** A flag to process all call serially's */ boolean callSeriallyPending; /** A flag to perform an invalidation of a Form */ boolean invalidatePending; /** An Item can be the cause of an invalidation */ Item invalidItem; /** An Item whose state has changed */ Item changedItem; /** The dirty region for any pending repaint */ int paintX1, paintY1, paintX2, paintY2; /** The optional target for the repaint */ Object paintTarget; /** The lock for manipulating queue data */ Object qLock; /** * Create a new default EventQueue */ public EventQueue() { qLock = new Object(); paintX1 = paintY1 = paintX2 = paintY2 = -1; } /** * Service any pending repaints. If there is a pending * repaint, process it immediately, otherwise return. */ public void serviceRepaints() { int x1, y1, x2, y2; Object target; synchronized (qLock) { if (paintX1 == -1) { return; } x1 = paintX1; y1 = paintY1; x2 = paintX2; y2 = paintY2; target = paintTarget; paintX1 = paintY1 = paintX2 = paintY2 = -1; paintTarget = null; } repaintScreenEvent(x1, y1, x2, y2, target); } /** * Push an event from the vm event stream * * @param vmEvent The integral vm event value */ public void push(int vmEvent) { synchronized (qLock) { this.vmEvent = vmEvent; } queuedEventHandler.process(); } /** * Push a callSerially event */ public void push() { synchronized (qLock) { callSeriallyPending = true; } queuedEventHandler.process(); } /** * Push either a Form invalidation event, or an * ItemStateChanged event * * @param src the Item src to the event * @param b true if it is an invalidation event */ public void push(Item src, boolean b) { synchronized (qLock) { if (b) { invalidatePending = true; invalidItem = src; } else { changedItem = src; } } queuedEventHandler.process(); } /** * Push a screen change * * @param parent parent Display of the Displayable * @param d The Displayable to change the current screen to */ public void push(Display parent, Displayable d) { synchronized (qLock) { parentOfNextScreen = parent; nextScreen = d; } queuedEventHandler.process(); } /** * Push a repaint * * @param x The x origin coordinate * @param y The y origin coordinate * @param w The width * @param h The height * @param target The optional paint target */ public void push(int x, int y, int w, int h, Object target) { try { w += x; // convert from width, height to absolute h += y; // x2, y2 if (x < 0) x = 0; if (y < 0) y = 0; synchronized (qLock) { if (paintX1 == -1) { // If we have no pending repaint // just store the region paintX1 = x; paintY1 = y; paintX2 = w; paintY2 = h; paintTarget = target; } else { // If there is a pending repaint // union the dirty regions if (paintX1 > x) { paintX1 = x; } if (paintY1 > y) { paintY1 = y; } if (paintX2 < w) { paintX2 = w; } if (paintY2 < h) { paintY2 = h; } paintTarget = null; } } // synchronized } catch (Throwable t) { t.printStackTrace(); } queuedEventHandler.process(); } } // ProfileEventHandler}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -