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: * SVGSplashScreen.java
044: *
045: * Created on June 19, 2006, 2:50 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.microedition.svg;
052:
053: import javax.microedition.lcdui.Command;
054: import javax.microedition.lcdui.CommandListener;
055: import javax.microedition.lcdui.Display;
056: import javax.microedition.m2g.SVGEventListener;
057: import javax.microedition.m2g.SVGImage;
058:
059: /**
060: * This component represents a splash screen, which is usually being displayed
061: * when the application starts. It waits for a specified amount of time (by default
062: * 5000 milliseconds) and then calls specified command listener commandAction method
063: * with DISMISS_COMMAND as command parameter. It displays an animation of SVG image.
064: * @author breh
065: */
066: public class SVGSplashScreen extends SVGAnimatorWrapper {
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: // timeout for the splashscreen
080: private int timeout = 5000;
081: // allow interrupt
082: private boolean allowTimeoutInterrupt = true;
083: // show timestamp
084: private static long currentDisplayTimestamp;
085:
086: /**
087: * Creates a new instance of SVGSplashScreen
088: *
089: * <p/> Please note, supplied SVGImage shouldn't be reused in other SVGAnimator.
090: */
091: public SVGSplashScreen(SVGImage svgImage, Display display)
092: throws IllegalArgumentException {
093: super (svgImage, display);
094: setFullScreenMode(true);
095: setSVGEventListener(new SplashScreenSvgEventListener());
096: }
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: * @return timeout value
114: */
115: public int getTimeout() {
116: return timeout;
117: }
118:
119: /**
120: * Set to true, when the timeout with a specified timeout interval can
121: * be interrupted by pressing a key.
122: * @param allow true if the user can interrupt the screen, false if the user need to wait
123: * until timeout.
124: */
125: public void setAllowTimeoutInterrupt(boolean allow) {
126: this .allowTimeoutInterrupt = allow;
127: }
128:
129: /**
130: * Can be the splashscreen interrupted (dismissed) by the user pressing a key?
131: * @return true if user can interrupt it, false otherwise
132: */
133: public boolean isAllowTimeoutInterrupt() {
134: return allowTimeoutInterrupt;
135: }
136:
137: // user dismiss method (calls commandAction with DISMISS_COMMAND as argument)
138: private void doDismiss() {
139: //System.err.println("doDismiss");
140: CommandListener cl = getCommandListener();
141: if (cl != null) {
142: cl.commandAction(DISMISS_COMMAND, this );
143: }
144: }
145:
146: // timer
147: private class Watchdog extends Thread {
148:
149: private int timeout;
150: private long currentDisplayTimestamp;
151:
152: private Watchdog(int timeout, long currentDisplayTimestamp) {
153: this .timeout = timeout;
154: this .currentDisplayTimestamp = currentDisplayTimestamp;
155: }
156:
157: public void run() {
158: try {
159: //System.err.println("Watchdog running");
160: Thread.sleep(timeout);
161: } catch (InterruptedException ie) {
162: }
163: // timeout (only if current display timout matches) - this means this
164: // splash screen is still being shown on the display
165: if (this .currentDisplayTimestamp == SVGSplashScreen.this .currentDisplayTimestamp) {
166: doDismiss();
167: }
168: }
169:
170: }
171:
172: // svg event listener listening on key presses
173: private class SplashScreenSvgEventListener implements
174: SVGEventListener {
175:
176: public void keyPressed(int i) {
177: //System.out.println("keyPressed");
178: if (allowTimeoutInterrupt) {
179: doDismiss();
180: }
181: }
182:
183: public void keyReleased(int i) {
184: }
185:
186: public void pointerPressed(int i, int i0) {
187: if (allowTimeoutInterrupt) {
188: doDismiss();
189: }
190: }
191:
192: public void pointerReleased(int i, int i0) {
193: }
194:
195: public void hideNotify() {
196: currentDisplayTimestamp = System.currentTimeMillis();
197: }
198:
199: public void showNotify() {
200: //System.out.println("showNotify");
201: // start watchdog task - only when applicable
202: currentDisplayTimestamp = System.currentTimeMillis();
203: if (timeout > 0) {
204: Watchdog w = new Watchdog(timeout,
205: currentDisplayTimestamp);
206: w.start();
207: }
208: }
209:
210: public void sizeChanged(int i, int i0) {
211: }
212:
213: }
214:
215: }
|