ShareComponent.java

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

import edu.ntnu.idi.idatt.model.portfolio.Share;
import edu.ntnu.idi.idatt.view.components.ui.UICompositor;
import edu.ntnu.idi.idatt.view.util.CssUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.List;
import java.util.function.Consumer;

/**
 * Share Component class.
 *
 * <p>
 * JavaFX Component for handeling repetative tasks.
 * Creates a visual representation of a standalone share with a sell button.
 * </p>
 * 
 * @see Share
 */
public class ShareComponent extends HBox {

  private final Share share;
  private Button sellButton;
  private HBox shareComponentUI;

  /**
   * Constructor for ShareComponent.
   * 
   * @param share - Share to create the component for.
   */
  public ShareComponent(Share share) {
    this.share = share;

    this.setMaxSize(Double.MAX_VALUE, 300);
    this.setPadding(new Insets(30));
    CssUtils.apply(this, CssUtils.GRAY);

    Label title = new Label(share.getStock().toString());
    Label quantity = new Label("Owned shares: " + share.getQuantity().toString());
    Label latestValueLabel = new Label("Net profit:");
    Label latestValue = new Label(String.format("%.2f $", share.getProfit()));
    Label latestValueChange = new Label(String.format("%.2f %%", share.getProfitPercent()));

    sellButton = new Button("Sell Share");

    List<Label> labels = List.of(title, quantity, latestValueLabel, latestValue, latestValueChange);
    labels.forEach(e -> e.getStyleClass().add("portfolio-box-text"));
    sellButton.getStyleClass().add("button");
    String color = CssUtils.generateValueColors(share.getProfit());
    CssUtils.apply(latestValue, color);
    CssUtils.apply(latestValueChange, color);

    UICompositor shareComponent = new UICompositor.Builder()
        .parent(new HBox())
        .growWithAlignment(Pos.CENTER)
        .addContent(title)
        .filler()
        .wrap(new VBox())
        .addAllContent(latestValueLabel, latestValue, latestValueChange)
        .unwrap()
        .filler()
        .addContent(quantity)
        .filler()
        .addContent(sellButton)
        .build();

    shareComponentUI = (HBox) shareComponent.makeUI();
    shareComponentUI.setCursor(Cursor.HAND);

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

  /**
   * Method for handeling sell button click.
   * 
   * @param handler - Functional interface with share reference.
   */
  public void onShareSellButton(Consumer<Share> handler) {
    sellButton.setOnAction((e) -> handler.accept(share));
  }

  /**
   * Method for handeling share component click.
   * 
   * @param handler - Functional interface with stock symbol reference.
   */
  public void onShareClick(Consumer<String> handler) {
    shareComponentUI.setOnMouseClicked(e -> handler.accept(share.getStock().getSymbol()));
  }

}