Skip to content

Commit

Permalink
Merge pull request #63 from c-eg/settings-page
Browse files Browse the repository at this point in the history
Settings page
  • Loading branch information
c-eg authored May 5, 2023
2 parents c8834c8 + d011800 commit ef643dc
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ public class ShowRenamerApplication extends Application {
*
* @param args the ags.
*/
public static void main(String[] args) {
public static void main(final String[] args) {
launch(args);
}

/**
* @inheritDoc
*/
@Override
public void start(Stage primaryStage) throws Exception {
final Parent root = FXMLLoader.load(ShowRenamerApplication.class.getResource("/view/rename.fxml"));
final Scene scene = new Scene(root, WIDTH, HEIGHT);

public void start(final Stage primaryStage) throws Exception {
primaryStage.setMinWidth(WIDTH);
primaryStage.setMinHeight(HEIGHT);
primaryStage.setTitle("Show Renamer");

final Parent root = FXMLLoader.load(ShowRenamerApplication.class.getResource("/view/rename.fxml"));
final Scene scene = new Scene(root);
primaryStage.setScene(scene);

final Image appIcon = new Image(ShowRenamerApplication.class.getResourceAsStream("/images/show-renamer-icon.png"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ShowRenamerLauncher {
*
* @param args the args.
*/
public static void main(String[] args) {
public static void main(final String[] args) {
ShowRenamerApplication.main(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is part of ShowRenamer.
*
* ShowRenamer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ShowRenamer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ShowRenamer. If not, see <https://www.gnu.org/licenses/>.
*/

package uk.co.conoregan.showrenamer.controller;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.HashMap;

/**
* The navigation controller, to be extended by JavaFX controllers to navigate to other scenes (pages).
*/
public class NavigationController {
/**
* The path to the .fxml files.
*/
private static final String VIEW_PATH = "/view/";

/**
* The scene mapping cache.
*/
private static final HashMap<String, Scene> SCENE_MAP = new HashMap<>();

/**
* Changes the scene on the stage passed.
*
* @param fxml the fxml file to load, e.g. "rename".
* @param stage the stage to set the scene on.
* @throws IOException thrown if fxml file is not found.
*/
public static void changeScene(@Nonnull final String fxml, @Nonnull final Stage stage) throws IOException {
Scene scene = SCENE_MAP.get(fxml);

// load if not in map
if (scene == null) {
final Parent root = FXMLLoader.load(NavigationController.class.getResource(VIEW_PATH + fxml + ".fxml"));
scene = new Scene(root);
SCENE_MAP.put(fxml, scene);
}

// keep height and width from current scene.
final double currentHeight = stage.getHeight();
final double currentWidth = stage.getWidth();

stage.setScene(scene);
stage.setHeight(currentHeight);
stage.setWidth(currentWidth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListCell;
Expand All @@ -32,6 +34,7 @@
import javafx.scene.input.TransferMode;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -49,7 +52,7 @@
/**
* The JavaFX controller for the rename.fxml file.
*/
public class RenameController implements Initializable {
public class RenameController extends NavigationController implements Initializable {
/**
* The logger.
*/
Expand Down Expand Up @@ -98,12 +101,6 @@ public class RenameController implements Initializable {
@FXML
private CheckBox checkboxIncludeSubFolder;

/**
* Checkbox to improve folder names.
*/
@FXML
private CheckBox checkboxImproveFolderNames;

/**
* Button to get suggested file names.
*/
Expand All @@ -128,6 +125,18 @@ public class RenameController implements Initializable {
@FXML
private VBox vboxSuggestedTitles;

@FXML
private void navigateToRenamePage(final ActionEvent event) throws IOException {
final Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
changeScene("rename", stage);
}

@FXML
private void navigateToSettingsPage(final ActionEvent event) throws IOException {
final Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
changeScene("settings", stage);
}

/**
* Event to handle a files drag over event.
*
Expand All @@ -151,8 +160,7 @@ private void handleDragOverFileUpload(final DragEvent event) {
*/
@FXML
private void handleDragDroppedFileUpload(final DragEvent event) {
final Dragboard dragboard = event.getDragboard();
final List<File> dragboardFiles = dragboard.getFiles();
final List<File> dragboardFiles = event.getDragboard().getFiles();

for (final File item : dragboardFiles) {
addFile(item);
Expand All @@ -163,8 +171,8 @@ private void handleDragDroppedFileUpload(final DragEvent event) {
* Open file dialog to select files to be renamed.
*/
@FXML
private void openFileDialog() {
final Window window = checkboxIncludeSubFolder.getScene().getWindow();
private void openFileDialog(final ActionEvent event) {
final Window window = ((Button) event.getSource()).getScene().getWindow();
final File dir = directoryChooser.showDialog(window);

if (dir != null) {
Expand All @@ -177,7 +185,7 @@ private void openFileDialog() {
*/
@FXML
private void getSuggestions() {
for (Map.Entry<File, File> entry : fileRenameMapping.entrySet()) {
for (final Map.Entry<File, File> entry : fileRenameMapping.entrySet()) {
final String fileNameWithoutExtension = getFileNameWithoutExtension(entry.getKey().getName());

/*
Expand Down Expand Up @@ -211,7 +219,7 @@ private void clearAll() {
*/
@FXML
public void saveAll() {
for (Map.Entry<File, File> entry : fileRenameMapping.entrySet()) {
for (final Map.Entry<File, File> entry : fileRenameMapping.entrySet()) {
if (entry.getValue() == null) {
LOGGER.info(String.format("Cannot rename: %s, no suggestion found.", entry.getKey().getName()));
continue;
Expand All @@ -235,12 +243,11 @@ public void saveAll() {
* @inheritDoc
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
public void initialize(final URL url, final ResourceBundle resourceBundle) {
initializeConstructor();

// stops checkbox box resizing when clicking on and off other controls
checkboxIncludeSubFolder.setFocusTraversable(false);
checkboxImproveFolderNames.setFocusTraversable(false);

setListViewCellFactorySettings(listViewCurrentTitles);
setListViewCellFactorySettings(listViewSuggestedTitles);
Expand All @@ -251,7 +258,8 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
// update list views based on fileRenameMapping.
fileRenameMapping.addListener((MapChangeListener<File, File>) change -> {
enableSectionCurrentTitles = !fileRenameMapping.keySet().isEmpty();
enableSectionSuggestedTitles = !fileRenameMapping.values().isEmpty() && !fileRenameMapping.values().stream().allMatch(Objects::isNull);
enableSectionSuggestedTitles = !fileRenameMapping.values().isEmpty() &&
!fileRenameMapping.values().stream().allMatch(Objects::isNull);

vboxCurrentTitles.setDisable(!enableSectionCurrentTitles);
vboxSuggestedTitles.setDisable(!enableSectionSuggestedTitles);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* This file is part of ShowRenamer.
*
* ShowRenamer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ShowRenamer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ShowRenamer. If not, see <https://www.gnu.org/licenses/>.
*/

package uk.co.conoregan.showrenamer.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
* The JavaFX controller for the settings.fxml file.
*/
public class SettingsController extends NavigationController implements Initializable {
/**
* The logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(SettingsController.class);

private static final String SETTINGS_NAV_ACTIVE_CSS = "-fx-border-color: #3298e3;";

@FXML
private VBox vboxRenameFormat;

@FXML
private VBox vboxAbout;

@FXML
private Button buttonSettingsNavRenameFormat;

@FXML
private Button buttonSettingsNavAbout;

private Button buttonActiveSettingsNav;

@FXML
private void navigateToRenamePage(final ActionEvent event) throws IOException {
final Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
changeScene("rename", stage);
}

@FXML
private void navigateToSettingsPage(final ActionEvent event) throws IOException {
final Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
changeScene("settings", stage);
}

/**
* @inheritDoc
*/
@Override
public void initialize(final URL location, final ResourceBundle resources) {
buttonSettingsNavRenameFormat.setStyle(SETTINGS_NAV_ACTIVE_CSS);
buttonActiveSettingsNav = buttonSettingsNavRenameFormat;
}

@FXML
private void handleSettingsNavClick(final ActionEvent event) {
buttonActiveSettingsNav.setStyle(null);

buttonActiveSettingsNav = (Button) event.getSource();
buttonActiveSettingsNav.setStyle(SETTINGS_NAV_ACTIVE_CSS);

if (buttonActiveSettingsNav.equals(buttonSettingsNavRenameFormat)) {
vboxRenameFormat.toFront();
vboxRenameFormat.setVisible(true);
vboxAbout.setVisible(false);
} else if (buttonActiveSettingsNav.equals(buttonSettingsNavAbout)) {
vboxAbout.toFront();
vboxAbout.setVisible(true);
vboxRenameFormat.setVisible(false);
}
}
}
Loading

0 comments on commit ef643dc

Please sign in to comment.