> Adding a default page resource bundle
> -------------------------------------
>
> Key: CLK-72
> URL:
http://www.avoka.com:8080/jira/browse/CLK-72> Project: Click
> Issue Type: Improvement
> Components: core
> Reporter: Claudio
> Assigned To: Malcolm Edgar
> Priority: Minor
>
> The Page getMessage() method search message key in a resource bundle named after the Page class name (getClass().getName())
> This is done loading a MessageMap instance in the getMessages() method.
> It would be handy to have a default page resource bundle where keys are looked up if they are not found on the page specific bundle.
> I was thinking of something like click-page (like click-control is default for controls).
> This way you can:
> 1 - share default application wide keys
> 2 - choose to have a single bundle to maintain and translate
> A discussion about implementation is on the development newsgroup:
>
http://news.gmane.org/find-root.php?group=gmane.comp.web.click.devel&article=498> The following is my implementation (that patches Click 0.18).
> _____________________________ MessageMap.java ____________________________
> package mypackage;
> import java.util.*;
> public class MessagesMap extends net.sf.click.util.MessagesMap {
> private net.sf.click.util.MessagesMap parent;
> public MessagesMap(String baseName, Locale locale,
> net.sf.click.util.MessagesMap parent) {
> super(baseName, locale);
> this.parent = parent;
> }
> public MessagesMap(String baseName, Locale locale) {
> this(baseName, locale, null);
> }
> public Object get(Object key) {
> if(parent == null) {
> return super.get(key);
> } else if(super.containsKey(key)) {
> return super.get(key);
> } else {
> return parent.get(key);
> }
> }
> // ****** not sure about the following methods: is it right to consider the presence of the parent MessageMap to determine the result of the methods?
> public boolean containsKey(Object key) {
> return super.containsKey(key) ||
> (parent != null && parent.containsKey(key));
> }
> public boolean containsValue(Object value) {
> return super.containsValue(value) ||
> (parent != null && parent.containsValue(value));
> }
> public Set keySet() {
> Set keySet = super.keySet();
> if(parent != null) {
> keySet.addAll(parent.keySet());
> }
> return keySet;
> }
> public Collection values() {
> Collection values = super.values();
> if(parent != null) {
> values.addAll(parent.values());
> }
> return values;
> }
> public Set entrySet() {
> Set entrySet = super.entrySet();
> if(parent != null) {
> entrySet.addAll(parent.entrySet());
> }
> return entrySet;
> }
> }
> ______________________ Page.java _____________________________
> package mypackage;
> import java.text.MessageFormat;
> import java.util.Locale;
> import java.util.Map;
> import it.graphite.click.util.MessagesMap;
> public class Page extends net.sf.click.Page {
> public Map getMessages() {
> if (messages == null) {
> if (getContext() != null) {
> String baseName = "click-page";
> Locale locale = getContext().getLocale();
> MessagesMap parentMessages = new MessagesMap(baseName, locale);
> baseName = getClass().getName();
> messages = new MessagesMap(baseName, locale, parentMessages);
> } else {
> String msg = "Context not set cannot initialize messages";
> throw new IllegalStateException(msg);
> }
> }
> return messages;
> }
> // *********** also useful and missing in the Click 0.18 *********
> public String getMessage(String name, Object arg) {
> Object[] args = new Object[] {
> arg};
> return getMessage(name, args);
> }
> public String getMessage(String name, Object[] args) {
> if (args == null) {
> throw new IllegalArgumentException("Null args parameter");
> }
> String value = getMessage(name);
> return MessageFormat.format(value, args);
> }
> }