<commit_before>package SW9.presentations;

import SW9.abstractions.Component;
import SW9.controllers.ComponentController;
import SW9.utility.colors.Color;
import SW9.utility.helpers.DragHelper;
import SW9.utility.helpers.MouseTrackable;
import SW9.utility.mouse.MouseTracker;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.layout.*;
import javafx.scene.shape.Path;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import org.kordamp.ikonli.javafx.FontIcon;

import java.io.IOException;
import java.net.URL;
import java.util.Random;

public class ComponentPresentation extends StackPane implements MouseTrackable {

    private final static double CORNER_SIZE = 60;

    private final ComponentController controller;
    private LocationPresentation initialLocationPresentation = null;
    private LocationPresentation finalLocationPresentation = null;

    public ComponentPresentation() {
        this(new Component("Component" + new Random().nextInt(5000))); // todo: find a new unique component name
    }

    public ComponentPresentation(final Component component) {
        final URL location = this.getClass().getResource("ComponentPresentation.fxml");

        final FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

        try {
            fxmlLoader.setRoot(this);
            fxmlLoader.load(location.openStream());

            // Set the width and the height of the view to the values in the abstraction
            setMinWidth(component.getWidth());
            setMaxWidth(component.getWidth());
            setMinHeight(component.getHeight());
            setMaxHeight(component.getHeight());

            controller = fxmlLoader.getController();
            controller.setComponent(component);

            initializeDefaultLocationsContainer();
            initializeToolbar();
            initializeFrame();
            initializeInitialLocation();
            initializeFinalLocation();
            initializeBackground();
            initializeName();

            // Make the component draggable
            DragHelper.makeDraggable(this, event -> controller.getComponent().getUnfinishedEdge() == null);
        } catch (final IOException ioe) {
            throw new IllegalStateException(ioe);
        }
    }

    private void initializeDefaultLocationsContainer() {
        if (initialLocationPresentation != null) {
            controller.defaultLocationsContainer.getChildren().remove(initialLocationPresentation);
        }

        if (finalLocationPresentation != null) {
            controller.defaultLocationsContainer.getChildren().remove(finalLocationPresentation);
        }

        // Instantiate views for the initial and final location
        final Component component = controller.getComponent();
        initialLocationPresentation = new LocationPresentation(component.getInitialLocation(), component);
        finalLocationPresentation = new LocationPresentation(component.getFinalLocation(), component);

        // Add the locations to the view
        controller.defaultLocationsContainer.getChildren().addAll(initialLocationPresentation, finalLocationPresentation);

        ComponentPresentation.this.controller.frame.setOnMouseEntered(event -> {
            new Thread(() -> {
                Platform.runLater(initialLocationPresentation::animateIn);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // do nothing
                }

                Platform.runLater(finalLocationPresentation::animateIn);
            }).start();
        });

        finalLocationPresentation.shakeAnimation();
    }

    private void initializeName() {
        final Component component = controller.getComponent();
        final BooleanProperty initialized = new SimpleBooleanProperty(false);

        controller.name.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue && !initialized.get()) {
                controller.root.requestFocus();
                initialized.setValue(true);<commit_msg>Components can now only be moved using left mouse button<commit_after>package SW9.presentations;

import SW9.abstractions.Component;
import SW9.controllers.ComponentController;
import SW9.utility.colors.Color;
import SW9.utility.helpers.DragHelper;
import SW9.utility.helpers.MouseTrackable;
import SW9.utility.mouse.MouseTracker;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import javafx.scene.shape.Path;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import org.kordamp.ikonli.javafx.FontIcon;

import java.io.IOException;
import java.net.URL;
import java.util.Random;

public class ComponentPresentation extends StackPane implements MouseTrackable {

    private final static double CORNER_SIZE = 60;

    private final ComponentController controller;
    private LocationPresentation initialLocationPresentation = null;
    private LocationPresentation finalLocationPresentation = null;

    public ComponentPresentation() {
        this(new Component("Component" + new Random().nextInt(5000))); // todo: find a new unique component name
    }

    public ComponentPresentation(final Component component) {
        final URL location = this.getClass().getResource("ComponentPresentation.fxml");

        final FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

        try {
            fxmlLoader.setRoot(this);
            fxmlLoader.load(location.openStream());

            // Set the width and the height of the view to the values in the abstraction
            setMinWidth(component.getWidth());
            setMaxWidth(component.getWidth());
            setMinHeight(component.getHeight());
            setMaxHeight(component.getHeight());

            controller = fxmlLoader.getController();
            controller.setComponent(component);

            initializeDefaultLocationsContainer();
            initializeToolbar();
            initializeFrame();
            initializeInitialLocation();
            initializeFinalLocation();
            initializeBackground();
            initializeName();

            // Make the component draggable
            DragHelper.makeDraggable(this, event -> {
                // We can only drag using left click
                if (!event.getButton().equals(MouseButton.PRIMARY)) return false;

                // We cant drag if we have an unfinished edge
                if (controller.getComponent().getUnfinishedEdge() != null) return false;

                return true;
            });
        } catch (final IOException ioe) {
            throw new IllegalStateException(ioe);
        }
    }

    private void initializeDefaultLocationsContainer() {
        if (initialLocationPresentation != null) {
            controller.defaultLocationsContainer.getChildren().remove(initialLocationPresentation);
        }

        if (finalLocationPresentation != null) {
            controller.defaultLocationsContainer.getChildren().remove(finalLocationPresentation);
        }

        // Instantiate views for the initial and final location
        final Component component = controller.getComponent();
        initialLocationPresentation = new LocationPresentation(component.getInitialLocation(), component);
        finalLocationPresentation = new LocationPresentation(component.getFinalLocation(), component);

        // Add the locations to the view
        controller.defaultLocationsContainer.getChildren().addAll(initialLocationPresentation, finalLocationPresentation);

        ComponentPresentation.this.controller.frame.setOnMouseEntered(event -> new Thread(() -> {
            Platform.runLater(initialLocationPresentation::animateIn);

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // do nothing
            }

            Platform.runLater(finalLocationPresentation::animateIn);
        }).start());

        finalLocationPresentation.shakeAnimation();
    }

    private void initializeName() {
        final Component component = controller.getComponent();
        final BooleanProperty initialized = new SimpleBooleanProperty(false);

        controller.name.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue && !initialized.get()) {
                controller.root.requestFocus();
                initialized.setValue(true);
{'fraction_non_alphanumeric': 0.08821016659547203, 'fraction_numerical': 0.0034173430158052115, 'mean_word_length': 3.09130624726955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4937511', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2195, 'n_words': 483}