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.util;
033:
034: import javax.microedition.m2g.SVGAnimator;
035: import javax.microedition.m2g.SVGEventListener;
036: import javax.microedition.m2g.SVGImage;
037:
038: import javax.microedition.lcdui.Canvas;
039:
040: public final class DefaultSVGAnimator extends SVGAnimator implements
041: SVGEventListener {
042: /**
043: * State constants. These are used to track the animator's state.
044: */
045: private static final int STATE_STOPPED = 0;
046: private static final int STATE_PAUSED = 1;
047: private static final int STATE_PLAYING = 2;
048:
049: /**
050: * Indicates the interruption while in the playing state (for example by an
051: * incomming call).
052: */
053: private static final int STATE_INTERRUPTED = 3;
054:
055: /** The decorated animator. */
056: private final SVGAnimator innerAnimator;
057:
058: /** The svg image. */
059: private final SVGImage svgImage;
060:
061: /**
062: * One of STATE_STOPPED, STATE_PAUSED, STATE_PLAYING or
063: * STATE_INTERRUPTED.
064: */
065: private int animatorState = STATE_INTERRUPTED;
066:
067: /**
068: * The SVGEventListener to which to forward event notifications from
069: * innerAnimator.
070: */
071: private SVGEventListener svgEventListener;
072:
073: private DefaultSVGAnimator(SVGAnimator innerAnimator,
074: SVGImage svgImage) {
075: this .innerAnimator = innerAnimator;
076: this .svgImage = svgImage;
077: innerAnimator.setSVGEventListener(this );
078:
079: Canvas targetCanvas = (Canvas) innerAnimator
080: .getTargetComponent();
081: targetCanvas.setFullScreenMode(true);
082:
083: sizeChanged(targetCanvas.getWidth(), targetCanvas.getHeight());
084: }
085:
086: public static SVGAnimator createAnimator(SVGImage svgImage) {
087: SVGAnimator innerAnimator = SVGAnimator
088: .createAnimator(svgImage);
089: SVGAnimator outerAnimator = new DefaultSVGAnimator(
090: innerAnimator, svgImage);
091:
092: return outerAnimator;
093: }
094:
095: public Object getTargetComponent() {
096: return innerAnimator.getTargetComponent();
097: }
098:
099: public float getTimeIncrement() {
100: return innerAnimator.getTimeIncrement();
101: }
102:
103: public void setTimeIncrement(float timeIncrement) {
104: innerAnimator.setTimeIncrement(timeIncrement);
105: }
106:
107: public void invokeAndWait(Runnable runnable)
108: throws InterruptedException {
109: innerAnimator.invokeAndWait(runnable);
110: }
111:
112: public void invokeLater(Runnable runnable) {
113: innerAnimator.invokeLater(runnable);
114: }
115:
116: public void play() {
117: if (animatorState != STATE_PLAYING) {
118: innerAnimator.play();
119: animatorState = STATE_PLAYING;
120: }
121: }
122:
123: public void pause() {
124: if (animatorState == STATE_PLAYING) {
125: innerAnimator.pause();
126: animatorState = STATE_PAUSED;
127: }
128: }
129:
130: public void stop() {
131: if (animatorState != STATE_STOPPED) {
132: innerAnimator.stop();
133: animatorState = STATE_STOPPED;
134: }
135: }
136:
137: public void setSVGEventListener(SVGEventListener svgEventListener) {
138: this .svgEventListener = svgEventListener;
139: }
140:
141: public void hideNotify() {
142: if (animatorState == STATE_PLAYING) {
143: innerAnimator.pause();
144: animatorState = STATE_INTERRUPTED;
145: }
146: if (svgEventListener != null) {
147: svgEventListener.hideNotify();
148: }
149: }
150:
151: public void showNotify() {
152: if (animatorState == STATE_INTERRUPTED) {
153: innerAnimator.play();
154: animatorState = STATE_PLAYING;
155: }
156: if (svgEventListener != null) {
157: svgEventListener.showNotify();
158: }
159: }
160:
161: public void keyPressed(int keyCode) {
162: if (svgEventListener != null) {
163: svgEventListener.keyPressed(keyCode);
164: }
165: }
166:
167: public void keyReleased(int keyCode) {
168: if (svgEventListener != null) {
169: svgEventListener.keyReleased(keyCode);
170: }
171: }
172:
173: public void pointerPressed(int x, int y) {
174: if (svgEventListener != null) {
175: svgEventListener.pointerPressed(x, y);
176: }
177: }
178:
179: public void pointerReleased(int x, int y) {
180: if (svgEventListener != null) {
181: svgEventListener.pointerReleased(x, y);
182: }
183: }
184:
185: public void sizeChanged(int width, int height) {
186: svgImage.setViewportWidth(width);
187: svgImage.setViewportHeight(height);
188:
189: if (svgEventListener != null) {
190: svgEventListener.sizeChanged(width, height);
191: }
192: }
193: }
|