1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.kit.editor.panel.hierarchy;
  33 
  34 import javafx.animation.KeyFrame;
  35 import javafx.animation.KeyValue;
  36 import javafx.animation.Timeline;
  37 import javafx.scene.control.ScrollBar;
  38 import javafx.util.Duration;
  39 
  40 /**
  41  * Used to schedule :
  42  *
  43  * - auto scrolling animation when reaching the TOP/BOTTOM of the hierarchy panel
  44  *
  45  * p
  46  * @treatAsPrivate
  47  */
  48 public class HierarchyAnimationScheduler {
  49 
  50     private Timeline timeline;
  51     // Rate value used to set the timeline duration.
  52     // The bigger it is, the slower the animation will be.
  53     private final double rate = 4.0;
  54 
  55     public void playDecrementAnimation(final ScrollBar scrollBar) {
  56         assert scrollBar != null;
  57         final double minValue = scrollBar.getMin();
  58         assert isTimelineRunning() == false;
  59         // If the scroll bar is not yet at its min value,
  60         // we play the scroll bar decrement animation
  61         if (scrollBar.getValue() > minValue) {
  62             // The timeline duration value depends on :
  63             // - the scroll bar height
  64             // - the scroll bar thumb size (visibleAmount property)
  65             // - the scroll bar value
  66             final double scrollBarHeight = scrollBar.getHeight();
  67             final double scrollBarVisibleAmount = scrollBar.getVisibleAmount();
  68             final double scrollBarValue = scrollBar.getValue();
  69 
  70             // Height between the scroll bar top and the scroll bar thumb
  71             final double height = scrollBarHeight * scrollBarValue;
  72             final double duration = height * rate / scrollBarVisibleAmount; // duration in millis
  73 
  74             getTimeline().getKeyFrames().setAll(new KeyFrame(
  75                     new Duration(duration),
  76                     new KeyValue(scrollBar.valueProperty(), minValue)));
  77             getTimeline().play();
  78         }
  79     }
  80 
  81     public void playIncrementAnimation(final ScrollBar scrollBar) {
  82         assert scrollBar != null;
  83         final double maxValue = scrollBar.getMax();
  84         assert isTimelineRunning() == false;
  85         // If the scroll bar is not yet at its max value,
  86         // we play the scroll bar increment animation
  87         if (scrollBar.getValue() < maxValue) {
  88             // The timeline duration value depends on :
  89             // - the scroll bar height
  90             // - the scroll bar thumb size (visibleAmount property)
  91             // - the scroll bar value
  92             final double scrollBarHeight = scrollBar.getHeight();
  93             final double scrollBarVisibleAmount = scrollBar.getVisibleAmount();
  94             final double scrollBarValue = scrollBar.getValue();
  95 
  96             // Height between the scroll bar thumb and the scroll bar bottom
  97             final double height = scrollBarHeight * (scrollBar.getMax() - scrollBarValue);
  98             final double duration = height * rate / scrollBarVisibleAmount; // duration in millis
  99 
 100             getTimeline().getKeyFrames().setAll(new KeyFrame(
 101                     new Duration(duration),
 102                     new KeyValue(scrollBar.valueProperty(), maxValue)));
 103             getTimeline().play();
 104         }
 105     }
 106 
 107     public boolean isTimelineRunning() {
 108         return timeline == null ? false
 109                 : timeline.getStatus() == Timeline.Status.RUNNING;
 110     }
 111 
 112     public void stopTimeline() {
 113         if (timeline != null) {
 114             timeline.stop();
 115             timeline = null;
 116         }
 117     }
 118 
 119     private Timeline getTimeline() {
 120         if (timeline == null) {
 121             timeline = new Timeline();
 122         }
 123         return timeline;
 124     }
 125 }