-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allows adding files and folders via filechooser and drag and drop #51 #82
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
503aa8c
allows adding files and folders via filechooser and drag and drop
wolfposd b2202cf
Reverting FileChooser and adding DirChooser
wolfposd 2936d65
looking through the whole hdd takes time, peform on background, updat…
wolfposd 112a0f8
tooltips for a great user experience =)
wolfposd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
*/ | ||
package nsusbloader.Controllers; | ||
|
||
import javafx.application.Platform; | ||
import javafx.collections.FXCollections; | ||
import javafx.collections.ObservableList; | ||
import javafx.fxml.FXML; | ||
|
@@ -40,11 +41,18 @@ | |
|
||
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.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
public class GamesController implements Initializable { | ||
|
||
private static final String REGEX_ONLY_NSP = ".*\\.nsp$"; | ||
private static final String REGEX_ALLFILES_TINFOIL = ".*\\.(nsp$|xci$|nsz$|xcz$)"; | ||
|
||
@FXML | ||
private AnchorPane usbNetPane; | ||
|
||
|
@@ -60,7 +68,7 @@ public class GamesController implements Initializable { | |
public NSTableViewController tableFilesListController; // Accessible from Mediator (for drag-n-drop support) | ||
|
||
@FXML | ||
private Button selectNspBtn, selectSplitNspBtn, uploadStopBtn; | ||
private Button selectNspBtn, selectSplitNspBtn, selectFolderBtn, uploadStopBtn; | ||
private String previouslyOpenedPath; | ||
private Region btnUpStopImage; | ||
private ResourceBundle resourceBundle; | ||
|
@@ -130,16 +138,18 @@ public void initialize(URL url, ResourceBundle resourceBundle) { | |
switchThemeBtn.setGraphic(btnSwitchImage); | ||
this.switchThemeBtn.setOnAction(e->switchTheme()); | ||
|
||
|
||
uploadStopBtn.setDisable(getSelectedProtocol().equals("TinFoil")); | ||
selectNspBtn.setOnAction(e-> selectFilesBtnAction()); | ||
selectNspBtn.getStyleClass().add("buttonSelect"); | ||
|
||
selectFolderBtn.setOnAction(e-> selectFoldersBtnAction()); | ||
selectFolderBtn.getStyleClass().add("buttonSelect"); | ||
selectFolderBtn.setTooltip(new Tooltip(resourceBundle.getString("btn_OpenFolders_tooltip"))); | ||
|
||
selectSplitNspBtn.setOnAction(e-> selectSplitBtnAction()); | ||
selectSplitNspBtn.getStyleClass().add("buttonSelect"); | ||
|
||
uploadStopBtn.setOnAction(e-> uploadBtnAction()); | ||
|
||
selectNspBtn.getStyleClass().add("buttonSelect"); | ||
uploadStopBtn.setDisable(getSelectedProtocol().equals("TinFoil")); | ||
|
||
this.btnUpStopImage = new Region(); | ||
btnUpStopImage.getStyleClass().add("regionUpload"); | ||
|
@@ -186,32 +196,111 @@ String getNsIp(){ | |
return nsIpTextField.getText(); | ||
} | ||
|
||
private boolean isGoldLeaf() { | ||
return getSelectedProtocol().equals("GoldLeaf"); | ||
} | ||
|
||
private boolean isTinfoil() { | ||
return getSelectedProtocol().equals("TinFoil"); | ||
} | ||
|
||
private boolean isNSPFileFilterForGL() { | ||
return MediatorControl.getInstance().getContoller().getSettingsCtrlr().getGoldleafSettings().getNSPFileFilterForGL(); | ||
} | ||
|
||
private boolean isXciNszXczSupport() { | ||
return MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTinfoilSettings().isXciNszXczSupport(); | ||
} | ||
|
||
/** | ||
* regex for selected program and selected file filter </br> | ||
* tinfoil + xcinszxcz </br> | ||
* tinfoil + nsponly </br> | ||
* goldleaf </br> | ||
* etc.. | ||
*/ | ||
private String getRegexForFiles() { | ||
if (isTinfoil() && isXciNszXczSupport()) | ||
return REGEX_ALLFILES_TINFOIL; | ||
else | ||
return REGEX_ONLY_NSP; | ||
// currently only tinfoil supports all filetypes | ||
// everything else only supports nsp | ||
// else if (isGoldLeaf()) | ||
// return REGEX_ONLY_NSP; | ||
// else | ||
} | ||
|
||
/** | ||
* Functionality for selecting NSP button. | ||
* */ | ||
private void selectFilesBtnAction(){ | ||
List<File> filesList; | ||
*/ | ||
private void selectFilesBtnAction() { | ||
FileChooser fileChooser = new FileChooser(); | ||
fileChooser.setTitle(resourceBundle.getString("btn_OpenFile")); | ||
|
||
fileChooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath))); | ||
|
||
if (getSelectedProtocol().equals("TinFoil") && MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTinfoilSettings().isXciNszXczSupport()) | ||
if (isTinfoil() && isXciNszXczSupport()) { | ||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP/XCI/NSZ/XCZ", "*.nsp", "*.xci", "*.nsz", "*.xcz")); | ||
else if (getSelectedProtocol().equals("GoldLeaf") && (! MediatorControl.getInstance().getContoller().getSettingsCtrlr().getGoldleafSettings().getNSPFileFilterForGL())) | ||
} else if (isGoldLeaf() && !isNSPFileFilterForGL()) { | ||
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Any file", "*.*"), | ||
new FileChooser.ExtensionFilter("NSP ROM", "*.nsp") | ||
); | ||
else | ||
new FileChooser.ExtensionFilter("NSP ROM", "*.nsp")); | ||
} else { | ||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP ROM", "*.nsp")); | ||
} | ||
|
||
filesList = fileChooser.showOpenMultipleDialog(usbNetPane.getScene().getWindow()); | ||
List<File> filesList = fileChooser.showOpenMultipleDialog(usbNetPane.getScene().getWindow()); | ||
if (filesList != null && !filesList.isEmpty()) { | ||
tableFilesListController.setFiles(filesList); | ||
uploadStopBtn.setDisable(false); | ||
previouslyOpenedPath = filesList.get(0).getParent(); | ||
} | ||
} | ||
|
||
/** | ||
* Functionality for selecting folders button. | ||
* will scan all folders recursively for nsp-files | ||
*/ | ||
private void selectFoldersBtnAction() { | ||
DirectoryChooser chooser = new DirectoryChooser(); | ||
chooser.setTitle(resourceBundle.getString("btn_OpenFolders")); | ||
chooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath))); | ||
|
||
File startFolder = chooser.showDialog(usbNetPane.getScene().getWindow()); | ||
|
||
performInBackgroundAndUpdate(() -> { | ||
final List<File> allFiles = new ArrayList<>(); | ||
collectFiles(allFiles, startFolder, getRegexForFiles()); | ||
return allFiles; | ||
}, (files) -> { | ||
if (!files.isEmpty()) { | ||
tableFilesListController.setFiles(files); | ||
uploadStopBtn.setDisable(false); | ||
previouslyOpenedPath = startFolder.getParent(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* used to recursively walk all directories, every file will be added to the storage list | ||
* @param storage used to hold files | ||
* @param startFolder where to start | ||
* @param regex for filenames | ||
*/ | ||
private void collectFiles(List<File> storage, File startFolder, final String regex) { | ||
if (startFolder.isDirectory()) { | ||
File[] files = startFolder.listFiles(); | ||
if(files != null) | ||
for (File f : files) { | ||
if (f.isDirectory()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To handle split-files should be |
||
collectFiles(storage, f, regex); | ||
} else if (f.getName().toLowerCase().matches(regex)) { | ||
storage.add(f); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Functionality for selecting Split NSP button. | ||
* */ | ||
|
@@ -324,25 +413,27 @@ private void handleDragOver(DragEvent event){ | |
* Drag-n-drop support (drop consumer) | ||
* */ | ||
@FXML | ||
private void handleDrop(DragEvent event){ | ||
List<File> filesDropped = event.getDragboard().getFiles(); | ||
SettingsController settingsController = MediatorControl.getInstance().getContoller().getSettingsCtrlr(); | ||
SettingsBlockTinfoilController tinfoilSettings = settingsController.getTinfoilSettings(); | ||
SettingsBlockGoldleafController goldleafController = settingsController.getGoldleafSettings(); | ||
|
||
if (getSelectedProtocol().equals("TinFoil") && tinfoilSettings.isXciNszXczSupport()) | ||
filesDropped.removeIf(file -> ! file.getName().toLowerCase().matches("(.*\\.nsp$)|(.*\\.xci$)|(.*\\.nsz$)|(.*\\.xcz$)")); | ||
else if (getSelectedProtocol().equals("GoldLeaf") && (! goldleafController.getNSPFileFilterForGL())) | ||
filesDropped.removeIf(file -> (file.isDirectory() && ! file.getName().toLowerCase().matches(".*\\.nsp$"))); | ||
else | ||
filesDropped.removeIf(file -> ! file.getName().toLowerCase().matches(".*\\.nsp$")); | ||
private void handleDrop(DragEvent event) { | ||
final String regex = getRegexForFiles(); | ||
|
||
if ( ! filesDropped.isEmpty() ) | ||
tableFilesListController.setFiles(filesDropped); | ||
List<File> files = event.getDragboard().getFiles(); | ||
|
||
event.setDropCompleted(true); | ||
event.consume(); | ||
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(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add); | ||
} | ||
return allFiles; | ||
}, allFiles -> { | ||
if (!allFiles.isEmpty()) | ||
tableFilesListController.setFiles(allFiles); | ||
|
||
event.setDropCompleted(true); | ||
event.consume(); | ||
}); | ||
} | ||
|
||
/** | ||
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission" | ||
* Called from mediator | ||
|
@@ -384,6 +475,21 @@ public void disableUploadStopBtn(boolean disable){ | |
else | ||
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 | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To handle #33 should be: