/**
* 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.
*
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>
private static final long serialVersionUID = 1L;
private boolean editable;
+ private String[] allowedValues;
/**
* Create a new {@link ConfigItemCombobox} for the given {@link MetaInfo}.
public ConfigItemCombobox(MetaInfo<E> info, boolean editable) {
super(info, true);
this.editable = editable;
+ this.allowedValues = info.getAllowedValues();
}
@Override
@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;
}
--- /dev/null
+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;
+ }
+}