better coding style

* used gettersand setter for accessing the mainparts of the UI elements, uch aus primarStage
* dbController -> DBController
This commit is contained in:
Jannik 2017-12-12 11:19:33 +01:00
parent 45c36aa09c
commit 9ccb7e6b42
4 changed files with 80 additions and 54 deletions

View File

@ -51,11 +51,13 @@ import javafx.scene.layout.AnchorPane;
public class Main extends Application { public class Main extends Application {
Stage primaryStage; private Stage primaryStage; // TODO same as #Test01
public MainWindowController mainWindowController; // TODO find a better way private MainWindowController mainWindowController; // TODO Needs more testing: if cemu_UI will work as
CloudController cloudController; //normally expected this waring can be removed #Test01
AnchorPane pane; // if not working correctly remove private!
Scene scene; // TODO make private private CloudController cloudController; // TODO same as #Test01
private AnchorPane pane; // TODO same as #Test01
private Scene scene; // TODO same as #Test01
private static String userHome = System.getProperty("user.home"); private static String userHome = System.getProperty("user.home");
private static String userName = System.getProperty("user.name"); private static String userName = System.getProperty("user.name");
private static String osName = System.getProperty("os.name"); private static String osName = System.getProperty("os.name");
@ -69,8 +71,6 @@ public class Main extends Application {
private File directory; private File directory;
private File configFile; private File configFile;
private File gamesDBFile; private File gamesDBFile;
@SuppressWarnings("unused")
private File localDB;
private File pictureCache; private File pictureCache;
private static Logger LOGGER; private static Logger LOGGER;
@ -106,13 +106,11 @@ public class Main extends Application {
directory = new File(dirLinux); directory = new File(dirLinux);
configFile = new File(dirLinux + "/config.xml"); configFile = new File(dirLinux + "/config.xml");
gamesDBFile = new File(dirLinux + "/games.db"); gamesDBFile = new File(dirLinux + "/games.db");
localDB = new File(dirLinux+"/localRoms.db");
pictureCache= new File(dirLinux+"/picture_cache"); pictureCache= new File(dirLinux+"/picture_cache");
} else { } else {
directory = new File(dirWin); directory = new File(dirWin);
configFile = new File(dirWin + "/config.xml"); configFile = new File(dirWin + "/config.xml");
gamesDBFile = new File(dirWin + "/games.db"); gamesDBFile = new File(dirWin + "/games.db");
localDB = new File(dirWin+"/localRoms.db");
pictureCache= new File(dirWin+"/picture_cache"); pictureCache= new File(dirWin+"/picture_cache");
} }
@ -165,7 +163,7 @@ public class Main extends Application {
// loading settings and initialize UI, dbController.main() loads all databases // loading settings and initialize UI, dbController.main() loads all databases
mainWindowController.init(); mainWindowController.init();
mainWindowController.dbController.main(); mainWindowController.dbController.init();
// if cloud sync is activated start sync // if cloud sync is activated start sync
if(mainWindowController.isCloudSync()) { if(mainWindowController.isCloudSync()) {
cloudController.initializeConnection(mainWindowController.getCloudService(), mainWindowController.getCemuPath()); cloudController.initializeConnection(mainWindowController.getCloudService(), mainWindowController.getCemuPath());
@ -308,4 +306,28 @@ public class Main extends Application {
public void stop() { public void stop() {
System.exit(0); System.exit(0);
} }
public Stage getPrimaryStage() {
return primaryStage;
}
public void setPrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
}
public CloudController getCloudController() {
return cloudController;
}
public void setCloudController(CloudController cloudController) {
this.cloudController = cloudController;
}
public AnchorPane getPane() {
return pane;
}
public void setPane(AnchorPane pane) {
this.pane = pane;
}
} }

View File

@ -54,7 +54,7 @@ import org.apache.logging.log4j.Logger;
import com.cemu_UI.controller.SmmdbAPIController; import com.cemu_UI.controller.SmmdbAPIController;
import com.cemu_UI.controller.UpdateController; import com.cemu_UI.controller.UpdateController;
import com.cemu_UI.controller.dbController; import com.cemu_UI.controller.DBController;
import com.cemu_UI.datatypes.CourseTableDataType; import com.cemu_UI.datatypes.CourseTableDataType;
import com.cemu_UI.datatypes.SmmdbApiDataType; import com.cemu_UI.datatypes.SmmdbApiDataType;
import com.cemu_UI.datatypes.UIROMDataType; import com.cemu_UI.datatypes.UIROMDataType;
@ -253,7 +253,7 @@ public class MainWindowController {
private JFXTreeTableColumn<CourseTableDataType, Integer> timeColumn = new JFXTreeTableColumn<>("time"); private JFXTreeTableColumn<CourseTableDataType, Integer> timeColumn = new JFXTreeTableColumn<>("time");
Main main; Main main;
dbController dbController; DBController dbController;
SmmdbAPIController smmdbAPIController; SmmdbAPIController smmdbAPIController;
playGame playGame; playGame playGame;
private static MainWindowController MWC; private static MainWindowController MWC;
@ -322,10 +322,10 @@ public class MainWindowController {
private ImageView cached_white = new ImageView(new Image("icons/ic_cached_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 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"); private Image close_black = new Image("icons/close_black_2048x2048.png");
public void setMain(Main main) { public void setMain(Main m) {
this.main = main; this.main = m;
dbController = new dbController(this); dbController = new DBController(this);
smmdbAPIController = new SmmdbAPIController(); smmdbAPIController = new SmmdbAPIController();
} }
@ -436,7 +436,7 @@ public class MainWindowController {
String headingText = "edit a game"; String headingText = "edit a game";
String bodyText = "You can edit the tile and rom/cover path."; String bodyText = "You can edit the tile and rom/cover path.";
JFXEditGameDialog editGameDialog = new JFXEditGameDialog(headingText, bodyText, dialogBtnStyle, 450, JFXEditGameDialog editGameDialog = new JFXEditGameDialog(headingText, bodyText, dialogBtnStyle, 450,
300, 1, MWC, main.primaryStage, main.pane); 300, 1, MWC, main.getPrimaryStage(), main.getPane());
editGameDialog.setTitle(gameInfo[0]); editGameDialog.setTitle(gameInfo[0]);
editGameDialog.setCoverPath(gameInfo[1]); editGameDialog.setCoverPath(gameInfo[1]);
editGameDialog.setRomPath(gameInfo[2]); editGameDialog.setRomPath(gameInfo[2]);
@ -471,12 +471,12 @@ public class MainWindowController {
EventHandler<ActionEvent> cancelAction = new EventHandler<ActionEvent>() { EventHandler<ActionEvent> cancelAction = new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
// do nothing LOGGER.info("Action canceld by user!");
} }
}; };
JFXOkayCancelDialog removeGameDialog = new JFXOkayCancelDialog(headingText, bodyText, JFXOkayCancelDialog removeGameDialog = new JFXOkayCancelDialog(headingText, bodyText,
dialogBtnStyle, 350, 170, okayAction, cancelAction, main.pane); dialogBtnStyle, 350, 170, okayAction, cancelAction, main.getPane());
removeGameDialog.show(); removeGameDialog.show();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("error while removing " + selectedGameTitle + "(" + selectedGameTitleID + ")", e); LOGGER.error("error while removing " + selectedGameTitle + "(" + selectedGameTitleID + ")", e);
@ -495,7 +495,7 @@ public class MainWindowController {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
DirectoryChooser directoryChooser = new DirectoryChooser(); DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirecroty = directoryChooser.showDialog(main.primaryStage); File selectedDirecroty = directoryChooser.showDialog(main.getPrimaryStage());
String updatePath = selectedDirecroty.getAbsolutePath(); String updatePath = selectedDirecroty.getAbsolutePath();
String[] parts = selectedGameTitleID.split("-"); // split string into 2 parts at "-" String[] parts = selectedGameTitleID.split("-"); // split string into 2 parts at "-"
File srcDir = new File(updatePath); File srcDir = new File(updatePath);
@ -529,12 +529,12 @@ public class MainWindowController {
EventHandler<ActionEvent> cancelAction = new EventHandler<ActionEvent>() { EventHandler<ActionEvent> cancelAction = new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
// do nothing LOGGER.info("Action canceld by user!");
} }
}; };
JFXOkayCancelDialog updateGameDialog = new JFXOkayCancelDialog(headingText, bodyText, JFXOkayCancelDialog updateGameDialog = new JFXOkayCancelDialog(headingText, bodyText,
dialogBtnStyle, 350, 170, okayAction, cancelAction, main.pane); dialogBtnStyle, 350, 170, okayAction, cancelAction, main.getPane());
updateGameDialog.show(); updateGameDialog.show();
} catch (Exception e) { } catch (Exception e) {
LOGGER.warn("trying to update " + selectedGameTitleID + ",which is not a valid type!", e); LOGGER.warn("trying to update " + selectedGameTitleID + ",which is not a valid type!", e);
@ -553,7 +553,7 @@ public class MainWindowController {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
DirectoryChooser directoryChooser = new DirectoryChooser(); DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirecroty = directoryChooser.showDialog(main.primaryStage); File selectedDirecroty = directoryChooser.showDialog(main.getPrimaryStage());
String dlcPath = selectedDirecroty.getAbsolutePath(); String dlcPath = selectedDirecroty.getAbsolutePath();
String[] parts = selectedGameTitleID.split("-"); // split string into 2 parts at "-" String[] parts = selectedGameTitleID.split("-"); // split string into 2 parts at "-"
File srcDir = new File(dlcPath); File srcDir = new File(dlcPath);
@ -587,12 +587,12 @@ public class MainWindowController {
EventHandler<ActionEvent> cancelAction = new EventHandler<ActionEvent>() { EventHandler<ActionEvent> cancelAction = new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
// do nothing LOGGER.info("Action canceld by user!");
} }
}; };
JFXOkayCancelDialog addDLCDialog = new JFXOkayCancelDialog(headingText, bodyText, dialogBtnStyle, JFXOkayCancelDialog addDLCDialog = new JFXOkayCancelDialog(headingText, bodyText, dialogBtnStyle,
350, 170, okayAction, cancelAction, main.pane); 350, 170, okayAction, cancelAction, main.getPane());
addDLCDialog.show(); addDLCDialog.show();
} catch (Exception e) { } catch (Exception e) {
LOGGER.warn("trying to add a dlc to " + selectedGameTitleID + ",which is not a valid type!", e); LOGGER.warn("trying to add a dlc to " + selectedGameTitleID + ",which is not a valid type!", e);
@ -650,7 +650,6 @@ public class MainWindowController {
public void changed(ObservableValue<?> observable, Object oldVal, Object newVal) { public void changed(ObservableValue<?> observable, Object oldVal, Object newVal) {
selected = courseTreeTable.getSelectionModel().getSelectedIndex(); // get selected item selected = courseTreeTable.getSelectionModel().getSelectedIndex(); // get selected item
// FIXME if a item is selected and you change the sorting,you can't select a new item
id = idColumn.getCellData(selected); // get name of selected item id = idColumn.getCellData(selected); // get name of selected item
for (int i = 0; i < courses.size(); i++) { for (int i = 0; i < courses.size(); i++) {
@ -750,14 +749,14 @@ public class MainWindowController {
} }
JFXTextAreaInfoDialog licenseDialog = new JFXTextAreaInfoDialog(headingText, bodyText, JFXTextAreaInfoDialog licenseDialog = new JFXTextAreaInfoDialog(headingText, bodyText,
dialogBtnStyle, 510, 450, main.pane); dialogBtnStyle, 510, 450, main.getPane());
licenseDialog.show(); licenseDialog.show();
licenseDialog.getTextArea().setEditable(false); licenseDialog.getTextArea().setEditable(false);
} }
}; };
JFXOkayCancelDialog licenseOverviewDialog = new JFXOkayCancelDialog(headingText, bodyText, dialogBtnStyle, JFXOkayCancelDialog licenseOverviewDialog = new JFXOkayCancelDialog(headingText, bodyText, dialogBtnStyle,
350, 275, okayAction, cancelAction, main.pane); 350, 275, okayAction, cancelAction, main.getPane());
licenseOverviewDialog.setCancelText("show licenses"); licenseOverviewDialog.setCancelText("show licenses");
licenseOverviewDialog.show(); licenseOverviewDialog.show();
} }
@ -779,7 +778,7 @@ public class MainWindowController {
+ "This Application is made with free Software\n" + "This Application is made with free Software\n"
+ "and licensed under the terms of GNU GPL 3.\n\n" + "and licensed under the terms of GNU GPL 3.\n\n"
+ "www.kellerkinder.xyz"; + "www.kellerkinder.xyz";
JFXInfoDialog aboutDialog = new JFXInfoDialog(headingText, bodyText, dialogBtnStyle, 350, 200, main.pane); JFXInfoDialog aboutDialog = new JFXInfoDialog(headingText, bodyText, dialogBtnStyle, 350, 200, main.getPane());
aboutDialog.show(); aboutDialog.show();
} }
@ -805,9 +804,9 @@ public class MainWindowController {
JFXSpinner spinner = new JFXSpinner(); JFXSpinner spinner = new JFXSpinner();
spinner.setPrefSize(30, 30); spinner.setPrefSize(30, 30);
spinner.setStyle(" -fx-background-color: #f4f4f4;"); spinner.setStyle(" -fx-background-color: #f4f4f4;");
main.pane.getChildren().add(spinner); main.getPane().getChildren().add(spinner);
AnchorPane.setTopAnchor(spinner, (main.pane.getHeight()-spinner.getPrefHeight())/2); AnchorPane.setTopAnchor(spinner, (main.getPane().getHeight()-spinner.getPrefHeight())/2);
AnchorPane.setLeftAnchor(spinner, (main.pane.getWidth()-spinner.getPrefWidth())/2); AnchorPane.setLeftAnchor(spinner, (main.getPane().getWidth()-spinner.getPrefWidth())/2);
Thread thread = new Thread(new Runnable() { Thread thread = new Thread(new Runnable() {
@Override @Override
@ -816,7 +815,7 @@ public class MainWindowController {
Platform.runLater(() -> { Platform.runLater(() -> {
refreshUIData(); // refresh the list of games displayed on screen refreshUIData(); // refresh the list of games displayed on screen
main.pane.getChildren().remove(spinner); main.getPane().getChildren().remove(spinner);
}); });
} }
}); });
@ -882,7 +881,7 @@ public class MainWindowController {
@FXML @FXML
void cemuTFBtnAction(ActionEvent event) { void cemuTFBtnAction(ActionEvent event) {
File cemuDirectory = directoryChooser.showDialog(main.primaryStage); File cemuDirectory = directoryChooser.showDialog(main.getPrimaryStage());
if (cemuDirectory == null) { if (cemuDirectory == null) {
LOGGER.info("No Directory selected"); LOGGER.info("No Directory selected");
} else { } else {
@ -900,7 +899,7 @@ public class MainWindowController {
@FXML @FXML
void romTFBtnAction(ActionEvent event) { void romTFBtnAction(ActionEvent event) {
File romDirectory = directoryChooser.showDialog(main.primaryStage); File romDirectory = directoryChooser.showDialog(main.getPrimaryStage());
if (romDirectory == null) { if (romDirectory == null) {
LOGGER.info("No Directory selected"); LOGGER.info("No Directory selected");
} else { } else {
@ -1050,8 +1049,8 @@ public class MainWindowController {
@Override @Override
public void run() { public void run() {
if (main.cloudController.initializeConnection(getCloudService(), getCemuPath())) { if (main.getCloudController().initializeConnection(getCloudService(), getCemuPath())) {
main.cloudController.sync(getCloudService(), getCemuPath()); main.getCloudController().sync(getCloudService(), getCemuPath());
saveSettings(); saveSettings();
} else { } else {
cloudSyncToggleBtn.setSelected(false); cloudSyncToggleBtn.setSelected(false);
@ -1061,7 +1060,7 @@ public class MainWindowController {
String bodyText = "There was some truble adding your game." String bodyText = "There was some truble adding your game."
+ "\nPlease upload the app.log (which can be found in the cemu_UI directory)" + "\nPlease upload the app.log (which can be found in the cemu_UI directory)"
+ "\nto \"https://github.com/Seil0/cemu_UI/issues\" so we can fix this."; + "\nto \"https://github.com/Seil0/cemu_UI/issues\" so we can fix this.";
JFXInfoDialog cloudSyncErrorDialog = new JFXInfoDialog(headingText, bodyText, dialogBtnStyle, 450, 170, main.pane); JFXInfoDialog cloudSyncErrorDialog = new JFXInfoDialog(headingText, bodyText, dialogBtnStyle, 450, 170, main.getPane());
cloudSyncErrorDialog.show(); cloudSyncErrorDialog.show();
} }
@ -1079,7 +1078,7 @@ public class MainWindowController {
}; };
JFXOkayCancelDialog cloudSyncErrorDialog = new JFXOkayCancelDialog(headingText, bodyText, dialogBtnStyle, JFXOkayCancelDialog cloudSyncErrorDialog = new JFXOkayCancelDialog(headingText, bodyText, dialogBtnStyle,
419, 140, okayAction, cancelAction, main.pane); 419, 140, okayAction, cancelAction, main.getPane());
cloudSyncErrorDialog.show(); cloudSyncErrorDialog.show();
} }
} }
@ -1095,7 +1094,7 @@ public class MainWindowController {
String headingText = "add a new game to cemu_UI"; String headingText = "add a new game to cemu_UI";
String bodyText = ""; String bodyText = "";
JFXEditGameDialog addGameDialog = new JFXEditGameDialog(headingText, bodyText, dialogBtnStyle, 450, 300, 0, JFXEditGameDialog addGameDialog = new JFXEditGameDialog(headingText, bodyText, dialogBtnStyle, 450, 300, 0,
this, main.primaryStage, main.pane); this, main.getPrimaryStage(), main.getPane());
addGameDialog.show(); addGameDialog.show();
} }
@ -1119,7 +1118,7 @@ public class MainWindowController {
String headingTextError = "Error while adding a new Game!"; String headingTextError = "Error while adding a new Game!";
String bodyTextError = "There was some truble adding your game." String bodyTextError = "There was some truble adding your game."
+ "\nOne of the needed values was empty, please try again to add your game."; + "\nOne of the needed values was empty, please try again to add your game.";
JFXInfoDialog errorDialog = new JFXInfoDialog(headingTextError, bodyTextError, dialogBtnStyle, 350, 170, main.pane); JFXInfoDialog errorDialog = new JFXInfoDialog(headingTextError, bodyTextError, dialogBtnStyle, 350, 170, main.getPane());
errorDialog.show(); errorDialog.show();
} else { } else {
@ -1556,7 +1555,7 @@ public class MainWindowController {
} else { } else {
props.setProperty("cloudService", getCloudService()); props.setProperty("cloudService", getCloudService());
} }
props.setProperty("folderID", main.cloudController.getFolderID(getCloudService())); props.setProperty("folderID", main.getCloudController().getFolderID(getCloudService()));
props.setProperty("windowWidth", String.valueOf(mainAnchorPane.getWidth())); props.setProperty("windowWidth", String.valueOf(mainAnchorPane.getWidth()));
props.setProperty("windowHeight", String.valueOf(mainAnchorPane.getHeight())); props.setProperty("windowHeight", String.valueOf(mainAnchorPane.getHeight()));
if(System.getProperty("os.name").equals("Linux")){ if(System.getProperty("os.name").equals("Linux")){
@ -1644,7 +1643,7 @@ public class MainWindowController {
} }
try { try {
main.cloudController.setFolderID(props.getProperty("folderID"), getCloudService()); main.getCloudController().setFolderID(props.getProperty("folderID"), getCloudService());
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("could not load folderID, disable cloud sync. Please contact an developer", e); LOGGER.error("could not load folderID, disable cloud sync. Please contact an developer", e);
setCloudSync(false); setCloudSync(false);

View File

@ -26,17 +26,17 @@ import java.io.IOException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.cemu_UI.controller.dbController; import com.cemu_UI.controller.DBController;
import javafx.application.Platform; import javafx.application.Platform;
public class playGame extends Thread{ public class playGame extends Thread{
MainWindowController mainWindowController; MainWindowController mainWindowController;
dbController dbController; DBController dbController;
private static final Logger LOGGER = LogManager.getLogger(playGame.class.getName()); private static final Logger LOGGER = LogManager.getLogger(playGame.class.getName());
public playGame(MainWindowController m, com.cemu_UI.controller.dbController db){ public playGame(MainWindowController m, com.cemu_UI.controller.DBController db){
mainWindowController = m; mainWindowController = m;
dbController = db; dbController = db;
} }
@ -52,7 +52,7 @@ public class playGame extends Thread{
Process p; Process p;
Platform.runLater(() -> { Platform.runLater(() -> {
mainWindowController.main.primaryStage.setIconified(true); mainWindowController.main.getPrimaryStage().setIconified(true); // minimize cemu_UI
}); });
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
try{ try{
@ -86,13 +86,13 @@ public class playGame extends Thread{
}else{ }else{
mainWindowController.totalPlaytimeBtn.setText(dbController.getTotalPlaytime(selectedGameTitleID)+ " min"); mainWindowController.totalPlaytimeBtn.setText(dbController.getTotalPlaytime(selectedGameTitleID)+ " min");
} }
mainWindowController.main.primaryStage.setIconified(false); mainWindowController.main.getPrimaryStage().setIconified(false); // maximize cemu_UI
}); });
// System.out.println(mainWindowController.getCemuPath()+"/mlc01/emulatorSave/"+); // System.out.println(mainWindowController.getCemuPath()+"/mlc01/emulatorSave/"+);
//sync savegame with cloud service //sync savegame with cloud service
if(mainWindowController.isCloudSync()) { if(mainWindowController.isCloudSync()) {
mainWindowController.main.cloudController.sync(mainWindowController.getCloudService(), mainWindowController.getCemuPath()); mainWindowController.main.getCloudController().sync(mainWindowController.getCloudService(), mainWindowController.getCemuPath());
} }
}catch (IOException | InterruptedException e){ }catch (IOException | InterruptedException e){

View File

@ -47,10 +47,10 @@ import org.xml.sax.SAXException;
import com.cemu_UI.application.MainWindowController; import com.cemu_UI.application.MainWindowController;
public class dbController { public class DBController {
public dbController(MainWindowController m) { public DBController(MainWindowController mwc) {
mainWindowController = m; mainWindowController = mwc;
} }
private MainWindowController mainWindowController; private MainWindowController mainWindowController;
@ -59,9 +59,14 @@ public class dbController {
private String DB_PATH_games; private String DB_PATH_games;
private Connection connection = null; private Connection connection = null;
private Connection connectionGames = null; private Connection connectionGames = null;
private static final Logger LOGGER = LogManager.getLogger(dbController.class.getName()); private static final Logger LOGGER = LogManager.getLogger(DBController.class.getName());
public void main(){ /**
* initialize the sqlite database controller
* load ROM and games database
* load all games
*/
public void init(){
LOGGER.info("<==========starting loading sql==========>"); LOGGER.info("<==========starting loading sql==========>");
loadRomDatabase(); loadRomDatabase();
loadGamesDatabase(); loadGamesDatabase();