From: Niki Roo Date: Thu, 30 May 2019 18:41:09 +0000 (+0200) Subject: ConfigItem: add locale support X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=commitdiff_plain;h=0f7de31e53a20527cdbe3887ea55916800fd3d5d ConfigItem: add locale support --- diff --git a/src/be/nikiroo/utils/resources/MetaInfo.java b/src/be/nikiroo/utils/resources/MetaInfo.java index 4722be6..8ec98f9 100644 --- a/src/be/nikiroo/utils/resources/MetaInfo.java +++ b/src/be/nikiroo/utils/resources/MetaInfo.java @@ -141,7 +141,7 @@ public class MetaInfo> implements Iterable> { /** * The allowed list of values that a {@link Format#FIXED_LIST} item is * allowed to be, or a list of suggestions for {@link Format#COMBO_LIST} - * items. + * items. Also works for {@link Format#LOCALE}. *

* Will always allow an empty string in addition to the rest. * @@ -159,6 +159,22 @@ public class MetaInfo> implements Iterable> { return withEmpty; } + /** + * Return all the languages known by the program for this bundle. + *

+ * This only works for {@link TransBundle}, and will return an empty list if + * this is not a {@link TransBundle}. + * + * @return the known language codes + */ + public List getKnownLanguages() { + if (bundle instanceof TransBundle) { + return ((TransBundle) bundle).getKnownLanguages(); + } + + return new ArrayList(); + } + /** * This item is a comma-separated list of values instead of a single value. *

diff --git a/src/be/nikiroo/utils/resources/TransBundle.java b/src/be/nikiroo/utils/resources/TransBundle.java index 4261129..28fa280 100644 --- a/src/be/nikiroo/utils/resources/TransBundle.java +++ b/src/be/nikiroo/utils/resources/TransBundle.java @@ -169,8 +169,7 @@ public class TransBundle> extends Bundle { } /** - * Return all the languages known by the program. - * + * Return all the languages known by the program for this bundle. * * @return the known language codes */ diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java index 32114c0..87e4379 100644 --- a/src/be/nikiroo/utils/ui/ConfigItem.java +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -141,8 +141,10 @@ public class ConfigItem> extends JPanel { case PASSWORD: configItem = new ConfigItemPassword(info); break; + case LOCALE: + configItem = new ConfigItemLocale(info); + break; case STRING: - case LOCALE: // TODO? default: configItem = new ConfigItemString(info); break; diff --git a/src/be/nikiroo/utils/ui/ConfigItemCombobox.java b/src/be/nikiroo/utils/ui/ConfigItemCombobox.java index 32bc7e7..28c5df8 100644 --- a/src/be/nikiroo/utils/ui/ConfigItemCombobox.java +++ b/src/be/nikiroo/utils/ui/ConfigItemCombobox.java @@ -9,6 +9,7 @@ class ConfigItemCombobox> extends ConfigItem { private static final long serialVersionUID = 1L; private boolean editable; + private String[] allowedValues; /** * Create a new {@link ConfigItemCombobox} for the given {@link MetaInfo}. @@ -21,6 +22,7 @@ class ConfigItemCombobox> extends ConfigItem { public ConfigItemCombobox(MetaInfo info, boolean editable) { super(info, true); this.editable = editable; + this.allowedValues = info.getAllowedValues(); } @Override @@ -59,7 +61,7 @@ class ConfigItemCombobox> extends ConfigItem { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected JComponent createField(int item) { - JComboBox field = new JComboBox(info.getAllowedValues()); + JComboBox field = new JComboBox(allowedValues); field.setEditable(editable); return field; } diff --git a/src/be/nikiroo/utils/ui/ConfigItemLocale.java b/src/be/nikiroo/utils/ui/ConfigItemLocale.java new file mode 100644 index 0000000..b648645 --- /dev/null +++ b/src/be/nikiroo/utils/ui/ConfigItemLocale.java @@ -0,0 +1,62 @@ +package be.nikiroo.utils.ui; + +import java.awt.Component; +import java.util.Locale; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JList; + +import be.nikiroo.utils.resources.MetaInfo; + +class ConfigItemLocale> extends ConfigItemCombobox { + private static final long serialVersionUID = 1L; + + /** + * Create a new {@link ConfigItemLocale} for the given {@link MetaInfo}. + * + * @param info + * the {@link MetaInfo} + */ + public ConfigItemLocale(MetaInfo info) { + super(info, true); + } + + // rawtypes for Java 1.6 (and 1.7 ?) support + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Override + protected JComponent createField(int item) { + JComboBox field = (JComboBox) super.createField(item); + field.setRenderer(new DefaultListCellRenderer() { + private static final long serialVersionUID = 1L; + + @Override + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, + boolean cellHasFocus) { + + String svalue = value == null ? "" : value.toString(); + String[] tab = svalue.split("-"); + Locale locale = null; + if (tab.length == 1) { + locale = new Locale(tab[0]); + } else if (tab.length == 2) { + locale = new Locale(tab[0], tab[1]); + } else if (tab.length == 3) { + locale = new Locale(tab[0], tab[1], tab[2]); + } + + String displayValue = svalue; + if (locale != null) { + displayValue = locale.getDisplayName(); + } + + return super.getListCellRendererComponent(list, displayValue, + index, isSelected, cellHasFocus); + } + }); + + return field; + } +}