1 /*
   2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  */
  24 package test.scenegraph.app;
  25 
  26 //import java.awt.Event;
  27 import javafx.animation.KeyFrame;
  28 import javafx.animation.Timeline;
  29 import javafx.event.ActionEvent;
  30 import javafx.event.Event;
  31 import javafx.event.EventHandler;
  32 import javafx.scene.Node;
  33 import javafx.scene.control.Button;
  34 import javafx.scene.control.ButtonBuilder;
  35 import javafx.scene.control.Label;
  36 import javafx.scene.layout.HBox;
  37 import javafx.scene.layout.Pane;
  38 import javafx.scene.layout.StackPane;
  39 import javafx.scene.layout.TilePane;
  40 import javafx.scene.layout.VBox;
  41 import javafx.scene.paint.Color;
  42 import javafx.scene.paint.Paint;
  43 import javafx.scene.shape.Rectangle;
  44 import javafx.scene.text.Font;
  45 import javafx.scene.text.FontWeight;
  46 import javafx.scene.text.Text;
  47 import javafx.util.Duration;
  48 import test.javaclient.shared.BasicButtonChooserApp;
  49 import test.javaclient.shared.TestNode;
  50 
  51 /**
  52  *
  53  * @author Sergey Grinev
  54  */
  55 public class TimelineApp extends BasicButtonChooserApp {
  56     public TimelineApp() {
  57         super(600, 600, "Timeline",false);
  58     }
  59 
  60     public static void main(String[] args) {
  61         test.javaclient.shared.Utils.launch(TimelineApp.class, args);
  62     }
  63     public static final Paint[] colors = new Paint[]{Color.RED, Color.GREEN, Color.GRAY, Color.BLUE, Color.CYAN};
  64 
  65     private Timeline createTimelineAndPlayButton(final Pane field, final String id) {
  66         final Timeline timeline = new Timeline();
  67 
  68         final StackPane stack = new StackPane();
  69         final Rectangle rect = new Rectangle(0, 10, 30, 30);
  70         final Text text = new Text();
  71         text.setFont(Font.font("Arial", FontWeight.BOLD, 18));
  72         text.setFill(Color.WHITE);
  73 
  74         stack.getChildren().add(rect);
  75         stack.getChildren().add(text);
  76 
  77         for (int i = 0; i < 5; i++) {
  78             final int ii = i;
  79             Duration d = new Duration(300 * (i + 1));
  80             KeyFrame fr = new KeyFrame(d, new EventHandler() {
  81                 public void handle(Event t) {
  82                     rect.setId("rect_" + id + "_" + ii);
  83                     rect.setFill(colors[ii]);
  84                     text.setText(Integer.toString(ii));
  85                 }
  86             });
  87             timeline.getKeyFrames().add(fr);
  88             /*
  89              * timeline.getKeyFrames().add(new KeyFrame(d, new Runnable() {
  90              * public void run() { rect.setId("rect_" + id + "_" + ii);
  91              * rect.setFill(colors[ii]); text.setText(Integer.toString(ii)); }
  92              * }));
  93              */
  94         }
  95 
  96         Button tb = new Button("play" + id);
  97         tb.setOnAction(new EventHandler<ActionEvent>() {
  98             public void handle(ActionEvent t) {
  99                 timeline.play();
 100             }
 101         });
 102 
 103         field.getChildren().add(tb);
 104         field.getChildren().add(stack);
 105 
 106         return timeline;
 107     }
 108 
 109     @Override
 110     protected TestNode setup() {
 111         TestNode root = new TestNode();
 112         TestNode page = new TestNode(){
 113             HBox root = null;
 114             private void addSlot(String name,Pane field){
 115                 VBox slot = new VBox();
 116                 slot.getChildren().addAll(new Label(name),field);
 117                 root.getChildren().add(slot);
 118             }
 119             public Node drawNode() {
 120                 root = new HBox();
 121                 //plain timeline
 122                 {
 123                     final Pane field = new HBox(5);
 124                     Timeline timeline = createTimelineAndPlayButton(field, "1");
 125                     timeline.setAutoReverse(false);
 126                     timeline.setCycleCount(1);
 127 
 128                     addSlot("plain", field);
 129                 }
 130                 //autoreverse
 131                 {
 132                     final Pane field = new HBox(5);
 133                     Timeline timeline = createTimelineAndPlayButton(field, "2");
 134                     timeline.setAutoReverse(true);
 135                     timeline.setCycleCount(2);
 136 
 137                     addSlot("autoreverse", field);
 138                 }
 139                 //infinite + stop
 140                 {
 141                     final TilePane field = new TilePane(5, 5);
 142                     field.setPrefColumns(2);
 143                     final Timeline timeline = createTimelineAndPlayButton(field, "3");
 144                     timeline.setAutoReverse(false);
 145                     timeline.setCycleCount(Timeline.INDEFINITE);
 146                     field.getChildren().add(ButtonBuilder.create().text("stop").onAction(new EventHandler<ActionEvent>() {
 147                         public void handle(ActionEvent t) {
 148                             timeline.stop();
 149                         }
 150                     }).build());
 151 
 152                     addSlot("infinite-stop", field);
 153                 }
 154                 //infinite + pause
 155                 {
 156                     final TilePane field = new TilePane(5, 5);
 157                     field.setPrefColumns(2);
 158                     final Timeline timeline = createTimelineAndPlayButton(field, "4");
 159                     timeline.setAutoReverse(false);
 160                     timeline.setCycleCount(Timeline.INDEFINITE);
 161                     field.getChildren().add(ButtonBuilder.create().text("pause").onAction(new EventHandler<ActionEvent>() {
 162                         public void handle(ActionEvent t) {
 163                             timeline.pause();
 164                         }
 165                     }).build());
 166 
 167                     addSlot("infinite-pause", field);
 168                 }
 169                 return root;
 170 
 171             }
 172         };
 173         root.add(page,"Timeline");
 174         this.selectNode(page);
 175         return root;
 176     }
 177 }