<commit_before>package pt.up.fe.aiad.gui;

import jade.wrapper.AgentContainer;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pt.up.fe.aiad.scheduler.SchedulerAgent;
import pt.up.fe.aiad.utils.FXUtils;
import pt.up.fe.aiad.utils.IPAddressValidator;
import pt.up.fe.aiad.utils.StringUtils;

import java.util.Random;

public class MainController {

    private static Random r = new Random();

    public static AgentContainer container = null;

    @FXML
    private TextField _addressTextField;
    @FXML
    private TextField _nicknameTextField;
    @FXML
    private CheckBox _showGUICheckBox;
    @FXML
    private Button _startClientButton;
    @FXML
    private Button _startServerButton;
    @FXML
    private ChoiceBox<SchedulerAgent.Type> _algorithmChoiceBox;

    private static String _exampleNicknames[] = new String[] { "John", "Ann", "Tom", "Alicia", "Edward", "Sarah" };

    @FXML
    void initialize() {
        _nicknameTextField.textProperty().addListener((observable, oldValue, newValue) -> validateClientInput());
        _addressTextField.textProperty().addListener((observable, oldValue, newValue) -> validateClientInput());

        _addressTextField.setText("127.0.0.1:1099");
        _nicknameTextField.setText(getNewNickname());
        _algorithmChoiceBox.getItems().addAll(SchedulerAgent.Type.values());
        _algorithmChoiceBox.setValue(SchedulerAgent.Type.values()[0]);
    }

    private static String getNewNickname() {
        return _exampleNicknames[r.nextInt(_exampleNicknames.length)];
    }

    @FXML
    void startClientButtonOnAction(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("client.fxml"));
            Stage stage = new Stage();
            stage.setTitle(_nicknameTextField.getText() + " | iScheduler");
            Scene scene = new Scene(loader.load());
            scene.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
            stage.setScene(scene);

            String addressSplit[] = _addressTextField.getText().split(":");

            ClientController controller = loader.<ClientController>getController();
            controller.initData(addressSplit[0], Integer.parseInt(addressSplit[1]),
                    _nicknameTextField.getText(), _algorithmChoiceBox.getValue());
            controller.start();

            stage.show();

            stage.setOnHiding(eventHiding -> controller.stop());

            _nicknameTextField.setText(getNewNickname());
        } catch (Exception e) {
            FXUtils.showExceptionDialog(e);
        }
    }

    @FXML
    void startServerButtonOnAction(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("server.fxml"));
            Stage stage = new Stage();
            stage.setTitle("Server | iScheduler");
            Scene scene = new Scene(loader.load());
            scene.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
            stage.setScene(scene);

            ServerController controller = loader.<ServerController>getController();
            controller.initData(stage, _showGUICheckBox.isSelected());
            controller.start();

            stage.show();

            _startServerButton.setDisable(true);
            stage.setOnHiding(eventHiding -> _startServerButton.setDisable(false));
        } catch (Exception e) {
            FXUtils.showExceptionDialog(e);
        }
    }

    private void validateClientInput() {
        _startClientButton.setDisable(true);

        if (StringUtils.isNullOrEmpty(_addressTextField.getText()))
            return;
<commit_msg>Disable address field after starting server<commit_after>package pt.up.fe.aiad.gui;

import jade.wrapper.AgentContainer;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pt.up.fe.aiad.scheduler.SchedulerAgent;
import pt.up.fe.aiad.utils.FXUtils;
import pt.up.fe.aiad.utils.IPAddressValidator;
import pt.up.fe.aiad.utils.StringUtils;

import java.util.Random;

public class MainController {

    private static Random r = new Random();

    public static AgentContainer container = null;

    @FXML
    private TextField _addressTextField;
    @FXML
    private TextField _nicknameTextField;
    @FXML
    private CheckBox _showGUICheckBox;
    @FXML
    private Button _startClientButton;
    @FXML
    private Button _startServerButton;
    @FXML
    private ChoiceBox<SchedulerAgent.Type> _algorithmChoiceBox;

    private static String _exampleNicknames[] = new String[] { "John", "Ann", "Tom", "Alicia", "Edward", "Sarah" };

    @FXML
    void initialize() {
        _nicknameTextField.textProperty().addListener((observable, oldValue, newValue) -> validateClientInput());
        _addressTextField.textProperty().addListener((observable, oldValue, newValue) -> validateClientInput());

        _addressTextField.setText("127.0.0.1:1099");
        _nicknameTextField.setText(getNewNickname());
        _algorithmChoiceBox.getItems().addAll(SchedulerAgent.Type.values());
        _algorithmChoiceBox.setValue(SchedulerAgent.Type.values()[0]);
    }

    private static String getNewNickname() {
        return _exampleNicknames[r.nextInt(_exampleNicknames.length)];
    }

    @FXML
    void startClientButtonOnAction(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("client.fxml"));
            Stage stage = new Stage();
            stage.setTitle(_nicknameTextField.getText() + " | iScheduler");
            Scene scene = new Scene(loader.load());
            scene.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
            stage.setScene(scene);

            String addressSplit[] = _addressTextField.getText().split(":");

            ClientController controller = loader.<ClientController>getController();
            controller.initData(addressSplit[0], Integer.parseInt(addressSplit[1]),
                    _nicknameTextField.getText(), _algorithmChoiceBox.getValue());
            controller.start();

            stage.show();

            stage.setOnHiding(eventHiding -> controller.stop());

            _nicknameTextField.setText(getNewNickname());
        } catch (Exception e) {
            FXUtils.showExceptionDialog(e);
        }
    }

    @FXML
    void startServerButtonOnAction(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("server.fxml"));
            Stage stage = new Stage();
            stage.setTitle("Server | iScheduler");
            Scene scene = new Scene(loader.load());
            scene.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
            stage.setScene(scene);

            ServerController controller = loader.<ServerController>getController();
            controller.initData(stage, _showGUICheckBox.isSelected());
            controller.start();

            stage.show();

            _startServerButton.setDisable(true);
            _addressTextField.setDisable(true);
            stage.setOnHiding(eventHiding -> {
                _startServerButton.setDisable(false);
                _addressTextField.setDisable(false);
            });
        } catch (Exception e) {
            FXUtils.showExceptionDialog(e);
        }
    }

    private void validateClientInput() {
        _startClientButton.setDisable(true);

        if (StringUtils.isNullOrEmpty(_addressTextField.getText()))
            return;

{'fraction_non_alphanumeric': 0.11426073926073926, 'fraction_numerical': 0.003246753246753247, 'mean_word_length': 3.0696138211382116, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 2, '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': '4542915', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2171, 'n_words': 377}