001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024: package com.sun.mmedia;
025:
026: import java.awt.*;
027: import java.awt.image.*;
028: import javax.microedition.media.MediaException;
029: import javax.microedition.media.Control;
030: import javax.microedition.media.control.VideoControl;
031:
032: public class AWTVideoRenderer extends VideoRenderer implements
033: VideoControl {
034: /**
035: * The video canvas object.
036: */
037: private VideoCanvas canvas;
038:
039: /**
040: * The display mode
041: */
042: private int mode = -1;
043:
044: /**
045: * Display X
046: */
047: private int dx;
048: /**
049: * Display Y
050: */
051: private int dy;
052: /**
053: * Display Width
054: */
055: private int dw;
056: /**
057: * Display Height
058: */
059: private int dh;
060:
061: /**
062: * Source width
063: */
064: private int sw;
065: /**
066: * Source height
067: */
068: private int sh;
069:
070: /*
071: * Locator string used to open the Player instance which this VideoControl is assoicated with.
072: */
073: private String locatorString;
074:
075: /**
076: *Constructor for the VideoRenderer object
077: */
078: public AWTVideoRenderer(BasicPlayer player, int sourceWidth,
079: int sourceHeight) {
080: locatorString = ((GIFPlayer) player).getLocator();
081: }
082:
083: public Control getVideoControl() {
084: return (VideoControl) this ;
085: }
086:
087: /**
088: * Description of the Method
089: *
090: * @param mode Description of the Parameter
091: * @param sourceWidth Description of the Parameter
092: * @param sourceHeight Description of the Parameter
093: */
094: public void initRendering(int mode, int sourceWidth,
095: int sourceHeight) {
096: setSourceSize(sourceWidth, sourceHeight);
097: }
098:
099: /**
100: * Sets the sourceSize attribute of the AWTVideoRenderer object
101: *
102: * @param sourceWidth The new sourceSize value
103: * @param sourceHeight The new sourceSize value
104: */
105: private void setSourceSize(int sourceWidth, int sourceHeight) {
106: sw = sourceWidth;
107: sh = sourceHeight;
108:
109: // Default display width and height
110: dw = sw;
111: dh = sh;
112: }
113:
114: /**
115: * Description of the Method
116: *
117: * @param rgbData Description of the Parameter
118: */
119: public void render(int[] rgbData) {
120: // csaito: adding this if.
121: if (canvas != null) {
122:
123: Image img = Toolkit.getDefaultToolkit().createImage(
124: new MemoryImageSource(sw, sh, rgbData, 0, sw));
125:
126: canvas.updateImage(img);
127: }
128:
129: }
130:
131: /**
132: * Gets the snapshot attribute of the VideoRenderer object
133: *
134: * @param imageType Description of the Parameter
135: * @return The snapshot value
136: * @exception MediaException Description of the Exception
137: */
138:
139: public byte[] getSnapshot(String imageType) throws MediaException {
140: checkPermission();
141: throw new MediaException("getSnapshot not implemented!");
142: // return null;
143: }
144:
145: /**
146: * Check for the image snapshot permission.
147: *
148: * @exception SecurityException if the permission is not
149: * allowed by this token
150: */
151: private void checkPermission() throws SecurityException {
152: try {
153: PermissionAccessor.checkPermissions(locatorString,
154: PermissionAccessor.PERMISSION_SNAPSHOT);
155: } catch (InterruptedException e) {
156: throw new SecurityException(
157: "Interrupted while trying to ask the user permission");
158: }
159: }
160:
161: /**
162: * Gets the displayWidth attribute of the VideoRenderer object
163: *
164: * @return The displayWidth value
165: */
166: public int getDisplayWidth() {
167: checkState();
168: return dw;
169: }
170:
171: /**
172: * Gets the displayHeight attribute of the VideoRenderer object
173: *
174: * @return The displayHeight value
175: */
176: public int getDisplayHeight() {
177: checkState();
178: return dh;
179: }
180:
181: /**
182: * Gets the sourceWidth attribute of the VideoRenderer object
183: *
184: * @return The sourceWidth value
185: */
186: public int getSourceWidth() {
187: return sw;
188: }
189:
190: /**
191: * Gets the sourceHeight attribute of the VideoRenderer object
192: *
193: * @return The sourceHeight value
194: */
195: public int getSourceHeight() {
196: return sh;
197: }
198:
199: /**
200: * Sets the displaySize attribute of the VideoRenderer object
201: *
202: * @param width The new displaySize value
203: * @param height The new displaySize value
204: * @exception javax.microedition.media.MediaException Description of the Exception
205: */
206: public void setDisplaySize(int width, int height)
207: throws javax.microedition.media.MediaException {
208: checkState();
209:
210: if (width <= 0 || height <= 0)
211: throw new IllegalArgumentException(
212: "Width and Height must be positive");
213:
214: if (mode == USE_GUI_PRIMITIVE) { // should be, as USE_DIRECT_VIDEO is unsupported
215: if (canvas != null) {
216: canvas.setSize(width, height);
217: }
218: }
219:
220: }
221:
222: /**
223: * Sets the displayFullScreen attribute of the VideoRenderer object
224: *
225: * @param fullScreenMode The new displayFullScreen value
226: * @exception javax.microedition.media.MediaException Description of the Exception
227: */
228: public void setDisplayFullScreen(boolean fullScreenMode)
229: throws javax.microedition.media.MediaException {
230: if (fullScreenMode) { // csaito: may want to implement this properly
231: throw new MediaException("No Fullscreen mode");
232: }
233: }
234:
235: /**
236: * Sets the visible attribute of the VideoRenderer object
237: *
238: * @param visible The new visible value
239: */
240: public void setVisible(boolean visible) {
241: checkState();
242: }
243:
244: /**
245: * Gets the displayX attribute of the VideoRenderer object
246: *
247: * @return The displayX value
248: */
249: public int getDisplayX() {
250: return dx;
251: }
252:
253: /**
254: * Gets the displayY attribute of the VideoRenderer object
255: *
256: * @return The displayY value
257: */
258: public int getDisplayY() {
259: return dy;
260: }
261:
262: /**
263: * Initializes the display mode.
264: *
265: * Currently only USE_GUI_PRIMITIVE is supported!
266: *
267: * @param mode the display mode
268: * @param container the container (an AWT component)
269: * @return returns the AWT component
270: */
271: public Object initDisplayMode(int mode, Object container) {
272: if (this .mode != -1) {
273: throw new IllegalStateException("mode already set");
274: }
275:
276: if (mode == USE_DIRECT_VIDEO && mode != USE_GUI_PRIMITIVE) {
277: throw new IllegalArgumentException("unsupported mode");
278: }
279:
280: // csaito: the spec seems to require "container" to be a String obj.
281: // thus this is a wrong logic...
282: //if (mode == USE_DIRECT_VIDEO && !(container instanceof Canvas)) {
283: // throw new IllegalArgumentException("container needs to be a Canvas");
284: //}
285:
286: if (mode == USE_GUI_PRIMITIVE && container != null
287: && !(container instanceof String)) {
288: throw new IllegalArgumentException(
289: "Invalid Object arg parameter");
290: }
291:
292: if (container != null) {
293: try {
294: Class awtClazz = Class.forName((String) container);
295: if (!(awtClazz
296: .isAssignableFrom(java.awt.Component.class))) {
297: throw new IllegalArgumentException(
298: "String does not represent a valid AWT class");
299: }
300: } catch (Exception e) {
301: throw new IllegalArgumentException(
302: "String does not represent a valid AWT class");
303: }
304: }
305:
306: this .mode = mode;
307:
308: canvas = new VideoCanvas();
309:
310: return (Object) canvas;
311: }
312:
313: /**
314: * Sets the displayLocation attribute of the VideoRenderer object
315: *
316: * @param x The new displayLocation value
317: * @param y The new displayLocation value
318: */
319: public void setDisplayLocation(int x, int y) {
320: checkState();
321:
322: if (mode == USE_DIRECT_VIDEO) {
323: dx = x;
324: dy = y;
325: canvas.repaint();
326: }
327: }
328:
329: /**
330: * Description of the Method
331: */
332: private void checkState() {
333: if (mode == -1) {
334: throw new IllegalStateException(
335: "initDisplayMode not called yet");
336: }
337: }
338:
339: /**
340: * Description of the Method
341: */
342: public void close() {
343: // release all resources
344: if (canvas != null) {
345: canvas.close();
346: canvas = null;
347: }
348: }
349:
350: /**
351: * Description of the Class
352: *
353: */
354: //class VideoCanvas extends Canvas {
355: class VideoCanvas extends Component {
356: Image img;
357:
358: int preferredWidth = -1, preferredHeight = -1;
359:
360: /**
361: * Description of the Method
362: *
363: * @param g Description of the Parameter
364: */
365: public void paint(Graphics g) {
366: if (img != null) {
367: g.drawImage(img, dx, dy, dw, dh, this );
368: }
369: }
370:
371: /**
372: * Description of the Method
373: *
374: * @param img Description of the Parameter
375: */
376: public void updateImage(Image img) {
377: if (img != null) {
378: if (preferredWidth == -1 || preferredHeight == -1) {
379: preferredWidth = img.getWidth(this );
380: preferredHeight = img.getHeight(this );
381: this .invalidate();
382: this .getParent().validate();
383: }
384: this .img = img;
385: repaint();
386: }
387: }
388:
389: public void close() {
390: img = null;
391: }
392:
393: public Dimension getPreferredSize() {
394: return new Dimension(preferredWidth, preferredHeight);
395: }
396:
397: public Dimension getMinimumSize() {
398: return getPreferredSize();
399: }
400: }
401: }
|