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: package com.sun.midp.main;
027:
028: import java.util.*;
029:
030: /**
031: * This class controls which MIDlet's display is in the foreground.
032: * Running only in the AMS Isolate (0) the controller consulted by the MIDlet
033: * proxy list for any foreground when various state changes occur in a MIDlet.
034: * The display controller defers the display decision to the user by choosing
035: * the foreground selector to be the next foreground.
036: * <p>
037: * From the user perspective when the last MIDlet the user launched sets its
038: * current displayable for the first time, that MIDlet should automatically
039: * get the foreground (see the midletCreated and foregroundRequest methods).
040: * <p>
041: * A MIDlet that is paused or destroyed is treated as if it has requested the
042: * background as described above.
043: */
044: public class MVMDisplayController extends DisplayController {
045: /** Foreground Selector MIDlet. */
046: private MIDletProxy foregroundSelector;
047:
048: /**
049: * Construct a DisplayController with a reference to the ProxyList.
050: *
051: * @param theMIDletProxyList reference to the MIDlet proxy list
052: * @param theForegroundSelector the proxy of foreground selector
053: */
054: public MVMDisplayController(MIDletProxyList theMIDletProxyList,
055: MIDletProxy theForegroundSelector) {
056: super (theMIDletProxyList);
057:
058: foregroundSelector = theForegroundSelector;
059:
060: /*
061: * Ensure the foreground selector will get the foreground
062: * when requested.
063: */
064: lastMidletCreated = foregroundSelector;
065: }
066:
067: /**
068: * Handles MIDlet foreground requests.
069: * <p>
070: * If proxy being updated belongs last MIDlet created in the proxy list,
071: * then put the MIDlet in the foreground.
072: * <p>
073: * Otherwise, the request will not be granted. Foreground will not change.
074: *
075: * @param midlet The proxy of the MIDlet that was updated
076: *
077: * @return Proxy of the next foreground MIDlet, may be the foreground
078: * MIDlet if the foreground should not change
079: */
080: MIDletProxy foregroundRequest(MIDletProxy midlet) {
081: /*
082: * When the last MIDlet started wants the foreground automatically
083: * put in the foreground this time only.
084: */
085: return (midlet == lastMidletCreated) ? midlet : midletProxyList
086: .getForegroundMIDlet();
087: }
088:
089: /**
090: * Handles MIDlet background requests.
091: * <p>
092: * If the MIDlet is the foreground MIDlet, then bring the foreground
093: * selector to the foreground.
094: *
095: * @param midlet The proxy of the MIDlet that was updated
096: *
097: * @return Proxy of the next foreground MIDlet, may be the foreground
098: * MIDlet if the foreground should not change
099: */
100: MIDletProxy backgroundRequest(MIDletProxy midlet) {
101: MIDletProxy foreground = midletProxyList.getForegroundMIDlet();
102:
103: if (midlet != foreground) {
104: // not in the foreground, so don't change the foreground
105: return foreground;
106: }
107:
108: /*
109: * Normal MVM mode case,
110: * Let the user choose the next foreground.
111: */
112: return getForegroundSelector();
113: }
114:
115: /**
116: * Preempt an Isolate's displays.
117: *
118: * @param preempting proxy of the preempting MIDlet to be put in the
119: * foreground when a preempted MIDlet gets the foreground
120: *
121: * @return Proxy of the next foreground MIDlet, may be the foreground
122: * MIDlet if the foreground should not change
123: *
124: */
125: MIDletProxy startPreempting(MIDletProxy preempting) {
126: Enumeration midlets;
127: MIDletProxy preempted;
128: MIDletProxy foreground;
129:
130: /*
131: * Preempt all of the MIDlets in the same Isolate as the preempting
132: * proxy.
133: */
134: midlets = midletProxyList.getMIDlets();
135: while (midlets.hasMoreElements()) {
136: MIDletProxy current = (MIDletProxy) midlets.nextElement();
137:
138: if (current.getIsolateId() != preempting.getIsolateId()) {
139: continue;
140: }
141:
142: preempting.setPreemptedMidlet(current);
143: current.setPreemptingDisplay(preempting);
144: midletProxyList.notifyListenersOfProxyUpdate(current,
145: MIDletProxyListListener.PREEMPTING_DISPLAY);
146: }
147:
148: foreground = midletProxyList.getForegroundMIDlet();
149: if (foreground == null) {
150: return preempting;
151: }
152:
153: if (foreground.getIsolateId() == preempting.getIsolateId()) {
154: preempting.setPreemptedMidlet(foreground);
155: return preempting;
156: }
157:
158: return foreground;
159: }
160:
161: /**
162: * Called to process a select foreground event.
163: * Returns the foreground selector MIDlet in the foreground.
164: *
165: * @param onlyFromLaunchedList true if midlet should
166: * be selected from the list of already launched midlets,
167: * if false then possibility to launch midlet is needed.
168: * @return Proxy of the next foreground MIDlet, may be the foreground
169: * MIDlet if the foreground should not change
170: */
171: MIDletProxy selectForeground(boolean onlyFromLaunchedList) {
172:
173: notifyListenersOfSelectForeground(onlyFromLaunchedList);
174: return getForegroundSelector();
175: }
176:
177: /**
178: * Returns the foreground selector MIDlet.
179: *
180: * @return Proxy of the foreground selector MIDlet
181: */
182: private MIDletProxy getForegroundSelector() {
183: return foregroundSelector;
184: }
185: }
|