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: * SVGWaitScreen.java
044: *
045: * Created on June 19, 2006, 4:32 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: import org.netbeans.microedition.util.CancellableTask;
059:
060: /**
061: * This component suits as a wait screen, which let the user to execute a blocking
062: * background task (e.g. a network communication) and waits for it until finished.
063: * During the execution of the task, an SVG image/animation is being shown
064: * on the screen.
065: * <p/>
066: * The background task is being started immediately prior the component is being
067: * shown on the screen.
068: * <p/>
069: * When the background task is finished, this component calls commandAction method
070: * on assigned CommandListener object. In the case of success, the commandAction method
071: * is called with SUCCESS_COMMAND as parameter, in the case of failure, the commandAction
072: * method is called with FAILURE_COMMAND as parameter.
073: * <p/>
074: * @author breh
075: */
076: public class SVGWaitScreen extends SVGAnimatorWrapper {
077:
078: /**
079: * Command fired when the background task was finished succesfully
080: */
081: public static final Command SUCCESS_COMMAND = new Command(
082: "Success", Command.OK, 0);
083:
084: /**
085: * Command fired when the background task failed (threw exception)
086: */
087: public static final Command FAILURE_COMMAND = new Command(
088: "Failure", Command.OK, 0);
089:
090: // background task
091: private CancellableTask task;
092: // background thread task executor
093: private Thread backgroundExecutor;
094:
095: /**
096: *
097: * Creates a new instance of SVGWaitScreen. It requires instance of SVGImage, which will
098: * be used for animation and display.
099: * <p/> Please note, supplied SVGImage shouldn't be reused in other SVGAnimator.
100: */
101: public SVGWaitScreen(SVGImage svgImage, Display display)
102: throws IllegalArgumentException {
103: super (svgImage, display);
104: setSVGEventListener(new WaitScreenSvgEventListener());
105: }
106:
107: /**
108: * Sets the task to be run on the background.
109: * @param task task to be executed
110: */
111: public void setTask(CancellableTask task) {
112: this .task = task;
113: }
114:
115: /**
116: * Gets the background task.
117: * @return task being executed in background while this component is being shown
118: * on the screen
119: */
120: public CancellableTask getTask() {
121: return task;
122: }
123:
124: // private stuff
125: private void doAction() {
126: CommandListener cl = getCommandListener();
127: if (cl != null) {
128: if ((task != null) && (task.hasFailed())) {
129: cl.commandAction(FAILURE_COMMAND, this );
130: } else {
131: // task didn't failed - success !!!
132: cl.commandAction(SUCCESS_COMMAND, this );
133: }
134: }
135:
136: }
137:
138: /**
139: * BackgroundExecutor task
140: */
141: private class BackgroundExecutor implements Runnable {
142:
143: private CancellableTask task;
144:
145: public BackgroundExecutor(CancellableTask task)
146: throws IllegalArgumentException {
147: if (task == null)
148: throw new IllegalArgumentException(
149: "Task parameter cannot be null");
150: this .task = task;
151: }
152:
153: public void run() {
154: try {
155: task.run();
156: } finally {
157: SVGWaitScreen.this .backgroundExecutor = null;
158: doAction();
159: }
160: }
161: }
162:
163: // svg event listener listening on key presses
164: private class WaitScreenSvgEventListener implements
165: SVGEventListener {
166:
167: public void keyPressed(int i) {
168: }
169:
170: public void keyReleased(int i) {
171: }
172:
173: public void pointerPressed(int i, int i0) {
174: }
175:
176: public void pointerReleased(int i, int i0) {
177: }
178:
179: public void hideNotify() {
180: }
181:
182: public void showNotify() {
183: if (task != null) {
184: if (backgroundExecutor == null) {
185: backgroundExecutor = new Thread(
186: new BackgroundExecutor(task));
187: backgroundExecutor.start();
188: }
189: } else {
190: // switch to next displayable immediatelly - no task was assigned
191: // do it when the task is repainted - on some devices there are
192: // some race-conditions
193: getDisplay().callSerially(new Runnable() {
194: public void run() {
195: doAction();
196: }
197: });
198: }
199: }
200:
201: public void sizeChanged(int i, int i0) {
202: }
203:
204: }
205:
206: }
|