IconComponent.java

package edu.ntnu.idi.idatt.view.components.elements;

import edu.ntnu.idi.idatt.view.components.primitives.ActionEventHandler;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;

/**
 * Icon Component class.
 *
 * <p>
 * JavaFX Component for handeling repetative tasks.
 * Creates a clickable icon with optional description.
 * </p>
 */
public class IconComponent extends StackPane {

  private Button icon;

  /**
   * Constructor for IconComponent
   * 
   * @param image       - Icon image
   * @param description - String describing icon button.
   * @param size        - width and height of the icon image.
   */
  public IconComponent(Image image, String description, int size) {

    ImageView iv = new ImageView();
    iv.setImage(image);
    iv.setFitHeight(size);
    iv.setFitWidth(size);

    icon = new Button(description, iv);
    icon.getStyleClass().clear(); // Remove parent buffers in case.
    icon.getStyleClass().add("icon");
    icon.setMaxHeight(Double.MAX_VALUE);

    this.getChildren().add(icon);
  }

  /**
   * Method for handeling icon click.
   * 
   * @param handler - Functional interface.
   */
  public void onIconClick(ActionEventHandler handler) {
    this.icon.setOnAction(e -> handler.handle());
  }

}