X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundle.java;h=84efceafabfacac5d1edf887dc49f3542bd76725;hb=919bbc354cd2555eb0955be0ef2dcf338047d022;hp=92b02eb43e75cc59eec8cb5a6c3bf80c20c7f300;hpb=e8aa5bf9227a0d6a6d0bb6a8bc0cc04d0f4d601a;p=fanfix.git diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java deleted file mode 100644 index 92b02eb..0000000 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ /dev/null @@ -1,846 +0,0 @@ -package be.nikiroo.utils.resources; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.MissingResourceException; -import java.util.PropertyResourceBundle; -import java.util.ResourceBundle; - -/** - * This class encapsulate a {@link ResourceBundle} in UTF-8. It allows to - * retrieve values associated to an enumeration, and allows some additional - * methods. - *
- * It also sports a writable change map, and you can save back the
- * {@link Bundle} to file with {@link Bundle#updateFile(String)}.
- *
- * @param
- * Will only accept suffixes that form an existing id.
- *
- * @param id
- * the id of the value to get
- * @param suffix
- * the runtime suffix
- *
- * @return the associated value, or NULL if not found (not present in the
- * resource file)
- */
- public String getStringX(E id, String suffix) {
- String key = id.name()
- + (suffix == null ? "" : "_" + suffix.toUpperCase());
-
- try {
- id = Enum.valueOf(type, key);
- return getString(id);
- } catch (IllegalArgumentException e) {
-
- }
-
- return null;
- }
-
- /**
- * Set the value associated to the given id as a {@link String} suffixed
- * with the runtime value "_suffix" (that is, "_" and suffix).
- *
- * Will only accept suffixes that form an existing id.
- *
- * @param id
- * the id of the value to get
- * @param suffix
- * the runtime suffix
- * @param value
- * the value
- */
- public void setStringX(E id, String suffix, String value) {
- String key = id.name()
- + (suffix == null ? "" : "_" + suffix.toUpperCase());
-
- try {
- id = Enum.valueOf(type, key);
- setString(id, value);
- } catch (IllegalArgumentException e) {
-
- }
- }
-
- /**
- * Return the value associated to the given id as a {@link Boolean}.
- *
- * @param id
- * the id of the value to get
- *
- * @return the associated value
- */
- public Boolean getBoolean(E id) {
- String str = getString(id);
- if (str != null && str.length() > 0) {
- if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("on")
- || str.equalsIgnoreCase("yes"))
- return true;
- if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("off")
- || str.equalsIgnoreCase("no"))
- return false;
-
- }
-
- return null;
- }
-
- /**
- * Return the value associated to the given id as a {@link Boolean}.
- *
- * @param id
- * the id of the value to get
- * @param def
- * the default value when it is not present in the config file or
- * if it is not a boolean value
- *
- * @return the associated value
- */
- public boolean getBoolean(E id, boolean def) {
- Boolean b = getBoolean(id);
- if (b != null)
- return b;
-
- return def;
- }
-
- /**
- * Return the value associated to the given id as an {@link Integer}.
- *
- * @param id
- * the id of the value to get
- *
- * @return the associated value
- */
- public Integer getInteger(E id) {
- try {
- return Integer.parseInt(getString(id));
- } catch (Exception e) {
- }
-
- return null;
- }
-
- /**
- * Return the value associated to the given id as an int.
- *
- * @param id
- * the id of the value to get
- * @param def
- * the default value when it is not present in the config file or
- * if it is not a int value
- *
- * @return the associated value
- */
- public int getInteger(E id, int def) {
- Integer i = getInteger(id);
- if (i != null)
- return i;
-
- return def;
- }
-
- /**
- * Return the value associated to the given id as a {@link Character}.
- *
- * @param id
- * the id of the value to get
- *
- * @return the associated value
- */
- public Character getCharacter(E id) {
- String s = getString(id).trim();
- if (s.length() > 0) {
- return s.charAt(0);
- }
-
- return null;
- }
-
- /**
- * Return the value associated to the given id as a {@link Character}.
- *
- * @param id
- * the id of the value to get
- * @param def
- * the default value when it is not present in the config file or
- * if it is not a char value
- *
- * @return the associated value
- */
- public char getCharacter(E id, char def) {
- String s = getString(id);
- if (s != null && s.length() > 0) {
- return s.trim().charAt(0);
- }
-
- return def;
- }
-
- /**
- * Return the value associated to the given id as a colour if it is found
- * and can be parsed.
- *
- * The returned value is an ARGB value.
- *
- * @param id
- * the id of the value to get
- *
- * @return the associated value
- */
- public Integer getColor(E id) {
- Integer rep = null;
-
- String bg = getString(id).trim();
-
- int r = 0, g = 0, b = 0, a = -1;
- if (bg.startsWith("#") && (bg.length() == 7 || bg.length() == 9)) {
- try {
- r = Integer.parseInt(bg.substring(1, 3), 16);
- g = Integer.parseInt(bg.substring(3, 5), 16);
- b = Integer.parseInt(bg.substring(5, 7), 16);
- if (bg.length() == 9) {
- a = Integer.parseInt(bg.substring(7, 9), 16);
- } else {
- a = 255;
- }
-
- } catch (NumberFormatException e) {
- // no changes
- }
- }
-
- // Try by name if still not found
- if (a == -1) {
- if ("black".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 0;
- b = 0;
- } else if ("white".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 255;
- b = 255;
- } else if ("red".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 0;
- b = 0;
- } else if ("green".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 255;
- b = 0;
- } else if ("blue".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 0;
- b = 255;
- } else if ("grey".equalsIgnoreCase(bg)
- || "gray".equalsIgnoreCase(bg)) {
- a = 255;
- r = 128;
- g = 128;
- b = 128;
- } else if ("cyan".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 255;
- b = 255;
- } else if ("magenta".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 0;
- b = 255;
- } else if ("yellow".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 255;
- b = 0;
- }
- }
-
- if (a != -1) {
- rep = ((a & 0xFF) << 24) //
- | ((r & 0xFF) << 16) //
- | ((g & 0xFF) << 8) //
- | ((b & 0xFF) << 0);
- }
-
- return rep;
- }
-
- /**
- * Set the value associated to the given id as a colour.
- *
- * The value is an BGRA value.
- *
- * @param id
- * the id of the value to set
- * @param color
- * the new colour
- */
- public void setColor(E id, Integer color) {
- int a = (color >> 24) & 0xFF;
- int r = (color >> 16) & 0xFF;
- int g = (color >> 8) & 0xFF;
- int b = (color >> 0) & 0xFF;
-
- String rs = Integer.toString(r, 16);
- String gs = Integer.toString(g, 16);
- String bs = Integer.toString(b, 16);
- String as = "";
- if (a < 255) {
- as = Integer.toString(a, 16);
- }
-
- setString(id, "#" + rs + gs + bs + as);
- }
-
- /**
- * 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).
- *
- * Will update the files in {@link Bundles#getDirectory()}; it MUST
- * be set.
- *
- * @throws IOException
- * in case of IO errors
- */
- public void updateFile() throws IOException {
- updateFile(Bundles.getDirectory());
- }
-
- /**
- * 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, MUST NOT be
- * NULL
- *
- * @throws IOException
- * in case of IO errors
- */
- public void updateFile(String path) throws IOException {
- File file = getUpdateFile(path);
-
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(file), "UTF-8"));
-
- writeHeader(writer);
- writer.write("\n");
- writer.write("\n");
-
- for (Field field : type.getDeclaredFields()) {
- Meta meta = field.getAnnotation(Meta.class);
- if (meta != null) {
- E id = Enum.valueOf(type, field.getName());
- String info = getMetaInfo(meta);
-
- if (info != null) {
- writer.write(info);
- writer.write("\n");
- }
-
- writeValue(writer, id);
- }
- }
-
- writer.close();
- }
-
- /**
- * Delete 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).
- *
- * Will delete the files in {@link Bundles#getDirectory()}; it MUST
- * be set.
- *
- * @return TRUE if the file was deleted
- */
- public boolean deleteFile() {
- return deleteFile(Bundles.getDirectory());
- }
-
- /**
- * Delete 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, MUST NOT be
- * NULL
- *
- * @return TRUE if the file was deleted
- */
- public boolean deleteFile(String path) {
- File file = getUpdateFile(path);
- return file.delete();
- }
-
- /**
- * The description {@link TransBundle}, that is, a {@link TransBundle}
- * dedicated to the description of the values of the given {@link Bundle}
- * (can be NULL).
- *
- * @return the description {@link TransBundle}
- */
- public TransBundle