001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2003  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix.messages.channel;
021    
022    import java.util.HashMap;
023    import java.util.Locale;
024    import java.util.Map;
025    
026    import net.jetrix.Language;
027    import net.jetrix.Protocol;
028    import net.jetrix.Server;
029    
030    /**
031     * A generic internationalized text message.
032     *
033     * @author Emmanuel Bourg
034     * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
035     */
036    public abstract class TextMessage extends ChannelMessage
037    {
038        private String text;
039        private String key;
040        private Object params[];
041        private Map<Locale, String> texts;
042        private Map<Protocol, Map<Locale, String>> rawMessages;
043    
044        /**
045         * Return the text of this message using the default server locale.
046         */
047        public String getText()
048        {
049            if (key == null)
050            {
051                return text;
052            }
053            else
054            {
055                Locale defaultLocale;
056                if (Server.getInstance() != null)
057                {
058                    // get the server locale configured in config.xml
059                    defaultLocale = Server.getInstance().getConfig().getLocale();
060                }
061                else
062                {
063                    // get the default system locale
064                    defaultLocale = Locale.getDefault();
065                }
066    
067                return getText(defaultLocale);
068            }
069        }
070    
071        /**
072         * Return the text of this message using the specified locale.
073         */
074        public String getText(Locale locale)
075        {
076            if (key == null)
077            {
078                return text;
079            }
080            else
081            {
082                if (texts == null)
083                {
084                    texts = new HashMap<Locale, String>();
085                }
086    
087                String s = texts.get(locale);
088    
089                if (s == null)
090                {
091                    s = Language.getText(key, locale, params);
092                    texts.put(locale, s);
093                }
094    
095                return s;
096            }
097        }
098    
099        /**
100         * Set the text of the message (locale independant).
101         */
102        public void setText(String text)
103        {
104            this.text = text;
105            this.key = null;
106        }
107    
108        /**
109         * Return the key of the message.
110         */
111        public String getKey()
112        {
113            return key;
114        }
115    
116        /**
117         * Set the key and the parameters of the message for internationalized
118         * text messages.
119         */
120        public void setKey(String key, Object... params)
121        {
122            this.key = key;
123            this.params = params;
124        }
125    
126        /**
127         * Return the parameters of the message.
128         */
129        public Object[] getParams()
130        {
131            return params;
132        }
133    
134        /**
135         * Set the parameters of the message.
136         */
137        public void setParams(Object... params)
138        {
139            this.params = params;
140        }
141    
142        public String getRawMessage(Protocol protocol, Locale locale)
143        {
144            if (key == null)
145            {
146                // use the default caching method for non localized messages
147                return super.getRawMessage(protocol, locale);
148            }
149            else
150            {
151                // use the caching on the (protocol, locale) combo
152                if (rawMessages == null)
153                {
154                    rawMessages = new HashMap<Protocol, Map<Locale, String>>();
155                }
156    
157                Map<Locale, String> i18nMessages = rawMessages.get(protocol);
158                if (i18nMessages == null)
159                {
160                    i18nMessages = new HashMap<Locale, String>();
161                    rawMessages.put(protocol, i18nMessages);
162                }
163    
164                String cachedMessage = i18nMessages.get(locale);
165                if (cachedMessage == null)
166                {
167                    cachedMessage = protocol.translate(this, locale);
168                    i18nMessages.put(locale, cachedMessage);
169                }
170    
171                return cachedMessage;
172            }
173        }
174    
175    }