package org.andork.bind2.ui;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.andork.awt.GridBagWizard;
import org.andork.awt.GridBagWizard.DefaultAutoInsets;
import org.andork.bind2.DefaultBinder;
import org.andork.i18n.I18n;
import org.andork.i18n.I18n.Localizer;
import org.andork.swing.CellRenderers;
import org.andork.swing.selector.DefaultSelector;
public class NewI18nTest {
public static void main(String[] args) {
I18n i18n = new I18n();
Localizer localizer = I18n.forClass(NewI18nTest.class);
JFrame frame = new JFrame();
Container content = frame.getContentPane();
JLabel localeLabel = new JLabel();
new JLabelTextBinding(localeLabel).textLink.bind(localizer.stringBinder("localeLabel.text"));
DefaultSelector<Locale> localeSelector = new DefaultSelector<>();
localeSelector.comboBox().setRenderer(
CellRenderers.map(value -> {
if (value == null) {
return null;
}
Locale locale = (Locale) value;
return locale.getDisplayLanguage(locale) + " (" + locale.getDisplayCountry(locale) + ")";
} , new DefaultListCellRenderer()));
I18n.setLocaleBinder(new ISelectorSelectionBinder<>(localeSelector));
localeSelector.addAvailableValue(Locale.US);
localeSelector.addAvailableValue(Locale.UK);
localeSelector.addAvailableValue(Locale.CANADA);
localeSelector.addAvailableValue(Locale.CANADA_FRENCH);
localeSelector.addAvailableValue(Locale.GERMANY);
localeSelector.addAvailableValue(Locale.CHINA);
localeSelector.addAvailableValue(Locale.JAPAN);
DefaultBinder<Date> dateBinder = new DefaultBinder<>(new Date());
new javax.swing.Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dateBinder.set(new Date());
}
}).start();
JLabel dateLabel = new JLabel();
new JLabelTextBinding(dateLabel).textLink.bind(localizer.formattedStringBinder(
new DefaultBinder<String>("dateLabel.text"),
I18n.formattedDateTimeBinder(dateBinder, DateFormat.MEDIUM, DateFormat.MEDIUM)));
JButton testButton = new JButton();
new ButtonTextBinding(testButton).textLink.bind(localizer.stringBinder("testButton.text"));
GridBagWizard gbw = GridBagWizard.create(content);
gbw.defaults().autoinsets(new DefaultAutoInsets(5, 5));
gbw.put(localeLabel, localeSelector.comboBox()).y(0).intoRow();
gbw.put(dateLabel).below(localeLabel, localeSelector.comboBox()).fillx();
gbw.put(testButton).below(dateLabel).fillx();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}