18
18
*/
19
19
package nsusbloader .Controllers ;
20
20
21
+ import javafx .application .Platform ;
21
22
import javafx .collections .FXCollections ;
22
23
import javafx .collections .ObservableList ;
23
24
import javafx .fxml .FXML ;
44
45
import java .util .LinkedList ;
45
46
import java .util .List ;
46
47
import java .util .ResourceBundle ;
48
+ import java .util .function .Consumer ;
49
+ import java .util .function .Supplier ;
47
50
48
51
public class GamesController implements Initializable {
49
52
@@ -140,6 +143,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
140
143
141
144
selectFolderBtn .setOnAction (e -> selectFoldersBtnAction ());
142
145
selectFolderBtn .getStyleClass ().add ("buttonSelect" );
146
+ selectFolderBtn .setTooltip (new Tooltip ("this is my tooltip" ));
143
147
144
148
selectSplitNspBtn .setOnAction (e -> selectSplitBtnAction ());
145
149
selectSplitNspBtn .getStyleClass ().add ("buttonSelect" );
@@ -263,18 +267,20 @@ private void selectFoldersBtnAction() {
263
267
chooser .setInitialDirectory (new File (FilesHelper .getRealFolder (previouslyOpenedPath )));
264
268
265
269
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 <>();
268
273
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 );
272
278
uploadStopBtn .setDisable (false );
273
279
previouslyOpenedPath = startFolder .getParent ();
274
280
}
275
- }
281
+ });
276
282
}
277
-
283
+
278
284
/**
279
285
* used to recursively walk all directories, every file will be added to the storage list
280
286
* @param storage used to hold files
@@ -284,13 +290,14 @@ private void selectFoldersBtnAction() {
284
290
private void collectFiles (List <File > storage , File startFolder , final String regex ) {
285
291
if (startFolder .isDirectory ()) {
286
292
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
+ }
292
300
}
293
- }
294
301
}
295
302
}
296
303
@@ -406,23 +413,27 @@ private void handleDragOver(DragEvent event){
406
413
* Drag-n-drop support (drop consumer)
407
414
* */
408
415
@ FXML
409
- private void handleDrop (DragEvent event ){
416
+ private void handleDrop (DragEvent event ) {
410
417
final String regex = getRegexForFiles ();
411
-
418
+
412
419
List <File > files = event .getDragboard ().getFiles ();
413
- List <File > allFiles = new ArrayList <>();
414
420
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 );
422
431
423
- event .setDropCompleted (true );
424
- event .consume ();
432
+ event .setDropCompleted (true );
433
+ event .consume ();
434
+ });
425
435
}
436
+
426
437
/**
427
438
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission"
428
439
* Called from mediator
@@ -464,6 +475,21 @@ public void disableUploadStopBtn(boolean disable){
464
475
else
465
476
uploadStopBtn .setDisable (false );
466
477
}
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
+
467
493
/**
468
494
* Get 'Recent' path
469
495
*/
0 commit comments