License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package fxpodcastviewer.media;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.media.MediaView;
import javafx.scene.media.Media;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.animation.Timeline;
import javafx.animation.Interpolator;
import javafx.scene.media.MediaError;
import javafx.scene.layout.Stack;

/**
 * @author Raghu Nair
 */

public class MediaBox extends CustomNode{

    //Media Title.
    public var mediaTitle: String;

    //media view width.
    public var mediaViewWidth: Number = 640;

    //media height
    public var mediaViewHeight: Number = 330;

    public var mediaPlayerAutoPlay: Boolean = false;

    public-read var myPlayer: MyPlayer = MyPlayer {
        onEndOfMedia: function() {
            myPlayer.currentTime = 0s;
            myPlayer.pause();
        }
        onError:function(e:MediaError){
            loading = false;
            errorString = e.toString();
        }
    }

    //media view
    var mediaView: MediaView = MediaView {
        mediaPlayer:  myPlayer
        preserveRatio: false
        fitWidth:  mediaViewWidth
        fitHeight:  mediaViewHeight
        
    }

//    var controllBar:ControllBar = ControllBar {
//        translateY: bind mediaView.layoutY + mediaViewHeight - Main.iconSize *.75
//        mediaPlayer: bind myPlayer
//    }
//
//    var bufferSpinner:BufferSpinner = BufferSpinner{
//        numArcs: bind (Main.iconSize * .75) as Integer
//        radius:bind Main.iconSize *2
//        translateX: bind mediaViewWidth /2 - bufferSpinner.radius
//        translateY: bind (mediaView.layoutY + mediaViewHeight)/2 - bufferSpinner.radius
//    }

    public var loading = false on replace {
//        if (loading) bufferSpinner.play() else bufferSpinner.stop()
    }

    public var mediaSourceURL: String on replace {

        if (mediaSourceURL != null ){
            myPlayer.progress = 0.0;
            myPlayer.bufferProgress = 0.0;
            myPlayer.media = Media{
                source:mediaSourceURL
                onError:function(e:MediaError){
                    errorString = e.toString();
                }
            }
        }
        if(mediaPlayerAutoPlay == true) {
            myPlayer.play();
        }
    }


    var background =Rectangle {
        width: bind mediaViewWidth
        height: bind mediaViewHeight
        fill:Color.BLACK
    }

    public var errorString ="" on replace {
        if ( errorString != "" ){
            loading = false;
            errorTimeline.playFromStart();
        }
    }

    var errorText:Text = Text {
        content: bind errorString;
        font: Font{ size: 20 }
        translateX: bind  (mediaViewWidth - errorText.boundsInLocal.width) / 2;
        translateY: bind  (mediaViewHeight - errorText.boundsInLocal.height) / 2;
        fill: Color.SKYBLUE
        wrappingWidth: mediaViewWidth - 50
    }


    public var errorTimeline:Timeline = Timeline {
        keyFrames: [
            at (0s) {
                errorText.opacity => 0.0;
            },
            at (1s) {
                errorText.opacity => 1.0 tween Interpolator.EASEBOTH;
                background.fill => Color.LIGHTSEAGREEN tween Interpolator.EASEIN;
            },
            at(30s){
                errorText.opacity => 0.0 tween Interpolator.LINEAR;
                background.fill => Color.BLACK tween Interpolator.LINEAR;
            }
        ]
    }


    override function create():Node{
        errorText.opacity = 0.0;
        Stack{
            content: [
                background,
                errorText,
                mediaView,
                //controllBar,
                //bufferSpinner,
                // to capture input events on top.
                Rectangle {
                    width: bind mediaViewWidth
                    height: bind mediaViewHeight
                    fill: Color.TRANSPARENT

                    /*onMouseEntered: function(me:MouseEvent) {
                        controllBar.show.playFromStart();
                    }
                    onMouseExited: function(me:MouseEvent) {
                        controllBar.show.rate = -1.0;
                        controllBar.opacity = 0.0;
                    }*/
                }

                ]
        }
    }
}