01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13: package org.rapla.client.internal;
14:
15: import java.awt.Component;
16: import java.util.Locale;
17:
18: import javax.swing.Action;
19: import javax.swing.DefaultListCellRenderer;
20: import javax.swing.JComboBox;
21: import javax.swing.JComponent;
22: import javax.swing.JList;
23:
24: import org.apache.avalon.framework.logger.AbstractLogEnabled;
25: import org.apache.avalon.framework.logger.Logger;
26: import org.rapla.components.xmlbundle.I18nBundle;
27: import org.rapla.framework.RaplaContext;
28: import org.rapla.framework.RaplaException;
29: import org.rapla.framework.RaplaLocale;
30: import org.rapla.gui.toolkit.RaplaWidget;
31:
32: final public class LanguageChooser extends AbstractLogEnabled implements
33: RaplaWidget {
34: JComboBox jComboBox;
35: String country;
36: RaplaContext context;
37:
38: public LanguageChooser(Logger logger, RaplaContext context)
39: throws RaplaException {
40: enableLogging(logger);
41: this .context = context;
42:
43: final I18nBundle i18n = (I18nBundle) context
44: .lookup(I18nBundle.ROLE + "/org.rapla.RaplaResources");
45: final RaplaLocale raplaLocale = (RaplaLocale) context
46: .lookup(RaplaLocale.ROLE);
47: country = raplaLocale.getLocale().getCountry();
48: String[] languages = raplaLocale.getAvailableLanguages();
49:
50: String[] entries = new String[languages.length + 1];
51: System.arraycopy(languages, 0, entries, 1, languages.length);
52: jComboBox = new JComboBox(entries);
53: jComboBox.setRenderer(new DefaultListCellRenderer() {
54: private static final long serialVersionUID = 1L;
55:
56: public Component getListCellRendererComponent(JList list,
57: Object value, int index, boolean isSelected,
58: boolean cellHasFocus) {
59: if (value != null) {
60: value = new Locale((String) value, country)
61: .getDisplayLanguage(raplaLocale.getLocale());
62: } else {
63: value = i18n.getString("default") + " "
64: + i18n.getString("preferences");
65: }
66: return super .getListCellRendererComponent(list, value,
67: index, isSelected, cellHasFocus);
68: }
69: });
70: //jComboBox.setSelectedItem( raplaLocale.getLocale().getLanguage());
71: }
72:
73: public JComponent getComponent() {
74: return jComboBox;
75: }
76:
77: public void setSelectedLanguage(String lang) {
78: jComboBox.setSelectedItem(lang);
79: }
80:
81: public String getSelectedLanguage() {
82: return (String) jComboBox.getSelectedItem();
83: }
84:
85: public void setChangeAction(Action languageChanged) {
86: jComboBox.setAction(languageChanged);
87: }
88:
89: }
|