From 74a20ccf6faed888ea2a36f7dba7135c4dcc6257 Mon Sep 17 00:00:00 2001 From: riwu Date: Sun, 11 Dec 2016 10:36:52 +0800 Subject: [PATCH] Added folder chooser GUI --- src/main/java/backend/Logic.java | 15 ++- .../java/gui/FolderChooserController.java | 121 ++++++++++++++++++ src/main/java/gui/MainApp.java | 13 ++ src/main/resources/layouts/FolderChooser.fxml | 39 ++++++ 4 files changed, 182 insertions(+), 6 deletions(-) create mode 100644 src/main/java/gui/FolderChooserController.java create mode 100644 src/main/resources/layouts/FolderChooser.fxml diff --git a/src/main/java/backend/Logic.java b/src/main/java/backend/Logic.java index e36de38..2fda602 100644 --- a/src/main/java/backend/Logic.java +++ b/src/main/java/backend/Logic.java @@ -72,18 +72,21 @@ public Command.Type executeCommand(String userInput) { // "Collate" command methods // ================================================================ - private void handleCollate(Command command) { + public void handleCollate(File readDirectory, String saveDirectory, + boolean willScanCurrentDirOnly, ArrayList fileTypes) { resetVariables(); - readDirectory = new File(command.getReadDirectory()); - storage = new Storage(command.getSaveDirectory()); - boolean willScanCurrentDirOnly = command.willScanCurrentDirOnly(); - ArrayList fileTypes = command.getFileTypes(); - + this.readDirectory = readDirectory; + this.storage = new Storage(saveDirectory); traverseDirectory(readDirectory, willScanCurrentDirOnly, fileTypes); saveCollatedFiles(); } + private void handleCollate(Command command) { + handleCollate(new File(command.getReadDirectory()), command.getSaveDirectory(), + command.willScanCurrentDirOnly(), command.getFileTypes()); + } + /** * Resets variables so that "collate" commands will not show data from * previously entered "collate" commands. diff --git a/src/main/java/gui/FolderChooserController.java b/src/main/java/gui/FolderChooserController.java new file mode 100644 index 0000000..638647c --- /dev/null +++ b/src/main/java/gui/FolderChooserController.java @@ -0,0 +1,121 @@ +package main.java.gui; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.control.ToggleButton; +import javafx.scene.layout.VBox; +import javafx.stage.DirectoryChooser; +import javafx.stage.Stage; + +/** + * This class handles the Folder Chooser GUI. + * + * @@author Riwu + * + */ +public class FolderChooserController extends VBox { + + @FXML + private Button inputFolderButton; + + @FXML + private Button outputFolderButton; + + @FXML + private ToggleButton omitSubFolderToggle; + + @FXML + private TextField fileTypes; + + private static final String FOLDER_CHOOSER_FXML = "/main/resources/layouts/FolderChooser.fxml"; + private static final String WINDOW_TITLE = "Collate - Folder chooser"; + private static final String CLICK_TO_OMIT_SUB_FOLDER = "Click to omit sub folders"; + private static final String CLICK_TO_INCLUDE_SUB_FOLDER = "Click to include sub folders"; + + private static final String FILE_TYPES_DELIMITER = "[,\\s]+"; + private static final String STRING_EMPTY = ""; + + private File inputFolder; + private File outputFolder; + + private boolean omitSubFolder = false; + + private Stage folderChooserStage; + private MainApp mainApp; + + public FolderChooserController(MainApp mainApp) { + this.mainApp = mainApp; + + FXMLLoader loader = new FXMLLoader(getClass().getResource(FOLDER_CHOOSER_FXML)); + loader.setController(this); + loader.setRoot(this); + + folderChooserStage = new Stage(); + folderChooserStage.setTitle(WINDOW_TITLE); + try { + folderChooserStage.setScene(new Scene(loader.load())); + folderChooserStage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + omitSubFolderToggle.setText(CLICK_TO_OMIT_SUB_FOLDER); + } + + private File getSelectedFolder(Button button) { + File file = new DirectoryChooser().showDialog(folderChooserStage); + if (file != null) { + button.setText(file.toString()); + } + return file; + } + + @FXML + private void onInputFolderClick() { + File file = getSelectedFolder(inputFolderButton); + if (file != null) { + inputFolder = file; + } + } + + @FXML + private void onOutputFolderClick() { + File file = getSelectedFolder(outputFolderButton); + if (file != null) { + outputFolder = file; + } + } + + @FXML + private void onOmitSubFolderToggle() { + omitSubFolder = !omitSubFolder; + omitSubFolderToggle.setText(omitSubFolder ? CLICK_TO_INCLUDE_SUB_FOLDER : CLICK_TO_OMIT_SUB_FOLDER); + } + + @FXML + private void onCollateButtonClick() { + if (inputFolder == null) { + return; + } + folderChooserStage.close(); + + String saveDirectory = (outputFolder == null) ? STRING_EMPTY : outputFolder.toString(); + + mainApp.handleCollateButtonClick(inputFolder, new File(saveDirectory).getAbsolutePath(), + omitSubFolder, getFileTypes()); + } + + private ArrayList getFileTypes() { + String fileTypesText = fileTypes.getText(); + return (fileTypesText.equals(STRING_EMPTY)) + ? new ArrayList() + : new ArrayList(Arrays.asList(fileTypesText.split(FILE_TYPES_DELIMITER))); + } +} diff --git a/src/main/java/gui/MainApp.java b/src/main/java/gui/MainApp.java index 2b19af0..f026399 100644 --- a/src/main/java/gui/MainApp.java +++ b/src/main/java/gui/MainApp.java @@ -1,6 +1,8 @@ package main.java.gui; +import java.io.File; import java.io.IOException; +import java.util.ArrayList; import javafx.application.Application; import javafx.fxml.FXMLLoader; @@ -25,6 +27,8 @@ public class MainApp extends Application { private static final String FEEDBACK_EMPTY = ""; private static final String FEEDBACK_INVALID_COMMAND = "Invalid command."; + private static final KeyCode FOLDER_CHOOSER_HOTKEY = KeyCode.F1; + private Stage primaryStage; private BorderPane rootLayout; @@ -98,6 +102,8 @@ public void handleKeyPress(KeyCode key, String userInput) { handleEnterPress(userInput); } else if (key == KeyCode.ESCAPE) { addSummary(this); + } else if (key == FOLDER_CHOOSER_HOTKEY) { + new FolderChooserController(this); } } @@ -135,4 +141,11 @@ public void handleMouseClick(AuthorBean selectedAuthor) { commandBarController.setFeedback(FEEDBACK_EMPTY); addFileStats(authorName); } + + public void handleCollateButtonClick(File inputFolder, String outputFolder, + boolean willScanCurrentDirOnly, ArrayList fileTypes) { + logic.handleCollate(inputFolder, outputFolder, willScanCurrentDirOnly, fileTypes); + commandBarController.setFeedback(FEEDBACK_COLLATE_SUCCESSFUL); + addSummary(this); + } } \ No newline at end of file diff --git a/src/main/resources/layouts/FolderChooser.fxml b/src/main/resources/layouts/FolderChooser.fxml new file mode 100644 index 0000000..f340211 --- /dev/null +++ b/src/main/resources/layouts/FolderChooser.fxml @@ -0,0 +1,39 @@ + + + + + + + + + + +