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 javax.microedition.lcdui.Displayable;
030: import javax.microedition.lcdui.game.GameCanvas;
031: import java.util.Hashtable;
032:
033: /**
034: * A class that maps between DisplayAccess objects and Displayable, GameCanvas.
035: * In future versions of the MIDP spec, GameCanvas may have been
036: * moved to lcdui package, in which case this class isn't needed.
037: */
038: public class GameMap {
039: /**
040: * The Displayable associated with the DisplayAccess
041: */
042: static private Displayable displayable;
043: /**
044: * The DisplayAccess associated with the GameCanvas
045: */
046: static private DisplayAccess displayAccess;
047:
048: /**
049: * The GraphicsAccess tunnel instance handed out from
050: * javax.microedition.lcdui package
051: */
052: static private GraphicsAccess graphicsAccess;
053:
054: /**
055: * The GameAccess tunnel instance handed out from
056: * javax.microedition.lcdui.game package
057: */
058: static private GameAccess gameAccess;
059:
060: /**
061: * Lock to ensure synchronized access to the displayable
062: */
063: static final private Object lock = new Object();
064:
065: /**
066: * Associates the given Displayable and DisplayAccess. This is a
067: * one-way association.
068: *
069: * @param c The GameCanvas to store
070: * @param d The DisplayAccess associated with the GameCanvas
071: */
072: public static void registerDisplayAccess(Displayable c,
073: DisplayAccess d) {
074: synchronized (lock) {
075: displayable = c;
076: displayAccess = d;
077: }
078: }
079:
080: /**
081: * Gets the DisplayAccess object for this Displayable.
082: * @param c The Displayable to get the DisplayAccess for
083: * @return DisplayAccess The DisplayAccess associated with the MIDlet
084: */
085: public static DisplayAccess getDisplayAccess(Displayable c) {
086: synchronized (lock) {
087: if (c == displayable) {
088: return displayAccess;
089: } else {
090: return null;
091: }
092: }
093: }
094:
095: /**
096: * Register given game package accessor instance
097: * @param gameAccess implementation of the GameAccess interface
098: */
099: public static void registerGameAccess(GameAccess gameAccess) {
100: synchronized (lock) {
101: GameMap.gameAccess = gameAccess;
102: }
103: }
104:
105: /**
106: * Gets the GameCanvasLFImpl object for this GameCanvas.
107: * @param c The GameCanvas to get the GameCanvasLFImpl for
108: * @return GameCanvasLFImpl, or null if there is no accessor to game package
109: */
110: public static GameCanvasLFImpl getGameCanvasImpl(GameCanvas c) {
111: if (gameAccess != null) {
112: return gameAccess.getGameCanvasLFImpl(c);
113: }
114: return null;
115: }
116:
117: /**
118: * Sets graphics accessor instance from javax.microedition.lcdui package
119: * to use extended package-private Image and Graphics APIs
120: *
121: * @param graphicsAccess graphics accessor tunnel
122: */
123: public static void registerGraphicsAccess(
124: GraphicsAccess graphicsAccess) {
125: synchronized (lock) {
126: GameMap.graphicsAccess = graphicsAccess;
127: }
128: }
129:
130: /**
131: * Gets GraphicsAccess instance needed to access extended
132: * Image and Graphics APIs
133: *
134: * @return GraphicsAccess tunnel instance
135: */
136: public static GraphicsAccess getGraphicsAccess() {
137: synchronized (lock) {
138: return graphicsAccess;
139: }
140: }
141: }
|