Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…helf.git into blTool-ImportFile-test
  • Loading branch information
Stevoisiak committed Apr 26, 2017
2 parents 8137fe1 + 4e0cef3 commit 58b09f6
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 94 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<property name="dist.dir" location="dist"/>
<property name="resources.dir" location="resources"/>
<property name="externals.dir" location="externals"/>
<property name="main.class" value="virtualgameshelf.gui.VirtualGameShelf"/>
<property name="main.class" value="virtualgameshelf.gui.GameShelf"/>

<path id="classpath">
<fileset dir="${externals.dir}" includes="**/*.jar"/>
Expand Down
12 changes: 12 additions & 0 deletions resources/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-fx-border-width: 0;
-fx-background-radius: 0;
-fx-background-color: transparent;
-fx-alignment: center-right;
}

.menu-button > .arrow-button {
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -25,14 +29,21 @@
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") */
protected static BiMap<String, String> systemNameMap;
/** Visual display of {@link #gameList}. */
private static VBox gameListVBox;

// used when deleting games
static ArrayList<String> selectedGamesString = new ArrayList<>();
static Button deleteButton;

// used when editing games
static Button editButton;

public static void main(String[] args) {
launch(args);
}
Expand All @@ -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
Expand All @@ -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<ButtonType> 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<Game> tempGameList = (ArrayList<Game>) 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<ButtonType> 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.
* <p>
Expand Down Expand Up @@ -125,12 +244,13 @@ public MenuButton createAddGameButton() {
* Should be called whenever gameList has been modified.
*/
public static void refreshGameList() {
TreeItem<String> rootNode = new TreeItem<>("Consoles", new ImageView("icons/gamepad.png"));
CheckBoxTreeItem<String> rootNode = new CheckBoxTreeItem<>("Consoles", new ImageView("icons/gamepad.png"));
rootNode.setExpanded(true);

for (Game g : gameList.getGameList()) {
TreeItem<String> gameLeaf = new TreeItem<>(g.getName() + "\n" + g.getSystem() +
CheckBoxTreeItem<String> 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());
Expand All @@ -144,26 +264,57 @@ public static void refreshGameList() {
}

if (!found) {
TreeItem<String> depNode = new TreeItem<>(displayName, new ImageView("icons/vintage.png"));
CheckBoxTreeItem<String> depNode = new CheckBoxTreeItem<>(displayName, new ImageView("icons/vintage.png"));
rootNode.getChildren().add(depNode);
depNode.getChildren().add(gameLeaf);
}
}

TreeView<String> treeView = new TreeView<>(rootNode);
CheckTreeView<String> checkTreeView = new CheckTreeView<>(rootNode);

treeView.getSelectionModel().selectedItemProperty().addListener((ChangeListener<TreeItem<String>>) (observable, oldValue, newValue) -> {
TreeItem<String> selectedItem = newValue;
// and listen to the relevant events (e.g. when the checked items change).
checkTreeView.getCheckModel().getCheckedItems().addListener((ListChangeListener<TreeItem<String>>) c -> {
ObservableList<TreeItem<String>> 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<String> 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. */
Expand Down Expand Up @@ -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<String> 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<ButtonType> result = alert.showAndWait();
if (result.get() == deleteGame){
index = getGameIndex(selectedItem);
gameList.getGameList().remove(index);
refreshGameList();
}
else if (result.get() == editGame) {
index = getGameIndex(selectedItem);
ArrayList<Game> tempGameList = (ArrayList<Game>) 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<String> 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());
Expand Down
Loading

0 comments on commit 58b09f6

Please sign in to comment.