[jboss-cvs] jboss-seam/src/main/org/jboss/seam/international ...
Gavin King
gavin.king at jboss.com
Tue Jun 19 15:12:44 EDT 2007
User: gavin
Date: 07/06/19 15:12:44
Added: src/main/org/jboss/seam/international Locale.java
LocaleSelector.java Messages.java
ResourceBundle.java TimeZone.java
TimeZoneSelector.java package-info.java
Log:
repackaged built-in components
sorry for breakage, but it had to happen eventually :-(
Revision Changes Path
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/Locale.java
Index: Locale.java
===================================================================
package org.jboss.seam.international;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.contexts.Contexts;
/**
* Manager component for the current user's locale
*
* @author Gavin King
*/
@Scope(ScopeType.STATELESS)
@Name("org.jboss.seam.core.locale")
@Install(precedence=BUILT_IN)
@Intercept(NEVER)
public class Locale {
@Unwrap
public java.util.Locale getLocale()
{
return LocaleSelector.instance().getLocale();
}
public static java.util.Locale instance()
{
if (Contexts.isApplicationContextActive()) {
return (java.util.Locale) Component.getInstance(Locale.class, ScopeType.STATELESS);
} else {
return java.util.Locale.US; // testing
}
}
}
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/LocaleSelector.java
Index: LocaleSelector.java
===================================================================
package org.jboss.seam.international;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.Seam;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.Selector;
import org.jboss.seam.util.Strings;
import org.jboss.seam.web.ServletContexts;
/**
* Selects the current user's locale
*
* @author Gavin King
*/
@Scope(ScopeType.SESSION)
@Name("org.jboss.seam.core.localeSelector")
@Intercept(NEVER)
@Install(precedence=BUILT_IN)
public class LocaleSelector extends Selector
{
private static final long serialVersionUID = -6087667065688208261L;
private String language;
private String country;
private String variant;
@Create
public void initLocale()
{
String localeString = getCookieValueIfEnabled();
if (localeString!=null) setLocaleString(localeString);
}
@Override
protected String getCookieName()
{
return "org.jboss.seam.core.Locale";
}
/**
* Force the resource bundle to reload, using the current locale,
* and raise the org.jboss.seam.localeSelected event.
*/
public void select()
{
FacesContext.getCurrentInstance().getViewRoot().setLocale( getLocale() );
Contexts.removeFromAllContexts( Seam.getComponentName(ResourceBundle.class) );
Contexts.removeFromAllContexts( Seam.getComponentName(Messages.class) );
setCookieValueIfEnabled( getLocaleString() );
if ( Events.exists() ) {
Events.instance().raiseEvent( "org.jboss.seam.localeSelected", getLocaleString() );
}
}
public void select(ValueChangeEvent event)
{
setLocaleString( (String) event.getNewValue() );
select();
}
/**
* Set the language and force resource bundle reload, useful for quick action links:
* <tt><h:commandLink value="DE" action="#{localeSelector.selectLanguage('de')}"/>"/></tt>
*/
public void selectLanguage(String language) {
setLanguage(language);
select();
}
public Locale calculateLocale(Locale jsfLocale)
{
if ( !Strings.isEmpty(variant) )
{
return new java.util.Locale(language, country, variant);
}
else if ( !Strings.isEmpty(country) )
{
return new java.util.Locale(language, country);
}
else if ( !Strings.isEmpty(language) )
{
return new java.util.Locale(language);
}
else
{
return jsfLocale;
}
}
public void setLocale(Locale locale)
{
language = Strings.nullIfEmpty( locale.getLanguage() );
country = Strings.nullIfEmpty( locale.getCountry() );
variant = Strings.nullIfEmpty( locale.getVariant() );
}
public String getLocaleString()
{
return getLocale().toString();
}
public void setLocaleString(String localeString)
{
StringTokenizer tokens = new StringTokenizer(localeString, "-_");
language = tokens.hasMoreTokens() ? tokens.nextToken() : null;
country = tokens.hasMoreTokens() ? tokens.nextToken() : null;
variant = tokens.hasMoreTokens() ? tokens.nextToken() : null;
}
public List<SelectItem> getSupportedLocales()
{
List<SelectItem> selectItems = new ArrayList<SelectItem>();
Iterator<Locale> locales = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
while ( locales.hasNext() )
{
Locale locale = locales.next();
if ( !Strings.isEmpty( locale.getLanguage() ) )
{
selectItems.add( new SelectItem( locale.toString(), locale.getDisplayName(locale) ) );
}
}
return selectItems;
}
/**
* Get the selected locale
*/
public Locale getLocale()
{
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext!=null)
{
//Note: this does a double dispatch back to LocaleSelector.calculateLocale()
return facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
}
ServletRequest request = ServletContexts.instance().getRequest();
if (request!=null)
{
Locale requestLocale = ( (HttpServletRequest) request ).getLocale();
if (requestLocale!=null)
{
return calculateLocale(requestLocale);
}
}
return calculateLocale( Locale.getDefault() );
}
public static LocaleSelector instance()
{
if ( !Contexts.isSessionContextActive() )
{
throw new IllegalStateException("No active session context");
}
return (LocaleSelector) Component.getInstance(LocaleSelector.class, ScopeType.SESSION);
}
public String getCountry() {
if (country==null) return getLocale().getCountry();
return country;
}
public void setCountry(String country) {
setDirty(this.country, country);
this.country = country;
}
public String getLanguage() {
if (language==null) return getLocale().getLanguage();
return language;
}
public void setLanguage(String language) {
setDirty(this.language, language);
this.language = language;
}
public String getVariant() {
if (variant==null) return getLocale().getVariant();
return variant;
}
public void setVariant(String variant) {
setDirty(this.variant, variant);
this.variant = variant;
}
}
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/Messages.java
Index: Messages.java
===================================================================
package org.jboss.seam.international;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Set;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.core.Interpolator;
/**
* Support for an application-global resource bundle
*
* @author Gavin King
*/
@Scope(ScopeType.SESSION)
@Intercept(NEVER)
@Name("org.jboss.seam.core.messages")
@Install(precedence=BUILT_IN)
public class Messages implements Serializable
{
private static final long serialVersionUID = 1292464253307553295L;
private transient Map<String, String> messages;
private void init()
{
messages = new AbstractMap<String, String>()
{
private java.util.ResourceBundle bundle = ResourceBundle.instance();
@Override
public String get(Object key)
{
if (key instanceof String)
{
String resourceKey = (String) key;
String resource=null;
if (bundle!=null)
{
try
{
resource = bundle.getString(resourceKey);
}
catch (MissingResourceException mre)
{
//Just swallow
}
}
return resource==null ?
resourceKey :
Interpolator.instance().interpolate(resource);
}
else
{
return null;
}
}
@Override
public Set<Map.Entry<String, String>> entrySet()
{
Enumeration<String> keys = bundle.getKeys();
Map<String, String> map = new HashMap<String, String>();
while ( keys.hasMoreElements() )
{
String key = keys.nextElement();
map.put( key, get(key) );
}
return map.entrySet();
}
};
}
@Unwrap
public Map<String, String> getMessages()
{
if (messages==null) init();
return messages;
}
public static Map<String, String> instance()
{
return (Map<String, String>) Component.getInstance(Messages.class, true );
}
}
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/ResourceBundle.java
Index: ResourceBundle.java
===================================================================
package org.jboss.seam.international;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.MissingResourceException;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.seam.navigation.Pages;
import org.jboss.seam.util.EnumerationEnumeration;
import org.jboss.seam.util.Strings;
/**
* Support for an application-global resource bundle
*
* @author Gavin King
*/
@Scope(ScopeType.SESSION)
@Intercept(NEVER)
@Name("org.jboss.seam.core.resourceBundle")
@Install(precedence=BUILT_IN)
public class ResourceBundle implements Serializable
{
private static final long serialVersionUID = -3236251335438092538L;
private static final LogProvider log = Logging.getLogProvider(ResourceBundle.class);
private String[] bundleNames = {"messages"};
private transient java.util.ResourceBundle bundle;
public String[] getBundleNames()
{
return bundleNames;
}
public void setBundleNames(String[] bundleNames)
{
this.bundleNames = bundleNames;
}
@Deprecated
public void setBundleName(String bundleName)
{
bundleNames = bundleName==null ? null : new String[] { bundleName };
}
@Deprecated
public String getBundleName()
{
return bundleNames==null || bundleNames.length==0 ? null : bundleNames[0];
}
/**
* Load a resource bundle by name (may be overridden by subclasses
* who want to use non-standard resource bundle types).
*
* @param bundleName the name of the resource bundle
* @return an instance of java.util.ResourceBundle
*/
protected java.util.ResourceBundle loadBundle(String bundleName)
{
try
{
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle(
bundleName,
Locale.instance(),
Thread.currentThread().getContextClassLoader()
);
log.debug("loaded resource bundle: " + bundleName);
return bundle;
}
catch (MissingResourceException mre)
{
log.debug("resource bundle missing: " + bundleName);
return null;
}
}
private void createUberBundle()
{
final List<java.util.ResourceBundle> littleBundles = new ArrayList<java.util.ResourceBundle>();
if (bundleNames!=null)
{
for (String bundleName: bundleNames)
{
java.util.ResourceBundle littleBundle = loadBundle(bundleName);
if (littleBundle!=null) littleBundles.add(littleBundle);
}
}
java.util.ResourceBundle validatorBundle = loadBundle("ValidatorMessages");
if (validatorBundle!=null) littleBundles.add(validatorBundle);
java.util.ResourceBundle validatorDefaultBundle = loadBundle("org/hibernate/validator/resources/DefaultValidatorMessages");
if (validatorDefaultBundle!=null) littleBundles.add(validatorDefaultBundle);
bundle = new java.util.ResourceBundle()
{
@Override
public java.util.Locale getLocale()
{
return Locale.instance();
}
@Override
public Enumeration<String> getKeys()
{
List<java.util.ResourceBundle> pageBundles = getPageResourceBundles();
Enumeration<String>[] enumerations = new Enumeration[ littleBundles.size() + pageBundles.size() ];
int i=0;
for (; i<pageBundles.size(); i++)
{
enumerations[i++] = pageBundles.get(i).getKeys();
}
for (; i<littleBundles.size(); i++)
{
enumerations[i] = littleBundles.get(i).getKeys();
}
return new EnumerationEnumeration<String>(enumerations);
}
@Override
protected Object handleGetObject(String key)
{
List<java.util.ResourceBundle> pageBundles = getPageResourceBundles();
for (java.util.ResourceBundle pageBundle: pageBundles)
{
try
{
return pageBundle.getObject(key);
}
catch (MissingResourceException mre) {}
}
for (java.util.ResourceBundle littleBundle: littleBundles)
{
if (littleBundle!=null)
{
try
{
return littleBundle.getObject(key);
}
catch (MissingResourceException mre) {}
}
}
return null; //superclass is responsible for throwing MRE
}
private List<java.util.ResourceBundle> getPageResourceBundles()
{
String viewId = Pages.getCurrentViewId();
if (viewId!=null)
{
return Pages.instance().getResourceBundles(viewId);
}
else
{
return Collections.EMPTY_LIST;
}
}
};
}
@Unwrap
public java.util.ResourceBundle getBundle()
{
if (bundle==null) createUberBundle();
return bundle;
}
@Override
public String toString()
{
String concat = bundleNames==null ? "" : Strings.toString( ", ", (Object[]) bundleNames );
return "ResourceBundle(" + concat + ")";
}
public static java.util.ResourceBundle instance()
{
return (java.util.ResourceBundle) Component.getInstance(ResourceBundle.class, true);
}
}
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/TimeZone.java
Index: TimeZone.java
===================================================================
package org.jboss.seam.international;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
/**
* Manager component for the current user's locale
*
* @author Gavin King
*/
@Scope(ScopeType.STATELESS)
@Name("org.jboss.seam.core.timeZone")
@Intercept(NEVER)
@Install(precedence=BUILT_IN)
public class TimeZone {
@Unwrap
public java.util.TimeZone getTimeZone()
{
return TimeZoneSelector.instance().getTimeZone();
}
public static java.util.TimeZone instance()
{
return (java.util.TimeZone) Component.getInstance(TimeZone.class, ScopeType.STATELESS);
}
}
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/TimeZoneSelector.java
Index: TimeZoneSelector.java
===================================================================
package org.jboss.seam.international;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import javax.faces.event.ValueChangeEvent;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.Selector;
/**
* Selects the current user's time zone, defaulting
* to the server time zone.
*
* @author Gavin King
*/
@Scope(ScopeType.SESSION)
@Name("org.jboss.seam.core.timeZoneSelector")
@Intercept(NEVER)
@Install(precedence=BUILT_IN)
public class TimeZoneSelector extends Selector
{
private static final long serialVersionUID = -5013819375360015369L;
private String id;
@Create
public void initTimeZone()
{
String timeZoneId = getCookieValueIfEnabled();
if (timeZoneId!=null) setTimeZoneId(timeZoneId);
}
@Override
protected String getCookieName()
{
return "org.jboss.seam.core.TimeZone";
}
/**
* Force the resource bundle to reload, using the current locale,
* and raise the org.jboss.seam.timeZoneSelected event
*/
public void select()
{
setCookieValueIfEnabled( getTimeZoneId() );
if ( Events.exists() )
{
Events.instance().raiseEvent( "org.jboss.seam.timeZoneSelected", getTimeZoneId() );
}
}
public void select(ValueChangeEvent event)
{
selectTimeZone( (String) event.getNewValue() );
}
public void selectTimeZone(String timeZoneId)
{
setTimeZoneId(timeZoneId);
select();
}
public void setTimeZone(java.util.TimeZone timeZone)
{
setTimeZoneId( timeZone.getID() );
}
public void setTimeZoneId(String id)
{
setDirty(this.id, id);
this.id = id;
}
public String getTimeZoneId()
{
return id;
}
/**
* Get the selected timezone
*/
public java.util.TimeZone getTimeZone()
{
if (id==null)
{
return java.util.TimeZone.getDefault();
}
else
{
return java.util.TimeZone.getTimeZone( getTimeZoneId() );
}
}
public static TimeZoneSelector instance()
{
if ( !Contexts.isSessionContextActive() )
{
throw new IllegalStateException("No active session context");
}
return (TimeZoneSelector) Component.getInstance(TimeZoneSelector.class, ScopeType.SESSION);
}
}
1.1 date: 2007/06/19 19:12:44; author: gavin; state: Exp;jboss-seam/src/main/org/jboss/seam/international/package-info.java
Index: package-info.java
===================================================================
@Namespace(value="http://jboss.com/products/seam/international", prefix="org.jboss.seam.international")
package org.jboss.seam.international;
import org.jboss.seam.annotations.Namespace;
More information about the jboss-cvs-commits
mailing list