001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.lcdui;
028:
029: import com.sun.midp.events.*;
030: import javax.microedition.lcdui.CustomItem;
031:
032: import com.sun.midp.security.Permissions;
033: import com.sun.midp.security.SecurityToken;
034:
035: import com.sun.midp.log.Logging;
036: import com.sun.midp.log.LogChannels;
037:
038: /**
039: * This is the event listener for LCDUI events.
040: */
041: public class LCDUIEventListener implements EventListener {
042:
043: // ------- Private Data Members ------- //
044:
045: /** The DisplayManager object handling the display events. */
046: private ItemEventConsumer itemEventConsumer;
047:
048: /** Cached reference to the MIDP event queue. */
049: private EventQueue eventQueue;
050:
051: // -------- Constructor --------- //
052:
053: /**
054: * The constructor for the default event handler for LCDUI.
055: *
056: * @param token security token that controls instance creation.
057: * @param theEventQueue the event queue
058: * @param theItemEventConsumer handler for item events
059: */
060: public LCDUIEventListener(SecurityToken token,
061: EventQueue theEventQueue,
062: ItemEventConsumer theItemEventConsumer) {
063:
064: token.checkIfPermissionAllowed(Permissions.MIDP);
065:
066: eventQueue = theEventQueue;
067: itemEventConsumer = theItemEventConsumer;
068:
069: /*
070: * All events handled by this object are of [ackage private
071: * LCDUIEvent class, so only this object knows how to
072: * decode their fields.
073: */
074: eventQueue.registerEventListener(
075: EventTypes.SCREEN_CHANGE_EVENT, this );
076: eventQueue.registerEventListener(
077: EventTypes.SCREEN_REPAINT_EVENT, this );
078: eventQueue.registerEventListener(EventTypes.INVALIDATE_EVENT,
079: this );
080: eventQueue.registerEventListener(EventTypes.ITEM_EVENT, this );
081: eventQueue.registerEventListener(
082: EventTypes.CALL_SERIALLY_EVENT, this );
083: }
084:
085: // ------- Public Methods -------- //
086:
087: /**
088: * Preprocess an event that is being posted to the event queue.
089: *
090: * @param newEvent event being posted
091: *
092: * @param waitingEvent previous event of this type waiting in the
093: * queue to be processed
094: *
095: * @return true to allow the post to continue, false to not post the
096: * event to the queue
097: */
098: public boolean preprocess(Event newEvent, Event waitingEvent) {
099: switch (newEvent.getType()) {
100: case EventTypes.INVALIDATE_EVENT:
101: case EventTypes.CALL_SERIALLY_EVENT:
102: return preprocessAllowOnlyOneEvent(newEvent, waitingEvent);
103: case EventTypes.SCREEN_CHANGE_EVENT:
104: return preprocessScreenChangeEvent(newEvent, waitingEvent);
105: // case EventTypes.ITEM_EVENT:
106: default:
107: return true;
108: }
109: }
110:
111: /**
112: * Process an event.
113: *
114: * @param event event to process
115: */
116: public void process(Event event) {
117: LCDUIEvent lcduiEvent = (LCDUIEvent) event;
118:
119: switch (event.getType()) {
120: case EventTypes.SCREEN_CHANGE_EVENT:
121: /*
122: * Target DisplayEventConsumer is obtained directly
123: * from event field.
124: * Assumed that target consumer is not null.
125: */
126: lcduiEvent.display
127: .handleScreenChangeEvent(lcduiEvent.nextScreen);
128: return;
129:
130: case EventTypes.SCREEN_REPAINT_EVENT:
131: /*
132: * Target DisplayEventConsumer is obtained directly
133: * from event field.
134: * Assumed that target consumer is not null.
135: */
136: lcduiEvent.display.handleScreenRepaintEvent();
137: return;
138:
139: case EventTypes.INVALIDATE_EVENT:
140: /*
141: * Target DisplayEventConsumer is obtained directly
142: * from event field.
143: * Assumed that target consumer is not null.
144: */
145: lcduiEvent.display.handleInvalidateEvent();
146: return;
147:
148: case EventTypes.ITEM_EVENT:
149: /*
150: * Item Events are processed by ItemEventConsumer.
151: */
152: itemEvent(lcduiEvent);
153: return;
154:
155: case EventTypes.CALL_SERIALLY_EVENT:
156: /*
157: * Target DisplayEventConsumer is obtained directly
158: * from event field.
159: * Assumed that target consumer is not null.
160: */
161: lcduiEvent.display.handleCallSeriallyEvent();
162: return;
163:
164: default:
165: if (Logging.REPORT_LEVEL <= Logging.WARNING) {
166: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
167: "unknown system event (" + event.getType()
168: + ")");
169: }
170: }
171: }
172:
173: // ------- Private Event Handling Routines -------- //
174:
175: /**
176: * Preprocess an event to only allow one in the queue
177: * at once.
178: *
179: * @param genericEvent1 LCDUI event
180: * @param genericEvent2 waiting LCDUI event
181: *
182: * @return true if the event should be put in the queue, false if
183: * the event should not be put in the queue because it has been merged
184: * with the event currently waiting in the queue.
185: */
186: private boolean preprocessAllowOnlyOneEvent(Event genericEvent1,
187: Event genericEvent2) {
188: if (genericEvent2 == null) {
189: // There is no other event, queue this event
190: return true;
191: }
192:
193: return false;
194: }
195:
196: /**
197: * Preprocess a screen change event to only allow one with the same
198: * screen in the queue at once.
199: *
200: * @param genericEvent1 LCDUI event
201: * @param genericEvent2 waiting LCDUI event
202: *
203: * @return true if the event should be put in the queue, false if
204: * the event should not be put in the queue because it is duplicate
205: * of the event currently waiting in the queue.
206: */
207: private boolean preprocessScreenChangeEvent(Event genericEvent1,
208: Event genericEvent2) {
209: LCDUIEvent newEvent;
210: LCDUIEvent waitingEvent;
211:
212: if (genericEvent2 == null) {
213: // There is no other event, queue this event
214: return true;
215: }
216:
217: newEvent = (LCDUIEvent) genericEvent1;
218: waitingEvent = (LCDUIEvent) genericEvent2;
219:
220: if (newEvent.nextScreen != waitingEvent.nextScreen) {
221: // The events are different, queue this event
222: return true;
223: }
224:
225: // The events are the same event , do not queue this event
226: return false;
227: }
228:
229: /**
230: * Processes event of ITEM_EVENT class
231: *
232: * @param lcduiEvent LCDUI event to be processed
233: */
234: private void itemEvent(LCDUIEvent lcduiEvent) {
235: switch (lcduiEvent.minorCode) {
236: case LCDUIEvent.ITEM_STATE_CHANGED:
237: itemEventConsumer
238: .handleItemStateChangeEvent(lcduiEvent.changedItem);
239: break;
240: case LCDUIEvent.ITEM_SIZE_REFRESH:
241: itemEventConsumer
242: .handleItemSizeRefreshEvent((CustomItem) lcduiEvent.changedItem);
243: break;
244: default:
245: if (Logging.REPORT_LEVEL <= Logging.WARNING) {
246: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
247: "ITEM_EVENT invalid minor code ("
248: + lcduiEvent.minorCode + ")");
249: }
250: break;
251: }
252: return;
253: }
254: }
|