From 505a9c3a53b25620bd42b309aa7065e86184f375 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Wed, 12 Aug 2009 20:04:36 +0000 Subject: [PATCH] Make MessageBundle.getResourceString public so MessageBundles can be composed instead of requiring inheritance to customize them, and open up the bundle creation process in MessageManager to subclass customization git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5901 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/util/MessageBundle.java | 8 ++--- .../com/threerings/util/MessageManager.java | 36 ++++++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/java/com/threerings/util/MessageBundle.java b/src/java/com/threerings/util/MessageBundle.java index 9a3fc85bf..708752af5 100644 --- a/src/java/com/threerings/util/MessageBundle.java +++ b/src/java/com/threerings/util/MessageBundle.java @@ -21,13 +21,12 @@ package com.threerings.util; +import java.text.MessageFormat; import java.util.Collection; import java.util.Enumeration; import java.util.MissingResourceException; import java.util.ResourceBundle; -import java.text.MessageFormat; - import com.samskivert.text.MessageUtil; import com.samskivert.util.StringUtil; @@ -236,11 +235,12 @@ public class MessageBundle /** * Get a String from the resource bundle, or null if there was an error. */ - protected String getResourceString (String key) + public String getResourceString (String key) { return getResourceString(key, true); } + /** * Get a String from the resource bundle, or null if there was an * error. @@ -249,7 +249,7 @@ public class MessageBundle * @param reportMissing whether or not the method should log an error * if the resource didn't exist. */ - protected String getResourceString (String key, boolean reportMissing) + public String getResourceString (String key, boolean reportMissing) { try { if (_bundle != null) { diff --git a/src/java/com/threerings/util/MessageManager.java b/src/java/com/threerings/util/MessageManager.java index 8ad549031..8168a4c5d 100644 --- a/src/java/com/threerings/util/MessageManager.java +++ b/src/java/com/threerings/util/MessageManager.java @@ -140,12 +140,13 @@ public class MessageManager // if the resource bundle contains a special resource, we'll interpret that as a derivation // of MessageBundle to instantiate for handling that class + MessageBundle customBundle = null; if (rbundle != null) { String mbclass = null; try { mbclass = rbundle.getString(MBUNDLE_CLASS_KEY).trim(); if (!StringUtil.isBlank(mbclass)) { - bundle = (MessageBundle)Class.forName(mbclass).newInstance(); + customBundle = (MessageBundle)Class.forName(mbclass).newInstance(); } } catch (MissingResourceException mre) { @@ -157,19 +158,38 @@ public class MessageManager } } - // if there was no custom class, or we failed to instantiate the custom class, use a - // standard message bundle - if (bundle == null) { - bundle = new MessageBundle(); - } - // initialize our message bundle, cache it and return it (if we couldn't resolve the // bundle, the message bundle will cope with its null resource bundle) - bundle.init(this, path, rbundle, _global); + bundle = createBundle(path, rbundle, customBundle); _cache.put(path, bundle); return bundle; } + /** + * Returns the bundle to use for the given path and resource bundle. If customBundle is + * non-null, it's an instance of the bundle class specified by the bundle itself and should be + * used as part of the created bundle. + */ + protected MessageBundle createBundle (String path, ResourceBundle rbundle, + MessageBundle customBundle) + { + // if there was no custom class, or we failed to instantiate the custom class, use a + // standard message bundle + if (customBundle == null) { + customBundle = new MessageBundle(); + } + initBundle(customBundle, path, rbundle); + return customBundle; + } + + /** + * Initializes the given bundle with this manager and the given path and resource bundle. + */ + protected void initBundle (MessageBundle bundle, String path, ResourceBundle rbundle) + { + bundle.init(this, path, rbundle, _global); + } + /** * Loads a bundle from the given path, or returns null if it can't be found. */