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 flowers;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import java.lang.String;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.RadioButton;
import javafx.geometry.VPos;
import javafx.geometry.HPos;
import javafx.scene.layout.Container;
import javafx.scene.layout.Flow;
import javafx.scene.layout.Panel;
import javafx.scene.layout.Stack;
import javafx.scene.layout.Tile;
import javafx.scene.layout.Container.*;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;




class MyControl extends CustomNode {

  var text: String;
  var content: Node[];
  var image: Image;
  override function create() {
    HBox {
      spacing: 5
      nodeVPos: VPos.CENTER
      content: bind [
        content,
        Label {
          text: text
          textFill: Color.rgb(176,  176, 176, 1)
        }
      ]
    }
  }
}

var selectedContainer:Container = null;


def images = [
Image {url: "{__DIR__}fl1.png"},
Image {url: "{__DIR__}fl2.png"},
Image {url: "{__DIR__}fl3.png"},
Image {url: "{__DIR__}fl4.png"},
Image {url: "{__DIR__}fl5.png"},
Image {url: "{__DIR__}fl6.png"},
Image {url: "{__DIR__}fl7.png"},
Image {url: "{__DIR__}fl8.png"},
Image {url: "{__DIR__}fl9.png"},
Image {url: "{__DIR__}fl10.png"},
Image {url: "{__DIR__}fl11.png"},
Image {url: "{__DIR__}fl12.png"},
Image {url: "{__DIR__}fl13.png"},
Image {url: "{__DIR__}fl14.png"},
Image {url: "{__DIR__}fl15.png"},
Image {url: "{__DIR__}fl16.png"},
];

def vbox = VBox{
    nodeHPos: HPos.CENTER // center nodes horizontally within the column
    hpos: HPos.CENTER // column will be centered within vbox width
    spacing: 15
    content: [ for (i in [0..4]) ImageView {image: images[i]} ]
}

def hbox = HBox{
    spacing: 10
    vpos: VPos.CENTER // center the row vertically within the container
    content: [ for (i in [0..8]) ImageView {image: images[i]} ]
}

def stack = Stack{
    content: [ for (i in [0..7]) ImageView {image: images[i]} ]
}

def PanelViews = [ for (i in [0..15]) ImageView {image: images[i]} ];

def panel: Panel = Panel{
    content: PanelViews
    onLayout: function() {
      for (node in getManaged(panel.content)) {
        def i = indexof node;
        positionNode(node, i*40,  i*10);
      }
    }
}

def flow = Flow{
    hgap: 5 // horizontal gap between nodes in a row
    vgap: 10 // vertical gap between rows
    hpos: HPos.CENTER
    vpos: VPos.CENTER
    nodeVPos: VPos.TOP
    content: [ for (i in [0..15]) ImageView {image: images[i]} ]
}

def tile = Tile{
   columns: 4
   rows: 4
   tileWidth: 100
   hgap: 5
   vgap: 5
   vertical: true
   hpos: HPos.CENTER
   content: [ for (i in [0..15]) ImageView {image: images[i]} ]
}

class  MyToggleGroup  extends ToggleGroup {
   override var selectedButton on replace {
       println("Button selected is {selectedButton.id}");
       var id = selectedButton.id;
       if (selectedButton != null)
         if (id.matches("VBox")) { selectedContainer = vbox; }
          else if (id.matches("HBox")) { selectedContainer = hbox; }
          else if (id.matches("Stack")) { selectedContainer = stack; }
          else if (id.matches("Panel")) { selectedContainer = panel; }
          else if (id.matches("Flow")) { selectedContainer = flow; }
          else if (id.matches("Tile")) { selectedContainer = tile; }
        selectedContainer.layoutY = 20;
        selectedContainer.width = 630;
        selectedContainer.height = 350;
   }
}

var toggleGroup = MyToggleGroup{};

Application {
  title: "JavaFX Controls"
  header: ImageView {
    image: Image {
      url: "{__DIR__}logo.png"
    }
  }
  background: Color.rgb(29,  29, 29, 1)

  content:  bind [
   //Substrate for the container
    Rectangle {x: -5 y: 10 width:655 height: 350 fill: Color.web("#C4DC92")}
   //The container to experiment
    selectedContainer,
   //Controls to switch between the layout models
    VBox{
    translateY: 380
    translateX: 30
    spacing: 10
    content: [
    HBox{    
        content:[
            Label{
                text: "Switch Layout:"
                textFill: Color.rgb(176,  176, 176, 1)
                font: Font{size: 14}
            }    
        ]
    },
    HBox {
      
      spacing: 50
      content: [
         MyControl {
          content: RadioButton { toggleGroup: toggleGroup selected: true id: "VBox"}
          text: "VBox"
         },
         MyControl {
          content: RadioButton { toggleGroup: toggleGroup id: "HBox"}
          text: "HBox"
         },
         MyControl {
          content: RadioButton { toggleGroup: toggleGroup id: "Stack"}
          text: "Stack"
         },
         MyControl {
          content: RadioButton { toggleGroup: toggleGroup id: "Flow" }
          text: "Flow"
         },
         MyControl {
          content: RadioButton { toggleGroup: toggleGroup id: "Tile"}
          text: "Tile"
         },
         MyControl {
          content: RadioButton { toggleGroup: toggleGroup id: "Panel"}
          text: "Panel"
         }
        ]
       }
     ]
   }
  ]
}