From cb775fea57275088616a342e881f7d7375dbcb81 Mon Sep 17 00:00:00 2001 From: Steven Vascellaro Date: Tue, 18 Apr 2017 11:28:11 -0400 Subject: [PATCH 1/2] Rename VirtualGameShelf.java to GameShelf.java (#110) Shorter name to make code references more convenient. --- build.xml | 2 +- .../gui/{VirtualGameShelf.java => GameShelf.java} | 2 +- src/virtualgameshelf/gui/MainMenuBar.java | 14 +++++++------- src/virtualgameshelf/gui/NewGameWindow.java | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) rename src/virtualgameshelf/gui/{VirtualGameShelf.java => GameShelf.java} (99%) diff --git a/build.xml b/build.xml index 21fc873..9b9118e 100644 --- a/build.xml +++ b/build.xml @@ -8,7 +8,7 @@ - + diff --git a/src/virtualgameshelf/gui/VirtualGameShelf.java b/src/virtualgameshelf/gui/GameShelf.java similarity index 99% rename from src/virtualgameshelf/gui/VirtualGameShelf.java rename to src/virtualgameshelf/gui/GameShelf.java index c2c76ac..adff1a5 100644 --- a/src/virtualgameshelf/gui/VirtualGameShelf.java +++ b/src/virtualgameshelf/gui/GameShelf.java @@ -25,7 +25,7 @@ import virtualgameshelf.backend.domain.Game; import virtualgameshelf.backend.domain.GameList; -public class VirtualGameShelf extends Application { +public class GameShelf extends Application { /** User's complete list of games. Static to allow for global access. */ public static GameList gameList; /** Used to look up full names of consoles. ("PS4" - "PlayStation 4") */ diff --git a/src/virtualgameshelf/gui/MainMenuBar.java b/src/virtualgameshelf/gui/MainMenuBar.java index e68b02e..aef8674 100644 --- a/src/virtualgameshelf/gui/MainMenuBar.java +++ b/src/virtualgameshelf/gui/MainMenuBar.java @@ -92,7 +92,7 @@ public MainMenuBar(Stage mainStage) { /** Clears current gameList. */ public void onNew() { - VirtualGameShelf.setGameList(new GameList()); + GameShelf.setGameList(new GameList()); } /** Display prompt to load gameList from file. */ @@ -142,7 +142,7 @@ public void onOpen() { // Replace gameList if (loadSuccess) { - VirtualGameShelf.setGameList(new GameList((ArrayList) loadedGameList)); + GameShelf.setGameList(new GameList((ArrayList) loadedGameList)); } } @@ -155,7 +155,7 @@ public void onSave() { // TODO: Error handling for "File in use" when overwriting a file. // initial setup - String appPath = VirtualGameShelf.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + String appPath = GameShelf.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String gameListFileName = "game_list.csv"; File userDataDirectory = new File(appPath + userDataDirName); @@ -171,7 +171,7 @@ public void onSave() { CSVWriter csvWriter = new CSVWriter(new FileWriter(outFile), ','); String[] header = Game.getColumnHeaders(); csvWriter.writeNext(header); - for (Game g : VirtualGameShelf.gameList.getGameList()) { + for (Game g : GameShelf.gameList.getGameList()) { csvWriter.writeNext(g.toStringArray()); } csvWriter.close(); @@ -197,7 +197,7 @@ public void onSaveAs() { CSVWriter csvWriter = new CSVWriter(new FileWriter(outFile), ','); String[] header = Game.getColumnHeaders(); csvWriter.writeNext(header); - for (Game g : VirtualGameShelf.gameList.getGameList()) { + for (Game g : GameShelf.gameList.getGameList()) { csvWriter.writeNext(g.toStringArray()); } csvWriter.close(); @@ -228,7 +228,7 @@ public static void onAboutGameShelf() { /** Debug method to print all games from the current game list. */ public static void onPrintCurrentGameList() { - ArrayList gameList = VirtualGameShelf.gameList.getGameList(); + ArrayList gameList = GameShelf.gameList.getGameList(); System.out.println("Game List:"); if (gameList != null && !gameList.isEmpty()) { for (Game g : gameList) { @@ -283,7 +283,7 @@ public static void onImportExportSystemList() { */ private static void configureFileChooser(FileChooser fileChooser) { // set initial directory - String appPath = VirtualGameShelf.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + String appPath = GameShelf.class.getProtectionDomain().getCodeSource().getLocation().getPath(); File userDataDirectory = new File(appPath + userDataDirName); File userDirectory = new File(System.getProperty("user.dir")); diff --git a/src/virtualgameshelf/gui/NewGameWindow.java b/src/virtualgameshelf/gui/NewGameWindow.java index 3499206..cf51480 100644 --- a/src/virtualgameshelf/gui/NewGameWindow.java +++ b/src/virtualgameshelf/gui/NewGameWindow.java @@ -64,7 +64,7 @@ public NewGameWindow(Game game) { systemRow.setSpacing(16); systemRow.setAlignment( Pos.CENTER_LEFT ); systemChooser = new ComboBox<>(); - for (String value : VirtualGameShelf.systemNameMap.values()) { + for (String value : GameShelf.systemNameMap.values()) { systemChooser.getItems().add(value); // populate system list } systemChooser.setPromptText("Choose a System"); @@ -178,7 +178,7 @@ private void onClickAddGame() { // Retrieve game system if (systemChooser.getValue() != null && !systemChooser.getValue().trim().isEmpty() ) { String system = systemChooser.getValue().trim(); - system = VirtualGameShelf.getSystemShortName(system); + system = GameShelf.getSystemShortName(system); newGame.setSystem(system); } else { newGame.setSystem("Other"); From 4e0cef34d638d9b29bf668a496387b8b2ac42fa0 Mon Sep 17 00:00:00 2001 From: atab06 Date: Fri, 21 Apr 2017 20:11:43 -0400 Subject: [PATCH 2/2] Selectable checkboxes for editing and deleting games (#108) * Changed deleting and editing game to use CheckTreeView. * Added global buttons to allow them to be used from multiple methods/functions. * Moved some padding, spacing, and alignment code to css. * Added a "footer" section to hold the edit button, delete button, and menu button. * Added functions/methods are at the top and can be moved. They don't have proper comment blocks for JavaDoc use. * getGameIndex(TreeItem selectedItem) was changed to getGameIndex(String selectedGame). --- resources/stylesheet.css | 12 ++ src/virtualgameshelf/gui/GameShelf.java | 252 ++++++++++++++++-------- 2 files changed, 181 insertions(+), 83 deletions(-) diff --git a/resources/stylesheet.css b/resources/stylesheet.css index fa69874..64ba53c 100644 --- a/resources/stylesheet.css +++ b/resources/stylesheet.css @@ -3,6 +3,7 @@ -fx-border-width: 0; -fx-background-radius: 0; -fx-background-color: transparent; + -fx-alignment: center-right; } .menu-button > .arrow-button { @@ -16,3 +17,14 @@ .tree-view { -fx-font-size: 15px; } + +.gameVBox { + -fx-padding: 16; + -fx-alignment: center; +} + +.footer { + -fx-padding: 16; + -fx-spacing: 30; + -fx-alignment: center; +} diff --git a/src/virtualgameshelf/gui/GameShelf.java b/src/virtualgameshelf/gui/GameShelf.java index adff1a5..980b164 100644 --- a/src/virtualgameshelf/gui/GameShelf.java +++ b/src/virtualgameshelf/gui/GameShelf.java @@ -8,12 +8,16 @@ import java.util.List; import java.util.Optional; +import org.controlsfx.control.CheckTreeView; + import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.opencsv.CSVReader; import javafx.application.*; import javafx.beans.value.*; +import javafx.collections.ListChangeListener; +import javafx.collections.ObservableList; import javafx.geometry.*; import javafx.scene.*; import javafx.scene.control.*; @@ -33,6 +37,13 @@ public class GameShelf extends Application { /** Visual display of {@link #gameList}. */ private static VBox gameListVBox; + // used when deleting games + static ArrayList selectedGamesString = new ArrayList<>(); + static Button deleteButton; + + // used when editing games + static Button editButton; + public static void main(String[] args) { launch(args); } @@ -52,6 +63,7 @@ public void start(Stage mainStage) throws Exception { // used to add a scroll bar to the page ScrollPane scroll = new ScrollPane(); scroll.setFitToWidth(true); + scroll.setFitToHeight(true); root.setCenter(scroll); // add stylesheet @@ -66,21 +78,128 @@ public void start(Stage mainStage) throws Exception { // used to display games in library gameListVBox = new VBox(); - gameListVBox.setPadding( new Insets(16) ); - gameListVBox.setSpacing(16); - gameListVBox.setAlignment( Pos.CENTER ); + gameListVBox.getStyleClass().add("gameVBox"); scroll.setContent(gameListVBox); refreshGameList(); + // used to display the delete button and add game button + HBox footer = new HBox(); + footer.getStyleClass().add("footer"); + + deleteButton = createDeleteButton(); + deleteButton.setAlignment(Pos.CENTER_LEFT); + deleteButton.setDisable(true); + + editButton = createEditButton(); + editButton.setAlignment(Pos.CENTER_LEFT); + editButton.setDisable(true); + // used to add games to the library MenuButton addGameButton = createAddGameButton(); - root.setMargin(addGameButton, new Insets(16)); - root.setBottom(addGameButton); - root.setAlignment(addGameButton, Pos.CENTER_RIGHT); + addGameButton.setAlignment(Pos.CENTER_RIGHT); + + footer.getChildren().addAll(deleteButton, editButton, addGameButton); + root.setBottom(footer); mainStage.show(); } + // creates button for deleting games + public Button createDeleteButton() { + Button deleteButton = new Button("Delete Game(s)"); + + deleteButton.setOnAction(e -> displayDeleteGameAlert()); + + return deleteButton; + } + + // creates button for editing games + public Button createEditButton() { + Button editButton = new Button("Edit Game"); + + editButton.setOnAction(e -> displayEditGameAlert()); + + return editButton; + } + + // creates alert for deleting games + public static void displayDeleteGameAlert() { + int index = -1; + + Alert alert = new Alert(AlertType.CONFIRMATION); + alert.setHeaderText(null); + alert.setContentText("Are you sure you want to delete the selected games?"); + + ButtonType deleteGame = new ButtonType("Delete Game(s)"); + ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE); + + alert.getButtonTypes().setAll(deleteGame, buttonTypeCancel); + + Optional result = alert.showAndWait(); + if (result.get() == deleteGame){ + for (String g : selectedGamesString) { + index = getGameIndex(g); + gameList.getGameList().remove(index); + } + + refreshGameList(); + deleteButton.setDisable(true); + } + else { + // ... user chose CANCEL or closed the dialog + } + } + + // creates alert for editing games + public static void displayEditGameAlert() { + int index = getGameIndex(selectedGamesString.get(0)); + ArrayList tempGameList = (ArrayList) gameList.getGameList().clone(); + + Alert alert = new Alert(AlertType.CONFIRMATION); + alert.setTitle("" + tempGameList.get(index).getName()); + alert.setHeaderText(null); + alert.setContentText("Would you like to edit the game " + tempGameList.get(index).getName() + "?"); + + ButtonType editGame = new ButtonType("Edit Game"); + ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE); + + alert.getButtonTypes().setAll(editGame, buttonTypeCancel); + + Optional result = alert.showAndWait(); + if (result.get() == editGame) { + NewGameWindow newGameWindow = new NewGameWindow(tempGameList.get(index) ); + Game newGame = newGameWindow.showAndAddGame(); + if (newGame != null) { + // Add title to game list + gameList.getGameList().remove(index); + gameList.addGame(newGame); + refreshGameList(); + editButton.setDisable(true); + deleteButton.setDisable(true); + } + } + else { + // ... user chose CANCEL or closed the dialog + } + } + + // Takes String and returns its location in the game list as an int + public static int getGameIndex(String selectedGame) { + int index = -1; + + for (Game g : gameList.getGameList()) { + if (selectedGame.equals(g.gameString())) { + for (int i = 0; i < gameList.getGameList().size(); i++) { + if (g.gameString().equals(gameList.getGameList().get(i).gameString())) { + index = i; + } + } + } + } + + return index; + } + /** * Creates button for adding new games. *

@@ -125,12 +244,13 @@ public MenuButton createAddGameButton() { * Should be called whenever gameList has been modified. */ public static void refreshGameList() { - TreeItem rootNode = new TreeItem<>("Consoles", new ImageView("icons/gamepad.png")); + CheckBoxTreeItem rootNode = new CheckBoxTreeItem<>("Consoles", new ImageView("icons/gamepad.png")); rootNode.setExpanded(true); for (Game g : gameList.getGameList()) { - TreeItem gameLeaf = new TreeItem<>(g.getName() + "\n" + g.getSystem() + + CheckBoxTreeItem gameLeaf = new CheckBoxTreeItem<>(g.getName() + "\n" + g.getSystem() + "\n" + g.getCompletion() + "\n" + g.getHours() + " hours played \n" + g.getRating() + " star(s)"); + gameLeaf.setIndependent(true); boolean found = false; String displayName = getSystemDisplayName(g.getSystem()); @@ -144,26 +264,57 @@ public static void refreshGameList() { } if (!found) { - TreeItem depNode = new TreeItem<>(displayName, new ImageView("icons/vintage.png")); + CheckBoxTreeItem depNode = new CheckBoxTreeItem<>(displayName, new ImageView("icons/vintage.png")); rootNode.getChildren().add(depNode); depNode.getChildren().add(gameLeaf); } } - TreeView treeView = new TreeView<>(rootNode); + CheckTreeView checkTreeView = new CheckTreeView<>(rootNode); - treeView.getSelectionModel().selectedItemProperty().addListener((ChangeListener>) (observable, oldValue, newValue) -> { - TreeItem selectedItem = newValue; + // and listen to the relevant events (e.g. when the checked items change). + checkTreeView.getCheckModel().getCheckedItems().addListener((ListChangeListener>) c -> { + ObservableList> selectedGames = checkTreeView.getCheckModel().getCheckedItems(); - // Ensures 'edit game' prompt only shows for games - if (selectedItem.isLeaf() && selectedItem.getParent() != null) { - displayEditGameAlert(selectedItem); - } - }); + if (selectedGames.size() > 0) { + selectedGamesString.clear(); + + if (selectedGames.size() == 1 && selectedGames.get(0).isLeaf()) { + editButton.setDisable(false); + deleteButton.setDisable(false); + + selectedGamesString.add(selectedGames.get(0).getValue()); + } + else { + deleteButton.setDisable(false); + + for (TreeItem s : selectedGames) { + String singleGame = s.getValue(); + + // Ensures 'delete game' prompt only shows for games + if (s.isLeaf() && s.getParent() != null ) { + selectedGamesString.add(singleGame); + } + } + } + if (selectedGames.size() > 1) { + editButton.setDisable(true); + } + + if(selectedGames.get(0).getValue().equals("Consoles") && selectedGames.size() == 1){ + editButton.setDisable(true); + deleteButton.setDisable(true); + } + } + else { + deleteButton.setDisable(true); + editButton.setDisable(true); + } + }); // Clear and redraw game list gameListVBox.getChildren().clear(); - gameListVBox.getChildren().add(treeView); + gameListVBox.getChildren().add(checkTreeView); } /** Initialize {@link #systemNameMap} for looking up console names. */ @@ -232,71 +383,6 @@ public static String getSystemShortName(String systemLongName) { } } - /** - * Display option to edit or delete a game. - * - * @param selectedItem - * selected item from {@link #gameListVBox}. - */ - public static void displayEditGameAlert(TreeItem selectedItem) { - int index = -1; - - Alert alert = new Alert(AlertType.CONFIRMATION); - alert.setHeaderText(null); // TODO: Use game title for header - alert.setContentText("Would you like to:"); - - ButtonType deleteGame = new ButtonType("Delete Game"); - ButtonType editGame = new ButtonType("Edit Game"); - ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE); - - alert.getButtonTypes().setAll(deleteGame, editGame, buttonTypeCancel); - - Optional result = alert.showAndWait(); - if (result.get() == deleteGame){ - index = getGameIndex(selectedItem); - gameList.getGameList().remove(index); - refreshGameList(); - } - else if (result.get() == editGame) { - index = getGameIndex(selectedItem); - ArrayList tempGameList = (ArrayList) gameList.getGameList().clone(); - - NewGameWindow newGameWindow = new NewGameWindow(tempGameList.get(index) ); - Game newGame = newGameWindow.showAndAddGame(); - if (newGame != null) { - // Add title to game list - gameList.getGameList().remove(index); - gameList.addGame(newGame); - refreshGameList(); - } - } - else { - // ... user chose CANCEL or closed the dialog - } - } - - /** - * Takes TreeItem and returns its location in {@link #gameList} as an int. - * - * @param selectedItem - * item from {@link #gameListVBox} - */ - public static int getGameIndex(TreeItem selectedItem) { - int index = -1; - - for (Game g : gameList.getGameList()) { - if (selectedItem.getValue().equals(g.gameString())) { - for (int i = 0; i < gameList.getGameList().size(); i++) { - if (g.gameString().equals(gameList.getGameList().get(i).gameString())) { - index = i; - } - } - } - } - - return index; - } - public static void setGameList(GameList newGameList) { gameList = newGameList; Collections.sort(gameList.getGameList());