Initial import for Diagrama.
[simdecs_seam.git] / Diagrama / src / java / org / ufcspa / simdecs / diagram / util / MessageResourceUtils.java
diff --git a/Diagrama/src/java/org/ufcspa/simdecs/diagram/util/MessageResourceUtils.java b/Diagrama/src/java/org/ufcspa/simdecs/diagram/util/MessageResourceUtils.java
new file mode 100755 (executable)
index 0000000..e220b52
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.ufcspa.simdecs.diagram.util;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+/**
+ *
+ * @author Maroni
+ */
+public class MessageResourceUtils {
+
+    protected static ClassLoader getCurrentClassLoader(Object defaultObject) {
+
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+
+        if (loader == null) {
+            loader = defaultObject.getClass().getClassLoader();
+        }
+
+        return loader;
+    }
+
+    public static String getMessageResourceString(
+            String bundleName,
+            String key,
+            Object params[],
+            Locale locale) {
+
+        String text = null;
+
+        ResourceBundle bundle =
+                ResourceBundle.getBundle(bundleName, locale,
+                getCurrentClassLoader(params));
+
+        try {
+            text = bundle.getString(key);
+        } catch (MissingResourceException e) {
+            text = "?? key " + key + " not found ??";
+        }
+
+        if (params != null) {
+            MessageFormat mf = new MessageFormat(text, locale);
+            text = mf.format(params, new StringBuffer(), null).toString();
+        }
+
+        return text;
+    }
+}