X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=inline;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundle.java;h=cfdc9e0836871347ec76f6c9f11c84acee4ed90d;hb=b3aad1f93ce78c9806361ca1659defcf99b45bdc;hp=1c63d69f04f84276eeaf19ee3a29e028f52fae54;hpb=ec1f3444e9f238ce1559d5fff32eb5a7ab8aba53;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 1c63d69..cfdc9e0 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -1,5 +1,6 @@ package be.nikiroo.utils.resources; +import java.awt.Color; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; @@ -8,7 +9,6 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; -import java.io.UnsupportedEncodingException; import java.io.Writer; import java.lang.reflect.Field; import java.util.ArrayList; @@ -18,9 +18,6 @@ import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; -import be.nikiroo.utils.resources.Bundles; -import be.nikiroo.utils.resources.Meta; - /** * This class encapsulate a {@link ResourceBundle} in UTF-8. It only allows to * retrieve values associated to an enumeration, and allows some additional @@ -61,7 +58,7 @@ public class Bundle> { * resource file) */ public String getString(E id) { - return getStringX(id, ""); + return getStringX(id, null); } /** @@ -78,8 +75,7 @@ public class Bundle> { */ public String getStringX(E id, String suffix) { String key = id.name() - + ((suffix == null || suffix.isEmpty()) ? "" : "_" - + suffix.toUpperCase()); + + (suffix == null ? "" : "_" + suffix.toUpperCase()); if (containsKey(key)) { return getString(key).trim(); @@ -184,10 +180,36 @@ public class Bundle> { } /** - * Create/update the .properties file. Will use the most likely candidate as - * base if the file does not already exists and this resource is - * translatable (for instance, "en_US" will use "en" as a base if the - * resource is a translation file). + * Return the value associated to the given id as a {@link Color}. + * + * @param the + * id of the value to get + * + * @return the associated value + */ + public Color getColor(E id) { + Color color = null; + + String bg = getString(id).trim(); + if (bg.startsWith("#") && bg.length() == 7) { + try { + color = new Color(Integer.parseInt(bg.substring(1, 3), 16), + Integer.parseInt(bg.substring(3, 5), 16), + Integer.parseInt(bg.substring(5, 7), 16)); + } catch (NumberFormatException e) { + color = null; // no changes + } + } + + return color; + } + + /** + * Create/update the .properties file. + *

+ * Will use the most likely candidate as base if the file does not already + * exists and this resource is translatable (for instance, "en_US" will use + * "en" as a base if the resource is a translation file). * * @param path * the path where the .properties files are @@ -223,6 +245,13 @@ public class Bundle> { writer.close(); } + /** + * Reload the {@link Bundle} data files. + */ + public void reload() { + setBundle(name, null); + } + /** * Check if the internal map contains the given key. * @@ -241,26 +270,17 @@ public class Bundle> { } /** - * Get the value for the given key if it exists in the internal map. + * Get the value for the given key if it exists in the internal map, or NULL + * if not. * * @param key * the key to check for * - * @return true if it does + * @return the value, or NULL */ protected String getString(String key) { if (containsKey(key)) { - try { - // Note: it is also possible to fix the borked charset issue - // with a custom ResourceBundle#Control class, but this one, - // while a workaround, depend less upon the JRE classes, which - // may change - return new String(map.getString(key).getBytes("ISO-8859-1"), - "UTF-8"); - } catch (UnsupportedEncodingException e) { - // Those 2 encodings are always supported - e.printStackTrace(); - } + return map.getString(key); } return null; @@ -380,7 +400,7 @@ public class Bundle> { value = ""; } - String[] lines = value.replaceAll("\\\t", "\\\\\\t").split("\n"); + String[] lines = value.replaceAll("\t", "\\t").split("\n"); for (int i = 0; i < lines.length; i++) { writer.write(lines[i]); if (i < lines.length - 1) { @@ -432,7 +452,7 @@ public class Bundle> { if (map == null) { map = ResourceBundle.getBundle(type.getPackage().getName() + "." - + name.name(), locale); + + name.name(), locale, new FixedResourceBundleControl()); } }