mirror of
https://github.com/developersu/ns-usbloader.git
synced 2025-05-14 07:04:50 -04:00
v0.2
This commit is contained in:
parent
c4d0959cf3
commit
f5a9ddf8df
13 changed files with 162 additions and 91 deletions
195
src/main/java/nsusbloader/Controllers/NSLMainController.java
Normal file
195
src/main/java/nsusbloader/Controllers/NSLMainController.java
Normal file
|
@ -0,0 +1,195 @@
|
|||
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 javafx.stage.FileChooser;
|
||||
import nsusbloader.AppPreferences;
|
||||
import nsusbloader.MediatorControl;
|
||||
import nsusbloader.NSLMain;
|
||||
import nsusbloader.UsbCommunications;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class NSLMainController implements Initializable {
|
||||
|
||||
private ResourceBundle resourceBundle;
|
||||
|
||||
@FXML
|
||||
public TextArea logArea; // Accessible from Mediator
|
||||
@FXML
|
||||
private Button selectNspBtn;
|
||||
@FXML
|
||||
private Button uploadStopBtn;
|
||||
private Region btnUpStopImage;
|
||||
@FXML
|
||||
public ProgressBar progressBar; // Accessible from Mediator
|
||||
@FXML
|
||||
private ChoiceBox<String> choiceProtocol;
|
||||
@FXML
|
||||
private Button switchThemeBtn;
|
||||
|
||||
@FXML
|
||||
private Pane specialPane;
|
||||
|
||||
@FXML
|
||||
public NSTableViewController tableFilesListController; // Accessible from Mediator
|
||||
|
||||
private Thread usbThread;
|
||||
|
||||
private String previouslyOpenedPath;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle rb) {
|
||||
this.resourceBundle = rb;
|
||||
logArea.setText(rb.getString("logsGreetingsMessage")+" "+ NSLMain.appVersion+"!\n");
|
||||
if (System.getProperty("os.name").toLowerCase().startsWith("lin"))
|
||||
if (!System.getProperty("user.name").equals("root"))
|
||||
logArea.appendText(rb.getString("logsEnteredAsMsg1")+System.getProperty("user.name")+"\n"+rb.getString("logsEnteredAsMsg2") + "\n");
|
||||
|
||||
logArea.appendText(rb.getString("logsGreetingsMessage2")+"\n");
|
||||
|
||||
MediatorControl.getInstance().setController(this);
|
||||
|
||||
specialPane.getStyleClass().add("special-pane-as-border"); // UI hacks
|
||||
|
||||
uploadStopBtn.setDisable(true);
|
||||
selectNspBtn.setOnAction(e->{ selectFilesBtnAction(); });
|
||||
uploadStopBtn.setOnAction(e->{ uploadBtnAction(); });
|
||||
|
||||
selectNspBtn.getStyleClass().add("buttonSelect");
|
||||
|
||||
this.btnUpStopImage = new Region();
|
||||
btnUpStopImage.getStyleClass().add("regionUpload");
|
||||
//uploadStopBtn.getStyleClass().remove("button");
|
||||
uploadStopBtn.getStyleClass().add("buttonUp");
|
||||
uploadStopBtn.setGraphic(btnUpStopImage);
|
||||
|
||||
ObservableList<String> choiceProtocolList = FXCollections.observableArrayList("TinFoil", "GoldLeaf");
|
||||
choiceProtocol.setItems(choiceProtocolList);
|
||||
choiceProtocol.getSelectionModel().select(0); // TODO: shared settings
|
||||
choiceProtocol.setOnAction(e->tableFilesListController.setNewProtocol(choiceProtocol.getSelectionModel().getSelectedItem())); // Add listener to notify tableView controller
|
||||
tableFilesListController.setNewProtocol(choiceProtocol.getSelectionModel().getSelectedItem()); // Notify tableView controller
|
||||
|
||||
this.previouslyOpenedPath = null;
|
||||
|
||||
Region btnSwitchImage = new Region();
|
||||
btnSwitchImage.getStyleClass().add("regionLamp");
|
||||
switchThemeBtn.setGraphic(btnSwitchImage);
|
||||
this.switchThemeBtn.setOnAction(e->switchTheme());
|
||||
|
||||
previouslyOpenedPath = AppPreferences.getInstance().getRecent();
|
||||
}
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Functionality for selecting NSP button.
|
||||
* Uses setReady and setNotReady to simplify code readability.
|
||||
* */
|
||||
private void selectFilesBtnAction(){
|
||||
List<File> filesList;
|
||||
FileChooser fileChooser = new FileChooser();
|
||||
fileChooser.setTitle(resourceBundle.getString("btnFileOpen"));
|
||||
|
||||
File validator = new File(previouslyOpenedPath);
|
||||
if (validator.exists())
|
||||
fileChooser.setInitialDirectory(validator); // TODO: read from prefs
|
||||
else
|
||||
fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); // TODO: read from prefs
|
||||
|
||||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP ROM", "*.nsp"));
|
||||
|
||||
filesList = fileChooser.showOpenMultipleDialog(logArea.getScene().getWindow());
|
||||
if (filesList != null && !filesList.isEmpty()) {
|
||||
tableFilesListController.setFiles(filesList);
|
||||
uploadStopBtn.setDisable(false);
|
||||
previouslyOpenedPath = filesList.get(0).getParent();
|
||||
}
|
||||
else{
|
||||
tableFilesListController.setFiles(null);
|
||||
uploadStopBtn.setDisable(true);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* It's button listener when no transmission executes
|
||||
* */
|
||||
private void uploadBtnAction(){
|
||||
if (usbThread == null || !usbThread.isAlive()){
|
||||
List<File> nspToUpload;
|
||||
if ((nspToUpload = tableFilesListController.getFiles()) == null) {
|
||||
resourceBundle.getString("logsNoFolderFileSelected");
|
||||
return;
|
||||
}else {
|
||||
logArea.setText(resourceBundle.getString("logsFilesToUploadTitle")+"\n");
|
||||
for (File item: nspToUpload)
|
||||
logArea.appendText(" "+item.getAbsolutePath()+"\n");
|
||||
}
|
||||
UsbCommunications usbCommunications = new UsbCommunications(nspToUpload, choiceProtocol.getSelectionModel().getSelectedItem());
|
||||
usbThread = new Thread(usbCommunications);
|
||||
usbThread.start();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* It's button listener when transmission in progress
|
||||
* */
|
||||
private void stopBtnAction(){
|
||||
if (usbThread != null && usbThread.isAlive()){
|
||||
usbThread.interrupt();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission"
|
||||
* Called from mediator
|
||||
* */
|
||||
public void notifyTransmissionStarted(boolean isTransmissionStarted){
|
||||
if (isTransmissionStarted) {
|
||||
selectNspBtn.setDisable(true);
|
||||
uploadStopBtn.setOnAction(e->{ stopBtnAction(); });
|
||||
|
||||
uploadStopBtn.setText(resourceBundle.getString("btnStop"));
|
||||
|
||||
btnUpStopImage.getStyleClass().remove("regionUpload");
|
||||
btnUpStopImage.getStyleClass().add("regionStop");
|
||||
|
||||
uploadStopBtn.getStyleClass().remove("buttonUp");
|
||||
uploadStopBtn.getStyleClass().add("buttonStop");
|
||||
}
|
||||
else {
|
||||
selectNspBtn.setDisable(false);
|
||||
uploadStopBtn.setOnAction(e->{ uploadBtnAction(); });
|
||||
|
||||
uploadStopBtn.setText(resourceBundle.getString("btnUpload"));
|
||||
|
||||
btnUpStopImage.getStyleClass().remove("regionStop");
|
||||
btnUpStopImage.getStyleClass().add("regionUpload");
|
||||
|
||||
uploadStopBtn.getStyleClass().remove("buttonStop");
|
||||
uploadStopBtn.getStyleClass().add("buttonUp");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Save preferences before exit
|
||||
* */
|
||||
public void exit(){
|
||||
AppPreferences.getInstance().setTheme(switchThemeBtn.getScene().getStylesheets().get(0));
|
||||
AppPreferences.getInstance().setRecent(previouslyOpenedPath);
|
||||
}
|
||||
}
|
|
@ -39,14 +39,14 @@ public class NSLRowModel {
|
|||
public void setStatus(EFileStatus status){ // TODO: Localization
|
||||
switch (status){
|
||||
case UPLOADED:
|
||||
this.status = "Uploaded";
|
||||
this.status = "Success";
|
||||
markForUpload = false;
|
||||
break;
|
||||
case FAILED:
|
||||
this.status = "Upload failed";
|
||||
this.status = "Failed";
|
||||
break;
|
||||
case INCORRECT_FILE_FAILED:
|
||||
this.status = "File incorrect";
|
||||
this.status = "Failed: Incorrect file";
|
||||
markForUpload = false;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -33,15 +33,29 @@ public class NSTableViewController implements Initializable {
|
|||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
rowsObsLst = FXCollections.observableArrayList();
|
||||
table.setPlaceholder(new Label());
|
||||
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
|
||||
|
||||
TableColumn<NSLRowModel, String> statusColumn = new TableColumn<>(resourceBundle.getString("tableStatusLbl"));
|
||||
TableColumn<NSLRowModel, String> fileNameColumn = new TableColumn<>(resourceBundle.getString("tableFileNameLbl"));
|
||||
TableColumn<NSLRowModel, String> fileSizeColumn = new TableColumn<>(resourceBundle.getString("tableSizeLbl"));
|
||||
TableColumn<NSLRowModel, Boolean> uploadColumn = new TableColumn<>(resourceBundle.getString("tableUploadLbl"));
|
||||
statusColumn.setMinWidth(70.0);
|
||||
fileNameColumn.setMinWidth(250.0);
|
||||
fileSizeColumn.setMinWidth(70.0);
|
||||
uploadColumn.setMinWidth(70.0);
|
||||
// See https://bugs.openjdk.java.net/browse/JDK-8157687
|
||||
statusColumn.setMinWidth(100.0);
|
||||
statusColumn.setPrefWidth(100.0);
|
||||
statusColumn.setMaxWidth(100.0);
|
||||
statusColumn.setResizable(false);
|
||||
|
||||
fileNameColumn.setMinWidth(25.0);
|
||||
|
||||
fileSizeColumn.setMinWidth(120.0);
|
||||
fileSizeColumn.setPrefWidth(120.0);
|
||||
fileSizeColumn.setMaxWidth(120.0);
|
||||
fileSizeColumn.setResizable(false);
|
||||
|
||||
uploadColumn.setMinWidth(100.0);
|
||||
uploadColumn.setPrefWidth(100.0);
|
||||
uploadColumn.setMaxWidth(100.0);
|
||||
uploadColumn.setResizable(false);
|
||||
|
||||
statusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
|
||||
fileNameColumn.setCellValueFactory(new PropertyValueFactory<>("nspFileName"));
|
||||
|
@ -76,12 +90,6 @@ public class NSTableViewController implements Initializable {
|
|||
|
||||
table.setItems(rowsObsLst);
|
||||
table.getColumns().addAll(statusColumn, fileNameColumn, fileSizeColumn, uploadColumn);
|
||||
/* debug content
|
||||
rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), true));
|
||||
rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), false));
|
||||
rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), false));
|
||||
rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), true));
|
||||
*/
|
||||
}
|
||||
/**
|
||||
* See uploadColumn callback. In case of GoldLeaf we have to restrict selection
|
||||
|
@ -145,6 +153,7 @@ public class NSTableViewController implements Initializable {
|
|||
model.setStatus(status);
|
||||
}
|
||||
}
|
||||
table.refresh();
|
||||
}
|
||||
/**
|
||||
* Called if selected different USB protocol
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue