looking through the whole hdd takes time, peform on background, update list on ui-thread
This commit is contained in:
parent
b2202cf483
commit
2936d65a31
1 changed files with 51 additions and 25 deletions
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package nsusbloader.Controllers;
|
package nsusbloader.Controllers;
|
||||||
|
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
@ -44,6 +45,8 @@ import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class GamesController implements Initializable {
|
public class GamesController implements Initializable {
|
||||||
|
|
||||||
|
@ -140,6 +143,7 @@ public class GamesController implements Initializable {
|
||||||
|
|
||||||
selectFolderBtn.setOnAction(e-> selectFoldersBtnAction());
|
selectFolderBtn.setOnAction(e-> selectFoldersBtnAction());
|
||||||
selectFolderBtn.getStyleClass().add("buttonSelect");
|
selectFolderBtn.getStyleClass().add("buttonSelect");
|
||||||
|
selectFolderBtn.setTooltip(new Tooltip("this is my tooltip"));
|
||||||
|
|
||||||
selectSplitNspBtn.setOnAction(e-> selectSplitBtnAction());
|
selectSplitNspBtn.setOnAction(e-> selectSplitBtnAction());
|
||||||
selectSplitNspBtn.getStyleClass().add("buttonSelect");
|
selectSplitNspBtn.getStyleClass().add("buttonSelect");
|
||||||
|
@ -263,16 +267,18 @@ public class GamesController implements Initializable {
|
||||||
chooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath)));
|
chooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath)));
|
||||||
|
|
||||||
File startFolder = chooser.showDialog(usbNetPane.getScene().getWindow());
|
File startFolder = chooser.showDialog(usbNetPane.getScene().getWindow());
|
||||||
if (startFolder != null) {
|
|
||||||
List<File> allFiles = new ArrayList<>();
|
|
||||||
collectFiles(allFiles, startFolder, getRegexForFiles());
|
|
||||||
|
|
||||||
if (!allFiles.isEmpty()) {
|
performInBackgroundAndUpdate(() -> {
|
||||||
tableFilesListController.setFiles(allFiles);
|
final List<File> allFiles = new ArrayList<>();
|
||||||
|
collectFiles(allFiles, startFolder, getRegexForFiles());
|
||||||
|
return allFiles;
|
||||||
|
}, (files) -> {
|
||||||
|
if (!files.isEmpty()) {
|
||||||
|
tableFilesListController.setFiles(files);
|
||||||
uploadStopBtn.setDisable(false);
|
uploadStopBtn.setDisable(false);
|
||||||
previouslyOpenedPath = startFolder.getParent();
|
previouslyOpenedPath = startFolder.getParent();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -284,6 +290,7 @@ public class GamesController implements Initializable {
|
||||||
private void collectFiles(List<File> storage, File startFolder, final String regex) {
|
private void collectFiles(List<File> storage, File startFolder, final String regex) {
|
||||||
if (startFolder.isDirectory()) {
|
if (startFolder.isDirectory()) {
|
||||||
File[] files = startFolder.listFiles();
|
File[] files = startFolder.listFiles();
|
||||||
|
if(files != null)
|
||||||
for (File f : files) {
|
for (File f : files) {
|
||||||
if (f.isDirectory()) {
|
if (f.isDirectory()) {
|
||||||
collectFiles(storage, f, regex);
|
collectFiles(storage, f, regex);
|
||||||
|
@ -406,23 +413,27 @@ public class GamesController implements Initializable {
|
||||||
* Drag-n-drop support (drop consumer)
|
* Drag-n-drop support (drop consumer)
|
||||||
* */
|
* */
|
||||||
@FXML
|
@FXML
|
||||||
private void handleDrop(DragEvent event){
|
private void handleDrop(DragEvent event) {
|
||||||
final String regex = getRegexForFiles();
|
final String regex = getRegexForFiles();
|
||||||
|
|
||||||
List<File> files = event.getDragboard().getFiles();
|
List<File> files = event.getDragboard().getFiles();
|
||||||
List<File> allFiles = new ArrayList<>();
|
|
||||||
|
|
||||||
if (files.size() != 0) {
|
performInBackgroundAndUpdate(() -> {
|
||||||
|
List<File> allFiles = new ArrayList<>();
|
||||||
|
if (files != null && files.size() != 0) {
|
||||||
files.stream().filter(File::isDirectory).forEach(f -> collectFiles(allFiles, f, regex));
|
files.stream().filter(File::isDirectory).forEach(f -> collectFiles(allFiles, f, regex));
|
||||||
files.stream().filter(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add);
|
files.stream().filter(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add);
|
||||||
}
|
}
|
||||||
|
return allFiles;
|
||||||
if ( ! allFiles.isEmpty() )
|
}, allFiles -> {
|
||||||
|
if (!allFiles.isEmpty())
|
||||||
tableFilesListController.setFiles(allFiles);
|
tableFilesListController.setFiles(allFiles);
|
||||||
|
|
||||||
event.setDropCompleted(true);
|
event.setDropCompleted(true);
|
||||||
event.consume();
|
event.consume();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission"
|
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission"
|
||||||
* Called from mediator
|
* Called from mediator
|
||||||
|
@ -464,6 +475,21 @@ public class GamesController implements Initializable {
|
||||||
else
|
else
|
||||||
uploadStopBtn.setDisable(false);
|
uploadStopBtn.setDisable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility function to perform a task in the background and pass the results to a task on the javafx-ui-thread
|
||||||
|
* @param background performed in background
|
||||||
|
* @param update performed with results on ui-thread
|
||||||
|
*/
|
||||||
|
private <T> void performInBackgroundAndUpdate(Supplier<T> background, Consumer<T> update) {
|
||||||
|
new Thread(() -> {
|
||||||
|
final T result = background.get();
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
update.accept(result);
|
||||||
|
});
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get 'Recent' path
|
* Get 'Recent' path
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue