/*
 * @(#)JMFVideoView.java  1.3.2  2006-07-23
 *
 * Copyright (c) 1999-2006 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.view;

import ch.randelshofer.util.ControllerTracker;

import java.awt.Component;

import java.net.URL;

import java.io.IOException;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
import javax.activation.FileDataSource;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;


import javax.swing.*;

/**
 * This viewer displays videos decoded by JMF (Java Media Framework).
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
 * @version 1.3.2 2006-07-23 InitCause in IOException. 
 * <br>1.3.1 2004-12-24 Changed text returned by method toString.
 * <br>1.3 2003-04-07 Automatic start of the player when the 'autoPlay'
 * feature is now handled by this class and not anymore by its superclass.
 * <br>1.1  2000-09-28   New super class: AbstractVideoView.
 * <br> 1.0.1   2000-06-11  Text constants are read from a ResourceBundle.
 * <br>history  1.0  1999-10-19
 */
public class JMFVideoView extends AbstractVideoView {
    private Player player;
    private Component playerVisualComponent; //??


    public JMFVideoView() {
    }

    /** Sets the command context for the bean. The receiver loads
     * the image data provided by the data handler and displays it.
     */
  @Override
    public void setCommandContext(String verb, DataHandler dh)
    throws IOException {
        dispose();
        if (dh != null) {
            try {
                DataSource source = dh.getDataSource();
                if (source instanceof URLDataSource) {
                    setURL( ((URLDataSource) source).getURL() );
                } else if (source instanceof FileDataSource) {
                    setURL( new URL("file:"+((FileDataSource)source).getFile()) );
                } else {
                    // no URL available?  can't do anything about it.
                    throw new IOException("cannot retrieve URL from data source");
                }
                player.realize();
                //player.start();
            } catch (Throwable e) {
               IOException ioe = new IOException(e.toString());
               ioe.initCause(e);
               throw ioe;
            }
        }
    }

    @Override
    public void startPlayer() {
        if (player != null) {
            player.start();
        }
    }
    @Override
    public void stopPlayer() {
        if (player != null) {
            player.stop();
        }
    }

    @Override
    public void init() {
        super.init();
        if (isAutoPlay()) {
            SwingUtilities.invokeLater(
            new Runnable() {
                @Override
                public void run() {
                    startPlayer();
                }
            }
            );
        }
    }

    public void setURL(URL url)
    throws IOException, NoPlayerException {
        dispose();

        setVideoComponent(null);
        setControllerComponent(null);

        player = Manager.createPlayer(url);
       
        player.realize();
        ControllerTracker tracker = new ControllerTracker(player);
        if (tracker.waitForState(player.Realized) == ControllerTracker.STATE_NOT_REACHED) {
            IOException ioe = new IOException(tracker.getMessage());
           
          //  ioe.initCause(tracker);
          //  player.
            throw ioe;
        }
        tracker.setController(null);
        setVideoComponent(player.getVisualComponent());
        setControllerComponent(player.getControlPanelComponent());
        validate();
    }

    @Override
    public void dispose() {
        if (player != null) {
            player.stop();
            player.close();
            player = null;
        }
    }

    @Override
    public String toString() {
        return "Java Media Framework View (JMF)";
    }
}
