/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems 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 bindcaution;

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.LayoutInfo;
import javafx.scene.layout.VBox;
import java.lang.System;
import javafx.scene.control.Slider;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.text.Font;

import javafx.scene.paint.Color;

var textFont = Font size: 12 };

/*********************************************
 * Use of bind + on-replace on load
 *********************************************/

var updateCountError = 0;
var errorUpdateLabel = Text {
    content: bind "Update {updateCountError} Time(s)!"
    wrappingWidth: 140
    font: textFont
    fill: Color.RED
    layoutInfo: LayoutInfo {
        width: 150
        height: 150
    }
}

var updateCountOpt = 0;
var optUpdateLabel = Text {
    content: bind "Update {updateCountOpt} Time(s)!"
    wrappingWidth: 140
    font: textFont
    fill: Color.GREEN
    layoutInfo: LayoutInfo {
        width: 150
        height: 150
    }
}

var x = on replace {
    updateError();
    updateOptimized();
}

var y = on replace {
    updateError();
    updateOptimized();
}

var width = on replace {
    updateError();
    updateOptimized();
}

var height = on replace {
    updateError();
    updateOptimized();
}

function updateError() {
    updateCountError++;
}

function updateOptimized() {
    if((x > 0and (y > 0and (width > 0and (height > 0)) {
        updateCountOpt++;
    }
}

/*********************************************
 * Use of bind + on-replace on Slider.value
 *********************************************/

var errorCount = 0;
var errorLabel = Text {
    content: bind "Complex Operation Performed {errorCount} Time(s)!"
    wrappingWidth: 140
    font: textFont
    fill: Color.RED
    layoutInfo: LayoutInfo {
        width: 150
        height: 150
    }
}

var errorSlider = Slider {
    min: 0
    max: 100
    value: 50
    vertical: false
    layoutInfo: LayoutInfo {
        width: 150
        height: 12
    }
}

var errorSliderValue = bind errorSlider.value on replace {
    /**
     * Perform Complex Operation!!!!
     */
    errorCount++;
}

var optCTM = System.currentTimeMillis();

var vBoxError = VBox {
    content: errorUpdateLabel, errorLabel, errorSlider ]
    spacing: 10
}

var optCount = 0;
var optLabel = Text {
    content: bind "Complex Operation Performed {optCount} Time(s)!"
    wrappingWidth: 140
    font: textFont
    fill: Color.GREEN
    layoutInfo: LayoutInfo {
        width: 150
        height: 150
    }
}

var optSlider = Slider {
    min: 0
    max: 100
    value: 50
    vertical: false
    layoutInfo: LayoutInfo {
        width: 150
        height: 12
    }
}

var timeline = Timeline {
    keyFrames: KeyFrame {
        time: 100ms
        action: function() {
            /**
             * Perform Complex Operation!!!!
             */
            optCount++;
        }
    }
}

var optSliderValue = bind optSlider.value on replace {
    timeline.playFromStart();
}

var vBoxOpt = VBox {
    content: optUpdateLabel, optLabel, optSlider ]
    spacing: 10
}

Stage {

    title: "Bind with Caution"
    scene: Scene {
        content: [
            HBox {
                content: vBoxError, vBoxOpt ]
                spacing: 10
                translateX: 10
                translateY: 10
            }
        ]
        width: 330
        height: 120
    }
    resizable: false
}

x = 10;
y = 10;
width = 600;
height = 400;