Skip to content
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

Added folder chooser GUI #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/main/java/backend/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> fileTypes) {
resetVariables();

readDirectory = new File(command.getReadDirectory());
storage = new Storage(command.getSaveDirectory());
boolean willScanCurrentDirOnly = command.willScanCurrentDirOnly();
ArrayList<String> 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.
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/gui/FolderChooserController.java
Original file line number Diff line number Diff line change
@@ -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<String> getFileTypes() {
String fileTypesText = fileTypes.getText();
return (fileTypesText.equals(STRING_EMPTY))
? new ArrayList<String>()
: new ArrayList<String>(Arrays.asList(fileTypesText.split(FILE_TYPES_DELIMITER)));
}
}
13 changes: 13 additions & 0 deletions src/main/java/gui/MainApp.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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<String> fileTypes) {
logic.handleCollate(inputFolder, outputFolder, willScanCurrentDirOnly, fileTypes);
commandBarController.setFeedback(FEEDBACK_COLLATE_SUCCESSFUL);
addSummary(this);
}
}
39 changes: 39 additions & 0 deletions src/main/resources/layouts/FolderChooser.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<fx:root maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
prefHeight="10.0" prefWidth="793.0" type="VBox"
xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="inputFolderButton" alignment="CENTER"
contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
mnemonicParsing="false" onAction="#onInputFolderClick" text="Click here to select input folder" />
<Button fx:id="outputFolderButton" alignment="CENTER"
contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
mnemonicParsing="false" onAction="#onOutputFolderClick"
text="Click here to select output folder (optional)" />
<HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<children>
<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
prefWidth="730.0">
<children>
<ToggleButton fx:id="omitSubFolderToggle" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" mnemonicParsing="false"
onAction="#onOmitSubFolderToggle" />
<TextField fx:id="fileTypes" alignment="CENTER"
maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
promptText="File types (separate by comma/space), leave blank to include all" />
</children>
</VBox>
<Button alignment="CENTER" maxHeight="1.7976931348623157E308"
mnemonicParsing="false" onMouseClicked="#onCollateButtonClick"
prefWidth="221.0" text="Collate" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</fx:root>