⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.java

📁 一个 JAVA 写的 J2ME 开源模拟器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * MicroEmulator 
 * Copyright (C) 2001-2007 Bartek Teodorczyk <barteo@barteo.net>
 * Copyright (C) 2007 Rushabh Doshi <radoshi@cs.stanford.edu> Pelago, Inc
 * 
 *  It is licensed under the following two licenses as alternatives:
 *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
 *    2. Apache License (the "AL") Version 2.0
 *
 *  You may not use this file except in compliance with at least one of
 *  the above two licenses.
 *
 *  You may obtain a copy of the LGPL at
 *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 *
 *  You may obtain a copy of the AL at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the LGPL or the AL for the specific language governing permissions and
 *  limitations.
 * 
 * Contributor(s): 
 *   3GLab
 *   Andres Navarro
 *   
 *  @version $Id: Display.java 1713 2008-05-14 13:06:56Z barteo $
 */
package javax.microedition.lcdui;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDlet;

import org.microemu.DisplayAccess;
import org.microemu.GameCanvasKeyAccess;
import org.microemu.MIDletBridge;
import org.microemu.device.DeviceFactory;
import org.microemu.device.ui.DisplayableUI;

public class Display {

	public static final int LIST_ELEMENT = 1;

	public static final int CHOICE_GROUP_ELEMENT = 2;

	public static final int ALERT = 3;

	public static final int COLOR_BACKGROUND = 0;

	public static final int COLOR_FOREGROUND = 1;

	public static final int COLOR_HIGHLIGHTED_BACKGROUND = 2;

	public static final int COLOR_HIGHLIGHTED_FOREGROUND = 3;

	public static final int COLOR_BORDER = 4;

	public static final int COLOR_HIGHLIGHTED_BORDER = 5;

	private Displayable current = null;

	private DisplayAccessor accessor = null;

	private static final String EVENT_DISPATCHER_NAME = "event-thread";

	private final EventDispatcher eventDispatcher = new EventDispatcher();

	private final class GaugePaintTask implements Runnable {

		public void run() {
			if (current != null) {
				if (current instanceof Alert) {
					Gauge gauge = ((Alert) current).indicator;
					if (gauge != null && gauge.hasIndefiniteRange() && gauge.getValue() == Gauge.CONTINUOUS_RUNNING) {
						gauge.updateIndefiniteFrame();
					}
				} else if (current instanceof Form) {
					Item[] items = ((Form) current).items;
					for (int i = 0; i < items.length; i++) {
						Item it = items[i];
						if (it != null && it instanceof Gauge) {
							Gauge gauge = (Gauge) it;

							if (gauge.hasIndefiniteRange() && gauge.getValue() == Gauge.CONTINUOUS_RUNNING) {
								gauge.updateIndefiniteFrame();
							}
						}
					}
				}
			}
		}
	}

	/**
	 * @author radoshi
	 * 
	 */
	private final class TickerPaintTask implements Runnable {

		public void run() {
			if (current != null) {
				Ticker ticker = current.getTicker();
				if (ticker != null) {
					synchronized (ticker) {
						if (ticker.resetTextPosTo != -1) {
							ticker.textPos = ticker.resetTextPosTo;
							ticker.resetTextPosTo = -1;
						}
						ticker.textPos -= Ticker.PAINT_MOVE;
					}
					repaint(current, 0, 0, current.getWidth(), current.getHeight());
				}
			}
		}
	}

	/**
	 * Wrap a key event as a runnable so it can be thrown into the event
	 * processing queue. Note that this may be a bit buggy, since events are
	 * supposed to propogate to the head of the queue and not get tied behind
	 * other repaints or serial calls in the queue.
	 * 
	 * @author radoshi
	 * 
	 */
	private final class KeyEvent extends EventDispatcher.Event {

		static final short KEY_PRESSED = 0;

		static final short KEY_RELEASED = 1;

		static final short KEY_REPEATED = 2;

		private short type;

		private int keyCode;

		KeyEvent(short type, int keyCode) {
			eventDispatcher.super();
			this.type = type;
			this.keyCode = keyCode;
		}

		public void run() {
			switch (type) {
			case KEY_PRESSED:
				if (current != null) {
					current.keyPressed(keyCode);
				}
				break;

			case KEY_RELEASED:
				if (current != null) {
					current.keyReleased(keyCode);
				}
				break;

			case KEY_REPEATED:
				if (current != null) {
					current.keyRepeated(keyCode);
				}
				break;
			}
		}
	}

	private final class ShowHideNotifyEvent extends EventDispatcher.Event {

		static final short SHOW_NOTIFY = 0;

		static final short HIDE_NOTIFY = 1;

		private short type;

		private Displayable current;

		private Displayable nextDisplayable;

		ShowHideNotifyEvent(short type, Displayable current, Displayable nextDisplayable) {
			eventDispatcher.super();
			this.type = type;
			this.current = current;
			this.nextDisplayable = nextDisplayable;
		}

		public void run() {
			switch (type) {
			case SHOW_NOTIFY:
				if (current != null) {
					eventDispatcher.put(new ShowHideNotifyEvent(ShowHideNotifyEvent.HIDE_NOTIFY, current,
							nextDisplayable));
				}

				if (nextDisplayable instanceof Alert) {
					setCurrent((Alert) nextDisplayable, current);
					return;
				}

				// Andres Navarro
				// TODO uncomment and test with JBenchmark2
				/*
				 * if (nextDisplayable instanceof GameCanvas) { // clear the
				 * keys of the GameCanvas
				 * MIDletBridge.getMIDletAccess().getGameCanvasKeyAccess().setActualKeyState(
				 * (GameCanvas) nextDisplayable, 0); }
				 */
				// Andres Navarro
				nextDisplayable.showNotify(Display.this);
				Display.this.current = nextDisplayable;

				setScrollUp(false);
				setScrollDown(false);
				updateCommands();
				nextDisplayable.repaint();

				break;
			case HIDE_NOTIFY:
				current.hideNotify(Display.this);
				break;
			}
		}
	}

	private class DisplayAccessor implements DisplayAccess {

		Display display;

		DisplayAccessor(Display d) {

			display = d;
		}

		public void commandAction(Command c, Displayable d) {
			if (c.equals(CommandManager.CMD_MENU)) {
				CommandManager.getInstance().commandAction(c);
			} else if (c.equals(CMD_SCREEN_UP)) {
				if (d != null && d instanceof Screen) {
					((Screen) d).scroll(Canvas.UP);
				}
			} else if (c.equals(CMD_SCREEN_DOWN)) {
				if (d != null && d instanceof Screen) {
					((Screen) d).scroll(Canvas.DOWN);
				}
			} else if (c.isRegularCommand()) {
				if (d == null) {
					return;
				}
				CommandListener listener = d.getCommandListener();
				if (listener == null) {
					return;
				}
				listener.commandAction(c, d);
			} else {
				// item contained command
				Item item = c.getFocusedItem();

				ItemCommandListener listener = item.getItemCommandListener();
				if (listener == null) {
					return;
				}
				listener.commandAction(c.getOriginalCommand(), item);
			}
		}

		public Display getDisplay() {
			return display;
		}

		// Andres Navarro
		private void processGameCanvasKeyEvent(GameCanvas c, int k, boolean press) {
			// TODO Game Canvas keys need more work
			// and better integration with the microemulator
			// maybe actualKeyState in GameCanvas should be
			// global and should update even while no GameCanvas
			// is current
			GameCanvasKeyAccess access = MIDletBridge.getMIDletAccess().getGameCanvasKeyAccess();
			int gameCode = c.getGameAction(k);
			boolean suppress = false;
			if (gameCode != 0) {
				// valid game key
				if (press)
					access.recordKeyPressed(c, gameCode);
				else
					access.recordKeyReleased(c, gameCode);
				suppress = access.suppressedKeyEvents(c);
			}
			if (!suppress) {
				if (press) {
					eventDispatcher.put(new KeyEvent(KeyEvent.KEY_PRESSED, k));
				} else {
					eventDispatcher.put(new KeyEvent(KeyEvent.KEY_RELEASED, k));
				}
			}
		}

		// TODO according to the specification this should be
		// only between show and hide notify...
		// check later
		// Andres Navarro
		public void keyPressed(int keyCode) {
			// Andres Navarro
			if (current != null && current instanceof GameCanvas) {
				processGameCanvasKeyEvent((GameCanvas) current, keyCode, true);
			} else {
				eventDispatcher.put(new KeyEvent(KeyEvent.KEY_PRESSED, keyCode));
			}
		}

		public void keyRepeated(int keyCode) {
			eventDispatcher.put(new KeyEvent(KeyEvent.KEY_REPEATED, keyCode));
		}

		public void keyReleased(int keyCode) {
			// Andres Navarro
			if (current != null && current instanceof GameCanvas) {
				processGameCanvasKeyEvent((GameCanvas) current, keyCode, false);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -