X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FMetaInfo.java;h=f7598f190c2f9a5d491fd2b56b9644e06c104d1f;hb=5584adbbbf5444c0039fed2b35dc7d5bb57b71b1;hp=15ff762b9e3cd80d3a1777c912ecec505840c0dc;hpb=76b51de96af0b8dfa9615db37823fffd093fcfe3;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/resources/MetaInfo.java b/src/be/nikiroo/utils/resources/MetaInfo.java index 15ff762..f7598f1 100644 --- a/src/be/nikiroo/utils/resources/MetaInfo.java +++ b/src/be/nikiroo/utils/resources/MetaInfo.java @@ -29,6 +29,8 @@ public class MetaInfo> implements Iterable> { private String name; private String description; + private boolean dirty; + /** * Create a new {@link MetaInfo} from a value (without children). *

@@ -62,24 +64,29 @@ public class MetaInfo> implements Iterable> { description = null; } } - if (description == null) { description = meta.description(); if (description == null) { description = ""; } - if (meta.info() != null && !meta.info().isEmpty()) { - if (!description.isEmpty()) { - description += "\n\n"; - } - description += meta.info(); + } + + String name = idToName(id, null); + + // Special rules for groups: + if (meta.group()) { + String groupName = description.split("\n")[0]; + description = description.substring(groupName.length()).trim(); + if (!groupName.isEmpty()) { + name = groupName; } } - String name = id.toString(); - if (name.length() > 1) { - name = name.substring(0, 1) + name.substring(1).toLowerCase(); - name = name.replace("_", " "); + if (meta.def() != null && !meta.def().isEmpty()) { + if (!description.isEmpty()) { + description += "\n\n"; + } + description += "(Default value: " + meta.def() + ")"; } this.name = name; @@ -89,20 +96,34 @@ public class MetaInfo> implements Iterable> { } /** - * The name of this item, deduced from its ID. + * For normal items, this is the name of this item, deduced from its ID (or + * in other words, it is the ID but presented in a displayable form). + *

+ * For group items, this is the first line of the description if it is not + * empty (else, it is the ID in the same way as normal items). *

- * In other words, it is the ID but presented in a displayable form. + * Never NULL. * - * @return the name + * + * @return the name, never NULL */ public String getName() { return name; } /** - * The description of this item (information to present to the user). + * A description for this item: what it is or does, how to explain that item + * to the user including what can be used here (i.e., %s = file name, %d = + * file size...). + *

+ * For group, the first line ('\\n'-separated) will be used as a title while + * the rest will be the description. + *

+ * If a default value is known, it will be specified here, too. + *

+ * Never NULL. * - * @return the description + * @return the description, not NULL */ public String getDescription() { return description; @@ -120,12 +141,38 @@ public class MetaInfo> implements Iterable> { /** * The allowed list of values that a {@link Format#FIXED_LIST} item is * allowed to be, or a list of suggestions for {@link Format#COMBO_LIST} - * items. + * items. Also works for {@link Format#LOCALE}. + *

+ * Will always allow an empty string in addition to the rest. * * @return the list of values */ public String[] getAllowedValues() { - return meta.list(); + String[] list = meta.list(); + + String[] withEmpty = new String[list.length + 1]; + withEmpty[0] = ""; + for (int i = 0; i < list.length; i++) { + withEmpty[i + 1] = list[i]; + } + + return withEmpty; + } + + /** + * Return all the languages known by the program for this bundle. + *

+ * This only works for {@link TransBundle}, and will return an empty list if + * this is not a {@link TransBundle}. + * + * @return the known language codes + */ + public List getKnownLanguages() { + if (bundle instanceof TransBundle) { + return ((TransBundle) bundle).getKnownLanguages(); + } + + return new ArrayList(); } /** @@ -142,6 +189,42 @@ public class MetaInfo> implements Iterable> { return meta.array(); } + /** + * A manual flag to specify if the data has been changed or not, which can + * be used by {@link MetaInfo#save(boolean)}. + * + * @return TRUE if it is dirty (if it has changed) + */ + public boolean isDirty() { + return dirty; + } + + /** + * A manual flag to specify that the data has been changed, which can be + * used by {@link MetaInfo#save(boolean)}. + */ + public void setDirty() { + this.dirty = true; + } + + /** + * The number of items in this item if it {@link MetaInfo#isArray()}, or -1 + * if not. + * + * @param useDefaultIfEmpty + * check the size of the default list instead if the list is + * empty + * + * @return -1 or the number of items + */ + public int getListSize(boolean useDefaultIfEmpty) { + if (!isArray()) { + return -1; + } + + return BundleHelper.getListSize(getString(-1, useDefaultIfEmpty)); + } + /** * This item is only used as a group, not as an option. *

@@ -158,73 +241,154 @@ public class MetaInfo> implements Iterable> { /** * The value stored by this item, as a {@link String}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * @param useDefaultIfEmpty + * use the default value instead of NULL if the setting is not + * set + * * @return the value */ - public String getString() { + public String getString(int item, boolean useDefaultIfEmpty) { + if (isArray() && item >= 0) { + List values = BundleHelper.parseList(value, -1); + if (values != null && item < values.size()) { + return values.get(item); + } + + if (useDefaultIfEmpty) { + return getDefaultString(item); + } + + return null; + } + + if (value == null && useDefaultIfEmpty) { + return getDefaultString(item); + } + return value; } /** * The default value of this item, as a {@link String}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * * @return the default value */ - public String getDefaultString() { + public String getDefaultString(int item) { + if (isArray() && item >= 0) { + List values = BundleHelper.parseList(meta.def(), item); + if (values != null && item < values.size()) { + return values.get(item); + } + + return null; + } + return meta.def(); } /** * The value stored by this item, as a {@link Boolean}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * @param useDefaultIfEmpty + * use the default value instead of NULL if the setting is not + * set + * * @return the value */ - public Boolean getBoolean() { - return BundleHelper.parseBoolean(getString()); + public Boolean getBoolean(int item, boolean useDefaultIfEmpty) { + return BundleHelper + .parseBoolean(getString(item, useDefaultIfEmpty), -1); } /** * The default value of this item, as a {@link Boolean}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * * @return the default value */ - public Boolean getDefaultBoolean() { - return BundleHelper.parseBoolean(getDefaultString()); + public Boolean getDefaultBoolean(int item) { + return BundleHelper.parseBoolean(getDefaultString(item), -1); } /** * The value stored by this item, as a {@link Character}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * @param useDefaultIfEmpty + * use the default value instead of NULL if the setting is not + * set + * * @return the value */ - public Character getCharacter() { - return BundleHelper.parseCharacter(getString()); + public Character getCharacter(int item, boolean useDefaultIfEmpty) { + return BundleHelper.parseCharacter(getString(item, useDefaultIfEmpty), + -1); } /** * The default value of this item, as a {@link Character}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * * @return the default value */ - public Character getDefaultCharacter() { - return BundleHelper.parseCharacter(getDefaultString()); + public Character getDefaultCharacter(int item) { + return BundleHelper.parseCharacter(getDefaultString(item), -1); } /** * The value stored by this item, as an {@link Integer}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * @param useDefaultIfEmpty + * use the default value instead of NULL if the setting is not + * set + * * @return the value */ - public Integer getInteger() { - return BundleHelper.parseInteger(getString()); + public Integer getInteger(int item, boolean useDefaultIfEmpty) { + return BundleHelper + .parseInteger(getString(item, useDefaultIfEmpty), -1); } /** * The default value of this item, as an {@link Integer}. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * * @return the default value */ - public Integer getDefaultInteger() { - return BundleHelper.parseInteger(getDefaultString()); + public Integer getDefaultInteger(int item) { + return BundleHelper.parseInteger(getDefaultString(item), -1); } /** @@ -233,10 +397,18 @@ public class MetaInfo> implements Iterable> { *

* The returned colour value is an ARGB value. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * @param useDefaultIfEmpty + * use the default value instead of NULL if the setting is not + * set + * * @return the value */ - public Integer getColor() { - return BundleHelper.parseColor(getString()); + public Integer getColor(int item, boolean useDefaultIfEmpty) { + return BundleHelper.parseColor(getString(item, useDefaultIfEmpty), -1); } /** @@ -245,10 +417,15 @@ public class MetaInfo> implements Iterable> { *

* The returned colour value is an ARGB value. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * * @return the value */ - public Integer getDefaultColor() { - return BundleHelper.parseColor(getDefaultString()); + public Integer getDefaultColor(int item) { + return BundleHelper.parseColor(getDefaultString(item), -1); } /** @@ -257,10 +434,18 @@ public class MetaInfo> implements Iterable> { * The list of values is comma-separated and each value is surrounded by * double-quotes; backslashes and double-quotes are escaped by a backslash. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * @param useDefaultIfEmpty + * use the default value instead of NULL if the setting is not + * set + * * @return the value */ - public List getList() { - return BundleHelper.parseList(getString()); + public List getList(int item, boolean useDefaultIfEmpty) { + return BundleHelper.parseList(getString(item, useDefaultIfEmpty), -1); } /** @@ -269,10 +454,15 @@ public class MetaInfo> implements Iterable> { * The list of values is comma-separated and each value is surrounded by * double-quotes; backslashes and double-quotes are escaped by a backslash. * + * @param item + * the item number to get for an array of values, or -1 to get + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + * * @return the value */ - public List getDefaultList() { - return BundleHelper.parseList(getDefaultString()); + public List getDefaultList(int item) { + return BundleHelper.parseList(getDefaultString(item), -1); } /** @@ -280,9 +470,17 @@ public class MetaInfo> implements Iterable> { * * @param value * the new value - */ - public void setString(String value) { - this.value = value; + * @param item + * the item number to set for an array of values, or -1 to set + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) + */ + public void setString(String value, int item) { + if (isArray() && item >= 0) { + this.value = BundleHelper.fromList(this.value, value, item); + } else { + this.value = value; + } } /** @@ -290,9 +488,13 @@ public class MetaInfo> implements Iterable> { * * @param value * the new value + * @param item + * the item number to set for an array of values, or -1 to set + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) */ - public void setBoolean(boolean value) { - setString(BundleHelper.fromBoolean(value)); + public void setBoolean(boolean value, int item) { + setString(BundleHelper.fromBoolean(value), item); } /** @@ -300,9 +502,13 @@ public class MetaInfo> implements Iterable> { * * @param value * the new value + * @param item + * the item number to set for an array of values, or -1 to set + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) */ - public void setCharacter(char value) { - setString(BundleHelper.fromCharacter(value)); + public void setCharacter(char value, int item) { + setString(BundleHelper.fromCharacter(value), item); } /** @@ -310,22 +516,30 @@ public class MetaInfo> implements Iterable> { * * @param value * the new value + * @param item + * the item number to set for an array of values, or -1 to set + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) */ - public void setInteger(int value) { - setString(BundleHelper.fromInteger(value)); + public void setInteger(int value, int item) { + setString(BundleHelper.fromInteger(value), item); } /** * The value stored by this item, as a colour (represented here as an * {@link Integer}) if it represents a colour, or NULL if it doesn't. *

- * The returned colour value is an ARGB value. + * The colour value is an ARGB value. * * @param value * the value + * @param item + * the item number to set for an array of values, or -1 to set + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) */ - public void setColor(int value) { - setString(BundleHelper.fromColor(value)); + public void setColor(int value, int item) { + setString(BundleHelper.fromColor(value), item); } /** @@ -336,10 +550,13 @@ public class MetaInfo> implements Iterable> { * * @param value * the {@link String} representation - * + * @param item + * the item number to set for an array of values, or -1 to set + * the whole value (has no effect if {@link MetaInfo#isArray()} + * is FALSE) */ - public void setList(List value) { - setString(BundleHelper.fromList(value)); + public void setList(List value, int item) { + setString(BundleHelper.fromList(value), item); } /** @@ -347,12 +564,17 @@ public class MetaInfo> implements Iterable> { * saved will be used. */ public void reload() { - value = bundle.getString(id); - for (Runnable listener : reloadedListeners) { + if (bundle.isSet(id, false)) { + value = bundle.getString(id); + } else { + value = null; + } + + // Copy the list so we can create new listener in a listener + for (Runnable listener : new ArrayList(reloadedListeners)) { try { listener.run(); } catch (Exception e) { - // TODO: error management? e.printStackTrace(); } } @@ -372,17 +594,27 @@ public class MetaInfo> implements Iterable> { /** * Save the current value to the {@link Bundle}. + *

+ * Note that listeners will be called before the dirty check and + * before saving the value. + * + * @param onlyIfDirty + * only save the data if the dirty flag is set (will reset the + * dirty flag) */ - public void save() { - for (Runnable listener : saveListeners) { + public void save(boolean onlyIfDirty) { + // Copy the list so we can create new listener in a listener + for (Runnable listener : new ArrayList(saveListeners)) { try { listener.run(); } catch (Exception e) { - // TODO: error management? e.printStackTrace(); } } - bundle.setString(id, value); + + if (!onlyIfDirty || isDirty()) { + bundle.setString(id, value); + } } /** @@ -452,6 +684,7 @@ public class MetaInfo> implements Iterable> { if (parent != null) { list.remove(i--); parent.children.add(info); + info.name = idToName(info.id, parent.id); } } @@ -495,4 +728,20 @@ public class MetaInfo> implements Iterable> { return group; } + + static private > String idToName(E id, E prefix) { + String name = id.toString(); + if (prefix != null && name.startsWith(prefix.toString())) { + name = name.substring(prefix.toString().length()); + } + + if (name.length() > 0) { + name = name.substring(0, 1).toUpperCase() + + name.substring(1).toLowerCase(); + } + + name = name.replace("_", " "); + + return name.trim(); + } }