001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.admin.ui;
023:
024: import org.jboss.portal.common.i18n.LocaleManager;
025: import org.jboss.portal.common.i18n.LocalizedString;
026:
027: import javax.faces.context.FacesContext;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Locale;
034: import java.util.Map;
035: import java.util.SortedMap;
036: import java.util.TreeMap;
037:
038: /**
039: * @author <a href="mailto:whales@redhat.com">Wesley Hales</a>
040: * @version $Revision: 630 $
041: */
042: public class DisplayNameBean {
043:
044: public Map createLocalizedStringMap(Locale locale,
045: LocalizedString displayName, String name) {
046: Map<Locale, String> map = new HashMap<Locale, String>();
047: if (displayName != null) {
048: Map<Locale, LocalizedString.Value> oldMap = displayName
049: .getValues();
050: Collection<LocalizedString.Value> values = oldMap.values();
051: for (LocalizedString.Value value : values) {
052: map.put(value.getLocale(), value.getString());
053: }
054: }
055: map.put(locale, name);
056: return map;
057: }
058:
059: public SortedMap getLocales() {
060: SortedMap smap = new TreeMap();
061: Locale[] localList = (Locale[]) LocaleManager.getLocales()
062: .toArray(new Locale[0]);
063:
064: for (int i = 0; i < localList.length; i++) {
065: Locale this Locale = localList[i];
066: Locale requestedLocale = FacesContext.getCurrentInstance()
067: .getExternalContext().getRequestLocale();
068: smap.put(convertLocaleDisplayName(this Locale
069: .getDisplayLanguage(requestedLocale)), this Locale
070: .getLanguage());
071: }
072: return smap;
073: }
074:
075: public List populateCurrentLocales(Map displayNames) {
076: List tempList = new ArrayList();
077: Iterator it = displayNames.entrySet().iterator();
078: while (it.hasNext()) {
079: Map.Entry pairs = (Map.Entry) it.next();
080: Locale this Locale = (Locale) pairs.getKey();
081: Locale requestedLocale = FacesContext.getCurrentInstance()
082: .getExternalContext().getRequestLocale();
083: tempList.add(new Value(this Locale,
084: convertLocaleDisplayName(this Locale
085: .getDisplayLanguage(requestedLocale)),
086: getDisplayNameByLocale(this Locale, displayNames)));
087:
088: }
089: return tempList;
090: }
091:
092: private String getDisplayNameByLocale(Locale locale,
093: Map displayNames) {
094: LocalizedString.Value lsv = (LocalizedString.Value) displayNames
095: .get(locale);
096: return lsv.getString();
097: }
098:
099: private String convertLocaleDisplayName(String str) {
100: if (str.length() > 0) {
101: return str.substring(0, 1).toUpperCase()
102: + str.substring(1, str.length());
103: } else {
104: return str;
105: }
106: }
107:
108: public class Value {
109: private String localeDisplayName;
110:
111: private Locale locale;
112:
113: private String text;
114:
115: public Value(Locale locale, String localeDisplayName,
116: String text) {
117: this .localeDisplayName = localeDisplayName;
118: this .locale = locale;
119: this .text = text;
120: }
121:
122: public String getLocaleDisplayName() {
123: return localeDisplayName;
124: }
125:
126: public void setLocaleDisplayName(String localeDisplayName) {
127: this .localeDisplayName = localeDisplayName;
128: }
129:
130: public String getText() {
131: return text;
132: }
133:
134: public void setText(String text) {
135: this .text = text;
136: }
137:
138: public Locale getLocale() {
139: return locale;
140: }
141:
142: public void setLocale(Locale locale) {
143: this.locale = locale;
144: }
145:
146: }
147: }
|