package com.ht.pagination;
/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

/**
 * A sample that demonstrates pagination
 *
 * @see javafx.scene.control.Pagination 
 * @resource animal1.jpg
 * @resource animal2.jpg
 * @resource animal3.jpg
 * @resource animal4.jpg
 * @resource animal5.jpg
 * @resource animal6.jpg
 * @resource animal7.jpg
 * @resource animal8.jpg
 */
public class PaginationSample extends Application {

    private Pagination pagination;
    private TilePane[] tilePanes = new TilePane[2];
    
    private Image[] images = new Image[7];

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setScene(new Scene(root));
        VBox outerBox = new VBox();
        outerBox.setAlignment(Pos.CENTER);
        //Images for our pages
        
        tilePanes[0] = new TilePane(4, 4);
        tilePanes[0].setPrefColumns(2);
        tilePanes[1] = new TilePane(4, 4);
        tilePanes[1].setPrefColumns(2);
        for (int i = 0; i < 7; i++) {
            images[i] = new Image(PaginationSample.class.getResource("animal" + (i + 1) + ".jpg").toExternalForm(), false);
            if (i < 4) {
            	tilePanes[0].getChildren().add(createAnimalPage(i));
            } else {
            	tilePanes[1].getChildren().add(createAnimalPage(i));
            }
        }

        pagination = PaginationBuilder.create().pageCount(2).pageFactory(new Callback<Integer, Node>() {           
            @Override public Node call(Integer pageIndex) {
                return tilePanes[pageIndex];
            }
        }).build();
        //Style can be numeric page indicators or bullet indicators
        Button styleButton = ButtonBuilder.create().text("Toggle pagination style").onAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent me) {
                if (!pagination.getStyleClass().contains(Pagination.STYLE_CLASS_BULLET)) {
                    pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
                } else {
                    pagination.getStyleClass().remove(Pagination.STYLE_CLASS_BULLET);
                }
            }
        }).build();

        outerBox.getChildren().addAll(pagination, styleButton);
        root.getChildren().add(outerBox);
    }
    //Creates the page content
    private VBox createAnimalPage(int pageIndex) {
        VBox box = new VBox();
        ImageView iv = new ImageView(images[pageIndex]);
        box.setAlignment(Pos.CENTER);
        Label desc = new Label("PAGE " + (pageIndex + 1));
        box.getChildren().addAll(iv, desc);
        return box;
    }

    @Override public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
    }
    public static void main(String[] args) { launch(args); }
}
