Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.gwtbootstrap3.extras.animate.client.ui;
  
  /*
   * #%L
   * GwtBootstrap3
   * %%
   * Copyright (C) 2013 - 2014 GwtBootstrap3
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 
 
Utility class to dynamically animate objects using CSS animations.

Author(s):
Pavel Zlámal
 
 public class Animate {
 
     // store used styles, so they are not injected to the DOM everytime.
     private static final ArrayList<StringusedStyles = new ArrayList<String>();

    
Animate any element with specific animation. Animation is done by CSS and runs only once. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Type of animation to apply.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
 
     public static <T extends UIObjectString animate(final T widgetfinal Animation animation) {
         return animate(widgetanimation, 1, -1, -1);
     }

    
Animate any element with specific animation. Animation is done by CSS and runs multiple times. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Type of animation to apply.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
 
     public static <T extends UIObjectString animate(final T widgetfinal Animation animationfinal int count) {
         return animate(widgetanimationcount, -1, -1);
     }

    
Animate any element with specific animation. Animation is done by CSS and runs multiple times. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Type of animation to apply.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
duration Animation duration in ms. 0 disables animation, any negative value keeps default of original animation.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
 
     public static <T extends UIObjectString animate(final T widgetfinal Animation animationfinal int countfinal int duration) {
         return animate(widgetanimationcountduration, -1);
     }

    
Animate any element with specific animation. Animation is done by CSS and runs multiple times. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Type of animation to apply.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
duration Animation duration in ms. 0 disables animation, any negative value keeps default of original animation.
delay Delay before starting the animation loop in ms. Value <= 0 means no delay.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
    public static <T extends UIObjectString animate(final T widgetfinal Animation animationfinal int countfinal int durationfinal int delay) {
        if (widget != null && animation != null) {
            // on valid input
            if (widget.getStyleName().contains(animation.getCssName())) {
                // animation is present, remove it and run again.
                stopAnimation(widgetanimation.getCssName() + " " + getStyleNameFromAnimation(animation.getCssName(),count,duration,delay));
                Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
                    @Override
                    public boolean execute() {
                        styleElement(widget.getElement(), animation.getCssName(), countdurationdelay);
                        return false;
                    }
                }, 200);
                return animation.getCssName() + " " + getStyleNameFromAnimation(animation.getCssName(),count,duration,delay);
            } else {
                // animation was not present, run immediately
                return styleElement(widget.getElement(), animation.getCssName(), countdurationdelay);
            }
        } else {
            return null;
        }
    }

    
Animate any element with specific animation. Animation is done by CSS and runs only once. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Custom CSS class name used as animation.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
    public static <T extends UIObjectString animate(final T widgetfinal String animation) {
        return animate(widgetanimation, 1, -1, -1);
    }

    
Animate any element with specific animation. Animation is done by CSS and runs multiple times. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Custom CSS class name used as animation.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
    public static <T extends UIObjectString animate(final T widgetfinal String animationfinal int count) {
        return animate(widgetanimationcount, -1, -1);
    }

    
Animate any element with specific animation. Animation is done by CSS and runs multiple times. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Custom CSS class name used as animation.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
duration Animation duration in ms. 0 disables animation, any negative value keeps default of original animation.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
    public static <T extends UIObjectString animate(final T widgetfinal String animationfinal int countfinal int duration) {
        return animate(widgetanimationcountduration, -1);
    }

    
Animate any element with specific animation. Animation is done by CSS and runs multiple times. Animation is started when element is appended to the DOM or new (not same) animation is added to already displayed element. Animation runs on hidden elements too and is not paused/stopped when element is set as hidden.

Parameters:
widget Widget to apply animation to.
animation Custom CSS class name used as animation.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
duration Animation duration in ms. 0 disables animation, any negative value keeps default of original animation.
delay Delay before starting the animation loop in ms. Value <= 0 means no delay.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
    public static <T extends UIObjectString animate(final T widgetfinal String animationfinal int countfinal int durationfinal int delay) {
        if (widget != null && animation != null) {
            // on valid input
            if (widget.getStyleName().contains(animation)) {
                // animation is present, remove it and run again.
                stopAnimation(widgetanimation);
                Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
                    @Override
                    public boolean execute() {
                        styleElement(widget.getElement(), animationcountdurationdelay);
                        return false;
                    }
                }, 200);
                return animation + " " + getStyleNameFromAnimation(animation,count,duration,delay);
            } else {
                // animation was not present, run immediately
                return styleElement(widget.getElement(), animationcountdurationdelay);
            }
        } else {
            return null;
        }
    }

    
Styles element with animation class. New class name is generated to customize count, duration and delay. Style is removed on animation end (if not set to infinite).

Parameters:
element Element to apply animation to.
animation Type of animation to apply.
count Number of animation repeats. 0 disables animation, any negative value set repeats to infinite.
duration Animation duration in ms. 0 disables animation, any negative value keeps default of original animation.
delay Delay before starting the animation loop in ms. Value <= 0 means no delay.
<T> Any object extending UIObject class (typically Widget).
Returns:
Animation's CSS class name, which can be removed to stop animation.
    private static <T extends UIObjectString styleElement(Element elementString animationint countint durationint delay) {
        if (!.contains(animation + " " + getStyleNameFromAnimation(animation,count,duration,delay))) {
            String styleSheet = "." + getStyleNameFromAnimation(animationcountdurationdelay) + " {";
            // 1 is default, 0 disable animation, any negative -> infinite loop
            if (count >= 0) {
                styleSheet += "-webkit-animation-iteration-count: " + count + ";" +
                        "-moz-animation-iteration-count:" + count + ";" +
                        "-ms-animation-iteration-count:" + count + ";" +
                        "-o-animation-iteration-count:" + count + ";" +
                        "animation-iteration-count:" + count + ";";
            } else {
                styleSheet += "-webkit-animation-iteration-count: infinite;" +
                        "-moz-animation-iteration-count: infinite;" +
                        "-ms-animation-iteration-count: infinite;" +
                        "-o-animation-iteration-count: infinite;" +
                        "animation-iteration-count: infinite;";
            }
            // if not default (any negative -> use default)
            if (duration >= 0) {
                styleSheet += "-webkit-animation-duration: " + duration + "ms;" +
                        "-moz-animation-duration:" + duration + "ms;" +
                        "-ms-animation-duration:" + duration + "ms;" +
                        "-o-animation-duration:" + duration + "ms;" +
                        "animation-duration:" + duration + "ms;";
            }
            // if not default (any negative -> use default)
            if (delay >= 0) {
                styleSheet += "-webkit-animation-delay: " + delay + "ms;" +
                        "-moz-animation-delay:" + delay + "ms;" +
                        "-ms-animation-delay:" + delay + "ms;" +
                        "-o-animation-delay:" + delay + "ms;" +
                        "animation-delay:" + delay + "ms;";
            }
            styleSheet += "}";
            // inject new style
            StyleInjector.injectAtEnd(styleSheettrue);
            .add(animation + " " + getStyleNameFromAnimation(animationcountdurationdelay));
        }
        // start animation
        element.addClassName(animation + " " + getStyleNameFromAnimation(animation,count,duration,delay));
        // remove animation on end so we could start it again
        // removeAnimationOnEnd(element, animation + " anim-"+count+"-"+duration+"-"+delay);
        return animation + " " + getStyleNameFromAnimation(animation,count,duration,delay);
    }

    
Removes custom animation class on animation end.

Parameters:
widget Element to remove style from.
animation Animation CSS class to remove.
    public static final <T extends UIObjectvoid removeAnimationOnEnd(final T widgetfinal String animation) {
        if (widget != null && animation != null) {
            removeAnimationOnEnd(widget.getElement(), animation);
        }
    }

    
Removes custom animation class on animation end.

Parameters:
element Element to remove style from.
animation Animation CSS class to remove.
    private static final native void removeAnimationOnEnd(Element elementString animation/*-{
        var elem = $wnd.jQuery(element);
        elem.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', { elem: elem }, function(event) {
            event.data.elem.removeClass(animation);
        });
    }-*/;

    
Removes custom animation class and stops animation.

Parameters:
widget Element to remove style from.
animation Animation CSS class to remove.
    public static final <T extends UIObjectvoid stopAnimation(final T widgetfinal String animation){
        if (widget != null && animation != null) {
            stopAnimation(widget.getElement(), animation);
        }
    }

    
Removes custom animation class and stops animation.

Parameters:
element Element to remove style from.
animation Animation CSS class to remove.
    private static final native void stopAnimation(Element elementString animation/*-{
        $wnd.jQuery(element).removeClass(animation);
    }-*/;

    
Helper method, which returns unique class name for combination of animation and it's settings.

Parameters:
animation Animation CSS class name.
count Number of animation repeats.
duration Animation duration in ms.
delay Delay before starting the animation loop in ms.
Returns:
String representation of class name like "animation-count-duration-delay".
    private static String getStyleNameFromAnimation(final String animationint countint durationint delay) {
        // fix input
        if (count < 0) count = -1;
        if (duration < 0) duration = -1;
        if (delay < 0) delay = -1;
        String styleName = "";
        // for all valid animations
        if (animation != null && !animation.isEmpty() && animation.split(" ").length > 1) {
            styleName += animation.split(" ")[1]+"-"+count+"-"+duration+"-"+delay;
        // for all custom animations
        } else if (animation != null && !animation.isEmpty() && animation.split(" ").length == 1) {
            styleName += animation+"-"+count+"-"+duration+"-"+delay;
        }
        return styleName;
    }
New to GrepCode? Check out our FAQ X