|
28 | 28 | import javafx.scene.layout.AnchorPane;
|
29 | 29 | import javafx.scene.layout.Region;
|
30 | 30 | import javafx.stage.DirectoryChooser;
|
31 |
| -import javafx.stage.FileChooser; |
32 | 31 | import nsusbloader.AppPreferences;
|
33 | 32 | import nsusbloader.com.net.NETCommunications;
|
34 | 33 | import nsusbloader.com.usb.UsbCommunications;
|
|
40 | 39 |
|
41 | 40 | import java.io.File;
|
42 | 41 | import java.net.URL;
|
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.Arrays; |
43 | 44 | import java.util.LinkedList;
|
44 | 45 | import java.util.List;
|
45 | 46 | import java.util.ResourceBundle;
|
46 | 47 |
|
| 48 | +import javax.swing.JFileChooser; |
| 49 | +import javax.swing.UIManager; |
| 50 | +import javax.swing.UnsupportedLookAndFeelException; |
| 51 | +import javax.swing.filechooser.FileFilter; |
| 52 | + |
47 | 53 | public class GamesController implements Initializable {
|
| 54 | + |
| 55 | + private static final String REGEX_ONLY_NSP = ".*\\.nsp$"; |
| 56 | + private static final String REGEX_ALLFILES_TINFOIL = ".*\\.(nsp$|xci$|nsz$|xcz$)"; |
| 57 | + |
48 | 58 | @FXML
|
49 | 59 | private AnchorPane usbNetPane;
|
50 | 60 |
|
@@ -186,32 +196,88 @@ String getNsIp(){
|
186 | 196 | return nsIpTextField.getText();
|
187 | 197 | }
|
188 | 198 |
|
| 199 | + |
| 200 | + private boolean isGoldLeaf() { |
| 201 | + return getSelectedProtocol().equals("GoldLeaf") |
| 202 | + && (!MediatorControl.getInstance().getContoller().getSettingsCtrlr().getGoldleafSettings().getNSPFileFilterForGL()); |
| 203 | + } |
| 204 | + |
| 205 | + private boolean isTinfoil() { |
| 206 | + return getSelectedProtocol().equals("TinFoil") |
| 207 | + && MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTinfoilSettings().isXciNszXczSupport(); |
| 208 | + } |
| 209 | + |
| 210 | + private String getRegexForFiles() { |
| 211 | + if (isTinfoil()) |
| 212 | + return REGEX_ALLFILES_TINFOIL; |
| 213 | + else if (isGoldLeaf()) |
| 214 | + return REGEX_ONLY_NSP; |
| 215 | + else |
| 216 | + return REGEX_ONLY_NSP; |
| 217 | + } |
| 218 | + |
189 | 219 | /**
|
190 | 220 | * Functionality for selecting NSP button.
|
191 | 221 | * */
|
192 | 222 | private void selectFilesBtnAction(){
|
193 |
| - List<File> filesList; |
194 |
| - FileChooser fileChooser = new FileChooser(); |
195 |
| - fileChooser.setTitle(resourceBundle.getString("btn_OpenFile")); |
196 |
| - |
197 |
| - fileChooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath))); |
198 |
| - |
199 |
| - if (getSelectedProtocol().equals("TinFoil") && MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTinfoilSettings().isXciNszXczSupport()) |
200 |
| - fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP/XCI/NSZ/XCZ", "*.nsp", "*.xci", "*.nsz", "*.xcz")); |
201 |
| - else if (getSelectedProtocol().equals("GoldLeaf") && (! MediatorControl.getInstance().getContoller().getSettingsCtrlr().getGoldleafSettings().getNSPFileFilterForGL())) |
202 |
| - fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Any file", "*.*"), |
203 |
| - new FileChooser.ExtensionFilter("NSP ROM", "*.nsp") |
204 |
| - ); |
205 |
| - else |
206 |
| - fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP ROM", "*.nsp")); |
| 223 | + final String regex = getRegexForFiles(); |
| 224 | + if(!UIManager.getLookAndFeel().isNativeLookAndFeel()) { |
| 225 | + try { |
| 226 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 227 | + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { |
| 228 | + // :shrug emoji: |
| 229 | + // defaults to Metal Look and Feel |
| 230 | + } |
| 231 | + } |
207 | 232 |
|
208 |
| - filesList = fileChooser.showOpenMultipleDialog(usbNetPane.getScene().getWindow()); |
209 |
| - if (filesList != null && !filesList.isEmpty()) { |
210 |
| - tableFilesListController.setFiles(filesList); |
211 |
| - uploadStopBtn.setDisable(false); |
212 |
| - previouslyOpenedPath = filesList.get(0).getParent(); |
| 233 | + JFileChooser fileChooser = new JFileChooser(new File(FilesHelper.getRealFolder(previouslyOpenedPath))); |
| 234 | + fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
| 235 | + fileChooser.setMultiSelectionEnabled(true); |
| 236 | + fileChooser.setFileFilter(new FileFilter() { |
| 237 | + public String getDescription() { |
| 238 | + return "Switch Files"; |
| 239 | + } |
| 240 | + public boolean accept(File f) { |
| 241 | + return f.isDirectory() || f.getName().toLowerCase().matches(regex); |
| 242 | + } |
| 243 | + }); |
| 244 | + |
| 245 | + if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
| 246 | + List<File> files = Arrays.asList(fileChooser.getSelectedFiles()); |
| 247 | + List<File> allFiles = new ArrayList<>(); |
| 248 | + |
| 249 | + if (files.size() != 0) { |
| 250 | + files.stream().filter(File::isDirectory).forEach(f -> collectFiles(allFiles, f, regex)); |
| 251 | + files.stream().filter(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add); |
| 252 | + } |
| 253 | + |
| 254 | + if (allFiles.size() > 0) { |
| 255 | + tableFilesListController.setFiles(allFiles); |
| 256 | + uploadStopBtn.setDisable(false); |
| 257 | + previouslyOpenedPath = allFiles.get(0).getParent(); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * used to recursively walk all directories, every file will be added to the storage list |
| 264 | + * @param storage used to hold files |
| 265 | + * @param startFolder where to start |
| 266 | + * @param regex for filenames |
| 267 | + */ |
| 268 | + private void collectFiles(List<File> storage, File startFolder, final String regex) { |
| 269 | + if (startFolder.isDirectory()) { |
| 270 | + File[] files = startFolder.listFiles(); |
| 271 | + for (File f : files) { |
| 272 | + if (f.isDirectory()) { |
| 273 | + collectFiles(storage, f, regex); |
| 274 | + } else if (f.getName().toLowerCase().matches(regex)) { |
| 275 | + storage.add(f); |
| 276 | + } |
| 277 | + } |
213 | 278 | }
|
214 | 279 | }
|
| 280 | + |
215 | 281 | /**
|
216 | 282 | * Functionality for selecting Split NSP button.
|
217 | 283 | * */
|
@@ -325,20 +391,18 @@ private void handleDragOver(DragEvent event){
|
325 | 391 | * */
|
326 | 392 | @FXML
|
327 | 393 | private void handleDrop(DragEvent event){
|
328 |
| - List<File> filesDropped = event.getDragboard().getFiles(); |
329 |
| - SettingsController settingsController = MediatorControl.getInstance().getContoller().getSettingsCtrlr(); |
330 |
| - SettingsBlockTinfoilController tinfoilSettings = settingsController.getTinfoilSettings(); |
331 |
| - SettingsBlockGoldleafController goldleafController = settingsController.getGoldleafSettings(); |
332 |
| - |
333 |
| - if (getSelectedProtocol().equals("TinFoil") && tinfoilSettings.isXciNszXczSupport()) |
334 |
| - filesDropped.removeIf(file -> ! file.getName().toLowerCase().matches("(.*\\.nsp$)|(.*\\.xci$)|(.*\\.nsz$)|(.*\\.xcz$)")); |
335 |
| - else if (getSelectedProtocol().equals("GoldLeaf") && (! goldleafController.getNSPFileFilterForGL())) |
336 |
| - filesDropped.removeIf(file -> (file.isDirectory() && ! file.getName().toLowerCase().matches(".*\\.nsp$"))); |
337 |
| - else |
338 |
| - filesDropped.removeIf(file -> ! file.getName().toLowerCase().matches(".*\\.nsp$")); |
339 |
| - |
340 |
| - if ( ! filesDropped.isEmpty() ) |
341 |
| - tableFilesListController.setFiles(filesDropped); |
| 394 | + final String regex = getRegexForFiles(); |
| 395 | + |
| 396 | + List<File> files = event.getDragboard().getFiles(); |
| 397 | + List<File> allFiles = new ArrayList<>(); |
| 398 | + |
| 399 | + if (files.size() != 0) { |
| 400 | + files.stream().filter(File::isDirectory).forEach(f -> collectFiles(allFiles, f, regex)); |
| 401 | + files.stream().filter(f -> f.getName().toLowerCase().matches(regex)).forEach(allFiles::add); |
| 402 | + } |
| 403 | + |
| 404 | + if ( ! allFiles.isEmpty() ) |
| 405 | + tableFilesListController.setFiles(allFiles); |
342 | 406 |
|
343 | 407 | event.setDropCompleted(true);
|
344 | 408 | event.consume();
|
|
0 commit comments