From 6fdf8ca30c75521d7d5830ae86f2f25b0725c047 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 6 May 2020 17:33:12 +0200 Subject: [PATCH] compat -> ui.compat --- src/be/nikiroo/utils/resources/Bundle.java | 42 +++++++++---------- src/be/nikiroo/utils/ui/ListModel.java | 6 +-- src/be/nikiroo/utils/ui/UIUtils.java | 35 ++++++++++++---- .../{ => ui}/compat/DefaultListModel6.java | 2 +- .../nikiroo/utils/{ => ui}/compat/JList6.java | 2 +- .../{ => ui}/compat/ListCellRenderer6.java | 2 +- .../utils/{ => ui}/compat/ListModel6.java | 2 +- 7 files changed, 55 insertions(+), 36 deletions(-) rename src/be/nikiroo/utils/{ => ui}/compat/DefaultListModel6.java (94%) rename src/be/nikiroo/utils/{ => ui}/compat/JList6.java (98%) rename src/be/nikiroo/utils/{ => ui}/compat/ListCellRenderer6.java (98%) rename src/be/nikiroo/utils/{ => ui}/compat/ListModel6.java (93%) diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index c757e2b..84efcea 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -133,8 +133,8 @@ public class Bundle> { * @param def * the default value when it is not present in the config file * - * @return the associated value, or NULL if not found (not present in the - * resource file) + * @return the associated value, or def if not found (not present + * in the resource file) */ public String getString(E id, String def) { return getString(id, def, -1); @@ -154,8 +154,9 @@ public class Bundle> { * the item number to get for an array of values, or -1 for * non-arrays * - * @return the associated value, or NULL if not found (not present in the - * resource file) + * @return the associated value, def if not found (not present in + * the resource file) or NULL if the item is specified (not -1) and + * does not exist */ public String getString(E id, String def, int item) { String rep = getString(id.name(), null); @@ -163,7 +164,7 @@ public class Bundle> { rep = getMetaDef(id.name()); } - if (rep == null || rep.isEmpty()) { + if (rep.isEmpty()) { return def; } @@ -273,8 +274,9 @@ public class Bundle> { * the item number to get for an array of values, or -1 for * non-arrays * - * @return the associated value, or NULL if not found (not present in the - * resource file) + * @return the associated value, def if not found (not present in + * the resource file), NULL if the item is specified (not -1) but + * does not exist and NULL if bad key */ public String getStringX(E id, String suffix, String def, int item) { String key = id.name() @@ -932,7 +934,7 @@ public class Bundle> { } /** - * The default {@link MetaInfo.def} value for the given enumeration name. + * The default {@link Meta#def()} value for the given enumeration name. * * @param id * the enumeration name (the "id") @@ -1209,22 +1211,18 @@ public class Bundle> { */ protected void resetMap(ResourceBundle bundle) { this.map.clear(); - for (Field field : type.getDeclaredFields()) { - try { - Meta meta = field.getAnnotation(Meta.class); - if (meta != null) { - E id = Enum.valueOf(type, field.getName()); - - String value; - if (bundle != null) { - value = bundle.getString(id.name()); - } else { - value = null; + if (bundle != null) { + for (Field field : type.getDeclaredFields()) { + try { + Meta meta = field.getAnnotation(Meta.class); + if (meta != null) { + E id = Enum.valueOf(type, field.getName()); + String value = bundle.getString(id.name()); + this.map.put(id.name(), + value == null ? null : value.trim()); } - - this.map.put(id.name(), value == null ? null : value.trim()); + } catch (MissingResourceException e) { } - } catch (MissingResourceException e) { } } } diff --git a/src/be/nikiroo/utils/ui/ListModel.java b/src/be/nikiroo/utils/ui/ListModel.java index 12f6aa6..d76c5dc 100644 --- a/src/be/nikiroo/utils/ui/ListModel.java +++ b/src/be/nikiroo/utils/ui/ListModel.java @@ -14,9 +14,9 @@ import javax.swing.JPopupMenu; import javax.swing.ListCellRenderer; import javax.swing.SwingWorker; -import be.nikiroo.utils.compat.DefaultListModel6; -import be.nikiroo.utils.compat.JList6; -import be.nikiroo.utils.compat.ListCellRenderer6; +import be.nikiroo.utils.ui.compat.DefaultListModel6; +import be.nikiroo.utils.ui.compat.JList6; +import be.nikiroo.utils.ui.compat.ListCellRenderer6; /** * A {@link javax.swing.ListModel} that can maintain 2 lists; one with the diff --git a/src/be/nikiroo/utils/ui/UIUtils.java b/src/be/nikiroo/utils/ui/UIUtils.java index 5861d00..7d78d1e 100644 --- a/src/be/nikiroo/utils/ui/UIUtils.java +++ b/src/be/nikiroo/utils/ui/UIUtils.java @@ -20,24 +20,45 @@ import javax.swing.UnsupportedLookAndFeelException; */ public class UIUtils { /** - * Set a fake "native look & feel" for the application if possible + * Set a fake "native Look & Feel" for the application if possible * (check for the one currently in use, then try GTK). *

* Must be called prior to any GUI work. + * + * @return TRUE if it succeeded */ - static public void setLookAndFeel() { + static public boolean setLookAndFeel() { // native look & feel + String noLF = "javax.swing.plaf.metal.MetalLookAndFeel"; + String lf = UIManager.getSystemLookAndFeelClassName(); + if (lf.equals(noLF)) + lf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; + + return setLookAndFeel(lf); + } + + /** + * Switch to the given Look & Feel for the application if possible + * (check for the one currently in use, then try GTK). + *

+ * Must be called prior to any GUI work. + * + * @param laf + * the Look & Feel to use + * + * @return TRUE if it succeeded + */ + static public boolean setLookAndFeel(String laf) { try { - String noLF = "javax.swing.plaf.metal.MetalLookAndFeel"; - String lf = UIManager.getSystemLookAndFeelClassName(); - if (lf.equals(noLF)) - lf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; - UIManager.setLookAndFeel(lf); + UIManager.setLookAndFeel(laf); + return true; } catch (InstantiationException e) { } catch (ClassNotFoundException e) { } catch (UnsupportedLookAndFeelException e) { } catch (IllegalAccessException e) { } + + return false; } /** diff --git a/src/be/nikiroo/utils/compat/DefaultListModel6.java b/src/be/nikiroo/utils/ui/compat/DefaultListModel6.java similarity index 94% rename from src/be/nikiroo/utils/compat/DefaultListModel6.java rename to src/be/nikiroo/utils/ui/compat/DefaultListModel6.java index 114ac42..3f7552f 100644 --- a/src/be/nikiroo/utils/compat/DefaultListModel6.java +++ b/src/be/nikiroo/utils/ui/compat/DefaultListModel6.java @@ -1,4 +1,4 @@ -package be.nikiroo.utils.compat; +package be.nikiroo.utils.ui.compat; import javax.swing.DefaultListModel; import javax.swing.JList; diff --git a/src/be/nikiroo/utils/compat/JList6.java b/src/be/nikiroo/utils/ui/compat/JList6.java similarity index 98% rename from src/be/nikiroo/utils/compat/JList6.java rename to src/be/nikiroo/utils/ui/compat/JList6.java index ca44165..a504abb 100644 --- a/src/be/nikiroo/utils/compat/JList6.java +++ b/src/be/nikiroo/utils/ui/compat/JList6.java @@ -1,4 +1,4 @@ -package be.nikiroo.utils.compat; +package be.nikiroo.utils.ui.compat; import javax.swing.JList; import javax.swing.ListCellRenderer; diff --git a/src/be/nikiroo/utils/compat/ListCellRenderer6.java b/src/be/nikiroo/utils/ui/compat/ListCellRenderer6.java similarity index 98% rename from src/be/nikiroo/utils/compat/ListCellRenderer6.java rename to src/be/nikiroo/utils/ui/compat/ListCellRenderer6.java index d004849..bc76e80 100644 --- a/src/be/nikiroo/utils/compat/ListCellRenderer6.java +++ b/src/be/nikiroo/utils/ui/compat/ListCellRenderer6.java @@ -1,4 +1,4 @@ -package be.nikiroo.utils.compat; +package be.nikiroo.utils.ui.compat; import java.awt.Component; diff --git a/src/be/nikiroo/utils/compat/ListModel6.java b/src/be/nikiroo/utils/ui/compat/ListModel6.java similarity index 93% rename from src/be/nikiroo/utils/compat/ListModel6.java rename to src/be/nikiroo/utils/ui/compat/ListModel6.java index a1f8c60..938da14 100644 --- a/src/be/nikiroo/utils/compat/ListModel6.java +++ b/src/be/nikiroo/utils/ui/compat/ListModel6.java @@ -1,4 +1,4 @@ -package be.nikiroo.utils.compat; +package be.nikiroo.utils.ui.compat; import javax.swing.JList; -- 2.27.0