diff --git a/src/main/java/com/cemu_UI/application/Main.java b/src/main/java/com/cemu_UI/application/Main.java index 22e6fc4..d0a5c92 100644 --- a/src/main/java/com/cemu_UI/application/Main.java +++ b/src/main/java/com/cemu_UI/application/Main.java @@ -148,7 +148,6 @@ public class Main extends Application { // init here as it loads the games to the mwc and the gui, therefore the window must exist mainWindowController.init(); - mainWindowController.getDbController().init(); // if cloud sync is activated start sync if (XMLController.isCloudSync()) { diff --git a/src/main/java/com/cemu_UI/application/MainWindowController.java b/src/main/java/com/cemu_UI/application/MainWindowController.java index 7fca061..bcaa281 100644 --- a/src/main/java/com/cemu_UI/application/MainWindowController.java +++ b/src/main/java/com/cemu_UI/application/MainWindowController.java @@ -56,7 +56,7 @@ import com.cemu_UI.datatypes.CourseTableDataType; import com.cemu_UI.datatypes.GlobalDataTypes.CloudService; import com.cemu_UI.datatypes.SmmdbApiDataType; import com.cemu_UI.datatypes.UIROMDataType; -import com.cemu_UI.uiElements.JFXEditGameDialog; +import com.cemu_UI.uiElements.JFXEditGameAlert; import com.cemu_UI.uiElements.JFXInfoAlert; import com.cemu_UI.uiElements.JFXOkayCancelAlert; import com.cemu_UI.uiElements.JFXTextAreaAlert; @@ -187,7 +187,7 @@ public class MainWindowController { private boolean menuTrue = false; private boolean playTrue = false; private String gameExecutePath; - private String dialogBtnStyle; + private String btnStyle; private String selectedGameTitleID; private String selectedGameTitle; private String id; @@ -215,22 +215,8 @@ public class MainWindowController { private MenuItem addDLC = new MenuItem("add DLC"); private ContextMenu gameContextMenu = new ContextMenu(edit, remove, addUpdate, addDLC); private Label lastGameLabel = new Label(); - - private ImageView add_circle_black = new ImageView(new Image("icons/ic_add_circle_black_24dp_1x.png")); - private ImageView info_black = new ImageView(new Image("icons/ic_info_black_24dp_1x.png")); - private ImageView settings_black = new ImageView(new Image("icons/ic_settings_black_24dp_1x.png")); - private ImageView cached_black = new ImageView(new Image("icons/ic_cached_black_24dp_1x.png")); - private ImageView smmdb_black = new ImageView(new Image("icons/ic_get_app_black_24dp_1x.png")); - private ImageView add_circle_white = new ImageView(new Image("icons/ic_add_circle_white_24dp_1x.png")); - private ImageView info_white = new ImageView(new Image("icons/ic_info_white_24dp_1x.png")); - private ImageView settings_white = new ImageView(new Image("icons/ic_settings_white_24dp_1x.png")); - private ImageView cached_white = new ImageView(new Image("icons/ic_cached_white_24dp_1x.png")); - private ImageView smmdb_white = new ImageView(new Image("icons/ic_get_app_white_24dp_1x.png")); - private Image close_black = new Image("icons/close_black_2048x2048.png"); // language support - private ResourceBundle bundle; - private String language; private String lastPlayed; private String today; private String yesterday; @@ -239,7 +225,7 @@ public class MainWindowController { public MainWindowController(Main main) { xmlController = new XMLController(); this.main = main; - dbController = new DBController(this); + dbController = DBController.getInstance(); smmdbAPIController = new SmmdbAPIController(); } @@ -252,6 +238,11 @@ public class MainWindowController { checkAutoUpdate(); initUI(); initActions(); + + dbController.init(); + dbController.refreshDataBase(); + games = dbController.loadAllGames(); + addAllGames(); } /** @@ -351,14 +342,14 @@ public class MainWindowController { String[] gameInfo = dbController.getGameInfo(selectedGameTitleID); // new edit dialog - String headingText = bundle.getString("editHeadingText") + " \"" + selectedGameTitle + "\""; - JFXEditGameDialog editGameDialog = new JFXEditGameDialog(headingText, bundle.getString("editBodyText"), - dialogBtnStyle, 450, 300, 1, MWC, primaryStage, main.getPane()); - editGameDialog.setTitle(gameInfo[0]); - editGameDialog.setCoverPath(gameInfo[1]); - editGameDialog.setRomPath(gameInfo[2]); - editGameDialog.setTitleID(gameInfo[3]); - editGameDialog.show(); + String headingText = XMLController.getLocalBundle().getString("editHeadingText") + " \"" + selectedGameTitle + "\""; + JFXEditGameAlert editGameAlert = new JFXEditGameAlert(headingText, + XMLController.getLocalBundle().getString("editBodyText"), btnStyle, 1, primaryStage, MWC); + editGameAlert.setTitle(gameInfo[0]); + editGameAlert.setCoverPath(gameInfo[1]); + editGameAlert.setRomPath(gameInfo[2]); + editGameAlert.setTitleID(gameInfo[3]); + editGameAlert.showAndWait(); } catch (Exception e) { LOGGER.warn("trying to edit " + selectedGameTitleID + ",which is not a valid type!", e); } @@ -367,10 +358,9 @@ public class MainWindowController { remove.setOnAction(event -> { try { LOGGER.info("remove " + selectedGameTitle + "(" + selectedGameTitleID + ")"); - String headingText = bundle.getString("removeHeadingText") + " \"" + selectedGameTitle + "\""; - String bodyText = bundle.getString("removeBodyText") + " " + selectedGameTitle + " ?"; - - JFXOkayCancelAlert removeGameAlert = new JFXOkayCancelAlert(headingText, bodyText, dialogBtnStyle, + String headingText = XMLController.getLocalBundle().getString("removeHeadingText") + " \"" + selectedGameTitle + "\""; + String bodyText = XMLController.getLocalBundle().getString("removeBodyText") + " " + selectedGameTitle + " ?"; + JFXOkayCancelAlert removeGameAlert = new JFXOkayCancelAlert(headingText, bodyText, btnStyle, primaryStage); removeGameAlert.setOkayAction(e -> { try { @@ -411,10 +401,10 @@ public class MainWindowController { try { LOGGER.info("copying the content of " + updatePath + " to " + destDir.toString()); - playBtn.setText(bundle.getString("playBtnUpdating")); + playBtn.setText(XMLController.getLocalBundle().getString("playBtnUpdating")); playBtn.setDisable(true); FileUtils.copyDirectory(srcDir, destDir); - playBtn.setText(bundle.getString("playBtnPlay")); + playBtn.setText(XMLController.getLocalBundle().getString("playBtnPlay")); playBtn.setDisable(false); LOGGER.info("copying files done!"); } catch (IOException e) { @@ -430,9 +420,9 @@ public class MainWindowController { } }; - String headingText = bundle.getString("addUpdateHeadingText") + " \"" + selectedGameTitle + "\""; + String headingText = XMLController.getLocalBundle().getString("addUpdateHeadingText") + " \"" + selectedGameTitle + "\""; JFXOkayCancelAlert updateGameAlert = new JFXOkayCancelAlert(headingText, - bundle.getString("addUpdateBodyText"), dialogBtnStyle, primaryStage); + XMLController.getLocalBundle().getString("addUpdateBodyText"), btnStyle, primaryStage); updateGameAlert.setOkayAction(okayAction); updateGameAlert.setCancelAction(cancelAction); updateGameAlert.showAndWait(); @@ -447,7 +437,7 @@ public class MainWindowController { public void handle(ActionEvent event) { try { LOGGER.info("add DLC: " + selectedGameTitleID); - String headingText = bundle.getString("addDLCHeadingText") + " \"" + selectedGameTitle + "\""; + String headingText = XMLController.getLocalBundle().getString("addDLCHeadingText") + " \"" + selectedGameTitle + "\""; EventHandler okayAction = new EventHandler() { @Override public void handle(ActionEvent event) { @@ -465,10 +455,10 @@ public class MainWindowController { try { LOGGER.info("copying the content of " + dlcPath + " to " + destDir.toString()); - playBtn.setText(bundle.getString("playBtnCopyingFiles")); + playBtn.setText(XMLController.getLocalBundle().getString("playBtnCopyingFiles")); playBtn.setDisable(true); FileUtils.copyDirectory(srcDir, destDir); - playBtn.setText(bundle.getString("playBtnPlay")); + playBtn.setText(XMLController.getLocalBundle().getString("playBtnPlay")); playBtn.setDisable(false); LOGGER.info("copying files done!"); } catch (IOException e) { @@ -485,7 +475,7 @@ public class MainWindowController { }; JFXOkayCancelAlert addDLCAlert = new JFXOkayCancelAlert(headingText, - bundle.getString("addDLCBodyText"), dialogBtnStyle, primaryStage); + XMLController.getLocalBundle().getString("addDLCBodyText"), btnStyle, primaryStage); addDLCAlert.setOkayAction(okayAction); addDLCAlert.setCancelAction(cancelAction); addDLCAlert.showAndWait(); @@ -561,7 +551,7 @@ public class MainWindowController { smmdbImageView.setImage(scaledImage); // set imageview to image } catch (MalformedURLException | URISyntaxException e) { LOGGER.warn("There was either a problem or no image!", e); - smmdbImageView.setImage(close_black); + smmdbImageView.setImage(new Image("icons/close_black_2048x2048.png")); } addCourseDescription(courses.get(i)); } @@ -613,16 +603,16 @@ public class MainWindowController { LOGGER.error("Cloud not read the license file!", ex); } - JFXTextAreaAlert licenseAlert = new JFXTextAreaAlert("cemu_UI", textAreaText, dialogBtnStyle, primaryStage); + JFXTextAreaAlert licenseAlert = new JFXTextAreaAlert("cemu_UI", textAreaText, btnStyle, primaryStage); licenseAlert.showAndWait(); }; JFXOkayCancelAlert licenseOverviewAlert = new JFXOkayCancelAlert( - bundle.getString("licensesLblHeadingText"), bundle.getString("licensesLblBodyText"), - dialogBtnStyle, primaryStage); + XMLController.getLocalBundle().getString("licensesLblHeadingText"), + XMLController.getLocalBundle().getString("licensesLblBodyText"), btnStyle, primaryStage); licenseOverviewAlert.setOkayAction(oE -> {}); licenseOverviewAlert.setCancelAction(cancelAction); - licenseOverviewAlert.setCancelText(bundle.getString("showLicenses")); + licenseOverviewAlert.setCancelText(XMLController.getLocalBundle().getString("showLicenses")); licenseOverviewAlert.showAndWait(); } }); @@ -633,7 +623,7 @@ public class MainWindowController { xmlController.saveSettings(); } else { String bodyText = newValue + ": No such file or directory"; - JFXInfoAlert fileErrorDialog = new JFXInfoAlert("Waring!", bodyText, dialogBtnStyle, primaryStage); + JFXInfoAlert fileErrorDialog = new JFXInfoAlert("Waring!", bodyText, btnStyle, primaryStage); fileErrorDialog.showAndWait(); LOGGER.warn(newValue + ": No such file or directory"); } @@ -646,7 +636,7 @@ public class MainWindowController { reloadRoms(); } else { String bodyText = newValue + ": No such file or directory"; - JFXInfoAlert fileErrorDialog = new JFXInfoAlert("Waring!", bodyText, dialogBtnStyle, primaryStage); + JFXInfoAlert fileErrorDialog = new JFXInfoAlert("Waring!", bodyText, btnStyle, primaryStage); fileErrorDialog.showAndWait(); LOGGER.warn(newValue + ": No such file or directory"); } @@ -662,10 +652,11 @@ public class MainWindowController { @FXML private void aboutBtnAction() { - String bodyText = "cemu_UI by @Seil0 \nVersion: " + version + " (" + buildNumber + ") \"" + versionName + "\" \n" - + bundle.getString("aboutBtnBodyText"); - - JFXInfoAlert infoAlert = new JFXInfoAlert(bundle.getString("aboutBtnHeadingText"), bodyText, dialogBtnStyle, primaryStage); + String bodyText = "cemu_UI by @Seil0 \nVersion: " + version + " (" + buildNumber + ") \"" + versionName + + "\" \n" + XMLController.getLocalBundle().getString("aboutBtnBodyText"); + + JFXInfoAlert infoAlert = new JFXInfoAlert(XMLController.getLocalBundle().getString("aboutBtnHeadingText"), + bodyText, btnStyle, primaryStage); infoAlert.showAndWait(); } @@ -694,7 +685,7 @@ public class MainWindowController { @Override public void run() { Platform.runLater(() -> { - smmdbDownloadBtn.setText(bundle.getString("smmdbDownloadBtnLoading")); + smmdbDownloadBtn.setText(XMLController.getLocalBundle().getString("smmdbDownloadBtnLoading")); smmdbDownloadBtn.setDisable(true); root.getChildren().remove(0,root.getChildren().size()); }); @@ -708,7 +699,7 @@ public class MainWindowController { Platform.runLater(() -> { root.getChildren().add(new TreeItem(helpCourse)); // add data to root-node - smmdbDownloadBtn.setText(bundle.getString("smmdbDownloadBtnDownload")); + smmdbDownloadBtn.setText(XMLController.getLocalBundle().getString("smmdbDownloadBtnDownload")); smmdbDownloadBtn.setDisable(false); }); } @@ -870,8 +861,8 @@ public class MainWindowController { // cloud sync init error dialog JFXInfoAlert cloudSyncErrorDialog = new JFXInfoAlert( - bundle.getString("cloudSyncErrorHeadingText"), - bundle.getString("cloudSyncErrorBodyText"), dialogBtnStyle, primaryStage); + XMLController.getLocalBundle().getString("cloudSyncErrorHeadingText"), + XMLController.getLocalBundle().getString("cloudSyncErrorBodyText"), btnStyle, primaryStage); cloudSyncErrorDialog.showAndWait(); } @@ -881,8 +872,8 @@ public class MainWindowController { }; JFXOkayCancelAlert cloudSyncWarningAlert = new JFXOkayCancelAlert( - bundle.getString("cloudSyncWaringHeadingText"), bundle.getString("cloudSyncWaringHeadingText"), - dialogBtnStyle, primaryStage); + XMLController.getLocalBundle().getString("cloudSyncWaringHeadingText"), + XMLController.getLocalBundle().getString("cloudSyncWaringHeadingText"), btnStyle, primaryStage); cloudSyncWarningAlert.setOkayAction(okayAction); cloudSyncWarningAlert.setCancelAction(e -> cloudSyncToggleBtn.setSelected(false)); cloudSyncWarningAlert.showAndWait(); @@ -897,10 +888,10 @@ public class MainWindowController { @FXML private void addBtnAction() { - JFXEditGameDialog addGameDialog = new JFXEditGameDialog(bundle.getString("addGameBtnHeadingText"), - bundle.getString("addGameBtnBodyText"), dialogBtnStyle, 450, 300, 0, this, primaryStage, - main.getPane()); - addGameDialog.show(); + JFXEditGameAlert addGameAlert = new JFXEditGameAlert( + XMLController.getLocalBundle().getString("addGameBtnHeadingText"), + XMLController.getLocalBundle().getString("addGameBtnBodyText"), btnStyle, 0, primaryStage, MWC); + addGameAlert.showAndWait(); } /** @@ -916,9 +907,10 @@ public class MainWindowController { if (romPath.length() == 0 || coverPath.length() == 0 || title.length() == 0 || titleID.length() == 0) { LOGGER.info("No parameter set!"); - //addGame error dialog - JFXInfoAlert errorAlert = new JFXInfoAlert(bundle.getString("addBtnReturnErrorHeadingText"), - bundle.getString("addBtnReturnErrorBodyText"), dialogBtnStyle, primaryStage); + // addGame error dialog + JFXInfoAlert errorAlert = new JFXInfoAlert( + XMLController.getLocalBundle().getString("addBtnReturnErrorHeadingText"), + XMLController.getLocalBundle().getString("addBtnReturnErrorBodyText"), btnStyle, primaryStage); errorAlert.showAndWait(); } else { @@ -936,7 +928,8 @@ public class MainWindowController { try { dbController.addGame(title, coverPath, romPath, titleID, "", "", "", "0"); - dbController.loadSingleGame(titleID); + games.add(dbController.loadSingleGame(titleID)); + addGame(games.stream().filter(x -> x.getRomPath().equals(romPath)).findFirst().orElse(null)); if (menuTrue) { sideMenuSlideOut(); burgerTask.setRate(-1.0); @@ -953,7 +946,8 @@ public class MainWindowController { public void editBtnReturn(String title, String coverPath, String romPath, String titleID) { dbController.setGameInfo(title, coverPath, romPath, titleID); games.remove(selectedUIDataIndex); - dbController.loadSingleGame(titleID); + games.add(dbController.loadSingleGame(titleID)); + addGame(games.stream().filter(x -> x.getRomPath().equals(romPath)).findFirst().orElse(null)); refreshUIData(); LOGGER.info("successfully edited " + titleID + ", new name is \"" + title + "\""); @@ -967,7 +961,7 @@ public class MainWindowController { * @param titleID : game ID */ public void addGame(String title, String coverPath, String romPath, String titleID){ - UIROMDataType uiROMElement = new UIROMDataType(titleID, romPath, title, coverPath, gameContextMenu); + UIROMDataType uiROMElement = new UIROMDataType(romPath, titleID, title, coverPath, gameContextMenu); uiROMElement.getButton().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() { @Override @@ -1038,6 +1032,85 @@ public class MainWindowController { games.add(uiROMElement); } + /** + * add the context menu and button action for all games + */ + private void addAllGames() { + for (UIROMDataType game : games) { + addGame(game); + } + + refreshUIData(); // refresh the list of games displayed on screen + } + + /** + * add the context menu and button action for one game + * @param game a game + */ + private void addGame(UIROMDataType game) { + game.getButton().setContextMenu(gameContextMenu); + game.getButton().addEventHandler(MouseEvent.MOUSE_CLICKED, (event) -> { + LOGGER.info("selected: " + game.getLabel().getText() + "; ID: " + game.getTitleID()); + // getting the selected game index by comparing event.getSource() with games.get(i).getButton() + for (int i = 0; i < games.size(); i++) { + if (games.get(i).getButton() == event.getSource()) { + selectedUIDataIndex = i; + } + } + + gameExecutePath = game.getRomPath(); + selectedGameTitleID = game.getTitleID(); + selectedGameTitle = game.getName(); + + // underlining selected Label + lastGameLabel.setStyle("-fx-underline: false;"); + games.get(selectedUIDataIndex).getLabel().setStyle("-fx-underline: true;"); + lastGameLabel = games.get(selectedUIDataIndex).getLabel(); + + // setting last played, if lastPlayed is empty game was never played before, else set correct date + if (dbController.getLastPlayed(game.getTitleID()).equals("") || dbController.getLastPlayed(game.getTitleID()).equals(null)) { + lastTimePlayedBtn.setText(lastPlayed + never); + totalPlaytimeBtn.setText(dbController.getTotalPlaytime(game.getTitleID()) + " min"); + } else { + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + int tToday = Integer.parseInt(dtf.format(LocalDate.now()).replaceAll("-", "")); + int tYesterday = Integer.parseInt(dtf.format(LocalDate.now().minusDays(1)).replaceAll("-", "")); + int tLastPlayedDay = Integer.parseInt(dbController.getLastPlayed(game.getTitleID()).replaceAll("-", "")); + + if (tLastPlayedDay == tToday) { + lastTimePlayedBtn.setText(lastPlayed + today); + } else if (tLastPlayedDay == tYesterday) { + lastTimePlayedBtn.setText(lastPlayed + yesterday); + } else { + lastTimePlayedBtn.setText(lastPlayed + dbController.getLastPlayed(game.getTitleID())); + } + } + + /** + * setting total playtime, if total playtime > 60 minutes, format is "x hours x + * minutes" (x h x min), else only minutes are showed + */ + if (Integer.parseInt(dbController.getTotalPlaytime(game.getTitleID())) > 60) { + int hoursPlayed = (int) Math.floor(Integer.parseInt(dbController.getTotalPlaytime(game.getTitleID())) / 60); + int minutesPlayed = Integer.parseInt(dbController.getTotalPlaytime(game.getTitleID())) - 60 * hoursPlayed; + totalPlaytimeBtn.setText(hoursPlayed + " h " + minutesPlayed + " min"); + } else { + totalPlaytimeBtn.setText(dbController.getTotalPlaytime(game.getTitleID()) + " min"); + } + + if (!playTrue) { + playBtnSlideIn(); + } + if (menuTrue) { + sideMenuSlideOut(); + burgerTask.setRate(-1.0); + burgerTask.play(); + menuTrue = false; + } + }); + } + /** * reload all ROMs from the ROM directory */ @@ -1055,10 +1128,10 @@ public class MainWindowController { dbController.loadRomDirectory(XMLController.getRomDirectoryPath()); // reload the ROM directory games.clear(); // remove all games from the mwc game list - dbController.loadAllGames(); // load all games from the database to the mwc + games = dbController.loadAllGames(); + addAllGames(); Platform.runLater(() -> { - refreshUIData(); // refresh the list of games displayed on screen mainAnchorPane.getChildren().remove(spinner); }); }; @@ -1077,58 +1150,55 @@ public class MainWindowController { switch (XMLController.getUsrLocal()) { case "en_US": XMLController.setLocalBundle(ResourceBundle.getBundle("locals.cemu_UI-Local", Locale.US)); // us_English - bundle = ResourceBundle.getBundle("locals.cemu_UI-Local", Locale.US); // us_English languageChoisBox.getSelectionModel().select(0); break; case "de_DE": XMLController.setLocalBundle(ResourceBundle.getBundle("locals.cemu_UI-Local", Locale.GERMAN)); // German - bundle = ResourceBundle.getBundle("locals.cemu_UI-Local", Locale.GERMAN); // German languageChoisBox.getSelectionModel().select(1); break; default: XMLController.setLocalBundle(ResourceBundle.getBundle("locals.cemu_UI-Local", Locale.US)); // default local - bundle = ResourceBundle.getBundle("locals.cemu_UI-Local", Locale.US); // default local languageChoisBox.getSelectionModel().select(0); break; } // Buttons - aboutBtn.setText(bundle.getString("aboutBtn")); - settingsBtn.setText(bundle.getString("settingsBtn")); - addBtn.setText(bundle.getString("addBtn")); - reloadRomsBtn.setText(bundle.getString("reloadRomsBtn")); - smmdbBtn.setText(bundle.getString("smmdbBtn")); - cemuTFBtn.setText(bundle.getString("cemuTFBtn")); - romTFBtn.setText(bundle.getString("romTFBtn")); - updateBtn.setText(bundle.getString("updateBtnCheckNow")); - smmdbDownloadBtn.setText(bundle.getString("smmdbDownloadBtn")); - playBtn.setText(bundle.getString("playBtn")); - cloudSyncToggleBtn.setText(bundle.getString("cloudSyncToggleBtn")); - autoUpdateToggleBtn.setText(bundle.getString("autoUpdateToggleBtn")); - fullscreenToggleBtn.setText(bundle.getString("fullscreenToggleBtn")); + aboutBtn.setText(XMLController.getLocalBundle().getString("aboutBtn")); + settingsBtn.setText(XMLController.getLocalBundle().getString("settingsBtn")); + addBtn.setText(XMLController.getLocalBundle().getString("addBtn")); + reloadRomsBtn.setText(XMLController.getLocalBundle().getString("reloadRomsBtn")); + smmdbBtn.setText(XMLController.getLocalBundle().getString("smmdbBtn")); + cemuTFBtn.setText(XMLController.getLocalBundle().getString("cemuTFBtn")); + romTFBtn.setText(XMLController.getLocalBundle().getString("romTFBtn")); + updateBtn.setText(XMLController.getLocalBundle().getString("updateBtnCheckNow")); + smmdbDownloadBtn.setText(XMLController.getLocalBundle().getString("smmdbDownloadBtn")); + playBtn.setText(XMLController.getLocalBundle().getString("playBtn")); + cloudSyncToggleBtn.setText(XMLController.getLocalBundle().getString("cloudSyncToggleBtn")); + autoUpdateToggleBtn.setText(XMLController.getLocalBundle().getString("autoUpdateToggleBtn")); + fullscreenToggleBtn.setText(XMLController.getLocalBundle().getString("fullscreenToggleBtn")); // Labels - cemu_UISettingsLbl.setText(bundle.getString("cemu_UISettingsLbl")); - cemuDirectoryLbl.setText(bundle.getString("cemuDirectoryLbl")); - romDirectoryLbl.setText(bundle.getString("romDirectoryLbl")); - mainColorLbl.setText(bundle.getString("mainColorLbl")); - languageLbl.setText(bundle.getString("languageLbl")); - updateLbl.setText(bundle.getString("updateLbl")); - branchLbl.setText(bundle.getString("branchLbl")); - cemuSettingsLbl.setText(bundle.getString("cemuSettingsLbl")); - licensesLbl.setText(bundle.getString("licensesLbl")); + cemu_UISettingsLbl.setText(XMLController.getLocalBundle().getString("cemu_UISettingsLbl")); + cemuDirectoryLbl.setText(XMLController.getLocalBundle().getString("cemuDirectoryLbl")); + romDirectoryLbl.setText(XMLController.getLocalBundle().getString("romDirectoryLbl")); + mainColorLbl.setText(XMLController.getLocalBundle().getString("mainColorLbl")); + languageLbl.setText(XMLController.getLocalBundle().getString("languageLbl")); + updateLbl.setText(XMLController.getLocalBundle().getString("updateLbl")); + branchLbl.setText(XMLController.getLocalBundle().getString("branchLbl")); + cemuSettingsLbl.setText(XMLController.getLocalBundle().getString("cemuSettingsLbl")); + licensesLbl.setText(XMLController.getLocalBundle().getString("licensesLbl")); // Columns - titleColumn.setText(bundle.getString("titleColumn")); - idColumn.setText(bundle.getString("idColumn")); - starsColumn.setText(bundle.getString("starsColumn")); - timeColumn.setText(bundle.getString("timeColumn")); + titleColumn.setText(XMLController.getLocalBundle().getString("titleColumn")); + idColumn.setText(XMLController.getLocalBundle().getString("idColumn")); + starsColumn.setText(XMLController.getLocalBundle().getString("starsColumn")); + timeColumn.setText(XMLController.getLocalBundle().getString("timeColumn")); // Strings - lastPlayed = bundle.getString("lastPlayed"); - today = bundle.getString("today"); - yesterday = bundle.getString("yesterday"); - never = bundle.getString("never"); + lastPlayed = XMLController.getLocalBundle().getString("lastPlayed"); + today = XMLController.getLocalBundle().getString("today"); + yesterday = XMLController.getLocalBundle().getString("yesterday"); + never = XMLController.getLocalBundle().getString("never"); } // if AutoUpdate, then check for updates @@ -1271,8 +1341,7 @@ public class MainWindowController { // change the color of all needed GUI elements private void applyColor() { String boxStyle = "-fx-background-color: #" + XMLController.getColor() + ";"; - String btnStyleBlack = "-fx-button-type: RAISED; -fx-background-color: #" + XMLController.getColor() + "; -fx-text-fill: BLACK;"; - String btnStyleWhite = "-fx-button-type: RAISED; -fx-background-color: #" + XMLController.getColor() + "; -fx-text-fill: WHITE;"; + String textFill = ""; BigInteger icolor = new BigInteger(XMLController.getColor(),16); BigInteger ccolor = new BigInteger("78909cff",16); @@ -1282,55 +1351,45 @@ public class MainWindowController { romTextField.setFocusColor(Color.valueOf(XMLController.getColor())); if (icolor.compareTo(ccolor) == -1) { - dialogBtnStyle = btnStyleWhite; + textFill = "-fx-text-fill: WHITE;"; - aboutBtn.setStyle("-fx-text-fill: WHITE;"); - settingsBtn.setStyle("-fx-text-fill: WHITE;"); - addBtn.setStyle("-fx-text-fill: WHITE;"); - reloadRomsBtn.setStyle("-fx-text-fill: WHITE;"); - smmdbBtn.setStyle("-fx-text-fill: WHITE;"); - playBtn.setStyle("-fx-text-fill: WHITE; -fx-font-family: Roboto Medium;"); - cemuTFBtn.setStyle(btnStyleWhite); - romTFBtn.setStyle(btnStyleWhite); - updateBtn.setStyle(btnStyleWhite); - smmdbDownloadBtn.setStyle(btnStyleWhite); - playBtn.setStyle(btnStyleWhite); - - aboutBtn.setGraphic(info_white); - settingsBtn.setGraphic(settings_white); - addBtn.setGraphic(add_circle_white); - reloadRomsBtn.setGraphic(cached_white); - smmdbBtn.setGraphic(smmdb_white); + aboutBtn.setGraphic(new ImageView(new Image("icons/ic_info_white_24dp_1x.png"))); + settingsBtn.setGraphic(new ImageView(new Image("icons/ic_settings_white_24dp_1x.png"))); + addBtn.setGraphic(new ImageView(new Image("icons/ic_add_circle_white_24dp_1x.png"))); + reloadRomsBtn.setGraphic(new ImageView(new Image("icons/ic_cached_white_24dp_1x.png"))); + smmdbBtn.setGraphic(new ImageView(new Image("icons/ic_get_app_white_24dp_1x.png"))); menuHam.getStyleClass().clear(); menuHam.getStyleClass().add("jfx-hamburgerW"); } else { - dialogBtnStyle = btnStyleBlack; - - aboutBtn.setStyle("-fx-text-fill: BLACK;"); - settingsBtn.setStyle("-fx-text-fill: BLACK;"); - addBtn.setStyle("-fx-text-fill: BLACK;"); - reloadRomsBtn.setStyle("-fx-text-fill: BLACK;"); - smmdbBtn.setStyle("-fx-text-fill: BLACK;"); - playBtn.setStyle("-fx-text-fill: BLACK; -fx-font-family: Roboto Medium;"); - cemuTFBtn.setStyle(btnStyleBlack); - romTFBtn.setStyle(btnStyleBlack); - updateBtn.setStyle(btnStyleBlack); - smmdbDownloadBtn.setStyle(btnStyleBlack); - playBtn.setStyle(btnStyleBlack); - - aboutBtn.setGraphic(info_black); - settingsBtn.setGraphic(settings_black); - addBtn.setGraphic(add_circle_black); - reloadRomsBtn.setGraphic(cached_black); - smmdbBtn.setGraphic(smmdb_black); + textFill = "-fx-text-fill: BLACK;"; + + aboutBtn.setGraphic(new ImageView(new Image("icons/ic_info_black_24dp_1x.png"))); + settingsBtn.setGraphic(new ImageView(new Image("icons/ic_settings_black_24dp_1x.png"))); + addBtn.setGraphic(new ImageView(new Image("icons/ic_add_circle_black_24dp_1x.png"))); + reloadRomsBtn.setGraphic(new ImageView(new Image("icons/ic_cached_black_24dp_1x.png"))); + smmdbBtn.setGraphic(new ImageView(new Image("icons/ic_get_app_black_24dp_1x.png"))); menuHam.getStyleClass().clear(); menuHam.getStyleClass().add("jfx-hamburgerB"); } - - for (int i = 0; i < games.size(); i++) { - games.get(i).getButton().setRipplerFill(Paint.valueOf(XMLController.getColor())); + + btnStyle = "-fx-button-type: RAISED; -fx-background-color: #" + XMLController.getColor() + "; " + textFill; + + aboutBtn.setStyle(textFill); + settingsBtn.setStyle(textFill); + addBtn.setStyle(textFill); + reloadRomsBtn.setStyle(textFill); + smmdbBtn.setStyle(textFill); + playBtn.setStyle(textFill); + cemuTFBtn.setStyle(btnStyle); + romTFBtn.setStyle(btnStyle); + updateBtn.setStyle(btnStyle); + smmdbDownloadBtn.setStyle(btnStyle); + playBtn.setStyle(btnStyle); + + for(UIROMDataType game : games) { + game.getButton().setRipplerFill(Paint.valueOf(XMLController.getColor())); } } @@ -1411,10 +1470,6 @@ public class MainWindowController { public Main getMain() { return main; } - - public DBController getDbController() { - return dbController; - } public Stage getPrimaryStage() { return primaryStage; @@ -1444,18 +1499,6 @@ public class MainWindowController { this.playBtn = playBtn; } - public String getLanguage() { - return language; - } - - public void setLanguage(String language) { - this.language = language; - } - - public ResourceBundle getBundle() { - return bundle; - } - public AnchorPane getMainAnchorPane() { return mainAnchorPane; } diff --git a/src/main/java/com/cemu_UI/controller/CloudController.java b/src/main/java/com/cemu_UI/controller/CloudController.java index 0716578..99c0fcc 100644 --- a/src/main/java/com/cemu_UI/controller/CloudController.java +++ b/src/main/java/com/cemu_UI/controller/CloudController.java @@ -41,6 +41,8 @@ import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; public class CloudController { + + // TODO make singleton public CloudController(MainWindowController mwc) { this.mwc = mwc; diff --git a/src/main/java/com/cemu_UI/controller/DBController.java b/src/main/java/com/cemu_UI/controller/DBController.java index 718d965..d29c28f 100644 --- a/src/main/java/com/cemu_UI/controller/DBController.java +++ b/src/main/java/com/cemu_UI/controller/DBController.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -45,92 +46,94 @@ import org.apache.logging.log4j.Logger; import org.w3c.dom.Document; import org.xml.sax.SAXException; -import com.cemu_UI.application.MainWindowController; +import com.cemu_UI.datatypes.UIROMDataType; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; public class DBController { - - public DBController(MainWindowController mainWindowController) { - this.mainWindowController = mainWindowController; - } - private MainWindowController mainWindowController; + private static DBController instance = null; private ArrayList entries = new ArrayList<>(); - private String DB_PATH_localRoms; - private String DB_PATH_games; - private Connection connection = null; - private Connection connectionGames = null; + private String DB_PATH_LocalGames; + private String DB_PATH_ReverenceGames; + private Connection connectionLocal = null; + private Connection connectionReverence = null; private static final Logger LOGGER = LogManager.getLogger(DBController.class.getName()); - /** - * initialize the sqlite database controller - * load ROM and games database - * load all games - */ - public void init() { - LOGGER.info("<========== starting loading sql ==========>"); - loadRomDatabase(); - loadGamesDatabase(); - createRomDatabase(); - LOGGER.info("<========== finished loading sql ==========>"); + public DBController() { + + } + + public static DBController getInstance() { + if (instance == null) { + instance = new DBController(); + } + + return instance; } /** - * set the path to the localRoms.db file and initialize the connection + * initialize the {@link DBController} with the database connection check if + * there is a need to create a new database refresh the database + */ + public void init() { + initDatabaseConnection(); + createRomDatabase(); + } + + /** + * create a new connection to the HomeFlix.db database + * AutoCommit is set to false to prevent some issues, so manual commit is active! * * TODO this should be called LocalGames */ - private void loadRomDatabase() { - if (System.getProperty("os.name").equals("Linux")) { - DB_PATH_localRoms = System.getProperty("user.home") + "/cemu_UI/localRoms.db"; - } else { - DB_PATH_localRoms = System.getProperty("user.home") + "\\Documents\\cemu_UI" + "\\" + "localRoms.db"; - } + private void initDatabaseConnection() { + DB_PATH_LocalGames = XMLController.getDirCemuUI() + "/localRoms.db"; + DB_PATH_ReverenceGames = XMLController.getRference_gamesFile().getAbsolutePath(); + try { // create a database connection - connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH_localRoms); - connection.setAutoCommit(false); // AutoCommit to false -> manual commit is active + connectionLocal = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH_LocalGames); + connectionLocal.setAutoCommit(false); + + connectionReverence = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH_ReverenceGames); + connectionReverence.setAutoCommit(false); } catch (SQLException e) { - // if the error message is "out of memory", probably no database file is found - LOGGER.error("error while loading the ROM database", e); + // if the error message is "out of memory", it probably means no database file is found + LOGGER.error("error while loading the Local- or ReverenceGames database", e); } - LOGGER.info("ROM database loaded successfull"); + + LOGGER.info("Local- and ReverenceGames database loaded successfull"); } - /** - * set the path to the localRoms.db file and initialize the connection - * games.db contains a reverence list to for the automatic detection mode - */ - private void loadGamesDatabase() { - DB_PATH_games = XMLController.getRference_gamesFile().getAbsolutePath(); - try { - // create a database connection - connectionGames = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH_games); - connectionGames.setAutoCommit(false); // AutoCommit to false -> manual commit is active - } catch (SQLException e) { - LOGGER.error("error while loading the games database", e); - } - LOGGER.info("games database loaded successfull"); - } - - /** * creating the local_roms table in localRoms.db * if the table has no entries, call loadRomDirectory * - * TODO the local_roms table should be called local_games + * TODO the local_roms table should be called LocalGames */ void createRomDatabase() { try { - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); stmt.executeUpdate("create table if not exists local_roms (title, coverPath, romPath, titleID, productCode, region, lastPlayed, timePlayed)"); + connectionLocal.commit(); stmt.close(); - connection.commit(); } catch (SQLException e) { - LOGGER.error("error while creating ROM database", e); + LOGGER.error("error while creating local-games database", e); } - + } + + /** + * refresh database to contain all (new added) games + */ + public void refreshDataBase() { + LOGGER.info("<========== starting refreshing database ==========>"); + + entries.clear(); + try { - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms"); while (rs.next()) { entries.add(rs.getString(2)); @@ -140,63 +143,80 @@ public class DBController { } catch (SQLException e) { LOGGER.error("error while loading ROMs from ROM database, local_roms table", e); } + if (entries.size() == 0) { - mainWindowController.reloadRoms(); - } else { loadAllGames(); - mainWindowController.refreshUIData(); } + + LOGGER.info("<========== finished refreshing database ==========>"); } // add a game to the database public void addGame(String title, String coverPath, String romPath, String titleID, String productCode, String region, String lastPlayed, String timePlayed) throws SQLException{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); stmt.executeUpdate("insert into local_roms values ('"+title+"','"+coverPath+"','"+romPath+"','"+titleID+"'," + "'"+productCode+"','"+region+"','"+lastPlayed+"','"+timePlayed+"')"); - connection.commit(); + connectionLocal.commit(); stmt.close(); LOGGER.info("added \""+title+"\" to ROM database"); } // remove a game from the database public void removeGame(String titleID) throws SQLException{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); stmt.executeUpdate("delete from local_roms where titleID = '"+titleID+"'"); - connection.commit(); + connectionLocal.commit(); stmt.close(); LOGGER.info("removed \""+titleID+"\" from ROM database"); } - //load all ROMs to the mainWindowController - public void loadAllGames(){ - LOGGER.info("loading all games on startup into the mainWindowController ..."); - try { - Statement stmt = connection.createStatement(); + /** + * load all games from the database to a ObservableList, order entries by title + * @return a ObservableList that contains all local games from the database + */ + public ObservableList loadAllGames() { + ObservableList games = FXCollections.observableArrayList(); + LOGGER.info("loading all local games from the database ..."); + try { + Statement stmt = connectionLocal.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms"); while (rs.next()) { - mainWindowController.addGame(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)); + games.add(new UIROMDataType(rs.getString("romPath"), rs.getString("titleID"), rs.getString("title"), + rs.getString("coverPath"))); } stmt.close(); rs.close(); - }catch (Exception e){ - LOGGER.error("error while loading all games into the mainWindowController", e); + } catch (Exception e) { + LOGGER.error("error while loading all local games from the database", e); } + + return games; } - //load a single ROM to the mainWindowController - public void loadSingleGame(String titleID){ - LOGGER.info("loading a single game (ID: "+titleID+") into the mainWindowController ..."); + /** + * load one game from the database + * @param titleID the titleID of the game you wish to get + * @return the game you asked for + */ + public UIROMDataType loadSingleGame(String titleID) { + UIROMDataType game = null; + LOGGER.info("loading a single game (ID: {}) from the database ...", titleID); try { - Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms where titleID = '"+titleID+"'"); + PreparedStatement ps = connectionLocal.prepareStatement("SELECT * FROM local_roms where titleID = ?"); + ps.setString(1, titleID); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { - mainWindowController.addGame(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)); + game = new UIROMDataType(rs.getString("romPath"), rs.getString("titleID"), rs.getString("title"), + rs.getString("coverPath")); } - stmt.close(); rs.close(); + ps.close(); }catch (Exception e){ LOGGER.error("error while loading a single game into the mainWindowController", e); } + + return game; } /** @@ -210,7 +230,7 @@ public class DBController { File pictureCache = XMLController.getPictureCache(); String coverPath; try { - Statement stmt = connectionGames.createStatement(); + Statement stmt = connectionReverence.createStatement(); List files = (List) FileUtils.listFiles(dir, extensions, true); LOGGER.info("<============================== start loading ROM Directory ==============================>"); LOGGER.info("Getting all .rpx files in " + dir.getCanonicalPath()+" including those in subdirectories"); @@ -256,7 +276,7 @@ public class DBController { * @throws SQLException */ private boolean checkAddEntry(String title) throws SQLException{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); boolean check = false; ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms WHERE title = '"+title+"';"); while (rs.next()) { @@ -274,15 +294,6 @@ public class DBController { //LOGGER.info("check if entry removed not done yet!"); } - private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) { - BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); - Graphics2D g = resizedImage.createGraphics(); - g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); - g.dispose(); - - return resizedImage; - } - /** * getting info for a game with titleID * @param titleID Title-ID of the Game @@ -292,7 +303,7 @@ public class DBController { String[] gameInfo = new String[4]; LOGGER.info("getting game info for titleID: "+titleID+" ..."); try { - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms where titleID = '"+titleID+"'"); while (rs.next()) { gameInfo[0] = rs.getString(1);// title @@ -311,10 +322,10 @@ public class DBController { public void setGameInfo(String title, String coverPath, String romPath, String titleID){ LOGGER.info("setting game info for titleID: "+titleID+" ..."); try { - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); stmt.executeUpdate("UPDATE local_roms SET title = '" + title + "', coverPath = '" + coverPath + "'," + " romPath = '" + romPath + "' WHERE titleID = '"+titleID+"';"); - connection.commit(); + connectionLocal.commit(); stmt.close(); }catch (Exception e){ LOGGER.error("error while setting game info", e); @@ -323,9 +334,9 @@ public class DBController { public void setLastPlayed(String titleID){ try{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); stmt.executeUpdate("UPDATE local_roms SET lastPlayed=date('now') WHERE titleID = '"+titleID+"';"); - connection.commit(); + connectionLocal.commit(); stmt.close(); }catch(SQLException e){ LOGGER.error("failed to set the last played", e); @@ -335,7 +346,7 @@ public class DBController { public String getLastPlayed(String titleID){ String lastPlayed = null; try{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); ResultSet rs = stmt.executeQuery("SELECT lastPlayed FROM local_roms WHERE titleID = '"+titleID+"';" ); lastPlayed = rs.getString(1); stmt.close(); @@ -348,9 +359,9 @@ public class DBController { public void setTotalPlaytime(String timePlayed, String titleID){ try{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); stmt.executeUpdate("UPDATE local_roms SET timePlayed='"+timePlayed+"' WHERE titleID = '"+titleID+"';"); - connection.commit(); + connectionLocal.commit(); stmt.close(); }catch(SQLException e){ LOGGER.error("failed to set total play time", e); @@ -361,7 +372,7 @@ public class DBController { public String getTotalPlaytime(String titleID){ String timePlayed = null; try{ - Statement stmt = connection.createStatement(); + Statement stmt = connectionLocal.createStatement(); ResultSet rs = stmt.executeQuery("SELECT timePlayed FROM local_roms WHERE titleID = '"+titleID+"';" ); timePlayed = rs.getString(1); stmt.close(); @@ -372,5 +383,13 @@ public class DBController { return timePlayed; } + private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) { + BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); + Graphics2D g = resizedImage.createGraphics(); + g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); + g.dispose(); + + return resizedImage; + } } diff --git a/src/main/java/com/cemu_UI/datatypes/UIROMDataType.java b/src/main/java/com/cemu_UI/datatypes/UIROMDataType.java index 3e797e4..66e2f4a 100644 --- a/src/main/java/com/cemu_UI/datatypes/UIROMDataType.java +++ b/src/main/java/com/cemu_UI/datatypes/UIROMDataType.java @@ -36,19 +36,32 @@ import javafx.scene.text.FontWeight; public class UIROMDataType extends VBox { - private String titleID; private String romPath; + private String titleID; + private String name; private Label label = new Label(); private JFXButton button = new JFXButton(); private ImageView imageView = new ImageView(); - public UIROMDataType(String titleID, String romPath, String text, String coverPath, ContextMenu contextMenu) { + public UIROMDataType(String romPath, String titleID, String name, String coverPath) { this(); - this.titleID = titleID; this.romPath = romPath; + this.titleID = titleID; + this.name = name; - label.setText(text); + label.setText(name); + imageView.setImage(new Image(new File(coverPath).toURI().toString())); + } + + public UIROMDataType(String romPath, String titleID, String name, String coverPath, ContextMenu contextMenu) { + this(); + + this.romPath = romPath; + this.titleID = titleID; + this.name = name; + + label.setText(name); imageView.setImage(new Image(new File(coverPath).toURI().toString())); button.setContextMenu(contextMenu); } @@ -67,12 +80,16 @@ public class UIROMDataType extends VBox { button.setGraphic(imageView); } + public String getRomPath() { + return romPath; + } + public String getTitleID() { return titleID; } - public String getRomPath() { - return romPath; + public String getName() { + return name; } public Label getLabel() { @@ -86,13 +103,17 @@ public class UIROMDataType extends VBox { public ImageView getImageView() { return imageView; } + + public void setRomPath(String romPath) { + this.romPath = romPath; + } public void setTitleID(String titleID) { this.titleID = titleID; } - public void setRomPath(String romPath) { - this.romPath = romPath; + public void setName(String name) { + this.name = name; } public void setLabel(Label label) { diff --git a/src/main/java/com/cemu_UI/uiElements/JFXEditGameAlert.java b/src/main/java/com/cemu_UI/uiElements/JFXEditGameAlert.java new file mode 100644 index 0000000..c128f1d --- /dev/null +++ b/src/main/java/com/cemu_UI/uiElements/JFXEditGameAlert.java @@ -0,0 +1,250 @@ +/** + * cemu_UI + * + * Copyright 2017-2019 <@Seil0> + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package com.cemu_UI.uiElements; + +import java.io.File; + +import com.cemu_UI.application.MainWindowController; +import com.cemu_UI.controller.XMLController; +import com.jfoenix.controls.JFXAlert; +import com.jfoenix.controls.JFXButton; +import com.jfoenix.controls.JFXDialogLayout; + +import javafx.event.ActionEvent; +import javafx.geometry.Insets; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.FileChooser; +import javafx.stage.Stage; + +public class JFXEditGameAlert { + + private String headingText; + private String bodyText; + private String title = ""; + private String coverPath = ""; + private String romPath = ""; + private String titleID = ""; + private String btnStyle; + private int mode; + private Stage stage; + private MainWindowController mwc; // TODO remove this! + + /** + * Creates a new JFoenix Alert to show some information with okay and cancel option + * @param headingText Heading text of the alert + * @param bodyText Content text of the alert + * @param btnStyle Style of the buttons + * @param mode Set to 0 for add and 1 for edit mode + * @param stage Stage to which the Alert belongs + * @param mwc a mwc dep, it's horrible i know + */ + public JFXEditGameAlert(String headingText, String bodyText, String btnStyle, int mode, Stage stage, MainWindowController mwc) { + this.headingText = headingText; + this.bodyText = bodyText; + this.btnStyle = btnStyle; + this.mode = mode; + this.stage = stage; + this.mwc = mwc; + } + + public void showAndWait( ) { + JFXAlert alert = new JFXAlert<>(stage); + + TextField gameTitleTF = new TextField(); + gameTitleTF.setPromptText(XMLController.getLocalBundle().getString("gameTitle")); + TextField gameTitleIDTF = new TextField(); + gameTitleIDTF.setPromptText(XMLController.getLocalBundle().getString("titleID")); + TextField romPathTF = new TextField(); + romPathTF.setPromptText(XMLController.getLocalBundle().getString("romPath")); + TextField gameCoverTF = new TextField(); + gameCoverTF.setPromptText(XMLController.getLocalBundle().getString("coverPath")); + + if (mode == 1) { + gameTitleTF.setText(title); + gameTitleIDTF.setText(titleID); + romPathTF.setText(romPath); + gameCoverTF.setText(coverPath); + + gameTitleIDTF.setEditable(false); + } + + JFXButton okayBtn = new JFXButton(XMLController.getLocalBundle().getString("okayBtnText")); + okayBtn.setOnAction(event -> { + if (romPathTF.getText().toString().length() == 0 || gameCoverTF.getText().toString().length() == 0 + || gameTitleTF.getText().toString().length() == 0 + || gameTitleIDTF.getText().toString().length() == 0) { + + // addGame error dialog + JFXInfoAlert errorDialog = new JFXInfoAlert( + XMLController.getLocalBundle().getString("editGameDialogHeadingTextError"), + XMLController.getLocalBundle().getString("editGameDialogBodyTextError"), btnStyle, stage); + errorDialog.showAndWait(); + } else { + switch (mode) { + case 0: + // add-mode title, coverPath, romPath, titleID + mwc.addBtnReturn(gameTitleTF.getText().toString(), gameCoverTF.getText().toString(), + romPathTF.getText().toString(), gameTitleIDTF.getText().toString()); + break; + case 1: + // edit mode + mwc.editBtnReturn(gameTitleTF.getText().toString(), gameCoverTF.getText().toString(), + romPathTF.getText().toString(), gameTitleIDTF.getText().toString()); + break; + default: + break; + } + + alert.close(); + } + }); + okayBtn.setButtonType(com.jfoenix.controls.JFXButton.ButtonType.RAISED); + okayBtn.setPrefHeight(32); + okayBtn.setStyle(btnStyle); + + JFXButton cancelBtn = new JFXButton(XMLController.getLocalBundle().getString("cancelBtnText")); + cancelBtn.addEventHandler(ActionEvent.ACTION, (e)-> alert.close()); + cancelBtn.setButtonType(com.jfoenix.controls.JFXButton.ButtonType.RAISED); + cancelBtn.setPrefHeight(32); + cancelBtn.setStyle(btnStyle); + + JFXButton selectPathBtn = new JFXButton(XMLController.getLocalBundle().getString("editGameDialogSelectPathBtn")); + selectPathBtn.setPrefWidth(110); + selectPathBtn.setStyle(btnStyle); + selectPathBtn.setOnAction(event -> { + FileChooser romDirectoryChooser = new FileChooser(); + File romDirectory = romDirectoryChooser.showOpenDialog(stage); + romPathTF.setText(romDirectory.getAbsolutePath()); + }); + + JFXButton selectCoverBtn = new JFXButton(XMLController.getLocalBundle().getString("editGameDialogSelectCoverBtn")); + selectCoverBtn.setPrefWidth(110); + selectCoverBtn.setStyle(btnStyle); + selectCoverBtn.setOnAction(event -> { + FileChooser coverDirectoryChooser = new FileChooser(); + File coverDirectory = coverDirectoryChooser.showOpenDialog(stage); + gameCoverTF.setText(coverDirectory.getAbsolutePath()); + }); + + GridPane grid = new GridPane(); + grid.setHgap(10); + grid.setVgap(10); + grid.setPadding(new Insets(15, 10, 10, 10)); + grid.add(new Label(XMLController.getLocalBundle().getString("gameTitle") + ":"), 0, 0); + grid.add(gameTitleTF, 1, 0); + grid.add(new Label(XMLController.getLocalBundle().getString("titleID") + ":"), 0, 1); + grid.add(gameTitleIDTF, 1, 1); + grid.add(new Label(XMLController.getLocalBundle().getString("romPath") + ":"), 0, 2); + grid.add(romPathTF, 1, 2); + grid.add(selectPathBtn, 2, 2); + grid.add(new Label(XMLController.getLocalBundle().getString("coverPath") + ":"), 0, 3); + grid.add(gameCoverTF, 1, 3); + grid.add(selectCoverBtn, 2, 3); + + VBox vbox = new VBox(); + vbox.getChildren().addAll(new Text(bodyText), grid); + + JFXDialogLayout content = new JFXDialogLayout(); + content.setActions(cancelBtn, okayBtn); + content.setHeading(new Text(headingText)); + content.setBody(vbox); + alert.setContent(content); + alert.showAndWait(); + } + + public String getHeadingText() { + return headingText; + } + + public String getBodyText() { + return bodyText; + } + + public String getTitle() { + return title; + } + + public String getCoverPath() { + return coverPath; + } + + public String getRomPath() { + return romPath; + } + + public String getTitleID() { + return titleID; + } + + public String getBtnStyle() { + return btnStyle; + } + + public int getMode() { + return mode; + } + + public Stage getStage() { + return stage; + } + + public void setHeadingText(String headingText) { + this.headingText = headingText; + } + + public void setBodyText(String bodyText) { + this.bodyText = bodyText; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setCoverPath(String coverPath) { + this.coverPath = coverPath; + } + + public void setRomPath(String romPath) { + this.romPath = romPath; + } + + public void setTitleID(String titleID) { + this.titleID = titleID; + } + + public void setBtnStyle(String btnStyle) { + this.btnStyle = btnStyle; + } + + public void setMode(int mode) { + this.mode = mode; + } + + public void setStage(Stage stage) { + this.stage = stage; + } + +} diff --git a/src/main/java/com/cemu_UI/uiElements/JFXEditGameDialog.java b/src/main/java/com/cemu_UI/uiElements/JFXEditGameDialog.java deleted file mode 100644 index 8aef197..0000000 --- a/src/main/java/com/cemu_UI/uiElements/JFXEditGameDialog.java +++ /dev/null @@ -1,230 +0,0 @@ -/** - * cemu_UI - * - * Copyright 2017-2019 <@Seil0> - * - * This program 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. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package com.cemu_UI.uiElements; - -import java.io.File; - -import com.cemu_UI.application.MainWindowController; -import com.cemu_UI.controller.XMLController; -import com.jfoenix.controls.JFXButton; -import com.jfoenix.controls.JFXDialog; -import com.jfoenix.controls.JFXDialogLayout; - -import javafx.event.ActionEvent; -import javafx.event.EventHandler; -import javafx.geometry.Insets; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; -import javafx.scene.layout.AnchorPane; -import javafx.scene.layout.GridPane; -import javafx.scene.layout.Pane; -import javafx.scene.layout.StackPane; -import javafx.scene.layout.VBox; -import javafx.scene.text.Text; -import javafx.stage.FileChooser; -import javafx.stage.Stage; - -public class JFXEditGameDialog { - - private String headingText; - private String bodyText; - private String dialogBtnStyle; - private String title = ""; - private String coverPath = ""; - private String romPath = ""; - private String titleID = ""; - private int dialogWidth; - private int dialogHeight; - private int mode; - private Stage stage; - private Pane pane; - private MainWindowController mwc; - - /** - * Creates a new JFoenix Dialog to show some information with okay and cancel option - * @param headingText Heading Text, just the heading - * @param bodyText body Text, all other text belongs here - * @param dialogBtnStyle Style of the okay button - * @param dialogWidth dialog width - * @param dialogHeight dialog height - * @param mode set to 0 for add and 1 for edit mode - * @param stage the primary stage - * @param pane pane to which the dialog belongs - */ - public JFXEditGameDialog(String headingText, String bodyText, String dialogBtnStyle, int dialogWidth, - int dialogHeight, int mode, MainWindowController mwc, Stage stage, Pane pane) { - this.headingText = headingText; - this.bodyText = bodyText; - this.dialogBtnStyle = dialogBtnStyle; - this.dialogWidth = dialogWidth; - this.dialogHeight = dialogHeight; - this.mode = mode; - this.mwc = mwc; - this.stage = stage; - this.pane = pane; - } - - public void show() { - JFXDialogLayout content = new JFXDialogLayout(); - StackPane stackPane = new StackPane(); - stackPane.autosize(); - JFXDialog dialog = new JFXDialog(stackPane, content, JFXDialog.DialogTransition.LEFT, true); - - TextField gameTitleTF = new TextField(); - gameTitleTF.setPromptText(XMLController.getLocalBundle().getString("gameTitle")); - TextField gameTitleIDTF = new TextField(); - gameTitleIDTF.setPromptText(XMLController.getLocalBundle().getString("titleID")); - TextField romPathTF = new TextField(); - romPathTF.setPromptText(XMLController.getLocalBundle().getString("romPath")); - TextField gameCoverTF = new TextField(); - gameCoverTF.setPromptText(XMLController.getLocalBundle().getString("coverPath")); - - if (mode == 1) { - gameTitleTF.setText(title); - gameTitleIDTF.setText(titleID); - romPathTF.setText(romPath); - gameCoverTF.setText(coverPath); - - gameTitleIDTF.setEditable(false); - } - - JFXButton okayBtn = new JFXButton(XMLController.getLocalBundle().getString("okayBtnText")); - okayBtn.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - if (romPathTF.getText().toString().length() == 0 || gameCoverTF.getText().toString().length() == 0 - || gameTitleTF.getText().toString().length() == 0 || gameTitleIDTF.getText().toString().length() == 0) { - - // LOGGER.info("No parameter set!"); - - // addGame error dialog - JFXInfoAlert errorDialog = new JFXInfoAlert( - XMLController.getLocalBundle().getString("editGameDialogHeadingTextError"), - XMLController.getLocalBundle().getString("editGameDialogBodyTextError"), dialogBtnStyle, - stage); - errorDialog.showAndWait(); - } else { - switch (mode) { - case 0: - // add-mode title, coverPath, romPath, titleID - mwc.addBtnReturn(gameTitleTF.getText().toString(), gameCoverTF.getText().toString(), - romPathTF.getText().toString(), gameTitleIDTF.getText().toString()); - dialog.close(); - break; - case 1: - // edit mode - mwc.editBtnReturn(gameTitleTF.getText().toString(), gameCoverTF.getText().toString(), - romPathTF.getText().toString(), gameTitleIDTF.getText().toString()); - dialog.close(); - break; - default: - dialog.close(); - break; - } - } - } - }); - okayBtn.setButtonType(com.jfoenix.controls.JFXButton.ButtonType.RAISED); - okayBtn.setPrefHeight(32); - okayBtn.setStyle(dialogBtnStyle); - - JFXButton cancelBtn = new JFXButton(XMLController.getLocalBundle().getString("cancelBtnText")); - cancelBtn.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - dialog.close(); - } - }); - cancelBtn.setButtonType(com.jfoenix.controls.JFXButton.ButtonType.RAISED); - cancelBtn.setPrefHeight(32); - cancelBtn.setStyle(dialogBtnStyle); - - JFXButton selectPathBtn = new JFXButton(XMLController.getLocalBundle().getString("editGameDialogSelectPathBtn")); - selectPathBtn.setPrefWidth(110); - selectPathBtn.setStyle(dialogBtnStyle); - selectPathBtn.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - FileChooser romDirectoryChooser = new FileChooser(); - File romDirectory = romDirectoryChooser.showOpenDialog(stage); - romPathTF.setText(romDirectory.getAbsolutePath()); - } - }); - - JFXButton selectCoverBtn = new JFXButton(XMLController.getLocalBundle().getString("editGameDialogSelectCoverBtn")); - selectCoverBtn.setPrefWidth(110); - selectCoverBtn.setStyle(dialogBtnStyle); - selectCoverBtn.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - FileChooser coverDirectoryChooser = new FileChooser(); - File coverDirectory = coverDirectoryChooser.showOpenDialog(stage); - gameCoverTF.setText(coverDirectory.getAbsolutePath()); - } - }); - - GridPane grid = new GridPane(); - grid.setHgap(10); - grid.setVgap(10); - grid.setPadding(new Insets(15, 10, 10, 10)); - grid.add(new Label(XMLController.getLocalBundle().getString("gameTitle") + ":"), 0, 0); - grid.add(gameTitleTF, 1, 0); - grid.add(new Label(XMLController.getLocalBundle().getString("titleID") + ":"), 0, 1); - grid.add(gameTitleIDTF, 1, 1); - grid.add(new Label(XMLController.getLocalBundle().getString("romPath") + ":"), 0, 2); - grid.add(romPathTF, 1, 2); - grid.add(selectPathBtn, 2, 2); - grid.add(new Label(XMLController.getLocalBundle().getString("coverPath") + ":"), 0, 3); - grid.add(gameCoverTF, 1, 3); - grid.add(selectCoverBtn, 2, 3); - - Text bdyText = new Text(bodyText); - - VBox vbox = new VBox(); - vbox.getChildren().addAll(bdyText, grid); - - content.setHeading(new Text(headingText)); - content.setBody(vbox); - content.setActions(cancelBtn, okayBtn); - content.setPrefSize(dialogWidth, dialogHeight); - pane.getChildren().add(stackPane); - AnchorPane.setTopAnchor(stackPane, (pane.getHeight()-content.getPrefHeight())/2); - AnchorPane.setLeftAnchor(stackPane, (pane.getWidth()-content.getPrefWidth())/2); - dialog.show(); - } - - public void setTitle(String title) { - this.title = title; - } - - public void setCoverPath(String coverPath) { - this.coverPath = coverPath; - } - - public void setRomPath(String romPath) { - this.romPath = romPath; - } - - public void setTitleID(String titleID) { - this.titleID = titleID; - } -} diff --git a/src/main/java/com/cemu_UI/uiElements/JFXInfoAlert.java b/src/main/java/com/cemu_UI/uiElements/JFXInfoAlert.java index e4ad63f..40de0a2 100644 --- a/src/main/java/com/cemu_UI/uiElements/JFXInfoAlert.java +++ b/src/main/java/com/cemu_UI/uiElements/JFXInfoAlert.java @@ -42,7 +42,7 @@ public class JFXInfoAlert { * @param headerText Heading text of the alert * @param bodyText Content text of the alert * @param btnStyle Style of the okay button - * @param stage stage to which the dialog belongs + * @param stage Stage to which the Alert belongs */ public JFXInfoAlert(String headingText, String bodyText, String btnStyle, Stage stage) { setHeadingText(headingText); diff --git a/src/main/java/com/cemu_UI/uiElements/JFXOkayCancelAlert.java b/src/main/java/com/cemu_UI/uiElements/JFXOkayCancelAlert.java index 8d3aa23..374f53b 100644 --- a/src/main/java/com/cemu_UI/uiElements/JFXOkayCancelAlert.java +++ b/src/main/java/com/cemu_UI/uiElements/JFXOkayCancelAlert.java @@ -44,6 +44,13 @@ public class JFXOkayCancelAlert { private String okayText = XMLController.getLocalBundle().getString("okayBtnText"); private String cancelText = XMLController.getLocalBundle().getString("cancelBtnText"); + /** + * + * @param headingText Heading text of the alert + * @param bodyText Content text of the alert + * @param btnStyle Style of the buttons + * @param stage Stage to which the Alert belongs + */ public JFXOkayCancelAlert(String headingText, String bodyText, String btnStyle, Stage stage) { setHeadingText(headingText); setBodyText(bodyText); @@ -55,18 +62,14 @@ public class JFXOkayCancelAlert { JFXAlert alert = new JFXAlert<>(stage); JFXButton okayBtn = new JFXButton(okayText); - okayBtn.addEventHandler(ActionEvent.ACTION, (e)-> { - alert.close(); - }); + okayBtn.addEventHandler(ActionEvent.ACTION, (e)-> alert.close()); okayBtn.addEventHandler(ActionEvent.ACTION, okayAction); okayBtn.setButtonType(com.jfoenix.controls.JFXButton.ButtonType.RAISED); okayBtn.setPrefHeight(32); okayBtn.setStyle(btnStyle); JFXButton cancelBtn = new JFXButton(cancelText); - cancelBtn.addEventHandler(ActionEvent.ACTION, (e)-> { - alert.close(); - }); + cancelBtn.addEventHandler(ActionEvent.ACTION, (e)-> alert.close()); cancelBtn.addEventHandler(ActionEvent.ACTION, cancelAction); cancelBtn.setButtonType(com.jfoenix.controls.JFXButton.ButtonType.RAISED); cancelBtn.setPrefHeight(32); diff --git a/src/main/java/com/cemu_UI/uiElements/JFXTextAreaAlert.java b/src/main/java/com/cemu_UI/uiElements/JFXTextAreaAlert.java index 2116eb9..c9bb42a 100644 --- a/src/main/java/com/cemu_UI/uiElements/JFXTextAreaAlert.java +++ b/src/main/java/com/cemu_UI/uiElements/JFXTextAreaAlert.java @@ -38,10 +38,10 @@ public class JFXTextAreaAlert { /** * Creates a new JFoenix Alert to show some information on a TextArea - * @param headingText just the heading - * @param textAreaText all text to needed on the TextArea belongs here + * @param headingText Heading text of the alert + * @param textAreaText Content text of the alert * @param btnStyle Style of the okay button - * @param stage stage to which the dialog belongs + * @param stage Stage to which the Alert belongs */ public JFXTextAreaAlert(String headingText, String textAreaText, String btnStyle, Stage stage) { setHeadingText(headingText);