Skip to content

Commit

Permalink
Update scan-folder-to-add-files function: set pop-up window with indi…
Browse files Browse the repository at this point in the history
…cator showing how many files did we scan and how many files would be added.

Minor color corrections on light theme for whoever use it
Fix TF progress-bar (can't recall when I broke it..)
  • Loading branch information
developersu committed Dec 9, 2020
1 parent 619a2b1 commit d8ea426
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 32 deletions.
120 changes: 120 additions & 0 deletions src/main/java/nsusbloader/Controllers/FilesDropHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.Controllers;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import nsusbloader.AppPreferences;
import nsusbloader.MediatorControl;

import java.io.File;
import java.util.List;
import java.util.ResourceBundle;

public class FilesDropHandle {

public FilesDropHandle(List<File> files, String filesRegex, String foldersRegex){

//
// TODO: ADD GRAPHICS BEFORE RELEASE !
//

FilesDropHandleTask filesDropHandleTask = new FilesDropHandleTask(files, filesRegex, foldersRegex);

ResourceBundle resourceBundle = MediatorControl.getInstance().getResourceBundle();
Button cancelButton = new Button(resourceBundle.getString("btn_Cancel"));

ProgressIndicator progressIndicator = new ProgressIndicator();
progressIndicator.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS);

Label downloadStatusLabel = new Label();
downloadStatusLabel.setWrapText(true);
downloadStatusLabel.textProperty().bind(filesDropHandleTask.messageProperty());

Pane fillerPane1 = new Pane();
Pane fillerPane2 = new Pane();

VBox parentVBox = new VBox();
parentVBox.setAlignment(Pos.TOP_CENTER);
parentVBox.setFillWidth(true);
parentVBox.setSpacing(5.0);
parentVBox.setPadding(new Insets(5.0));
parentVBox.setFillWidth(true);
parentVBox.getChildren().addAll(
downloadStatusLabel,
fillerPane1,
progressIndicator,
fillerPane2,
cancelButton
); // TODO:FIX

VBox.setVgrow(fillerPane1, Priority.ALWAYS);
VBox.setVgrow(fillerPane2, Priority.ALWAYS);

Stage stage = new Stage();
stage.setTitle(resourceBundle.getString("windowTitleAddingFiles"));
stage.getIcons().addAll(
new Image("/res/dwnload_ico32x32.png"), //TODO: REDRAW
new Image("/res/dwnload_ico48x48.png"),
new Image("/res/dwnload_ico64x64.png"),
new Image("/res/dwnload_ico128x128.png")
);
stage.setMinWidth(300);
stage.setMinHeight(175);
stage.setAlwaysOnTop(true);
Scene mainScene = new Scene(parentVBox, 310, 185);

mainScene.getStylesheets().add(AppPreferences.getInstance().getTheme());

stage.setOnHidden(windowEvent -> filesDropHandleTask.cancel(true ) );

stage.setScene(mainScene);
stage.show();
stage.toFront();

filesDropHandleTask.setOnSucceeded(event -> {
cancelButton.setText(resourceBundle.getString("btn_Close"));

List<File> allFiles = filesDropHandleTask.getValue();

if (! allFiles.isEmpty()) {
MediatorControl.getInstance().getGamesController().tableFilesListController.setFiles(allFiles);
}
stage.close();
});

new Thread(filesDropHandleTask).start();

cancelButton.setOnAction(actionEvent -> {
filesDropHandleTask.cancel(true);
stage.close();
});
}
}
96 changes: 96 additions & 0 deletions src/main/java/nsusbloader/Controllers/FilesDropHandleTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.Controllers;

import javafx.concurrent.Task;
import nsusbloader.MediatorControl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FilesDropHandleTask extends Task<List<File>> {
private final String filesRegex;
private final String foldersRegex;

private final List<File> filesDropped;
private final List<File> allFiles;

private String messageTemplate;
private long filesScanned = 0;
private long filesAdded = 0;

FilesDropHandleTask(List<File> files,
String filesRegex,
String foldersRegex) {
this.filesDropped = files;
this.filesRegex = filesRegex;
this.foldersRegex = foldersRegex;
this.allFiles = new ArrayList<>();
this.messageTemplate = MediatorControl.getInstance().getResourceBundle().getString("windowBodyFilesScanned");
}

@Override
protected List<File> call() {
if (filesDropped == null || filesDropped.size() == 0)
return allFiles;

for (File file : filesDropped){
if (isCancelled())
return new ArrayList<>();
collectFiles(file);
updateMessage(String.format(messageTemplate, filesScanned++, filesAdded));
}

return allFiles;
}

private void collectFiles(File startFolder) {
if (startFolder == null)
return;

final String startFolderNameInLowercase = startFolder.getName().toLowerCase();

if (startFolder.isFile()) {
if (startFolderNameInLowercase.matches(filesRegex)) {
allFiles.add(startFolder);
filesAdded++;
}
return;
}

if (startFolderNameInLowercase.matches(foldersRegex)) {
allFiles.add(startFolder);
filesAdded++;
return;
}

File[] files = startFolder.listFiles();
if (files == null)
return;

for (File file : files) {
if (isCancelled())
return;
collectFiles(file);
updateMessage(String.format(messageTemplate, filesScanned++, filesAdded));
}
}

}
27 changes: 5 additions & 22 deletions src/main/java/nsusbloader/Controllers/GamesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand Down Expand Up @@ -294,6 +291,7 @@ private void selectFoldersBtnAction() {
* @param filesRegex for filenames
*/
// TODO: Too sophisticated. Should be moved to simple class to keep things simplier

private void collectFiles(List<File> storage,
File startFolder,
final String filesRegex,
Expand Down Expand Up @@ -437,24 +435,10 @@ private void handleDragOver(DragEvent event){
* */
@FXML
private void handleDrop(DragEvent event) {
final String regexForFiles = getRegexForFiles();
final String regexForFolders = getRegexForFolders();

List<File> files = event.getDragboard().getFiles();

performInBackgroundAndUpdate(() -> {
List<File> allFiles = new ArrayList<>();
if (files != null && files.size() != 0) {
files.forEach(f -> collectFiles(allFiles, f, regexForFiles, regexForFolders));
}
return allFiles;
}, allFiles -> {
if (!allFiles.isEmpty())
tableFilesListController.setFiles(allFiles);

event.setDropCompleted(true);
event.consume();
});
new FilesDropHandle(files, getRegexForFiles(), getRegexForFolders());
event.setDropCompleted(true);
event.consume();
}

/**
Expand Down Expand Up @@ -527,7 +511,6 @@ private void setFilesSelectorButtonBehaviour(boolean isDirectoryChooser){
selectSplitNspBtn.setVisible(true);
}
selectNspBtn.setGraphic(btnSelectImage);
//selectFolderBtn.setTooltip(new Tooltip(resourceBundle.getString("btn_OpenFolders_tooltip")));
}
/**
* Get 'Recent' path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void setFile(File file){
/**
* Add files when user selected them
* */
public void setFiles(List<File> newFiles){
public synchronized void setFiles(List<File> newFiles){
if (!rowsObsLst.isEmpty()){
List<String> filesAlreayInList = new ArrayList<>();
for (NSLRowModel model : rowsObsLst)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nsusbloader/com/usb/TinFoil.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ void sendSplitFile(File nspFile, long size, long offset) throws IOException, Nul
if ((currentOffset + chunk) >= size )
chunk = Math.toIntExact(size - currentOffset);
//System.out.println("CO: "+currentOffset+"\t\tEO: "+size+"\t\tRP: "+chunk); // NOTE: DEBUG
logPrinter.updateProgress((currentOffset + chunk) / (size / 100.0) / 100.0);

readBuffer = new byte[chunk]; // TODO: not perfect moment, consider refactoring.

Expand All @@ -232,6 +231,7 @@ void sendSplitFile(File nspFile, long size, long offset) throws IOException, Nul
if (writeUsb(readBuffer))
throw new IOException("TF Failure during file transfer.");
currentOffset += chunk;
logPrinter.updateProgress((double)currentOffset / (double)size);
}
nsSplitReader.close();
logPrinter.updateProgress(1.0);
Expand All @@ -251,7 +251,6 @@ void sendNormalFile(File nspFile, long size, long offset) throws IOException, Nu
if ((currentOffset + chunk) >= size)
chunk = Math.toIntExact(size - currentOffset);
//System.out.println("CO: "+currentOffset+"\t\tEO: "+receivedRangeSize+"\t\tRP: "+chunk); // NOTE: DEBUG
logPrinter.updateProgress((currentOffset + chunk) / (size / 100.0) / 100.0);

readBuffer = new byte[chunk];

Expand All @@ -261,6 +260,7 @@ void sendNormalFile(File nspFile, long size, long offset) throws IOException, Nu
if (writeUsb(readBuffer))
throw new IOException("TF Failure during file transfer.");
currentOffset += chunk;
logPrinter.updateProgress((double)currentOffset / (double)size);
}
bufferedInStream.close();
logPrinter.updateProgress(1.0);
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/locale.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ tab2_Cb_GlVersion=GoldLeaf version
tab2_Cb_GLshowNspOnly=Show only *.nsp in GoldLeaf.
windowBodyPleaseStopOtherProcessFirst=Please stop other active process before continuing.
tab2_Cb_foldersSelectorForRoms=Select folder with ROM files instead of selecting ROMs individually.
tab2_Cb_foldersSelectorForRomsDesc=Changes 'Select files' button behaviour on 'Games' tab: instead of selecting ROM files one-by-one you can choose folder to add every supported file at once.
tab2_Cb_foldersSelectorForRomsDesc=Changes 'Select files' button behaviour on 'Games' tab: instead of selecting ROM files one-by-one you can choose folder to add every supported file at once.
windowTitleAddingFiles=Searching for files...
windowBodyFilesScanned=Files scanned: %d\nWould be added: %d
4 changes: 3 additions & 1 deletion src/main/resources/locale_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ tab2_Cb_GlVersion=GoldLeaf version
tab2_Cb_GLshowNspOnly=Show only *.nsp in GoldLeaf.
windowBodyPleaseStopOtherProcessFirst=Please stop other active process before continuing.
tab2_Cb_foldersSelectorForRoms=Select folder with ROM files instead of selecting ROMs individually.
tab2_Cb_foldersSelectorForRomsDesc=Changes 'Select files' button behaviour on 'Games' tab: instead of selecting ROM files one-by-one you can choose folder to add every supported file at once.
tab2_Cb_foldersSelectorForRomsDesc=Changes 'Select files' button behaviour on 'Games' tab: instead of selecting ROM files one-by-one you can choose folder to add every supported file at once.
windowTitleAddingFiles=Searching for files...
windowBodyFilesScanned=Files scanned: %d\nWould be added: %d
2 changes: 2 additions & 0 deletions src/main/resources/locale_ru_RU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ tab2_Cb_GlVersion=\u0412\u0435\u0440\u0441\u0438\u044F GoldLeaf
windowBodyPleaseStopOtherProcessFirst=\u041F\u043E\u0436\u0430\u043B\u0443\u0439\u0441\u0442\u0430, \u043E\u0441\u0442\u0430\u043D\u043E\u0432\u0438\u0442\u0435 \u0434\u0440\u0443\u0433\u043E\u0439 \u0430\u043A\u0442\u0438\u0432\u043D\u044B\u0439 \u043F\u0440\u043E\u0446\u0435\u0441\u0441 \u043F\u0435\u0440\u0435\u0434 \u0442\u0435\u043C, \u043A\u0430\u043A \u043F\u0440\u043E\u0434\u043E\u043B\u0436\u0438\u0442\u044C.
tab2_Cb_foldersSelectorForRomsDesc=\u041C\u0435\u043D\u044F\u0435\u0442 \u043F\u043E\u0432\u0435\u0434\u0435\u043D\u0438\u0435 \u043A\u043D\u043E\u043F\u043A\u0438 "\u0412\u044B\u0431\u0440\u0430\u0442\u044C \u0444\u0430\u0439\u043B\u044B" \u043D\u0430 \u0432\u043A\u043B\u0430\u0434\u043A\u0435 '\u0418\u0433\u0440\u044B'. \u0412\u043C\u0435\u0441\u0442\u043E \u0432\u044B\u0431\u043E\u0440\u0430 ROM \u0444\u0430\u0439\u043B\u043E\u0432 \u043F\u043E \u043E\u0434\u043D\u043E\u043C\u0443 \u0432\u044B \u0443\u043A\u0430\u0437\u044B\u0432\u0430\u0435\u0442\u0435 \u043F\u0430\u043F\u043A\u0443, \u043F\u043E\u0441\u043B\u0435 \u0447\u0435\u0433\u043E \u0432\u0441\u0435 \u043F\u043E\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043C\u044B\u0435 \u043E\u0431\u0440\u0430\u0437\u044B \u0434\u043E\u0431\u0430\u0432\u043B\u044F\u044E\u0442\u0441\u044F.
tab2_Cb_foldersSelectorForRoms=\u0412\u044B\u0431\u0438\u0440\u0430\u0442\u044C \u043F\u0430\u043F\u043A\u0443 \u0441 ROM \u0444\u0430\u0439\u043B\u0430\u043C\u0438 \u0432\u043C\u0435\u0441\u0442\u043E \u0432\u044B\u0431\u043E\u0440\u0430 \u0444\u0430\u0439\u043B\u043E\u0432 \u043F\u043E\u043E\u0434\u0438\u043D\u043E\u0447\u043A\u0435.
windowTitleAddingFiles=\u0418\u0449\u0435\u043C \u0444\u0430\u0439\u043B\u044B...
windowBodyFilesScanned=\u0424\u0430\u0439\u043B\u043E\u0432 \u043F\u0440\u043E\u0441\u043A\u0430\u043D\u0438\u0440\u043E\u0432\u0430\u043D\u043E: %d\n\u0418\u0437 \u043A\u043E\u0442\u043E\u0440\u044B\u0445 \u0431\u0443\u0434\u0435\u0442 \u0434\u043E\u0431\u0430\u0432\u043B\u0435\u043D\u043E: %d

2 changes: 2 additions & 0 deletions src/main/resources/locale_uk_UA.properties
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ tab2_Cb_GlVersion=\u0412\u0435\u0440\u0441\u0456\u044F GoldLeaf
windowBodyPleaseStopOtherProcessFirst=\u0411\u0443\u0434\u044C \u043B\u0430\u0441\u043A\u0430, \u0437\u0443\u043F\u0438\u043D\u0456\u0442\u044C \u0456\u043D\u0448\u0438\u0439 \u0430\u043A\u0442\u0438\u0432\u043D\u0438\u0439 \u043F\u0440\u043E\u0446\u0435\u0441 \u043F\u0435\u0440\u0448 \u043D\u0456\u0436 \u043F\u0440\u043E\u0434\u043E\u0432\u0436\u0438\u0442\u0438.
tab2_Cb_foldersSelectorForRomsDesc=\u0417\u043C\u0456\u043D\u044E\u0454 \u043F\u043E\u0432\u0435\u0434\u0456\u043D\u043A\u0443 \u043A\u043D\u043E\u043F\u043A\u0438 \u043A\u043D\u043E\u043F\u043A\u0438 "\u0412\u0438\u0431\u0440\u0430\u0442\u0438 \u0444\u0430\u0439\u043B\u0438" \u043D\u0430 \u0432\u043A\u043B\u0430\u0434\u0446\u0456 '\u0406\u0433\u0440\u0438'. \u0417\u0430\u043C\u0456\u0441\u0442\u044C \u0432\u0438\u0431\u043E\u0440\u0443 ROM \u0444\u0430\u0439\u043B\u0456\u0432 \u043E\u0434\u0438\u043D \u0437\u0430 \u043E\u0434\u043D\u0438\u043C \u0432\u0438 \u0432\u043A\u0430\u0437\u0443\u0454\u0442\u0435 \u043F\u0430\u043F\u043A\u0443, \u043F\u0456\u0441\u043B\u044F \u0447\u043E\u0433\u043E \u0434\u043E\u0434\u0430\u044E\u0442\u044C\u0441\u044F \u0443\u0441\u0456 \u0444\u0430\u0439\u043B\u0438, \u0449\u043E \u043F\u0456\u0434\u0442\u0440\u0438\u043C\u0443\u044E\u0442\u044C\u0441\u044F.
tab2_Cb_foldersSelectorForRoms=\u0412\u0438\u0431\u0438\u0440\u0430\u0442\u0438 \u043F\u0430\u043F\u043A\u0443 \u0437 ROM \u0444\u0430\u0439\u043B\u0430\u043C\u0438 \u0437\u0430\u043C\u0456\u0441\u0442\u044C \u0432\u0438\u0431\u043E\u0440\u0443 \u0444\u0430\u0439\u043B\u0456\u0432 \u043F\u043E\u043E\u0434\u0438\u043D\u0446\u0456.
windowTitleAddingFiles=\u0428\u0443\u043A\u0430\u0454\u043C\u043E \u0444\u0430\u0439\u043B\u0438...
windowBodyFilesScanned=\u0424\u0430\u0439\u043B\u0456\u0432 \u043F\u0440\u043E\u0441\u043A\u0430\u043D\u043E\u0432\u0430\u043D\u043E: %d\n\u0417 \u044F\u043A\u0438\u0445 \u0431\u0443\u0434\u0435 \u0434\u043E\u0434\u0430\u043D\u043E: %d
6 changes: 5 additions & 1 deletion src/main/resources/res/app_dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
-fx-padding: 0.23em;
}

.progress-indicator {
-fx-progress-color: #00bce4;
}

.dialog-pane {
-fx-background-color: #4f4f4f;
}
Expand Down Expand Up @@ -131,7 +135,7 @@
}


// -======================== TAB PANE =========================-
/* -======================== TAB PANE =========================- */
.tab-pane .tab SVGPath{
-fx-fill: #f7fafa;
}
Expand Down
Loading

0 comments on commit d8ea426

Please sign in to comment.