Legal Terms and Copyright Notice
/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
 * Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.
 * 
 * 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, 
        trademark notice, this list of conditions, and the following disclaimer.

 *   *  Redistributions in binary form must reproduce the above copyright notice, 
        trademark 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 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 shoulddrag;

import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.AppletStageExtension;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


var inbrowser = true;

// Following variable is true if browser and Java configuration support applet dragging.
var draggable = AppletStageExtension.appletDragSupported;

// Rectangle for the window title bar
var dragRect: Rectangle;
dragRect = Rectangle {  //define draggable region
    x: 0
    y: 0
    width: 240
    height: 30
    fill: LinearGradient {
        startX: 0.0
        startY: 0.0
        endX: 0.0
        endY: 1.0
        stops: [
            Stop {
                color: Color.BLACK
                offset: 0.0
            },
            Stop {
                color: Color.LIGHTSLATEGRAY
                offset: 0.3
            },
            Stop {
                color: Color.BLACK
                offset: 1.0
            },

        ]
    }
    /* The following code implements the standard dragging behavior after
    the applet has moved outside of the browser.
    */
    onMouseDragged: function(e:MouseEvent):Void {
        stage.x += e.dragX;
        stage.y += e.dragY;
    }

};

/* The following variable makes the window title message visible if the following
two conditions are true:
- applet dragging is supported by platform and Java version;
- the mouse is hovering over the draggable area;
*/
var dragTextVisible = bind draggable and dragRect.hover;

//Text for window title bar
var can_drag_me_text: Text = Text {
    content: bind if(inbrowser) {
        "Drag this bar out of the browser!"
        }
        else {
        "Click 'x' to return me to the browser"
        }
    fill: Color.WHITE
    underline: bind if (inbrowser) false else true
    font: Font {
    size: 12
//        embolden: true
        name: "Arial Bold"
    }
      visible: bind dragTextVisible
    x: 18
    y: 18
};

var stage = Stage {
    title: "A Draggable Applet"
    width: 240
    height: 270
    style: StageStyle.TRANSPARENT

    scene: Scene {
       fill: LinearGradient {
            startX: 0.0
            startY: 0.0
            endX: 1.0
            endY: 1.0
            stops: [
              Stop {
                  color: Color.POWDERBLUE
                  offset: 0.0
                  },
                  Stop {
                      color: Color.DARKSLATEGRAY
                      offset: 1.0
                      },
                      ]
        }


        content: [
                Circle {
            centerX: 120, centerY: 150
            radius: 120
            fill: bind if (inbrowser) Color.WHITE else Color.MIDNIGHTBLUE
            opacity: bind if (inbrowser) 0.85 else 0.30
        }

                Text {
                     content: bind if (inbrowser) {
                           "Qualified users see text \n "
                              "in the top bar when \n "
                              "they mouse over it. \n "
                              "They can drag the applet \n"
                              "out of the window."
                              }
                              else { "I'm free!" }

                     textAlignment: TextAlignment.CENTER
                     fill: bind if (inbrowser) Color.DARKSLATEGRAY else Color.WHITE
                         font: bind if (inbrowser) {
                           Font {
                             size: 14
                             embolden: true
                             name: "Arial"
                             }
                             }
                             else {
                                 Font {
                                size: 48
                             embolden: true
                             name: "Arial"
                             }
                           }

                        x: 30
                        y: bind if (inbrowser) 120 else 170
                 }
            dragRect,
            can_drag_me_text

        ]
    }
    extensions: [
        AppletStageExtension {
            //define mouse state if cursor is in draggable region
            shouldDragStart: function(e): Boolean {
                return e.primaryButtonDown and dragRect.hover;
            }
            //set variable to indicate when applet is out of the browser
            onDragFinished: function(): Void  {
                inbrowser=false;
            }
            //set variable to indicate when applet is in the browser
            onAppletRestored: function(): Void  {
                inbrowser=true;
            }

            useDefaultClose: true
        }
    ]

}