dsfixgui.view.DSFUnsafeSettingsPane.java Source code

Java tutorial

Introduction

Here is the source code for dsfixgui.view.DSFUnsafeSettingsPane.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dsfixgui.view;

import static dsfixgui.DSFixGUIConstants.APPLY_SETTINGS;
import static dsfixgui.DSFixGUIConstants.DIALOG_BUTTON_TEXTS;
import static dsfixgui.DSFixGUIConstants.DIALOG_MSG_RESTORE_SETTINGS;
import static dsfixgui.DSFixGUIConstants.DIALOG_TITLE_RESET;
import static dsfixgui.DSFixGUIConstants.DISABLE_ENABLE;
import static dsfixgui.DSFixGUIConstants.FORCE_WINDOW_MODE_LABEL;
import static dsfixgui.DSFixGUIConstants.FULLSCREEN_HZ_TT;
import static dsfixgui.DSFixGUIConstants.INPUT_GREATER_THAN_EQ;
import static dsfixgui.DSFixGUIConstants.INPUT_TOO_LARGE;
import static dsfixgui.DSFixGUIConstants.INVALID_INPUT;
import static dsfixgui.DSFixGUIConstants.REFRESH_RATE_LABEL;
import static dsfixgui.DSFixGUIConstants.RESTORE_DEFAULTS;
import static dsfixgui.DSFixGUIConstants.SETTINGS;
import static dsfixgui.DSFixGUIConstants.UNSAFE_OPS;
import static dsfixgui.DSFixGUIConstants.UNSAFE_TT;
import static dsfixgui.DSFixGUIConstants.VSYNC_LABEL;
import static dsfixgui.DSFixGUIConstants.VSYNC_TT;
import static dsfixgui.DSFixGUIConstants.WINDOW_MODES;
import dsfixgui.configs.DSFConfiguration;
import dsfixgui.dialog.ContinueDialog;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.math.NumberUtils;

/**
 *
 * @author SeanTheLawn
 */
class DSFUnsafeSettingsPane extends ScrollPane {

    //Main UI Components
    GridPane primaryPane;
    ColumnConstraints spacerColumn;
    ColumnConstraints primaryColumn;
    VBox primaryVBox;
    HBox titleBar;
    Label titleLabel;
    HBox restoreDefaultsBar;
    public Button applySettingsButton;
    Button restoreDefaultsButton;
    HBox spacerHBox;
    HBox bottomSpacerHBox;

    //Settings UI Components
    //
    FlowPane windowModePane;
    Label windowModeLabel;
    ToggleGroup windowModeChoice;
    RadioButton neitherWindowMode;
    RadioButton forceWindowed;
    RadioButton forceFullscreen;
    //
    FlowPane vsyncPane;
    Label vsyncLabel;
    ComboBox<String> vsyncPicker;
    //
    FlowPane refreshRatePane;
    Label refreshRateLabel;
    TextField refreshRateField;
    //

    //Instance Variables
    DSFixInterface ui;
    DSFConfiguration config;

    public DSFUnsafeSettingsPane(DSFixInterface initUI) {
        ui = initUI;
        config = ui.getConfig();
        initialize();
    }

    //Build the pane
    private void initialize() {

        //Basic layout
        this.setFitToWidth(true);

        spacerColumn = new ColumnConstraints();
        spacerColumn.setFillWidth(true);
        spacerColumn.setPercentWidth(3.0);
        primaryColumn = new ColumnConstraints();
        primaryColumn.setFillWidth(true);
        primaryColumn.setPercentWidth(95.0);
        primaryPane = new GridPane();
        primaryPane.getColumnConstraints().addAll(spacerColumn, primaryColumn);
        primaryVBox = new VBox();
        primaryVBox.getStyleClass().add("spacing_15");
        primaryPane.add(primaryVBox, 1, 0);
        titleLabel = new Label(UNSAFE_OPS.toUpperCase() + " " + SETTINGS.toUpperCase());
        titleLabel.getStyleClass().addAll("settings_title", "red_text");
        titleLabel.setTooltip(new Tooltip(UNSAFE_TT));
        titleBar = new HBox();
        titleBar.setAlignment(Pos.CENTER);
        titleBar.getChildren().add(titleLabel);
        restoreDefaultsBar = new HBox();
        restoreDefaultsBar.setAlignment(Pos.CENTER);
        restoreDefaultsBar.setSpacing(5.0);
        applySettingsButton = new Button(APPLY_SETTINGS);
        restoreDefaultsButton = new Button(RESTORE_DEFAULTS);
        applySettingsButton.getStyleClass().add("translate_y_4");
        restoreDefaultsButton.getStyleClass().add("translate_y_4");
        restoreDefaultsBar.getChildren().addAll(applySettingsButton, restoreDefaultsButton);
        spacerHBox = new HBox();
        spacerHBox.setMinHeight(10.0);
        bottomSpacerHBox = new HBox();
        bottomSpacerHBox.setMinHeight(10.0);

        /////////////////////SETTINGS PANES/////////////////////
        //
        //
        //Force Window Modes
        windowModePane = new FlowPane();
        windowModePane.getStyleClass().add("settings_pane");
        windowModeLabel = new Label(FORCE_WINDOW_MODE_LABEL + "  ");
        windowModeLabel.getStyleClass().addAll("bold_text", "font_12_pt");
        windowModeChoice = new ToggleGroup();
        neitherWindowMode = new RadioButton(WINDOW_MODES[0] + "   ");
        neitherWindowMode.setToggleGroup(windowModeChoice);
        forceWindowed = new RadioButton(WINDOW_MODES[1]);
        forceWindowed.setToggleGroup(windowModeChoice);
        forceFullscreen = new RadioButton(WINDOW_MODES[2]);
        forceFullscreen.setToggleGroup(windowModeChoice);
        if (config.forceWindowed.get() == 0 && config.forceFullscreen.get() == 0) {
            neitherWindowMode.setSelected(true);
        } else if (config.forceWindowed.get() == 1) {
            forceWindowed.setSelected(true);
            config.forceFullscreen.set(0);
        } else {
            forceFullscreen.setSelected(true);
        }
        windowModePane.getChildren().addAll(windowModeLabel, neitherWindowMode, forceWindowed, forceFullscreen);
        //
        //Toggle Vsync
        vsyncPane = new FlowPane();
        vsyncPane.getStyleClass().add("settings_pane");
        vsyncLabel = new Label(VSYNC_LABEL + "  ");
        vsyncLabel.getStyleClass().addAll("bold_text", "font_12_pt");
        vsyncLabel.setTooltip(new Tooltip(VSYNC_TT));
        vsyncPicker = new ComboBox(FXCollections.observableArrayList(DISABLE_ENABLE));
        if (config.enableVsync.get() == 0) {
            vsyncPicker.setValue(vsyncPicker.getItems().get(0));
        } else {
            vsyncPicker.setValue(vsyncPicker.getItems().get(1));
        }
        vsyncPane.getChildren().addAll(vsyncLabel, vsyncPicker);
        //
        //Fullscreen Refresh Rate
        refreshRatePane = new FlowPane();
        refreshRatePane.getStyleClass().add("settings_pane");
        refreshRateLabel = new Label(REFRESH_RATE_LABEL + " ");
        refreshRateLabel.getStyleClass().addAll("bold_text", "font_12_pt");
        refreshRateLabel.setTooltip(new Tooltip(FULLSCREEN_HZ_TT));
        refreshRateField = new TextField("" + config.fullscreenHz.get());
        refreshRateField.getStyleClass().add("settings_text_field");
        refreshRatePane.getChildren().addAll(refreshRateLabel, refreshRateField);
        //

        primaryVBox.getChildren().addAll(titleBar, restoreDefaultsBar, spacerHBox, windowModePane, vsyncPane,
                refreshRatePane, bottomSpacerHBox);
        initializeEventHandlers();

        this.setContent(primaryPane);
    }

    private void initializeEventHandlers() {

        applySettingsButton.setOnAction(e -> {
            ui.applyDSFConfig();
        });

        restoreDefaultsButton.setOnAction(e -> {
            ContinueDialog cD = new ContinueDialog(300.0, 80.0, DIALOG_TITLE_RESET, DIALOG_MSG_RESTORE_SETTINGS,
                    DIALOG_BUTTON_TEXTS[2], DIALOG_BUTTON_TEXTS[1]);

            if (cD.show()) {
                config.restoreDefaultUnsafeOptions();
                ui.refreshUI();
            }
        });

        neitherWindowMode.setOnAction(e -> {
            config.forceFullscreen.set(0);
            config.forceWindowed.set(0);
        });

        forceFullscreen.setOnAction(e -> {
            config.forceFullscreen.set(1);
            config.forceWindowed.set(0);
        });

        forceWindowed.setOnAction(e -> {
            config.forceFullscreen.set(0);
            config.forceWindowed.set(1);
        });

        vsyncPicker.setOnAction(e -> {
            if (vsyncPicker.getValue().equals(DISABLE_ENABLE[0])) {
                config.enableVsync.set(0);
            } else {
                config.enableVsync.set(1);
            }
        });

        refreshRateField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldText, String newText) {
                try {
                    if (!NumberUtils.isParsable(newText) || Integer.parseInt(newText) < 1) {
                        refreshRateField.pseudoClassStateChanged(INVALID_INPUT, true);
                        refreshRateField.setTooltip(new Tooltip(INPUT_GREATER_THAN_EQ + "1"));
                    } else {
                        refreshRateField.pseudoClassStateChanged(INVALID_INPUT, false);
                        refreshRateField.setTooltip(new Tooltip(""));
                        config.fullscreenHz.set(Integer.parseInt(newText));
                    }
                } catch (NumberFormatException nFE) {
                    ui.printConsole(INPUT_TOO_LARGE);
                    refreshRateField.setText("");
                }
            }
        });
    }

    public boolean hasInvalidInputs() {

        if (refreshRateField.getPseudoClassStates().toString().contains("invalid")) {
            return true;
        }

        return false;
    }
}