CssUtils.java

package edu.ntnu.idi.idatt.view.util;

import java.math.BigDecimal;

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.util.Duration;

/**
 * Utility CSS class.
 *
 * <p>
 * Creates static access to widely CSS tools and
 * styles to simplify CSS appliance.
 * </p>
 */
public class CssUtils {

  public static final String BIG_TEXT_32 = "big-text-32";
  public static final String MED_TEXT_16 = "med-text-16";
  public static final String MED_TEXT_24 = "med-text-24";
  public static final String SMALL_TEXT_12 = "small-text-12";
  public static final String RED = "red";
  public static final String GREEN = "green";
  public static final String GRAY = "gray";

  /**
   * Method for applying a css styling.
   * 
   * @param parent   - Parent object to apply for.
   * @param cssClass - String of css class.
   */
  public static void apply(Parent parent, String cssClass) {
    parent.getStyleClass().add(cssClass);
  }

  /**
   * Method for setting one css styling.
   * 
   * @param parent   - Parent object to apply for.
   * @param cssClass - String of css class.
   */
  public static void set(Parent parent, String cssClass) {
    parent.getStyleClass().clear();
    parent.getStyleClass().add(cssClass);
  }

  /**
   * Method for generating colors based on value.
   *
   * <p>
   * Returns red or green if a value is below or over/equal zero correspondingly.
   * </p>
   * 
   * @param value - BigDecimal value.
   */
  public static String generateValueColors(BigDecimal value) {
    return value.compareTo(BigDecimal.ZERO) >= 0 ? GREEN : RED;
  }

  /**
   * Method for generating colors based on value.
   *
   * <p>
   * Returns red or green if a value is below or over/equal zero correspondingly.
   * </p>
   * 
   * @param value - double value.
   */
  public static String generateValueColors(double value) {
    return value >= 0 ? GREEN : RED;
  }

  /**
   * Method that applies glow effect of new news in newspaper.
   * 
   * @param node - The node to animate.
   */
  public static void applyNewspaperGlow(Node node) {
    DropShadow glow = new DropShadow(7, Color.WHITE);

    Timeline timeline = new Timeline(
        new KeyFrame(Duration.ZERO,
            new KeyValue(glow.radiusProperty(), 10)),
        new KeyFrame(Duration.seconds(0.5),
            new KeyValue(glow.radiusProperty(), 50)));

    timeline.setAutoReverse(true);
    timeline.setCycleCount(Animation.INDEFINITE);

    node.setEffect(glow);
    timeline.play();
  }

  /**
   * Method for creating a fade in animation.
   *
   * @param node - The node to animate
   *
   */
  public static void fadeInAnimation(Node node) {
    node.setOpacity(0.0);

    FadeTransition fadeIn = new FadeTransition(Duration.seconds(0.5), node);
    fadeIn.setFromValue(0.0);
    fadeIn.setToValue(1.0);
    fadeIn.setCycleCount(1);
    fadeIn.setAutoReverse(false);

    fadeIn.play();
  }

}