All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jfoenix.animation.JFXAnimationManager Maven / Gradle / Ivy

There is a newer version: 9.0.10
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

package com.jfoenix.animation;

import java.util.HashMap;

import javafx.animation.Animation;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.util.Pair;

public class JFXAnimationManager {

    private static HashMap, Animation> nodesMap = new HashMap<>();
    private static HashMap nodesStatesMap = new HashMap<>();

    public static StringProperty registerNodeAnimation(Node node, String state, Animation animation) {
        if (animation == null || state == null || node == null || state.isEmpty()) {
            return null;
        }
        nodesMap.put(new Pair(node, state.toLowerCase()), animation);
        StringProperty stateProperty;
        if (!nodesStatesMap.containsKey(node)) {
            stateProperty = new SimpleStringProperty();
            stateProperty.addListener((o, oldVal, newVal) -> {
                Animation oldAnimation = nodesMap.get(new Pair(node, oldVal == null ? "" : oldVal.toLowerCase()));
                Animation newAnimation = nodesMap.get(new Pair(node, newVal == null ? "" : newVal.toLowerCase()));
                if (oldAnimation != null) {
                    oldAnimation.stop();
                }
                if (newAnimation != null) {
                    newAnimation.play();
                }
            });
            nodesStatesMap.put(node, stateProperty);
        } else {
            stateProperty = nodesStatesMap.get(node);
        }
        return stateProperty;
    }

    public static void triggerAnimation(Node node, String state) {
        if (nodesStatesMap.containsKey(node)) {
            nodesStatesMap.get(node).set(state);
        }
    }

    public static String getLastNodeAnimation(Node node) {
        return nodesStatesMap.get(node) != null ? nodesStatesMap.get(node).get() : null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy