- Extended GlobalizedMessage: It is now possible to pass a custom ResourceBundle.Control instance to a

GlobalizedMessage for controlling the loading of responsible ResourceBundle
- Created RelationAttributeResourceBundle: A custom ResourceBundle.Control implementation which uses 
  a RelationAttributeCollection as data source for creating a ListResourceBundle. This allows it to
  This allows it to use GlobalizedMessage when showing RelationAttribute values in Bebop Controls
- Altered GenericOrganizationalUnitPersonAddForm to use GlobalizedMessage and RelationAttributeResourceControl


git-svn-id: https://svn.libreccm.org/ccm/trunk@2746 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2014-07-10 10:35:33 +00:00
parent 1af42506ca
commit fad219c1fe
3 changed files with 187 additions and 108 deletions

View File

@ -0,0 +1,46 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.arsdigita.cms;
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.ResourceBundle;
/**
*
* @author Jens Pelzetter <jens.pelzetter@scientificcms.org>
*/
public class RelationAttributeResourceBundleControl extends ResourceBundle.Control {
@Override
public ResourceBundle newBundle(final String baseName,
final Locale locale,
final String format,
final ClassLoader classLoader,
final boolean reload) {
final RelationAttributeCollection values = new RelationAttributeCollection(baseName);
values.addLanguageFilter(locale.getLanguage());
final Object[][] contents = new Object[(int)values.size()][2];
int i = 0;
while(values.next()) {
contents[i][0] = values.getKey();
contents[i][1] = values.getName();
i++;
}
return new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return contents;
}
};
}
}

View File

@ -38,6 +38,7 @@ import com.arsdigita.cms.ContentType;
import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.cms.RelationAttribute;
import com.arsdigita.cms.RelationAttributeCollection;
import com.arsdigita.cms.RelationAttributeResourceBundleControl;
import com.arsdigita.cms.contenttypes.GenericOrganizationalUnit;
import com.arsdigita.cms.contenttypes.GenericOrganizationalUnitPersonCollection;
import com.arsdigita.cms.contenttypes.GenericPerson;
@ -45,6 +46,7 @@ import com.arsdigita.cms.contenttypes.util.ContenttypesGlobalizationUtil;
import com.arsdigita.cms.ui.ItemSearchWidget;
import com.arsdigita.cms.ui.authoring.BasicItemForm;
import com.arsdigita.globalization.GlobalizationHelper;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.util.UncheckedWrapperException;
import java.util.TooManyListenersException;
import org.apache.log4j.Logger;
@ -116,7 +118,12 @@ public class GenericOrganizationalUnitPersonAddForm
while (roles.next()) {
RelationAttribute role;
role = roles.getRelationAttribute();
target.addOption(new Option(role.getKey(), role.getName()));
//target.addOption(new Option(role.getKey(), role.getName()));
target.addOption(new Option(role.getKey(),
new Label(new GlobalizedMessage(
role.getKey(),
getRoleAttributeName(),
new RelationAttributeResourceBundleControl()))));
}
}
@ -148,7 +155,11 @@ public class GenericOrganizationalUnitPersonAddForm
while (statusColl.next()) {
RelationAttribute status;
status = statusColl.getRelationAttribute();
target.addOption(new Option(status.getKey(), status.getName()));
//target.addOption(new Option(status.getKey(), status.getName()));
target.addOption(new Option(status.getKey(),
new Label(new GlobalizedMessage(status.getKey(),
getStatusAttributeName(),
new RelationAttributeResourceBundleControl()))));
}
}

View File

@ -27,13 +27,12 @@ import org.apache.log4j.Logger;
/**
* <p>
* Represents a key into a ResourceBundle, a target ResourceBundle, and
* possibly an array of arguments to interpolate into the retrieved message
* using the MessageFormat class.
* Represents a key into a ResourceBundle, a target ResourceBundle, and possibly an array of
* arguments to interpolate into the retrieved message using the MessageFormat class.
* </p>
* <p>
* This class should be used in any situation where the application needs to
* output localizeable objects.
* This class should be used in any situation where the application needs to output localizeable
* objects.
* </p>
*
* @see java.text.MessageFormat
@ -44,22 +43,28 @@ import org.apache.log4j.Logger;
*/
public class GlobalizedMessage {
/** Internal logger instance to faciliate debugging. Enable logging output
* by editing /WEB-INF/conf/log4j.properties int hte runtime environment
* and set com.arsdigita.globalization.GlobalizedMessage=DEBUG
* by uncommenting or adding the line. */
private static final Logger LOGGER = Logger.getLogger(GlobalizedMessage
.class.getName());
/**
* Internal logger instance to faciliate debugging. Enable logging output by editing
* /WEB-INF/conf/log4j.properties int hte runtime environment and set
* com.arsdigita.globalization.GlobalizedMessage=DEBUG by uncommenting or adding the line.
*/
private static final Logger LOGGER = Logger.getLogger(GlobalizedMessage.class.getName());
private String m_key = "";
private String m_bundleName = "";
/**
* {@link ResourceBundle.Control} used by this {@code GlobalizedMessage} for looking up
* the ResourceBundle. Defaults to {@link ResourceBundle.Control#getNoFallbackControl(java.util.List)}
* to avoid that the locale of the server is taken into account.
*/
private ResourceBundle.Control rbControl = ResourceBundle.Control.getNoFallbackControl(
ResourceBundle.Control.FORMAT_DEFAULT);
private Object[] m_args = null;
/**
* <p>
* Constructor. Takes in a key to be used to look up a message in the
* ResourceBundle for the current running application. The base name of
* the ResourceBundle to do the lookup in is retrieved from the
* ApplicationContext.
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle for the
* current running application. The base name of the ResourceBundle to do the lookup in is
* retrieved from the ApplicationContext.
* </p>
*
* @param key The key to use to look up a message in the ResourceBundle.
@ -71,8 +76,7 @@ public class GlobalizedMessage {
/**
* <p>
* Constructor. Takes in a key to be used to look up a message in the
* ResourceBundle specified.
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle specified.
* </p>
*
* @param key The key to use to look up a message in the ResourceBundle.
@ -85,16 +89,14 @@ public class GlobalizedMessage {
/**
* <p>
* Constructor. Takes in a key to be used to look up a message in the
* ResourceBundle for the current running application. The base name of
* the ResourceBundle to do the lookup in is retrieved from the
* ApplicationContext. Also takes in an Object[] of arguments to
* interpolate into the retrieved message using the MessageFormat class.
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle for the
* current running application. The base name of the ResourceBundle to do the lookup in is
* retrieved from the ApplicationContext. Also takes in an Object[] of arguments to interpolate
* into the retrieved message using the MessageFormat class.
* </p>
*
* @param key The key to use to look up a message in the ResourceBundle.
* @param args An Object[] of arguments to interpolate into the retrieved
* message.
* @param args An Object[] of arguments to interpolate into the retrieved message.
*/
public GlobalizedMessage(final String key, final Object[] args) {
this(key);
@ -103,15 +105,14 @@ public class GlobalizedMessage {
/**
* <p>
* Constructor. Takes in a key to be used to look up a message in the
* ResourceBundle specified. Also takes in an Object[] of arguments to
* interpolate into the retrieved message using the MessageFormat class.
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle specified.
* Also takes in an Object[] of arguments to interpolate into the retrieved message using the
* MessageFormat class.
* </p>
*
* @param key The key to use to look up a message in the ResourceBundle.
* @param bundleName The base name of the target ResourceBundle.
* @param args An Object[] of arguments to interpolate into the retrieved
* message.
* @param args An Object[] of arguments to interpolate into the retrieved message.
*/
public GlobalizedMessage(final String key,
final String bundleName,
@ -120,6 +121,34 @@ public class GlobalizedMessage {
setArgs(args);
}
public GlobalizedMessage(final String key,
final ResourceBundle.Control rbControl) {
this(key);
this.rbControl = rbControl;
}
public GlobalizedMessage(final String key,
final Object[] args,
final ResourceBundle.Control rbControl) {
this(key, args);
this.rbControl = rbControl;
}
public GlobalizedMessage(final String key,
final String bundleName,
final ResourceBundle.Control rbControl) {
this(key, bundleName);
this.rbControl = rbControl;
}
public GlobalizedMessage(final String key,
final String bundleName,
final Object[] args,
final ResourceBundle.Control rbControl) {
this(key, bundleName, args);
this.rbControl = rbControl;
}
/**
* <p>
* Get the key for this GlobalizedMessage.
@ -166,20 +195,17 @@ public class GlobalizedMessage {
/**
* <p>
* Localize this message. If no message is found the key is returned as
* the message. This is done so that developers or translators can see the
* messages that still need localization.
* Localize this message. If no message is found the key is returned as the message. This is
* done so that developers or translators can see the messages that still need localization.
* </p>
* <p>
* Any arguments this message has are interpolated into it using the
* java.text.MessageFormat class.
* Any arguments this message has are interpolated into it using the java.text.MessageFormat
* class.
* </p>
*
* @return Object Represents the localized version of this
* message. The reason this method returns an Object and
* not a String is because we might want to localize
* resources other than strings, such as icons or sound
* bites. Maybe this class should have been called
* @return Object Represents the localized version of this message. The reason this method
* returns an Object and not a String is because we might want to localize resources other than
* strings, such as icons or sound bites. Maybe this class should have been called
* GlobalizedObject?
*/
public Object localize() {
@ -189,23 +215,20 @@ public class GlobalizedMessage {
/**
* <p>
* Localize this message according the specified request. If no message is
* found the key is returned as the message. This is done so that
* developers or translators can see the messages that still need
* localization.
* Localize this message according the specified request. If no message is found the key is
* returned as the message. This is done so that developers or translators can see the messages
* that still need localization.
* </p>
* <p>
* Any arguments this message has are interpolated into it using the
* java.text.MessageFormat class.
* Any arguments this message has are interpolated into it using the java.text.MessageFormat
* class.
* </p>
*
* @param request The current running request.
*
* @return Object Represents the localized version of this
* message. The reason this method returns an Object and
* not a String is because we might want to localize
* resources other than strings, such as icons or sound
* bites. Maybe this class should have been called
* @return Object Represents the localized version of this message. The reason this method
* returns an Object and not a String is because we might want to localize resources other than
* strings, such as icons or sound bites. Maybe this class should have been called
* GlobalizedObject?
*/
public Object localize(final HttpServletRequest request) {
@ -215,23 +238,20 @@ public class GlobalizedMessage {
/**
* <p>
* Localize this message with the provided locale. If no message is
* found the key is returned as the message. This is done so that
* developers or translators can see the messages that still need
* localization.
* Localize this message with the provided locale. If no message is found the key is returned as
* the message. This is done so that developers or translators can see the messages that still
* need localization.
* </p>
* <p>
* Any arguments this message has are interpolated into it using the
* java.text.MessageFormat class.
* Any arguments this message has are interpolated into it using the java.text.MessageFormat
* class.
* </p>
*
* @param locale The locale to try to use to localize this message.
*
* @return Object Represents the localized version of this
* message. The reason this method returns an Object and
* not a String is because we might want to localize
* resources other than strings, such as icons or sound
* bites. Maybe this class should have been called
* @return Object Represents the localized version of this message. The reason this method
* returns an Object and not a String is because we might want to localize resources other than
* strings, such as icons or sound bites. Maybe this class should have been called
* GlobalizedObject?
*/
public Object localize(final Locale locale) {
@ -260,10 +280,13 @@ public class GlobalizedMessage {
// Therefore, all what was to do was to change the call of getBundle here from
// ResourceBundle#getBundle(String, Locale) to ResourceBundle#getBundle(String, Locale, ResourceControl)
// with ResourceBundle.Control.getNoFallbackControl(List<String>).
// jensp 2014-07-10:
// It is now possible to pass the custom implementation of ResourceBundle.Control to
// a GlobalizedMessage
resourceBundle = ResourceBundle.getBundle(
getBundleName(),
locale,
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT));
rbControl);
} catch (MissingResourceException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
@ -304,9 +327,8 @@ public class GlobalizedMessage {
/**
* For debugging, not for localizing.
*
* If you need a String, use an additional localize() to get an object
* and cast it to String. e.g.
* String label = (String) GlobalizedMessage(key,bundleName).localize();
* If you need a String, use an additional localize() to get an object and cast it to String.
* e.g. String label = (String) GlobalizedMessage(key,bundleName).localize();
*
* @return The contents in String form for debugging.
*/