1 /*
   2  * Copyright (c) 2010, 2016, 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  * questions.
  24  */
  25 
  26 package javafx.animation;
  27 
  28 import javafx.beans.property.ObjectProperty;
  29 import javafx.beans.property.ObjectPropertyBase;
  30 import javafx.util.Duration;
  31 
  32 /**
  33  * This {@code Transition} executes an {@link Animation#onFinished} at the end of its
  34  * {@link #durationProperty() duration}.
  35  *
  36  * <p>
  37  * Code Segment Example:
  38  * </p>
  39  *
  40  * <pre>
  41  * <code>
  42  * import javafx.scene.shape.*;
  43  * import javafx.animation.*;
  44  *
  45  * ...
  46  *
  47  *     Rectangle rect = new Rectangle (100, 40, 100, 100);
  48  *     rect.setArcHeight(50);
  49  *     rect.setArcWidth(50);
  50  *     rect.setFill(Color.VIOLET);
  51  *
  52  *     RotateTransition rt = new RotateTransition(Duration.millis(3000), rect);
  53  *     rt.setByAngle(180);
  54  *     rt.setCycleCount(4f);
  55  *     rt.setAutoReverse(true);
  56  *     SequentialTransition seqTransition = new SequentialTransition (
  57  *         new PauseTransition(Duration.millis(1000)), // wait a second
  58  *         rt
  59  *     );
  60  *     seqTransition.play();
  61  *
  62  * ...
  63  *
  64  * </code>
  65  * </pre>
  66  *
  67  * @see Transition
  68  * @see Animation
  69  *
  70  * @since JavaFX 2.0
  71  */
  72 public final class PauseTransition extends Transition {
  73 
  74     /**
  75      * The duration of this {@code Transition}.
  76      * <p>
  77      * It is not possible to change the {@code duration} of a running
  78      * {@code PauseTransition}. If the value of {@code duration} is changed for a
  79      * running {@code PauseTransition}, the animation has to be stopped and started again to
  80      * pick up the new value.
  81      * <p>
  82      * Note: While the unit of {@code duration} is a millisecond, the
  83      * granularity depends on the underlying operating system and will in
  84      * general be larger. For example animations on desktop systems usually run
  85      * with a maximum of 60fps which gives a granularity of ~17 ms.
  86      *
  87      * Setting duration to value lower than {@link Duration#ZERO} will result
  88      * in {@link IllegalArgumentException}.
  89      *
  90      * @defaultValue 400ms
  91      */
  92     private ObjectProperty<Duration> duration;
  93     private static final Duration DEFAULT_DURATION = Duration.millis(400);
  94 
  95     public final void setDuration(Duration value) {
  96         if ((duration != null) || (!DEFAULT_DURATION.equals(value))) {
  97             durationProperty().set(value);
  98         }
  99     }
 100 
 101     public final Duration getDuration() {
 102         return (duration == null)? DEFAULT_DURATION : duration.get();
 103     }
 104 
 105     public final ObjectProperty<Duration> durationProperty() {
 106         if (duration == null) {
 107             duration = new ObjectPropertyBase<Duration>(DEFAULT_DURATION) {
 108 
 109                 @Override
 110                 public void invalidated() {
 111                     try {
 112                         setCycleDuration(getDuration());
 113                     } catch (IllegalArgumentException e) {
 114                         if (isBound()) {
 115                             unbind();
 116                         }
 117                         set(getCycleDuration());
 118                         throw e;
 119                     }
 120                 }
 121 
 122                 @Override
 123                 public Object getBean() {
 124                     return PauseTransition.this;
 125                 }
 126 
 127                 @Override
 128                 public String getName() {
 129                     return "duration";
 130                 }
 131             };
 132         }
 133         return duration;
 134     }
 135 
 136     /**
 137      * The constructor of {@code PauseTransition}.
 138      *
 139      * @param duration
 140      *            The duration of the {@code PauseTransition}
 141      */
 142     public PauseTransition(Duration duration) {
 143         setDuration(duration);
 144         setCycleDuration(duration);
 145     }
 146 
 147     /**
 148      * The constructor of {@code PauseTransition}
 149      */
 150     public PauseTransition() {
 151         this(DEFAULT_DURATION);
 152     }
 153 
 154     /**
 155      * {@inheritDoc}
 156      */
 157     @Override
 158     public void interpolate(double frac) {
 159         // no-op
 160     }
 161 }