From 64f0b7d0c8450ee3e62e9f4a060529b402a6a299 Mon Sep 17 00:00:00 2001 From: teridax Date: Mon, 17 Jul 2023 18:22:24 +0200 Subject: [PATCH] setting look and feel automatically --- src/me/teridax/jcash/Main.java | 31 +++++++++++++++++++++++++ src/me/teridax/jcash/gui/Loader.java | 5 ++-- src/me/teridax/jcash/lang/languages.csv | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/me/teridax/jcash/Main.java b/src/me/teridax/jcash/Main.java index d438122..937f592 100644 --- a/src/me/teridax/jcash/Main.java +++ b/src/me/teridax/jcash/Main.java @@ -40,11 +40,42 @@ public final class Main { Locales.autodetectDefaultLocale(); + setPlatformDependingTheme(); + // create main instance and show the login screen instance(); getInstance().showLoginScreen(); } + private static void setPlatformDependingTheme() { + // default look and feel + var laf = UIManager.getCrossPlatformLookAndFeelClassName(); + + // runtime os + String os = System.getProperty("os.name").toLowerCase(); + LOGGER.fine("Detected operating system: " + os); + + // set look and feel class name depending on the platform we run on + if (os.contains("win") || os.contains("mac")) { + LOGGER.info("Detected Windows or MacOS based operating system, using system look and feel"); + laf = UIManager.getSystemLookAndFeelClassName(); + } else if (os.contains("nix") || os.contains("nux") || os.contains("aix") || os.contains("sunos")) { + LOGGER.info("Detected Unix/Linux based operating system, using GTK look and feel"); + laf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; + } else { + LOGGER.warning("Unable to detect operating system: " + os); + } + + try { + LOGGER.info("Setting look and feel to class: " + laf); + UIManager.setLookAndFeel(laf); + } catch (ClassNotFoundException | UnsupportedLookAndFeelException e) { + LOGGER.severe("Look and feel class not found: " + e.getMessage()); + } catch (InstantiationException | IllegalAccessException e) { + LOGGER.warning("Could not set look and feel: " + e.getMessage()); + } + } + /** * Get the main singleton instance of this program * @return the singleton instance of this class diff --git a/src/me/teridax/jcash/gui/Loader.java b/src/me/teridax/jcash/gui/Loader.java index a9fce6a..7d446b9 100644 --- a/src/me/teridax/jcash/gui/Loader.java +++ b/src/me/teridax/jcash/gui/Loader.java @@ -8,6 +8,7 @@ import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import static javax.swing.JFileChooser.APPROVE_OPTION; +import static me.teridax.jcash.lang.Translator.translate; /** * Utility class for loading a BMS configuration from a csv file. @@ -17,7 +18,7 @@ public class Loader { /** * Filter that only allows for files with *.csv extension */ - private static final FileNameExtensionFilter FILE_FILTER = new FileNameExtensionFilter("Comma separated value spreadsheet", "csv", "CSV"); + private static final FileNameExtensionFilter FILE_FILTER = new FileNameExtensionFilter(translate("Comma separated value spreadsheet"), "csv", "CSV"); private Loader() {} @@ -35,7 +36,7 @@ public class Loader { fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); fileChooser.setAcceptAllFileFilterUsed(false); - if (fileChooser.showDialog(null, Translator.translate("Load database")) == APPROVE_OPTION) { + if (fileChooser.showDialog(null, translate("Load database")) == APPROVE_OPTION) { // parse file content try { return BankingManagementSystem.loadFromCsv(fileChooser.getSelectedFile().toPath()); diff --git a/src/me/teridax/jcash/lang/languages.csv b/src/me/teridax/jcash/lang/languages.csv index 84ce604..46e76d2 100644 --- a/src/me/teridax/jcash/lang/languages.csv +++ b/src/me/teridax/jcash/lang/languages.csv @@ -46,3 +46,4 @@ Address,Adresse,Dirección,Adresse,地址 Account,Konto,Cuenta,Compte,账户 Closing JCash,JCash wird geschlossen,JCash está cerrado,JCash est fermé,JCash 已关闭 you're logged out,Du bist abgemeldet,Ha cerrado la sesión,Tu es déconnecté,您已注销 +Comma separated value spreadsheet,Tabelle mit kommagetrennten Werten,Planilla de valores separados por comas,Feuille de calcul à valeurs séparées par des virgules,逗号分隔值电子表格 \ No newline at end of file