From 4840f1a45e02bb85a5990780594c4d8080d3c880 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 30 Oct 2019 08:39:15 +0100 Subject: [PATCH] Bundles/TransBundles: fix for def values --- src/be/nikiroo/utils/resources/Bundle.java | 38 ++++++++++++++----- src/be/nikiroo/utils/resources/MetaInfo.java | 9 +++++ .../nikiroo/utils/resources/TransBundle.java | 6 +++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 0c57bf99..c757e2b1 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -95,8 +95,8 @@ public class Bundle> { * @param name * the id of the setting to check * @param includeDefaultValue - * TRUE to only return false when the setting is not set AND - * there is no default value + * TRUE to only return false when the setting is explicitly set + * to NULL (and not just "no set") in the change maps * * @return TRUE if the setting is set */ @@ -160,13 +160,7 @@ public class Bundle> { public String getString(E id, String def, int item) { String rep = getString(id.name(), null); if (rep == null) { - try { - Meta meta = type.getDeclaredField(id.name()).getAnnotation( - Meta.class); - rep = meta.def(); - } catch (NoSuchFieldException e) { - } catch (SecurityException e) { - } + rep = getMetaDef(id.name()); } if (rep == null || rep.isEmpty()) { @@ -937,9 +931,35 @@ public class Bundle> { return changeMap.containsKey(key) || map.containsKey(key); } + /** + * The default {@link MetaInfo.def} value for the given enumeration name. + * + * @param id + * the enumeration name (the "id") + * + * @return the def value in the {@link MetaInfo} or "" if none (never NULL) + */ + protected String getMetaDef(String id) { + String rep = ""; + try { + Meta meta = type.getDeclaredField(id).getAnnotation(Meta.class); + rep = meta.def(); + } catch (NoSuchFieldException e) { + } catch (SecurityException e) { + } + + if (rep == null) { + rep = ""; + } + + return rep; + } + /** * Get the value for the given key if it exists in the internal map, or * def if not. + *

+ * DO NOT get the default meta value (MetaInfo.def()). * * @param key * the key to check for diff --git a/src/be/nikiroo/utils/resources/MetaInfo.java b/src/be/nikiroo/utils/resources/MetaInfo.java index f7598f19..917c2100 100644 --- a/src/be/nikiroo/utils/resources/MetaInfo.java +++ b/src/be/nikiroo/utils/resources/MetaInfo.java @@ -649,6 +649,15 @@ public class MetaInfo> implements Iterable> { return children; } + /** + * The number of sub-items, if any. + * + * @return the number or 0 + */ + public int size() { + return children.size(); + } + @Override public Iterator> iterator() { return children.iterator(); diff --git a/src/be/nikiroo/utils/resources/TransBundle.java b/src/be/nikiroo/utils/resources/TransBundle.java index 28fa2802..7b2edb1c 100644 --- a/src/be/nikiroo/utils/resources/TransBundle.java +++ b/src/be/nikiroo/utils/resources/TransBundle.java @@ -138,6 +138,9 @@ public class TransBundle> extends Bundle { result = "[" + key.toLowerCase() + "]"; } else if (containsKey(key)) { result = getString(key, null); + if (result == null) { + result = getMetaDef(id.name()); + } } else { result = null; } @@ -326,6 +329,9 @@ public class TransBundle> extends Bundle { String name = id.name() + "_NOUTF"; if (containsKey(name)) { String value = getString(name, null); + if (value == null) { + value = getMetaDef(id.name()); + } boolean set = isSet(id, false); writeValue(writer, name, value, set); } -- 2.27.0