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.Label;
  35 import javafx.scene.layout.HBox;
  36 import javafx.scene.layout.Pane;
  37 import javafx.scene.layout.StackPane;
  38 import javafx.scene.layout.TilePane;
  39 import javafx.scene.layout.VBox;
  40 import javafx.scene.paint.Color;
  41 import javafx.scene.paint.Paint;
  42 import javafx.scene.shape.Rectangle;
  43 import javafx.scene.text.Font;
  44 import javafx.scene.text.FontWeight;
  45 import javafx.scene.text.Text;
  46 import javafx.util.Duration;
  47 import test.javaclient.shared.BasicButtonChooserApp;
  48 import test.javaclient.shared.TestNode;
  49 
  50 /**
  51  *
  52  * @author Sergey Grinev
  53  */
  54 public class TimelineApp extends BasicButtonChooserApp {
  55     public TimelineApp() {
  56         super(600, 600, "Timeline",false);
  57     }
  58 
  59     public static void main(String[] args) {
  60         test.javaclient.shared.Utils.launch(TimelineApp.class, args);
  61     }
  62     public static final Paint[] colors = new Paint[]{Color.RED, Color.GREEN, Color.GRAY, Color.BLUE, Color.CYAN};
  63 
  64     private Timeline createTimelineAndPlayButton(final Pane field, final String id) {
  65         final Timeline timeline = new Timeline();
  66 
  67         final StackPane stack = new StackPane();
  68         final Rectangle rect = new Rectangle(0, 10, 30, 30);
  69         final Text text = new Text();
  70         text.setFont(Font.font("Arial", FontWeight.BOLD, 18));
  71         text.setFill(Color.WHITE);
  72 
  73         stack.getChildren().add(rect);
  74         stack.getChildren().add(text);
  75 
  76         for (int i = 0; i < 5; i++) {
  77             final int ii = i;
  78             Duration d = new Duration(300 * (i + 1));
  79             KeyFrame fr = new KeyFrame(d, new EventHandler() {
  80                 public void handle(Event t) {
  81                     rect.setId("rect_" + id + "_" + ii);
  82                     rect.setFill(colors[ii]);
  83                     text.setText(Integer.toString(ii));
  84                 }
  85             });
  86             timeline.getKeyFrames().add(fr);
  87             /*
  88              * timeline.getKeyFrames().add(new KeyFrame(d, new Runnable() {
  89              * public void run() { rect.setId("rect_" + id + "_" + ii);
  90              * rect.setFill(colors[ii]); text.setText(Integer.toString(ii)); }
  91              * }));
  92              */
  93         }
  94 
  95         Button tb = new Button("play" + id);
  96         tb.setOnAction(new EventHandler<ActionEvent>() {
  97             public void handle(ActionEvent t) {
  98                 timeline.play();
  99             }
 100         });
 101 
 102         field.getChildren().add(tb);
 103         field.getChildren().add(stack);
 104 
 105         return timeline;
 106     }
 107 
 108     @Override
 109     protected TestNode setup() {
 110         TestNode root = new TestNode();
 111         TestNode page = new TestNode(){
 112             HBox root = null;
 113             private void addSlot(String name,Pane field){
 114                 VBox slot = new VBox();
 115                 slot.getChildren().addAll(new Label(name),field);
 116                 root.getChildren().add(slot);
 117             }
 118             public Node drawNode() {
 119                 root = new HBox();
 120                 //plain timeline
 121                 {
 122                     final Pane field = new HBox(5);
 123                     Timeline timeline = createTimelineAndPlayButton(field, "1");
 124                     timeline.setAutoReverse(false);
 125                     timeline.setCycleCount(1);
 126 
 127                     addSlot("plain", field);
 128                 }
 129                 //autoreverse
 130                 {
 131                     final Pane field = new HBox(5);
 132                     Timeline timeline = createTimelineAndPlayButton(field, "2");
 133                     timeline.setAutoReverse(true);
 134                     timeline.setCycleCount(2);
 135 
 136                     addSlot("autoreverse", field);
 137                 }
 138                 //infinite + stop
 139                 {
 140                     final TilePane field = new TilePane(5, 5);
 141                     field.setPrefColumns(2);
 142                     final Timeline timeline = createTimelineAndPlayButton(field, "3");
 143                     timeline.setAutoReverse(false);
 144                     timeline.setCycleCount(Timeline.INDEFINITE);
 145                     Button temp1 = new Button("stop");
 146                     temp1.setOnAction(new EventHandler<ActionEvent>() {
 147                         public void handle(ActionEvent t) {
 148                             timeline.stop();
 149                         }
 150                     });
 151                     field.getChildren().add(temp1);
 152 
 153                     addSlot("infinite-stop", field);
 154                 }
 155                 //infinite + pause
 156                 {
 157                     final TilePane field = new TilePane(5, 5);
 158                     field.setPrefColumns(2);
 159                     final Timeline timeline = createTimelineAndPlayButton(field, "4");
 160                     timeline.setAutoReverse(false);
 161                     timeline.setCycleCount(Timeline.INDEFINITE);
 162                     Button temp2 = new Button("pause");
 163                     temp2.setOnAction(new EventHandler<ActionEvent>() {
 164                         public void handle(ActionEvent t) {
 165                             timeline.pause();
 166                         }
 167                     });
 168                     field.getChildren().add(temp2);
 169 
 170                     addSlot("infinite-pause", field);
 171                 }
 172                 return root;
 173 
 174             }
 175         };
 176         root.add(page,"Timeline");
 177         this.selectNode(page);
 178         return root;
 179     }
 180 }