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

com.jfoenix.transitions.template.JFXAnimationTemplate 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.transitions.template;

import javafx.animation.Timeline;
import javafx.scene.Node;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Class which represents a general {@link JFXAnimationTemplate}. 
* This class is responsible for providing methods where it's possible to build a CSS like * structured animation. * * @author Marcel Schlegel (schlegel11) * @version 1.0 * @since 2018-09-18 */ public class JFXAnimationTemplate implements JFXTemplateConfig, JFXTemplateBuilder { private static final String NO_KEY_FOUND_MESSAGE = "No animation objects with key \"%s\" found.\n" + "Please check your build method where the " + "namedAnimationObjects are defined or your " + "withAnimationObject methods where the keys are accessed."; private final Set percents = new HashSet<>(); private final Map< Double, List< Function< JFXAnimationTemplateAction.InitBuilder, JFXAnimationTemplateAction.Builder>>> creatorValueBuilderFunctions = new HashMap<>(); private final Class animationObjectType; private Map> animationObjects; private Function creatorConfigBuilderFunction; private boolean clearPercents; private JFXAnimationTemplate(Class animationObjectType) { this.animationObjectType = animationObjectType; } /** * Create a {@link JFXTemplateProcess} with a specific default animation type.
* The default animation objects with this type are set later in the {@link * JFXTemplateBuilder#build(Object)} method.
* These objects are generally used in {@link JFXTemplateAction#action(Function)} methods. * * @param animationObjectType a specific animation object type. * @param the specific type. * @return a {@link JFXTemplateProcess} instance. */ public static JFXTemplateProcess create(Class animationObjectType) { return new JFXAnimationTemplate<>(animationObjectType); } /** * Same method as {@link #create(Class)} but with the default type {@link Node}. This type is a * general default type in a {@link JFXAnimationTemplate}. * * @see #create(Class) * @return a {@link JFXTemplateProcess} instance. */ public static JFXTemplateProcess create() { return create(Node.class); } public Map>> buildAndGetAnimationValues() { Map>> animationValueMap = new HashMap<>(); creatorValueBuilderFunctions.forEach( (percent, animationValueBuilderFunctions) -> { List> animationValues = animationValueBuilderFunctions .stream() .flatMap( builderFunction -> builderFunction .apply(JFXAnimationTemplateAction.builder(animationObjectType)) .buildActions(this::getAnimationObjectsWithKeys)) .collect(Collectors.toList()); animationValueMap.put(percent, animationValues); }); return animationValueMap; } private List getAnimationObjectsWithKeys(Collection keys) { List animationObjectList = new ArrayList<>(); for (String key : keys) { Collection animationObjectsPerKey = animationObjects.get(key); if (animationObjectsPerKey == null) { throw new NoSuchElementException(String.format(NO_KEY_FOUND_MESSAGE, key)); } animationObjectList.addAll(animationObjectsPerKey); } return animationObjectList; } public JFXAnimationTemplateConfig buildAndGetTemplateConfig() { return creatorConfigBuilderFunction.apply(JFXAnimationTemplateConfig.builder()).build(); } @Override public JFXTemplateAction percent(double first, double... rest) { if (clearPercents) { percents.clear(); clearPercents = false; } // Clamp value between 0 and 100. percents.add(Math.max(0, Math.min(100, first))); creatorValueBuilderFunctions.put(first, new ArrayList<>()); for (double percent : rest) { percents.add(Math.max(0, Math.min(100, percent))); creatorValueBuilderFunctions.put(percent, new ArrayList<>()); } return this; } @Override public JFXTemplateAction from() { return percent(0); } @Override public JFXTemplateAction to() { return percent(100); } @Override public JFXTemplateConfig action( Function, JFXAnimationTemplateAction.Builder> valueBuilderFunction) { for (Double percent : percents) { creatorValueBuilderFunctions.get(percent).add(valueBuilderFunction); } clearPercents = true; return this; } @Override public JFXTemplateConfig action( JFXAnimationTemplateAction.Builder animationValueBuilder) { return action(builder -> animationValueBuilder); } @Override public JFXTemplateBuilder config( Function configBuilderFunction) { creatorConfigBuilderFunction = configBuilderFunction; return this; } @Override public JFXTemplateBuilder config(JFXAnimationTemplateConfig.Builder configBuilder) { return config(builder -> configBuilder); } @Override public B build( Function, B> builderFunction, Function, JFXAnimationObjectMapBuilder> mapBuilderFunction) { animationObjects = mapBuilderFunction.apply(JFXAnimationObjectMapBuilder.builder()).getAnimationObjects(); // Provide a null value as default animation object if it's absent. animationObjects.putIfAbsent( JFXAnimationObjectMapBuilder.DEFAULT_ANIMATION_OBJECT_NAME, Collections.singletonList(null)); return builderFunction.apply(this); } @Override public B build( Function, B> builderFunction, N defaultAnimationObject) { return build(builderFunction, b -> b.defaultObject(defaultAnimationObject)); } @Override public Timeline build( Function, JFXAnimationObjectMapBuilder> mapBuilderFunction) { return build(JFXAnimationTemplates::buildTimeline, mapBuilderFunction); } @Override public Timeline build(N defaultAnimationObject) { return build(b -> b.defaultObject(defaultAnimationObject)); } @Override public Timeline build() { return build(Function.identity()); } }