components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/AnimatedBooleanProperty.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing;
       
    27 
       
    28 import java.awt.EventQueue;
       
    29 import org.jdesktop.animation.timing.*;
       
    30 import org.jdesktop.animation.timing.Animator.Direction;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class AnimatedBooleanProperty implements TimingTarget {
       
    34     //
       
    35     // Instance data
       
    36     //
       
    37 
       
    38     private boolean value;
       
    39     private Animator animator;
       
    40     private TimingTarget target;
       
    41 
       
    42     //
       
    43     // Constructors
       
    44     //
       
    45 
       
    46     public AnimatedBooleanProperty(int duration, TimingTarget target) {
       
    47 	this.target = target;
       
    48 	animator = new Animator(duration, this);
       
    49     }
       
    50 
       
    51     public AnimatedBooleanProperty(int duration) {
       
    52 	this(duration, null);
       
    53     }
       
    54 
       
    55     //
       
    56     // TimingTarget methods
       
    57     //
       
    58 
       
    59     @Override
       
    60     public void begin() {
       
    61 	if (target != null) {
       
    62 	    target.begin();
       
    63 	}
       
    64     }
       
    65 
       
    66     @Override
       
    67     public void end() {
       
    68 	if (target != null) {
       
    69 	    target.end();
       
    70 	}
       
    71     }
       
    72 
       
    73     @Override
       
    74     public void repeat() {
       
    75 	if (target != null) {
       
    76 	    target.repeat();
       
    77 	}
       
    78     }
       
    79 
       
    80     @Override
       
    81     public void timingEvent(float fraction) {
       
    82 	if (target != null) {
       
    83 	    target.timingEvent(fraction);
       
    84 	}
       
    85     }
       
    86 
       
    87     //
       
    88     // AnimatedBooleanProperty methods
       
    89     //
       
    90 
       
    91     public Animator getAnimator() {
       
    92 	return animator;
       
    93     }
       
    94 
       
    95     public boolean getValue() {
       
    96 	return value;
       
    97     }
       
    98 
       
    99     public void setValue(final boolean value, boolean animated) {
       
   100 	if (this.value != value) {
       
   101 	    if (animated) {
       
   102 		float start;
       
   103 		if (animator.isRunning()) {
       
   104 		    start = animator.getTimingFraction();
       
   105 		    animator.stop();
       
   106 		} else {
       
   107 		    start = value ? 0f : 1f;
       
   108 		}
       
   109 		animator.setStartFraction(start);
       
   110 
       
   111 		animator.setStartDirection(value ?
       
   112 		    Animator.Direction.FORWARD : Animator.Direction.BACKWARD);
       
   113 
       
   114 		this.value = value;
       
   115 
       
   116 		animator.start();
       
   117 	    } else {
       
   118 		animator.stop();
       
   119 		this.value = value;
       
   120 		EventQueue.invokeLater(
       
   121 		    new Runnable() {
       
   122 			@Override
       
   123 			public void run() {
       
   124 			    timingEvent(value ? 1f : 0f);
       
   125 			}
       
   126 		    });
       
   127 	    }
       
   128 	}
       
   129     }
       
   130 
       
   131     public void setValue(boolean value) {
       
   132 	setValue(value, true);
       
   133     }
       
   134 }