Skip to content

Commit 2936d65

Browse files
committed
looking through the whole hdd takes time, peform on background, update list on ui-thread
1 parent b2202cf commit 2936d65

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

src/main/java/nsusbloader/Controllers/GamesController.java

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package nsusbloader.Controllers;
2020

21+
import javafx.application.Platform;
2122
import javafx.collections.FXCollections;
2223
import javafx.collections.ObservableList;
2324
import javafx.fxml.FXML;
@@ -44,6 +45,8 @@
4445
import java.util.LinkedList;
4546
import java.util.List;
4647
import java.util.ResourceBundle;
48+
import java.util.function.Consumer;
49+
import java.util.function.Supplier;
4750

4851
public class GamesController implements Initializable {
4952

@@ -140,6 +143,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
140143

141144
selectFolderBtn.setOnAction(e-> selectFoldersBtnAction());
142145
selectFolderBtn.getStyleClass().add("buttonSelect");
146+
selectFolderBtn.setTooltip(new Tooltip("this is my tooltip"));
143147

144148
selectSplitNspBtn.setOnAction(e-> selectSplitBtnAction());
145149
selectSplitNspBtn.getStyleClass().add("buttonSelect");
@@ -263,18 +267,20 @@ private void selectFoldersBtnAction() {
263267
chooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath)));
264268

265269
File startFolder = chooser.showDialog(usbNetPane.getScene().getWindow());
266-
if (startFolder != null) {
267-
List<File> allFiles = new ArrayList<>();
270+
271+
performInBackgroundAndUpdate(() -> {
272+
final List<File> allFiles = new ArrayList<>();
268273
collectFiles(allFiles, startFolder, getRegexForFiles());
269-
270-
if (!allFiles.isEmpty()) {
271-
tableFilesListController.setFiles(allFiles);
274+
return allFiles;
275+
}, (files) -> {
276+
if (!files.isEmpty()) {
277+
tableFilesListController.setFiles(files);
272278
uploadStopBtn.setDisable(false);
273279
previouslyOpenedPath = startFolder.getParent();
274280
}
275-
}
281+
});
276282
}
277-
283+
278284
/**
279285
* used to recursively walk all directories, every file will be added to the storage list
280286
* @param storage used to hold files
@@ -284,13 +290,14 @@ private void selectFoldersBtnAction() {
284290
private void collectFiles(List<File> storage, File startFolder, final String regex) {
285291
if (startFolder.isDirectory()) {
286292
File[] files = startFolder.listFiles();
287-
for (File f : files) {
288-
if (f.isDirectory()) {
289-
collectFiles(storage, f, regex);
290-
} else if (f.getName().toLowerCase().matches(regex)) {
291-
storage.add(f);
293+
if(files != null)
294+
for (File f : files) {
295+
if (f.isDirectory()) {
296+
collectFiles(storage, f, regex);
297+
} else if (f.getName().toLowerCase().matches(regex)) {
298+
storage.add(f);
299+
}
292300
}
293-
}
294301
}
295302
}
296303

@@ -406,23 +413,27 @@ private void handleDragOver(DragEvent event){
406413
* Drag-n-drop support (drop consumer)
407414
* */
408415
@FXML
409-
private void handleDrop(DragEvent event){
416+
private void handleDrop(DragEvent event) {
410417
final String regex = getRegexForFiles();
411-
418+
412419
List<File> files = event.getDragboard().getFiles();
413-
List<File> allFiles = new ArrayList<>();
414420

415-
if (files.size() != 0) {
416-
files.stream().filter(File::isDirectory).forEach(f -> collectFiles(allFiles, f, regex));
417-
files.stream().filter(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add);
418-
}
419-
420-
if ( ! allFiles.isEmpty() )
421-
tableFilesListController.setFiles(allFiles);
421+
performInBackgroundAndUpdate(() -> {
422+
List<File> allFiles = new ArrayList<>();
423+
if (files != null && files.size() != 0) {
424+
files.stream().filter(File::isDirectory).forEach(f -> collectFiles(allFiles, f, regex));
425+
files.stream().filter(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add);
426+
}
427+
return allFiles;
428+
}, allFiles -> {
429+
if (!allFiles.isEmpty())
430+
tableFilesListController.setFiles(allFiles);
422431

423-
event.setDropCompleted(true);
424-
event.consume();
432+
event.setDropCompleted(true);
433+
event.consume();
434+
});
425435
}
436+
426437
/**
427438
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission"
428439
* Called from mediator
@@ -464,6 +475,21 @@ public void disableUploadStopBtn(boolean disable){
464475
else
465476
uploadStopBtn.setDisable(false);
466477
}
478+
479+
/**
480+
* Utility function to perform a task in the background and pass the results to a task on the javafx-ui-thread
481+
* @param background performed in background
482+
* @param update performed with results on ui-thread
483+
*/
484+
private <T> void performInBackgroundAndUpdate(Supplier<T> background, Consumer<T> update) {
485+
new Thread(() -> {
486+
final T result = background.get();
487+
Platform.runLater(() -> {
488+
update.accept(result);
489+
});
490+
}).start();
491+
}
492+
467493
/**
468494
* Get 'Recent' path
469495
*/

0 commit comments

Comments
 (0)