001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library 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 GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: LocaleChooser.java,v $
023   Revision 1.4  2004/05/12 18:36:55  markl
024   comment block updates
025
026   Revision 1.3  2003/01/19 09:50:53  markl
027   Javadoc & comment header updates.
028
029   Revision 1.2  2001/03/12 09:27:57  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.1  1999/04/23 07:26:46  markl
033   Initial revision
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.ui;
038
039import java.awt.*;
040import java.util.*;
041import javax.swing.*;
042
043/** This class represents a combo box for selecting a locale. Locales are
044  * presented in their own specific localized form.
045  *
046  * @author Mark Lindner
047  */
048
049public class LocaleChooser extends JComboBox
050  {
051  private JComboBox c_country, c_language;
052  private Vector supportedLocales = new Vector();
053
054  /** Construct a new <code>LocaleChooser</code>.
055   *
056   * @param localeList A string containing a comma-separated list of locales,
057   * each of which is of the form
058   * "&lt;language&gt;:&lt;country&gt;[:&lt;variant&gt;]". For example:
059   * "en:US,fr:FR".
060   *
061   * @param localizeDisplay If <code>true</code>, each locale entry is
062   * localized to itself. Otherwise, all entries are displayed in the current
063   * locale.
064   */
065   
066  public LocaleChooser(String localeList, boolean localizeDisplay)
067    {
068    StringTokenizer st = new StringTokenizer(localeList, ",");
069    while(st.hasMoreTokens())
070      {
071      String s = st.nextToken();
072      StringTokenizer st2 = new StringTokenizer(s, ":");
073      if(st2.countTokens() < 2) continue;
074      String language = st2.nextToken();
075      String country = st2.nextToken();
076      String variant = (st2.hasMoreTokens() ? st2.nextToken() : null);
077      Locale l;
078      if(variant != null)
079        l = new Locale(language, country, variant);
080      else
081        l = new Locale(language, country);
082      supportedLocales.addElement(l);
083
084      String lang = (localizeDisplay ? l.getDisplayLanguage(l)
085                     : l.getDisplayLanguage());
086      if(lang.length() > 0)
087        {
088        String first = lang.substring(0, 1);
089        first = (localizeDisplay ? first.toUpperCase(l)
090                 : first.toUpperCase());
091        lang = first + lang.substring(1);
092        }
093
094      String item = lang + " - " + (localizeDisplay ? l.getDisplayCountry(l)
095                                    : l.getDisplayCountry());
096      addItem(item);
097      }
098    }
099
100  /** Get the currently selected locale.
101   *
102   * @return A <code>Locale</code> object corresponding to the currently-
103   * selected locale, or <code>null</code> if there is no selection.
104   */
105   
106  public Locale getSelectedLocale()
107    {
108    int x = getSelectedIndex();
109    if(x < 0) return(null);
110    return((Locale)(supportedLocales.elementAt(x)));
111    }
112
113  }
114
115/* end of source file */