001: /*
002: * 02/02/2002 - 20:54:54
003: *
004: * JLocaleChooser.java - A bean for choosing locales
005: * Copyright (C) 2002 Kai Toedter
006: * kai@toedter.com
007: * www.toedter.com
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023:
024: package com.toedter.components;
025:
026: import java.awt.event.ItemEvent;
027: import java.awt.event.ItemListener;
028: import java.util.Calendar;
029: import java.util.Locale;
030:
031: import javax.swing.JComboBox;
032: import javax.swing.JFrame;
033:
034: /**
035: * JLocaleChooser is a bean for choosing locales.
036: *
037: * @version 1.1 02/04/02
038: * @author Kai Toedter
039: */
040: public class JLocaleChooser extends JComboBox implements ItemListener {
041: private static final long serialVersionUID = 3258413949719689528L;
042:
043: /**
044: * Default JLocaleChooser constructor.
045: */
046: public JLocaleChooser() {
047: super ();
048: addItemListener(this );
049: locales = Calendar.getAvailableLocales();
050: localeCount = locales.length;
051:
052: for (int i = 0; i < localeCount; i++) {
053: if (locales[i].getCountry().length() > 0) {
054: addItem(locales[i].getDisplayName());
055: }
056: }
057:
058: setLocale(Locale.getDefault());
059: }
060:
061: /**
062: * The ItemListener for the locales.
063: */
064: public void itemStateChanged(ItemEvent iEvt) {
065: String item = (String) iEvt.getItem();
066: int i;
067:
068: for (i = 0; i < localeCount; i++) {
069: if (locales[i].getDisplayName().equals(item))
070: break;
071: }
072: setLocale(locales[i], false);
073: }
074:
075: /**
076: * Sets the locale.
077: *
078: * @see #getLocale
079: */
080: private void setLocale(Locale l, boolean select) {
081: Locale oldLocale = locale;
082: locale = l;
083: int n = 0;
084:
085: if (select) {
086: for (int i = 0; i < localeCount; i++) {
087: if (locales[i].getCountry().length() > 0) {
088: if (locales[i].equals(locale))
089: setSelectedIndex(n);
090: n += 1;
091: }
092: }
093: }
094:
095: firePropertyChange("locale", oldLocale, locale);
096: }
097:
098: /**
099: * Sets the locale.
100: * This is a bound property.
101: *
102: * @see #getLocale
103: */
104: public void setLocale(Locale l) {
105: setLocale(l, true);
106: }
107:
108: /**
109: * Returns the locale.
110: *
111: * @see #setLocale
112: */
113: public Locale getLocale() {
114: return locale;
115: }
116:
117: /**
118: * Creates a JFrame with a JLocaleChooser inside and can be used for testing.
119: */
120: static public void main(String[] s) {
121: JFrame frame = new JFrame("LocaleChooser");
122: frame.getContentPane().add(new JLocaleChooser());
123: frame.pack();
124: frame.setVisible(true);
125: }
126:
127: private Locale[] locales;
128: private Locale locale;
129: private int localeCount;
130: }
|