package net.sf.myjaut.i18n;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import net.sf.myjaut.URLProvider;
public class I18nMenu extends JMenu implements I18nListener {
public I18nMenu(final I18nManager i18nManager) {
ButtonGroup group = new ButtonGroup();
for (final Locale language : i18nManager.getLocales()) {
JMenuItem mnuItem = new JRadioButtonMenuItem(i18nManager.getLocaleName(language), i18nManager.getCurrentLocale().equals(language));
mnuItem.setMnemonic(i18nManager.getLocaleName(language).charAt(0));
try {
mnuItem.setIcon(new ImageIcon(new URLProvider("icons/flags/" + language + ".gif").getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH)));
}
catch (IOException exc) {
// This shouldn't happen: icons are in JAR.
}
mnuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
i18nManager.setCurrentLocale(language);
}
});
mnuItemsByLanguage.put(language, mnuItem);
add(mnuItem);
group.add(mnuItem);
}
i18nManager.addI18nListener(this);
}
public void signalI18nChange(I18nManager i18nManager) {
((JMenuItem) mnuItemsByLanguage.get(i18nManager.getCurrentLocale())).setSelected(true);
}
private Map<Locale, JMenuItem> mnuItemsByLanguage = new Hashtable<Locale, JMenuItem>();
}