From 2b5956d4f2ba32eb598f8b38001103f599d7106d Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Wed, 10 Apr 2019 13:07:28 +0300 Subject: [PATCH] v0.4.1: Code refactoring Updating checks functionality changes: avoid new version pop-ups for special builds (versions that have -DEV postfixes) --- .../Controllers/FrontController.java | 122 +++++++++++++++++ .../Controllers/NSLMainController.java | 125 +++--------------- .../ModelControllers/MessagesConsumer.java | 2 +- .../ModelControllers/UpdatesChecker.java | 8 +- src/main/java/nsusbloader/NSLMain.java | 2 +- .../nsusbloader/USB/UsbCommunications.java | 2 +- src/main/resources/FrontTab.fxml | 51 +++++++ src/main/resources/NSLMain.fxml | 39 +----- 8 files changed, 204 insertions(+), 147 deletions(-) create mode 100644 src/main/java/nsusbloader/Controllers/FrontController.java create mode 100644 src/main/resources/FrontTab.fxml diff --git a/src/main/java/nsusbloader/Controllers/FrontController.java b/src/main/java/nsusbloader/Controllers/FrontController.java new file mode 100644 index 0000000..2e41a46 --- /dev/null +++ b/src/main/java/nsusbloader/Controllers/FrontController.java @@ -0,0 +1,122 @@ +package nsusbloader.Controllers; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.*; +import javafx.scene.layout.Pane; +import javafx.scene.layout.Region; +import nsusbloader.AppPreferences; + +import java.net.URL; +import java.util.ResourceBundle; + +public class FrontController implements Initializable { + @FXML + private Pane specialPane; + + @FXML + private ChoiceBox choiceProtocol, choiceNetUsb; + @FXML + private Label nsIpLbl; + @FXML + private TextField nsIpTextField; + @FXML + private Button switchThemeBtn; + @FXML + public NSTableViewController tableFilesListController; // Accessible from Mediator + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + specialPane.getStyleClass().add("special-pane-as-border"); // UI hacks + + ObservableList choiceProtocolList = FXCollections.observableArrayList("TinFoil", "GoldLeaf"); + choiceProtocol.setItems(choiceProtocolList); + choiceProtocol.getSelectionModel().select(AppPreferences.getInstance().getProtocol()); + choiceProtocol.setOnAction(e-> { + tableFilesListController.setNewProtocol(choiceProtocol.getSelectionModel().getSelectedItem()); + if (choiceProtocol.getSelectionModel().getSelectedItem().equals("GoldLeaf")) { + choiceNetUsb.setDisable(true); + choiceNetUsb.getSelectionModel().select("USB"); + nsIpLbl.setVisible(false); + nsIpTextField.setVisible(false); + } + else { + choiceNetUsb.setDisable(false); + if (choiceNetUsb.getSelectionModel().getSelectedItem().equals("NET")) { + nsIpLbl.setVisible(true); + nsIpTextField.setVisible(true); + } + } + }); // Add listener to notify tableView controller + tableFilesListController.setNewProtocol(choiceProtocol.getSelectionModel().getSelectedItem()); // Notify tableView controller + + ObservableList choiceNetUsbList = FXCollections.observableArrayList("USB", "NET"); + choiceNetUsb.setItems(choiceNetUsbList); + choiceNetUsb.getSelectionModel().select(AppPreferences.getInstance().getNetUsb()); + if (choiceProtocol.getSelectionModel().getSelectedItem().equals("GoldLeaf")) { + choiceNetUsb.setDisable(true); + choiceNetUsb.getSelectionModel().select("USB"); + } + choiceNetUsb.setOnAction(e->{ + if (choiceNetUsb.getSelectionModel().getSelectedItem().equals("NET")){ + nsIpLbl.setVisible(true); + nsIpTextField.setVisible(true); + } + else{ + nsIpLbl.setVisible(false); + nsIpTextField.setVisible(false); + } + }); + // Set and configure NS IP field behavior + nsIpTextField.setText(AppPreferences.getInstance().getNsIp()); + if (choiceProtocol.getSelectionModel().getSelectedItem().equals("TinFoil") && choiceNetUsb.getSelectionModel().getSelectedItem().equals("NET")){ + nsIpLbl.setVisible(true); + nsIpTextField.setVisible(true); + } + nsIpTextField.setTextFormatter(new TextFormatter<>(change -> { + if (change.getControlNewText().contains(" ") | change.getControlNewText().contains("\t")) + return null; + else + return change; + })); + // Set and configure switch theme button + Region btnSwitchImage = new Region(); + btnSwitchImage.getStyleClass().add("regionLamp"); + switchThemeBtn.setGraphic(btnSwitchImage); + this.switchThemeBtn.setOnAction(e->switchTheme()); + } + /** + * Changes UI theme on the go + * */ + private void switchTheme(){ + if (switchThemeBtn.getScene().getStylesheets().get(0).equals("/res/app_dark.css")) { + switchThemeBtn.getScene().getStylesheets().remove("/res/app_dark.css"); + switchThemeBtn.getScene().getStylesheets().add("/res/app_light.css"); + } + else { + switchThemeBtn.getScene().getStylesheets().remove("/res/app_light.css"); + switchThemeBtn.getScene().getStylesheets().add("/res/app_dark.css"); + } + AppPreferences.getInstance().setTheme(switchThemeBtn.getScene().getStylesheets().get(0)); + } + /** + * Get selected protocol (GL/TF) + * */ + String getSelectedProtocol(){ + return choiceProtocol.getSelectionModel().getSelectedItem(); + } + /** + * Get selected protocol (USB/NET) + * */ + String getSelectedNetUsb(){ + return choiceNetUsb.getSelectionModel().getSelectedItem(); + } + /** + * Get NS IP address + * */ + String getNsIp(){ + return nsIpTextField.getText(); + } +} diff --git a/src/main/java/nsusbloader/Controllers/NSLMainController.java b/src/main/java/nsusbloader/Controllers/NSLMainController.java index 14edb68..2faa075 100644 --- a/src/main/java/nsusbloader/Controllers/NSLMainController.java +++ b/src/main/java/nsusbloader/Controllers/NSLMainController.java @@ -1,15 +1,12 @@ package nsusbloader.Controllers; import javafx.application.HostServices; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; import javafx.scene.input.DragEvent; import javafx.scene.input.TransferMode; -import javafx.scene.layout.Pane; import javafx.scene.layout.Region; import javafx.stage.FileChooser; import nsusbloader.*; @@ -36,22 +33,11 @@ public class NSLMainController implements Initializable { private Region btnUpStopImage; @FXML public ProgressBar progressBar; // Accessible from Mediator - @FXML - private ChoiceBox choiceProtocol, choiceNetUsb; - @FXML - private Button switchThemeBtn; - @FXML - private Pane specialPane; - - @FXML - public NSTableViewController tableFilesListController; // Accessible from Mediator @FXML private SettingsController SettingsTabController; @FXML - private TextField nsIpTextField; - @FXML - private Label nsIpLbl; + public FrontController FrontTabController; // Accessible from Mediator | todo: incapsulate private Task usbNetCommunications; private Thread workThread; @@ -70,8 +56,6 @@ public class NSLMainController implements Initializable { MediatorControl.getInstance().setController(this); - specialPane.getStyleClass().add("special-pane-as-border"); // UI hacks - uploadStopBtn.setDisable(true); selectNspBtn.setOnAction(e->{ selectFilesBtnAction(); }); uploadStopBtn.setOnAction(e->{ uploadBtnAction(); }); @@ -80,67 +64,11 @@ public class NSLMainController implements Initializable { this.btnUpStopImage = new Region(); btnUpStopImage.getStyleClass().add("regionUpload"); - //uploadStopBtn.getStyleClass().remove("button"); + uploadStopBtn.getStyleClass().add("buttonUp"); uploadStopBtn.setGraphic(btnUpStopImage); - ObservableList choiceProtocolList = FXCollections.observableArrayList("TinFoil", "GoldLeaf"); - choiceProtocol.setItems(choiceProtocolList); - choiceProtocol.getSelectionModel().select(AppPreferences.getInstance().getProtocol()); - choiceProtocol.setOnAction(e-> { - tableFilesListController.setNewProtocol(choiceProtocol.getSelectionModel().getSelectedItem()); - if (choiceProtocol.getSelectionModel().getSelectedItem().equals("GoldLeaf")) { - choiceNetUsb.setDisable(true); - choiceNetUsb.getSelectionModel().select("USB"); - nsIpLbl.setVisible(false); - nsIpTextField.setVisible(false); - } - else { - choiceNetUsb.setDisable(false); - if (choiceNetUsb.getSelectionModel().getSelectedItem().equals("NET")) { - nsIpLbl.setVisible(true); - nsIpTextField.setVisible(true); - } - } - }); // Add listener to notify tableView controller - tableFilesListController.setNewProtocol(choiceProtocol.getSelectionModel().getSelectedItem()); // Notify tableView controller - - ObservableList choiceNetUsbList = FXCollections.observableArrayList("USB", "NET"); - choiceNetUsb.setItems(choiceNetUsbList); - choiceNetUsb.getSelectionModel().select(AppPreferences.getInstance().getNetUsb()); - if (choiceProtocol.getSelectionModel().getSelectedItem().equals("GoldLeaf")) { - choiceNetUsb.setDisable(true); - choiceNetUsb.getSelectionModel().select("USB"); - } - choiceNetUsb.setOnAction(e->{ - if (choiceNetUsb.getSelectionModel().getSelectedItem().equals("NET")){ - nsIpLbl.setVisible(true); - nsIpTextField.setVisible(true); - } - else{ - nsIpLbl.setVisible(false); - nsIpTextField.setVisible(false); - } - }); - nsIpTextField.setText(AppPreferences.getInstance().getNsIp()); - if (choiceProtocol.getSelectionModel().getSelectedItem().equals("TinFoil") && choiceNetUsb.getSelectionModel().getSelectedItem().equals("NET")){ - nsIpLbl.setVisible(true); - nsIpTextField.setVisible(true); - } - nsIpTextField.setTextFormatter(new TextFormatter<>(change -> { - if (change.getControlNewText().contains(" ") | change.getControlNewText().contains("\t")) - return null; - else - return change; - })); - this.previouslyOpenedPath = null; - - Region btnSwitchImage = new Region(); - btnSwitchImage.getStyleClass().add("regionLamp"); - switchThemeBtn.setGraphic(btnSwitchImage); - this.switchThemeBtn.setOnAction(e->switchTheme()); - - previouslyOpenedPath = AppPreferences.getInstance().getRecent(); + this.previouslyOpenedPath = AppPreferences.getInstance().getRecent(); if (AppPreferences.getInstance().getAutoCheckUpdates()){ Task> updTask = new UpdatesChecker(); @@ -164,20 +92,7 @@ public class NSLMainController implements Initializable { * Provide hostServices to Settings tab * */ public void setHostServices(HostServices hs ){ SettingsTabController.registerHostServices(hs);} - /** - * Changes UI theme on the go - * */ - private void switchTheme(){ - if (switchThemeBtn.getScene().getStylesheets().get(0).equals("/res/app_dark.css")) { - switchThemeBtn.getScene().getStylesheets().remove("/res/app_dark.css"); - switchThemeBtn.getScene().getStylesheets().add("/res/app_light.css"); - } - else { - switchThemeBtn.getScene().getStylesheets().remove("/res/app_light.css"); - switchThemeBtn.getScene().getStylesheets().add("/res/app_dark.css"); - } - AppPreferences.getInstance().setTheme(switchThemeBtn.getScene().getStylesheets().get(0)); - } + /** * Functionality for selecting NSP button. * Uses setReady and setNotReady to simplify code readability. @@ -189,15 +104,15 @@ public class NSLMainController implements Initializable { File validator = new File(previouslyOpenedPath); if (validator.exists()) - fileChooser.setInitialDirectory(validator); // TODO: read from prefs + fileChooser.setInitialDirectory(validator); else - fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); // TODO: read from prefs + fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP ROM", "*.nsp")); filesList = fileChooser.showOpenMultipleDialog(logArea.getScene().getWindow()); if (filesList != null && !filesList.isEmpty()) { - tableFilesListController.setFiles(filesList); + FrontTabController.tableFilesListController.setFiles(filesList); uploadStopBtn.setDisable(false); previouslyOpenedPath = filesList.get(0).getParent(); } @@ -209,7 +124,7 @@ public class NSLMainController implements Initializable { if ((workThread == null || !workThread.isAlive())){ // Collect files List nspToUpload; - if ((nspToUpload = tableFilesListController.getFilesForUpload()) == null) { + if ((nspToUpload = FrontTabController.tableFilesListController.getFilesForUpload()) == null) { logArea.setText(resourceBundle.getString("logsNoFolderFileSelected")); return; } @@ -219,23 +134,23 @@ public class NSLMainController implements Initializable { logArea.appendText(" "+item.getAbsolutePath()+"\n"); } // If USB selected - if (choiceProtocol.getSelectionModel().getSelectedItem().equals("GoldLeaf") || + if (FrontTabController.getSelectedProtocol().equals("GoldLeaf") || ( - choiceProtocol.getSelectionModel().getSelectedItem().equals("TinFoil") - && choiceNetUsb.getSelectionModel().getSelectedItem().equals("USB") + FrontTabController.getSelectedProtocol().equals("TinFoil") + && FrontTabController.getSelectedNetUsb().equals("USB") ) ){ - usbNetCommunications = new UsbCommunications(nspToUpload, choiceProtocol.getSelectionModel().getSelectedItem()); + usbNetCommunications = new UsbCommunications(nspToUpload, FrontTabController.getSelectedProtocol()); workThread = new Thread(usbNetCommunications); workThread.setDaemon(true); workThread.start(); } else { // NET INSTALL OVER TINFOIL - if (SettingsTabController.isNsIpValidate() && !nsIpTextField.getText().matches("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$")) + if (SettingsTabController.isNsIpValidate() && ! FrontTabController.getNsIp().matches("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$")) if (!ServiceWindow.getConfirmationWindow(resourceBundle.getString("windowTitleBadIp"),resourceBundle.getString("windowBodyBadIp"))) return; - String nsIP = nsIpTextField.getText(); + String nsIP = FrontTabController.getNsIp(); if (!SettingsTabController.getExpertModeSelected()) usbNetCommunications = new NETCommunications(nspToUpload, nsIP, false, "", "", ""); @@ -261,7 +176,7 @@ public class NSLMainController implements Initializable { * */ private void stopBtnAction(){ if (workThread != null && workThread.isAlive()){ - usbNetCommunications.cancel(false); // TODO: add something abstract to use also for network + usbNetCommunications.cancel(false); } } /** @@ -332,19 +247,19 @@ public class NSLMainController implements Initializable { se.printStackTrace(); } if (!filesDropped.isEmpty()) - tableFilesListController.setFiles(filesDropped); + FrontTabController.tableFilesListController.setFiles(filesDropped); event.setDropCompleted(true); } /** * Save preferences before exit * */ - public void exit(){ // TODO: add method to set all in AppPreferences + public void exit(){ AppPreferences.getInstance().setAll( - choiceProtocol.getSelectionModel().getSelectedItem(), + FrontTabController.getSelectedProtocol(), previouslyOpenedPath, - choiceNetUsb.getSelectionModel().getSelectedItem(), - nsIpTextField.getText().trim(), + FrontTabController.getSelectedNetUsb(), + FrontTabController.getNsIp(), SettingsTabController.isNsIpValidate(), SettingsTabController.getExpertModeSelected(), SettingsTabController.getAutoIpSelected(), diff --git a/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java b/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java index 9ffb253..d0bbcc6 100644 --- a/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java +++ b/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java @@ -33,7 +33,7 @@ public class MessagesConsumer extends AnimationTimer { this.progressBar = MediatorControl.getInstance().getContoller().progressBar; this.statusMap = statusMap; - this.tableViewController = MediatorControl.getInstance().getContoller().tableFilesListController; + this.tableViewController = MediatorControl.getInstance().getContoller().FrontTabController.tableFilesListController; progressBar.setProgress(0.0); diff --git a/src/main/java/nsusbloader/ModelControllers/UpdatesChecker.java b/src/main/java/nsusbloader/ModelControllers/UpdatesChecker.java index 7e00f1b..21f300c 100644 --- a/src/main/java/nsusbloader/ModelControllers/UpdatesChecker.java +++ b/src/main/java/nsusbloader/ModelControllers/UpdatesChecker.java @@ -50,8 +50,14 @@ public class UpdatesChecker extends Task> { return null; } + String currentVersion; + if (NSLMain.appVersion.matches("^v(([0-9])+?\\.)+[0-9]+(-.+)$")) // if current version have postfix like v0.1-Experimental + currentVersion = NSLMain.appVersion.replaceAll("(-.*$)", ""); // cut postfix + else + currentVersion = NSLMain.appVersion; + List returningValue = new ArrayList<>(); - if (!newVersion.equals(NSLMain.appVersion)) { // if latest noted version in GitHub is different to this version + if (!newVersion.equals(currentVersion)) { // if latest noted version in GitHub is different to this version returningValue.add(newVersion); returningValue.add(changeLog); return returningValue; diff --git a/src/main/java/nsusbloader/NSLMain.java b/src/main/java/nsusbloader/NSLMain.java index 7a70297..5e7aa9c 100644 --- a/src/main/java/nsusbloader/NSLMain.java +++ b/src/main/java/nsusbloader/NSLMain.java @@ -12,7 +12,7 @@ import java.util.Locale; import java.util.ResourceBundle; public class NSLMain extends Application { - public static final String appVersion = "v0.4"; + public static final String appVersion = "v0.4.1"; @Override public void start(Stage primaryStage) throws Exception{ diff --git a/src/main/java/nsusbloader/USB/UsbCommunications.java b/src/main/java/nsusbloader/USB/UsbCommunications.java index 3c9972d..428113a 100644 --- a/src/main/java/nsusbloader/USB/UsbCommunications.java +++ b/src/main/java/nsusbloader/USB/UsbCommunications.java @@ -456,7 +456,7 @@ public class UsbCommunications extends Task { * GoldLeaf processing * */ private class GoldLeaf{ - // CMD G L U C ID 0 0 0 + // CMD G L U C private final byte[] CMD_GLUC = new byte[]{0x47, 0x4c, 0x55, 0x43}; private final byte[] CMD_ConnectionRequest = new byte[]{0x00, 0x00, 0x00, 0x00}; // Write-only command private final byte[] CMD_NSPName = new byte[]{0x02, 0x00, 0x00, 0x00}; // Write-only command diff --git a/src/main/resources/FrontTab.fxml b/src/main/resources/FrontTab.fxml new file mode 100644 index 0000000..733cb1d --- /dev/null +++ b/src/main/resources/FrontTab.fxml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + +