Skip to content

Commit

Permalink
Avoid Duplication Of Methods (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
hardaman committed Nov 10, 2023
1 parent 1c91361 commit 6643571
Showing 1 changed file with 37 additions and 46 deletions.
83 changes: 37 additions & 46 deletions src/main/java/me/goral/keepmypassworddesktop/MainApp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.goral.keepmypassworddesktop;

import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
Expand All @@ -11,70 +13,59 @@
import me.goral.keepmypassworddesktop.util.AlertsUtil;
import me.goral.keepmypassworddesktop.util.ConfUtil;

import java.util.Locale;
import java.util.ResourceBundle;

public class MainApp extends Application {

private static Stage guiStage;
private Stage guiStage;

public static Stage getStage() {
return guiStage;
}

public static Locale loc = setLocale();
public static ResourceBundle lang = setLanguageBundle(loc);

/**
* The function returns a Locale object that is set to the language specified in the configuration file
*
* @return The locale object.
*/
public static Locale setLocale(){
String lang = ConfUtil.getConfigLanguage();
return new Locale(lang);
}

/**
* This function returns a ResourceBundle object that contains the localized strings for the given locale
*
* @param loc The locale of the language you want to use.
* @return The ResourceBundle object.
*/
public static ResourceBundle setLanguageBundle(Locale loc){
return ResourceBundle
.getBundle("language", loc); //NON-NLS
}
private static final int WINDOW_WIDTH = 750;
private static final int WINDOW_HEIGHT = 500;

@Override
public void start(Stage stage) {

try {
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("layouts/main-app-view.fxml"));
guiStage = stage;
Parent root = loader.load();
Scene scene = new Scene(root);

if (ConfUtil.setWorkingDirectory() == 0) throw new Exception("Error while reading os directory");
if (ConfUtil.setWorkingDirectory() == 0) {
throw new Exception("Error while reading os directory");
}
Locale locale = loadLocale();
ResourceBundle resourceBundle = loadResourceBundle(locale);

String css = MainApp.class.getResource("styles/main.css").toExternalForm();
MainAppController mainController = loader.getController();
FXMLLoader loader = new FXMLLoader(getClass().getResource("layouts/main-app-view.fxml"));
Parent root = loader.load();

Scene scene = new Scene(root);
String css = getClass().getResource("styles/main.css").toExternalForm();
scene.getStylesheets().add(css);
guiStage.initStyle(StageStyle.DECORATED);
guiStage.setTitle(lang.getString("appName"));

guiStage.setTitle(resourceBundle.getString("appName"));
guiStage.getIcons().add(new Image(getClass().getResourceAsStream("/me/goral/keepmypassworddesktop/images/access-32.png")));
guiStage.setResizable(false);
guiStage.getIcons().add(new Image(MainApp.class.getResourceAsStream("/me/goral/keepmypassworddesktop/images/access-32.png")));
guiStage.setWidth(750);
guiStage.setHeight(500);
guiStage.initStyle(StageStyle.DECORATED);
guiStage.setScene(scene);
guiStage.show();
guiStage.setWidth(WINDOW_WIDTH);
guiStage.setHeight(WINDOW_HEIGHT);

MainAppController mainController = loader.getController();
mainController.setResourceBundle(resourceBundle);
mainController.handleAppRun();
} catch (Exception e){

guiStage.show();
} catch (Exception e) {
AlertsUtil.showExceptionStackTraceDialog(e);
}
}

private Locale loadLocale() {
String lang = ConfUtil.getConfigLanguage();
return new Locale(lang);
}

private ResourceBundle loadResourceBundle(Locale locale) {
return ResourceBundle.getBundle("language", locale, new UTF8Control());
}

public static void main(String[] args) {
launch();
}
}
}

0 comments on commit 6643571

Please sign in to comment.