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 java.util.Vector;
030:
031: import com.sun.midp.security.Permissions;
032: import com.sun.midp.security.SecurityToken;
033:
034: /**
035: * Stores array of active displays that either belong to MIdlets,
036: * or dynamically created for display preemption.
037: */
038: public class DisplayContainer {
039:
040: /** ID of the Isolate this instance is created in */
041: private final int isolateId;
042:
043: /** Last local Display count used to create Display ID */
044: private int lastLocalDisplayId;
045:
046: /** Active displays. */
047: private Vector displays = new Vector(5, 5);
048:
049: /**
050: * Default constructor.
051: *
052: * @param token security token for initilaization
053: * @param isolateId id of the Isolate this instance is created in
054: */
055: public DisplayContainer(SecurityToken token, int isolateId) {
056: token.checkIfPermissionAllowed(Permissions.MIDP);
057: this .isolateId = isolateId;
058: }
059:
060: /**
061: * Adds a display object to the container and sets a the display's ID to
062: * new unique value for this isolate, as a single atomic operation.
063: * <p>
064: * Intended to be called from Display constructor.
065: *
066: * @param da display object to add
067: */
068: public synchronized void addDisplay(DisplayAccess da) {
069: if (displays.indexOf(da) == -1) {
070: int newId = createDisplayId();
071: da.setDisplayId(newId);
072: displays.addElement(da);
073: }
074: }
075:
076: /**
077: * Get a display to request the foreground on behalf of the MIDlet.
078: *
079: * @param nameOfOwner class name of the MIDlet that owns this display
080: */
081: public void requestForegroundForDisplay(String nameOfOwner) {
082: DisplayAccess da = findDisplayByOwner(nameOfOwner);
083:
084: da.requestForeground();
085: }
086:
087: /**
088: * Removes display object from the container.
089: *
090: * @param nameOfOwner class name of the MIDlet that owns this display
091: *
092: * @return true if display has been succcessfully removed,
093: * false, if display object has not been found in the container.
094: */
095: public synchronized boolean removeDisplay(String nameOfOwner) {
096: DisplayAccess da = findDisplayByOwner(nameOfOwner);
097:
098: return displays.removeElement(da);
099: }
100:
101: /**
102: * Find a display by ID.
103: *
104: * @param displayId ID of the display
105: *
106: * @return a display access object or null if not found
107: */
108: synchronized DisplayAccess findDisplayById(int displayId) {
109: int size = displays.size();
110:
111: for (int i = 0; i < size; i++) {
112: DisplayAccess current = (DisplayAccess) displays
113: .elementAt(i);
114:
115: if (current.getDisplayId() == displayId) {
116: return current;
117: }
118: }
119:
120: return null;
121: }
122:
123: /**
124: * Find a display by owner.
125: *
126: * @param nameOfOwner class name of the MIDlet that owns this display
127: *
128: * @return a display access object or null if not found
129: */
130: public synchronized DisplayAccess findDisplayByOwner(
131: String nameOfOwner) {
132: int size = displays.size();
133:
134: for (int i = 0; i < size; i++) {
135: DisplayAccess current = (DisplayAccess) displays
136: .elementAt(i);
137:
138: if (current.getNameOfOwner().equals(nameOfOwner)) {
139: return current;
140: }
141: }
142:
143: return null;
144: }
145:
146: /**
147: * Find a display event consumer by ID.
148: *
149: * @param displayId ID of the display
150: *
151: * @return a display event consumer object or null if not found
152: */
153: public DisplayEventConsumer findDisplayEventConsumer(int displayId) {
154: DisplayAccess da = findDisplayById(displayId);
155:
156: if (da == null) {
157: return null;
158: }
159:
160: return da.getDisplayEventConsumer();
161: }
162:
163: /**
164: * Find a foreground event consumer by ID.
165: *
166: * @param displayId ID of the display
167: *
168: * @return a foreground event consumer object or null if not found
169: */
170: public ForegroundEventConsumer findForegroundEventConsumer(
171: int displayId) {
172: DisplayAccess da = findDisplayById(displayId);
173:
174: if (da == null) {
175: return null;
176: }
177:
178: return da.getForegroundEventConsumer();
179: }
180:
181: /**
182: * Creates an Display Id that is unique across all Isolates.
183: * Graphics subsystem depends on this uniqueness, which allows
184: * quick check on whether a Display is in the foreground
185: * without having to check Isolate id.
186: *
187: * @return a new unique display Id with high 8 bits as Isolate ID,
188: * low 24 bits as local display counter.
189: */
190: private int createDisplayId() {
191: int id;
192:
193: do {
194: lastLocalDisplayId++;
195: // [high 8 bits: isolate id][low 24 bits: display id]]
196: id = ((isolateId & 0xff) << 24)
197: | (lastLocalDisplayId & 0x00ffffff);
198: } while (findDisplayById(id) != null);
199:
200: return id;
201: }
202: }
|