001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package com.sun.svg.component;
033:
034: import com.sun.perseus.demo.*;
035: import com.sun.svg.util.ProgressiveInputStream;
036:
037: import javax.microedition.lcdui.Displayable;
038: import javax.microedition.m2g.SVGImage;
039: import javax.microedition.m2g.SVGAnimator;
040:
041: public class LoadingScreen implements ProgressiveInputStream.Listener {
042: /**
043: * Interface that a class waiting for the SVGImage resource to be loaded
044: * should implement.
045: */
046: public interface Listener {
047: public void svgImageLoaded(SVGImage svgImage);
048: }
049:
050: /**
051: * The class which is waiting for the associated SVGImage to complete
052: * loading.
053: */
054: private Listener listener;
055:
056: /**
057: * The SVGAnimator playing the loading animation.
058: */
059: private SVGAnimator svgAnimator;
060:
061: /**
062: * The SVGImamge containing the loading animation definition.
063: */
064: private SVGImage svgImage;
065:
066: /**
067: * The path to the SVGImage to load
068: */
069: private String svgImagePath;
070:
071: /**
072: * The expected length for the SVGImage to load.
073: */
074: private int svgImageSize;
075:
076: /**
077: * Used to track progress in the loading screen.
078: */
079: private UpdateProgress updateProgress = new UpdateProgress();
080:
081: /**
082: * Horizontal Progress Bar. The Progress Bar is used in the initial load screen.
083: */
084: protected SVGProgressBar loadProgressBar;
085:
086: class UpdateProgress implements Runnable {
087: float value;
088:
089: public void run() {
090: loadProgressBar.setProgress(value);
091: }
092: }
093:
094: class SVGImageLoadingTask implements Runnable {
095: SVGImage loadedSVGImage;
096:
097: public void run() {
098:
099: try {
100: loadedSVGImage = (javax.microedition.m2g.SVGImage) javax.microedition.m2g.SVGImage
101: .createImage(
102: new ProgressiveInputStream(
103: ContactListMidlet.class
104: .getResourceAsStream(svgImagePath),
105: svgImageSize,
106: LoadingScreen.this ), null);
107: } catch (java.io.IOException exception) {
108: exception.printStackTrace();
109: }
110:
111: listener.svgImageLoaded(loadedSVGImage);
112: }
113: }
114:
115: /**
116: * Creates a new instance of SVGImageLoadingScreen
117: * @param svgAnimator the associated SVGAnimator instance
118: * @param svgImage the associated SVGImage instance
119: * @param svgImagePath the path of the SVG image file to load
120: * @param svgImageSize the expected size for the SVGImage to load.
121: */
122: public LoadingScreen(final SVGAnimator svgAnimator,
123: final SVGImage svgImage, final String svgImagePath,
124: final int svgImageSize, final Listener listener) {
125: this .svgAnimator = svgAnimator;
126: this .svgImage = svgImage;
127: this .svgImagePath = svgImagePath;
128: this .svgImageSize = svgImageSize;
129: this .listener = listener;
130:
131: loadProgressBar = new SVGProgressBar("loadProgressBar");
132: loadProgressBar.hookSkin(svgImage.getDocument());
133:
134: // Load the ContactList skin here
135: Thread th = new Thread(new SVGImageLoadingTask());
136: th.start();
137: }
138:
139: /**
140: * Called when more bytes have been read from the input stream.
141: *
142: * @param p the current penetration in the input stream, in the [0, 1] interval.
143: */
144: public void streamProgress(final float p) {
145: if (p < 0.90f && (p - updateProgress.value) < 0.1f) {
146: return;
147: }
148:
149: updateProgress.value = p;
150: try {
151: svgAnimator.invokeAndWait(updateProgress);
152: } catch (InterruptedException ie) {
153: } catch (IllegalStateException ise) {
154: }
155: }
156:
157: }
|