001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * SplashScreen.java
044: *
045: * Created on August 26, 2005, 10:19 AM
046: */
047:
048: package org.netbeans.microedition.lcdui;
049:
050: import javax.microedition.lcdui.Command;
051: import javax.microedition.lcdui.CommandListener;
052: import javax.microedition.lcdui.Display;
053:
054: /**
055: * This component represents a splash screen, which is usually being displayed
056: * when the application starts. It waits for a specified amount of time (by default
057: * 5000 milliseconds) and then calls specified command listener commandAction method
058: * with DISMISS_COMMAND as command parameter.
059: * <p/>
060: * This version is using CommandListener and static Command pattern, but is still
061: * compatible with older version. So if there is no command listener specified,
062: * it still can use setNextDisplayable() method to specify the dismiss screen and
063: * automatically switch to it.
064: * @author breh
065: */
066: public class SplashScreen extends AbstractInfoScreen {
067:
068: /**
069: * Command fired when the screen is about to be dismissed
070: */
071: public static final Command DISMISS_COMMAND = new Command(
072: "Dismiss", Command.OK, 0);
073:
074: /**
075: * Timeout value which wait forever. Value is "0".
076: */
077: public static final int FOREVER = 0;
078:
079: private static final int DEFAULT_TIMEOUT = 5000;
080:
081: private int timeout = DEFAULT_TIMEOUT;
082: private boolean allowTimeoutInterrupt = true;
083:
084: private long currentDisplayTimestamp;
085:
086: /**
087: * Creates a new instance of SplashScreen
088: * @param display display - cannot be null
089: * @throws java.lang.IllegalArgumentException when the display parameter is null
090: */
091: public SplashScreen(Display display)
092: throws IllegalArgumentException {
093: super (display);
094: }
095:
096: // properties
097:
098: /**
099: * Sets the timeout of the splash screen - i.e. the time in milliseconds for
100: * how long the splash screen is going to be shown on the display.
101: * <p/>
102: * If the supplied timeout is 0, then the splashscreen waits forever (it needs to
103: * be dismissed by pressing a key)
104: *
105: * @param timeout in milliseconds
106: */
107: public void setTimeout(int timeout) {
108: this .timeout = timeout;
109: }
110:
111: /**
112: * Gets current timeout of the splash screen
113: *
114: * @return timeout value
115: */
116: public int getTimeout() {
117: return timeout;
118: }
119:
120: /**
121: * When set to true, the splashscreen timeout can be interrupted
122: * (and thus dismissed) by pressing a key.
123: *
124: * @param allow true if the user can interrupt the screen, false if the user need to wait
125: * until timeout.
126: */
127: public void setAllowTimeoutInterrupt(boolean allow) {
128: this .allowTimeoutInterrupt = allow;
129: }
130:
131: /**
132: * Can be the splashscreen interrupted (dismissed) by the user pressing a key?
133: * @return true if user can interrupt it, false otherwise
134: */
135: public boolean isAllowTimeoutInterrupt() {
136: return allowTimeoutInterrupt;
137: }
138:
139: // canvas methods
140:
141: /**
142: * keyPressed callback
143: * @param keyCode
144: */
145: protected void keyPressed(int keyCode) {
146: if (allowTimeoutInterrupt) {
147: doDismiss();
148: }
149: }
150:
151: /**
152: * pointerPressed callback
153: * @param x
154: * @param y
155: */
156: protected void pointerPressed(int x, int y) {
157: if (allowTimeoutInterrupt) {
158: doDismiss();
159: }
160: }
161:
162: /**
163: * starts the coundown of the timeout
164: */
165: protected void showNotify() {
166: super .showNotify();
167: // start watchdog task - only when applicable
168: currentDisplayTimestamp = System.currentTimeMillis();
169: if (timeout > 0) {
170: Watchdog w = new Watchdog(timeout, currentDisplayTimestamp);
171: w.start();
172: }
173: }
174:
175: protected void hideNotify() {
176: super .hideNotify();
177: currentDisplayTimestamp = System.currentTimeMillis();
178: }
179:
180: // private stuff
181:
182: private void doDismiss() {
183: CommandListener commandListener = getCommandListener();
184: if (commandListener == null) {
185: switchToNextDisplayable(); // @deprecated - works only if
186: // appropriate setters were called and no command listener
187: // was assigned to this component
188: } else {
189: commandListener.commandAction(DISMISS_COMMAND, this );
190: }
191: }
192:
193: private class Watchdog extends Thread {
194:
195: private int timeout;
196: private long currentDisplayTimestamp;
197:
198: private Watchdog(int timeout, long currentDisplayTimestamp) {
199: this .timeout = timeout;
200: this .currentDisplayTimestamp = currentDisplayTimestamp;
201: }
202:
203: public void run() {
204: try {
205: Thread.sleep(timeout);
206: } catch (InterruptedException ie) {
207: }
208: // doDismiss (only if current display timout matches) - this means this
209: // splash screen is still being shown on the display
210: if (this.currentDisplayTimestamp == SplashScreen.this.currentDisplayTimestamp) {
211: doDismiss();
212: }
213: }
214:
215: }
216:
217: }
|