<commit_before>package moonlightowl.openblocks;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import moonlightowl.openblocks.io.JSON;
import moonlightowl.openblocks.structure.Block;
import moonlightowl.openblocks.structure.Joint;
import moonlightowl.openblocks.structure.Wire;
import moonlightowl.openblocks.ui.About;
import moonlightowl.openblocks.ui.ToolButton;
import moonlightowl.openblocks.ui.ToolPane;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.*;

/**
 * OpenBlocks.OpenBlocks
 * Created by MoonlightOwl on 10/25/15.
 * ===
 * Entry point and main window
 */

public class OpenBlocks extends Application {
    private Stage parentStage;
    // FXML links
    public AnchorPane rootPane;
    public MenuBar menuBar;
    public HBox toolBar;
    public ScrollPane scroller;
    // Custom elements
    private ToolPane[] tools;
    private About about;
    private Workspace workspace;

    private boolean selectedTrash = false;
    private Blocks.Id selected;
    private ImageView selectedIcon;
    private Wire wire;

    private File projectFile;

    @Override
    public void start(Stage primaryStage) throws Exception {
        parentStage = primaryStage;

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
        loader.setController(this);
        loader.load();

        // Resources
        Assets.load();

        // Generate GUI
        initUI();

        // Create IDE window
        setTitle(Settings.UNTITLED);
        primaryStage.setScene(new Scene(rootPane, Settings.WIDTH, Settings.HEIGHT));
        primaryStage.setMinWidth(500);
        primaryStage.setMinHeight(400);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

    public void setTitle(String title){
        if(title != null)
            parentStage.setTitle(title + " - OpenBlocks " + Settings.VERSION);
        else
            parentStage.setTitle("OpenBlocks " + Settings.VERSION);
    }

    public void initUI(){
        about = new About(parentStage);
        workspace = new Workspace(scroller);

        selectedIcon = new ImageView();
        selectedIcon.setScaleX(0.3);
        selectedIcon.setScaleY(0.3);
        rootPane.getChildren().add(selectedIcon);

        initToolsPanels();
        initToolBar();

        /** Event listenters */
        // Wire operations
        Joint.setOnClickListenter(event ->{
            Joint joint = (Joint)event.getSource();
            if(event.getButton() == MouseButton.PRIMARY) {
                // Replace existing one
                if(joint.isAttached()){
                    Wire old = joint.getWire();
                    joint.attachWire(wire);
                    wire = old;
                    // Or create / attach new wire
                } else {
                    if (wire == null) {
                        wire = new Wire();
                        joint.attachWire(wire);
                        wire.reposition(joint.getAbsX(), joint.getAbsY());
                        workspace.addWire(wire);
                    } else {
                        joint.attachWire(wire);
                        wire = null;
                    }
                }
            }
        });
        // Block removement
        Block.setOnClickListenter(event -> {
            if (selectedTrash) {
                Block block = (Block)event.getSource();
                workspace.removeBlock(block);
            }
        });
        // Wire removement
        Wire.setOnClickListenter(event -> {
            if (selectedTrash) {
                Wire wire = (Wire)event.getSource();
                workspace.removeWire(wire);
            }
        });

        // Add new objects to workspace
        rootPane.setOnMouseClicked(event -> {
            if(hasOpenedPanes())
                closeAllToolPanes();
            else
            if(event.getButton() == MouseButton.PRIMARY) {
                if(selected != null) {
                    Block block = selected.getInstance()
                            .setPosition(workspace.projectX(event.getX()),
                                    workspace.projectY(event.getY()));
                    workspace.addBlock(block);
                }
            } else if(event.getButton() == MouseButton.SECONDARY) {
                // Deselect current block type
                deselect();
                // Remove current uncomplete wire
                if(wire != null){
                    workspace.removeWire(wire);
                    wire = null;
                }
            }
        });

        // Move mouse tool icon & current wire loose end (if any)
        rootPane.setOnMouseMoved(event -> {
            selectedIcon.setTranslateX(event.getSceneX());
            selectedIcon.setTranslateY(event.getSceneY());
            if(wire != null) wire.reposition(workspace.projectX(event.getX()),
                    workspace.projectY(event.getY()));
        });
        rootPane.addEventFilter(MouseEvent.MOUSE_DRAGGED, rootPane.getOnMouseMoved());

        // Keyboard tool selector
        rootPane.setOnKeyPressed(event -> {
            if(event.getCode() == KeyCode.DIGIT1) toggleToolPane(0);
            if(event.getCode() == KeyCode.DIGIT2) toggleToolPane(1);
            if(event.getCode() == KeyCode.DIGIT3) toggleToolPane(2);
            if(event.getCode() == KeyCode.DIGIT4) toggleToolPane(3);
        });
    }

    /** UI generation */<commit_msg>Project changes indicator<commit_after>package moonlightowl.openblocks;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import moonlightowl.openblocks.io.JSON;
import moonlightowl.openblocks.structure.Block;
import moonlightowl.openblocks.structure.Joint;
import moonlightowl.openblocks.structure.Wire;
import moonlightowl.openblocks.ui.About;
import moonlightowl.openblocks.ui.ToolButton;
import moonlightowl.openblocks.ui.ToolPane;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.*;

/**
 * OpenBlocks.OpenBlocks
 * Created by MoonlightOwl on 10/25/15.
 * ===
 * Entry point and main window
 */

public class OpenBlocks extends Application {
    private Stage parentStage;
    // FXML links
    public AnchorPane rootPane;
    public MenuBar menuBar;
    public HBox toolBar;
    public ScrollPane scroller;
    // Custom elements
    private ToolPane[] tools;
    private About about;
    private Workspace workspace;

    private boolean selectedTrash = false;
    private Blocks.Id selected;
    private ImageView selectedIcon;
    private Wire wire;

    private File projectFile;
    private boolean changed = false;

    @Override
    public void start(Stage primaryStage) throws Exception {
        parentStage = primaryStage;

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
        loader.setController(this);
        loader.load();

        // Resources
        Assets.load();

        // Generate GUI
        initUI();

        // Create IDE window
        setTitle(Settings.UNTITLED);
        primaryStage.setScene(new Scene(rootPane, Settings.WIDTH, Settings.HEIGHT));
        primaryStage.setMinWidth(500);
        primaryStage.setMinHeight(400);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

    public void setTitle(String title){
        if(title != null)
            parentStage.setTitle(title + " - OpenBlocks " + Settings.VERSION);
        else
            parentStage.setTitle("OpenBlocks " + Settings.VERSION);
        changed = false;
    }
    public void projectChanged() {
        if(!changed){
            parentStage.setTitle("*"+parentStage.getTitle());
            changed = true;
        }
    }

    public void initUI(){
        about = new About(parentStage);
        workspace = new Workspace(scroller);

        selectedIcon = new ImageView();
        selectedIcon.setScaleX(0.3);
        selectedIcon.setScaleY(0.3);
        rootPane.getChildren().add(selectedIcon);

        initToolsPanels();
        initToolBar();

        /** Event listenters */
        // Wire operations
        Joint.setOnClickListenter(event ->{
            Joint joint = (Joint)event.getSource();
            if(event.getButton() == MouseButton.PRIMARY) {
                // Replace existing one
                if(joint.isAttached()){
                    Wire old = joint.getWire();
                    joint.attachWire(wire);
                    wire = old;
                    // Or create / attach new wire
                } else {
                    if (wire == null) {
                        wire = new Wire();
                        joint.attachWire(wire);
                        wire.reposition(joint.getAbsX(), joint.getAbsY());
                        workspace.addWire(wire);
                    } else {
                        joint.attachWire(wire);
                        wire = null;
                    }
                }
                projectChanged();
            }
        });
        // Block removement
        Block.setOnClickListenter(event -> {
            if (selectedTrash) {
                Block block = (Block)event.getSource();
                workspace.removeBlock(block);
                projectChanged();
            }
        });
        // Wire removement
        Wire.setOnClickListenter(event -> {
            if (selectedTrash) {
                Wire wire = (Wire)event.getSource();
                workspace.removeWire(wire);
                projectChanged();
            }
        });

        // Add new objects to workspace
        rootPane.setOnMouseClicked(event -> {
            if(hasOpenedPanes())
                closeAllToolPanes();
            else
            if(event.getButton() == MouseButton.PRIMARY) {
                if(selected != null) {
                    Block block = selected.getInstance()
                            .setPosition(workspace.projectX(event.getX()),
                                    workspace.projectY(event.getY()));
                    workspace.addBlock(block);
                    projectChanged();
                }
            } else if(event.getButton() == MouseButton.SECONDARY) {
                // Deselect current block type
                deselect();
                // Remove current uncomplete wire
                if(wire != null){
                    workspace.removeWire(wire);
                    wire = null;
                }
            }
        });

        // Move mouse tool icon & current wire loose end (if any)
        rootPane.setOnMouseMoved(event -> {
            selectedIcon.setTranslateX(event.getSceneX());
            selectedIcon.setTranslateY(event.getSceneY());
            if(wire != null) wire.reposition(workspace.projectX(event.getX()),
                    workspace.projectY(event.getY()));
        });
        rootPane.addEventFilter(MouseEvent.MOUSE_DRAGGED, rootPane.getOnMouseMoved());

        // Keyboard tool selector
        rootPane.setOnKeyPressed(event -> {
            if(event.getCode() == KeyCode.DIGIT1) toggleToolPane(0);
            if(event.getCode() == KeyCode.DIGIT2) toggleToolPane(1);
            if(event.getCode() == KeyCode.DIGIT3) toggleToolPane(2);
            if(event.getCode() == KeyCode.DIGIT4) toggleToolPane(3);
        });
    }

    /** UI generation */
{'fraction_non_alphanumeric': 0.10489230522860117, 'fraction_numerical': 0.003843382176315157, 'mean_word_length': 1.9182242990654206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '242448', 'n_tokens_mistral': 3405, 'n_tokens_neox': 3236, 'n_words': 645}