001: package com.xoetrope.swing;
002:
003: import com.xoetrope.swing.video.XVideoController;
004: import java.awt.BorderLayout;
005: import java.io.IOException;
006:
007: import java.awt.Color;
008: import java.awt.Component;
009: import java.awt.Dimension;
010: import java.awt.Graphics;
011: import java.awt.event.ComponentEvent;
012: import java.awt.event.ComponentListener;
013: import javax.swing.JComponent;
014: import javax.swing.JLayeredPane;
015: import javax.swing.JPanel;
016: import net.xoetrope.xui.XAttributedComponent;
017: import net.xoetrope.xui.XProject;
018: import net.xoetrope.xui.XProjectManager;
019:
020: /**
021: * Video Playback Control
022: *
023: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
024: * the GNU Public License (GPL), please see license.txt for more details. If
025: * you make commercial use of this software you must purchase a commercial
026: * license from Xoetrope.</p>
027: * <p> $Revision: 1.14 $</p>
028: */
029: public class XVideo extends JComponent implements XAttributedComponent,
030: ComponentListener {
031: private XVideoController controller;
032: private boolean autoLoop = false;
033: private boolean autoStart = false;
034: private boolean showController = true;
035: private JLayeredPane layeredPane;
036: private JPanel videoPanel;
037: private java.awt.Panel componentPanel;
038: private String fileName;
039:
040: /**
041: * The owner project and the context in which this object operates.
042: */
043: protected XProject currentProject = XProjectManager
044: .getCurrentProject();
045:
046: /**
047: * Create a new video component
048: */
049: public XVideo() {
050: setLayout(new BorderLayout());
051: layeredPane = new JLayeredPane();
052: videoPanel = new JPanel();
053: videoPanel.setLayout(new BorderLayout());
054: videoPanel.setDoubleBuffered(true);
055:
056: layeredPane.add(videoPanel, new Integer(1));
057:
058: addComponentListener(this );
059: super .add(layeredPane, BorderLayout.CENTER);
060:
061: try {
062: init();
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: }
067:
068: /**
069: * Render the component
070: * @param g the graphics context
071: */
072: public void paintComponent(Graphics g) {
073: int w = getSize().width - 1;
074: int h = getSize().height - 1;
075:
076: g.setColor(Color.blue);
077: g.drawRect(0, 0, w, h);
078:
079: g.drawLine(w / 10, 0, w / 10, h);
080: g.drawLine(9 * w / 10, 0, 9 * w / 10, h);
081:
082: for (int i = 0; i < 10; i++) {
083: g.drawLine(0, i * h / 10, w / 10, i * h / 10);
084: g.drawLine(9 * w / 10, i * h / 10, w, i * h / 10);
085: }
086: }
087:
088: /**
089: * Performs any post creation initialisation of the control.
090: * @throws java.io.IOException problems setting up the video
091: */
092: public void init() throws IOException {
093: // Add ourselves as a listener for a player's events
094: try {
095: controller = (XVideoController) getClass().forName(
096: "com.xoetrope.swing.video.XControllerListener")
097: .newInstance();
098: controller.setVideoPanel(videoPanel);
099: setShowController(showController);
100: if (autoLoop)
101: loop();
102: } catch (Exception ex) {
103: ex.printStackTrace();
104: }
105: }
106:
107: /**
108: * Destroy the video controller and release its resources
109: */
110: public void destroy() {
111: if (controller != null)
112: controller.destroy();
113: }
114:
115: /**
116: * Get the loop flag
117: * @return true if the video loops upon completion
118: */
119: public boolean getLoop() {
120: return autoLoop;
121: }
122:
123: /**
124: * Set the loop flag
125: * @param value true if the video loops upon completion
126: */
127: public void setLoop(boolean value) {
128: autoLoop = value;
129: }
130:
131: /**
132: * Gets the name of the video or sound to play.
133: * @return the name of the video to play.
134: */
135: public String getValue() {
136: return fileName;
137: }
138:
139: /**
140: * Sets the name of the video or sound to play.
141: * @param _fileName name of the video to play.
142: */
143: public void setValue(String _fileName) {
144: fileName = _fileName;
145: stop();
146: try {
147: init();
148: } catch (Exception e) {
149: }
150: if (controller != null)
151: controller.setFile(currentProject.findResource(_fileName)
152: .toString());
153:
154: setShowController(showController);
155: if (autoStart)
156: start();
157: }
158:
159: /**
160: * Set and display the video controller
161: * @param display true to display the controller
162: */
163: public void setShowController(boolean display) {
164: if (controller != null)
165: controller.showController(display);
166: showController = display;
167: }
168:
169: /**
170: * Gets the show video controller flag
171: * @return true if the playback controller is displayed
172: */
173: public boolean getShowController() {
174: return showController;
175: }
176:
177: /**
178: * Set the autostart flag
179: * @param state true to autostart the video on display
180: */
181: public void setAutoStart(boolean state) {
182: autoStart = state;
183: }
184:
185: /**
186: * Get the autostart flag
187: * @return the current flag value
188: */
189: public boolean getAutoStart() {
190: return autoStart;
191: }
192:
193: /**
194: * Starts playing the video
195: */
196: public void start() {
197: if (controller != null)
198: controller.start();
199: }
200:
201: /**
202: * Continuous playback of the video.
203: */
204: public void loop() {
205: if (controller != null)
206: controller.loop();
207: }
208:
209: /**
210: * Stops playback of the video.
211: */
212: public void stop() {
213: if (controller != null)
214: controller.stop();
215: }
216:
217: /**
218: * Set one or more attributes of the component. Currently this handles the
219: * attributes
220: * <OL>
221: * <LI>content, value=the video filename</LI>
222: * <LI>loop, value=true for continuous loop playback</LI>
223: * <LI>start, value=true for automatically start the playback when the video is displayed</LI>
224: * <LI>controller, value=true to show the video playback controls</LI>
225: * </OL>
226: * @param attribName the attribute name
227: * @param attribValue the attribute value
228: * @return 0 for success, non zero otherwise
229: */
230: public int setAttribute(String attribName, Object attribValue) {
231: String attribNameLwr = attribName.toLowerCase();
232: String attribValueStr = (String) attribValue;
233: String attribValueLwr = attribValueStr.toLowerCase();
234: if (attribNameLwr.equals("content")
235: || attribNameLwr.equals("video"))
236: setValue(attribValueStr);
237: else if (attribNameLwr.equals("loop"))
238: autoLoop = attribValueLwr.equals("true");
239: else if (attribNameLwr.equals("start")
240: || attribNameLwr.equals("autostart")) {
241: if (autoStart = attribValueLwr.equals("true"))
242: start();
243: } else if (attribNameLwr.equals("controller"))
244: setShowController(attribValueLwr.equals("true"));
245:
246: return 0;
247: }
248:
249: /**
250: * Add a component to the components layer. Due to limitations in JMF a heavy
251: * weight component is created for video playback. Therefore a heavyweight
252: * host for added components needs to be added or the video will obscure those
253: * components. The first component added to the video should be a panel
254: * @param c the component to add
255: * @return the added component
256: */
257: public Component add(Component c) {
258: setupComponentPanel(c);
259: return componentPanel.add(c);
260: }
261:
262: /**
263: * Add a component to the components layer. Due to limitations in JMF a heavy
264: * weight component is created for video playback. Therefore a heavyweight
265: * host for added components needs to be added or the video will obscure those
266: * components. The first component added to the video should be a panel
267: * @param c the component to add
268: * @param constraint the layout constraints
269: */
270: public void add(Component c, Object constraint) {
271: setupComponentPanel(c);
272: componentPanel.add(c, constraint);
273: }
274:
275: /**
276: * Setup a heavy weight component to host the other components
277: */
278: private void setupComponentPanel(Component c) {
279: if (componentPanel == null) {
280: componentPanel = new java.awt.Panel();
281: componentPanel.setLayout(new BorderLayout());
282: layeredPane.add(componentPanel, new Integer(10));
283: componentPanel.add(c, BorderLayout.CENTER);
284: componentPanel.setBounds(c.getBounds());
285: }
286: }
287:
288: /**
289: * Invoked when the component's size changes.
290: * @param e the resize event
291: */
292: public void componentResized(ComponentEvent e) {
293: Dimension sz = getSize();
294: videoPanel.setBounds(0, 0, sz.width, sz.height);
295: videoPanel.doLayout();
296: if (componentPanel != null)
297: componentPanel.doLayout();
298: videoPanel.repaint();
299: if (componentPanel != null)
300: componentPanel.repaint();
301: }
302:
303: /**
304: * Invoked when the component's position changes.
305: * @param e the resize event
306: */
307: public void componentMoved(ComponentEvent e) {
308: videoPanel.repaint();
309: if (componentPanel != null)
310: componentPanel.repaint();
311: }
312:
313: /**
314: * Invoked when the component has been made visible.
315: * @param e the resize event
316: */
317: public void componentShown(ComponentEvent e) {
318: videoPanel.repaint();
319: if (componentPanel != null)
320: componentPanel.repaint();
321: }
322:
323: /**
324: * Invoked when the component has been made invisible.
325: * @param e the resize event
326: */
327: public void componentHidden(ComponentEvent e) {
328: }
329: }
|