ConfigItem: add locale support
authorNiki Roo <niki@nikiroo.be>
Thu, 30 May 2019 18:41:09 +0000 (20:41 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 30 May 2019 18:41:09 +0000 (20:41 +0200)
src/be/nikiroo/utils/resources/MetaInfo.java
src/be/nikiroo/utils/resources/TransBundle.java
src/be/nikiroo/utils/ui/ConfigItem.java
src/be/nikiroo/utils/ui/ConfigItemCombobox.java
src/be/nikiroo/utils/ui/ConfigItemLocale.java [new file with mode: 0644]

index 4722be6f65c1a7328f4c30198eddccf5c5a2b2f8..8ec98f9935168b9f0dbe231b1af917e5ce21a316 100644 (file)
@@ -141,7 +141,7 @@ public class MetaInfo<E extends Enum<E>> implements Iterable<MetaInfo<E>> {
        /**
         * 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}.
         * <p>
         * Will always allow an empty string in addition to the rest.
         * 
@@ -159,6 +159,22 @@ public class MetaInfo<E extends Enum<E>> implements Iterable<MetaInfo<E>> {
                return withEmpty;
        }
 
+       /**
+        * Return all the languages known by the program for this bundle.
+        * <p>
+        * 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<String> getKnownLanguages() {
+               if (bundle instanceof TransBundle) {
+                       return ((TransBundle<E>) bundle).getKnownLanguages();
+               }
+
+               return new ArrayList<String>();
+       }
+
        /**
         * This item is a comma-separated list of values instead of a single value.
         * <p>
index 426112900ef6d4ebcf4ef5057812dcd90ae2329f..28fa2802e215eab074ff51405d7488b733f84459 100644 (file)
@@ -169,8 +169,7 @@ public class TransBundle<E extends Enum<E>> extends Bundle<E> {
        }
 
        /**
-        * Return all the languages known by the program.
-        * 
+        * Return all the languages known by the program for this bundle.
         * 
         * @return the known language codes
         */
index 32114c00d91087abb2f8daaabe09b59adf0d4db1..87e43795094332ca19ec7d0f8f5baa2125cfe72b 100644 (file)
@@ -141,8 +141,10 @@ public class ConfigItem<E extends Enum<E>> extends JPanel {
                case PASSWORD:
                        configItem = new ConfigItemPassword<E>(info);
                        break;
+               case LOCALE:
+                       configItem = new ConfigItemLocale<E>(info);
+                       break;
                case STRING:
-               case LOCALE: // TODO?
                default:
                        configItem = new ConfigItemString<E>(info);
                        break;
index 32bc7e72ac4c938504fc559806144093774b7176..28c5df8d89002fa219040a3a3441421f60be2466 100644 (file)
@@ -9,6 +9,7 @@ class ConfigItemCombobox<E extends Enum<E>> extends ConfigItem<E> {
        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<E extends Enum<E>> extends ConfigItem<E> {
        public ConfigItemCombobox(MetaInfo<E> info, boolean editable) {
                super(info, true);
                this.editable = editable;
+               this.allowedValues = info.getAllowedValues();
        }
 
        @Override
@@ -59,7 +61,7 @@ class ConfigItemCombobox<E extends Enum<E>> extends ConfigItem<E> {
        @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 (file)
index 0000000..b648645
--- /dev/null
@@ -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<E extends Enum<E>> extends ConfigItemCombobox<E> {
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * Create a new {@link ConfigItemLocale} for the given {@link MetaInfo}.
+        * 
+        * @param info
+        *            the {@link MetaInfo}
+        */
+       public ConfigItemLocale(MetaInfo<E> 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;
+       }
+}