001package jmri.jmrit.logixng.util.swing;
002
003import java.nio.charset.Charset;
004import java.util.List;
005
006import javax.annotation.CheckForNull;
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
011import jmri.jmrit.logixng.util.LogixNG_SelectCharset;
012import jmri.jmrit.logixng.util.LogixNG_SelectCharset.Addressing;
013import jmri.util.swing.JComboBoxUtil;
014
015/**
016 * Swing class for jmri.jmrit.logixng.util.LogixNG_SelectCharset.
017 *
018 * @author Daniel Bergqvist (C) 2023
019 */
020public class LogixNG_SelectCharsetSwing {
021
022    private final JDialog _dialog;
023    private final LogixNG_SelectStringSwing _selectUserSpecified;
024
025    private JTabbedPane _tabbedPane;
026    private JPanel _panelStandard;
027    private JPanel _panelAll;
028    private JPanel _panelUserSpecified;
029    private JComboBox<Charset> _standardValueComboBox;
030    private JComboBox<Charset> _allValueComboBox;
031
032
033    public LogixNG_SelectCharsetSwing(
034            @Nonnull JDialog dialog,
035            @Nonnull SwingConfiguratorInterface swi) {
036        _dialog = dialog;
037        _selectUserSpecified = new LogixNG_SelectStringSwing(_dialog, swi);
038    }
039
040    public JPanel createPanel(@CheckForNull LogixNG_SelectCharset selectCharset) {
041
042        JPanel panel = new JPanel();
043
044        _tabbedPane = new JTabbedPane();
045        _panelStandard = new javax.swing.JPanel();
046        _panelAll = new javax.swing.JPanel();
047
048        _standardValueComboBox = new JComboBox<>();
049        _standardValueComboBox = new JComboBox<>();
050        for (Charset charset : LogixNG_SelectCharset.STANDARD_CHARSETS) {
051            _standardValueComboBox.addItem(charset);
052        }
053        JComboBoxUtil.setupComboBoxMaxRows(_standardValueComboBox);
054        _panelStandard.add(_standardValueComboBox);
055
056        _allValueComboBox = new JComboBox<>();
057        _allValueComboBox = new JComboBox<>();
058        for (Charset charset : Charset.availableCharsets().values()) {
059            _allValueComboBox.addItem(charset);
060        }
061        JComboBoxUtil.setupComboBoxMaxRows(_allValueComboBox);
062        _panelAll.add(_allValueComboBox);
063
064        if (selectCharset != null) {
065            _panelUserSpecified = _selectUserSpecified.createPanel(selectCharset.getSelectUserSpecified());
066        } else {
067            _panelUserSpecified = _selectUserSpecified.createPanel(null);
068        }
069
070        _tabbedPane.addTab(Addressing.Standard.toString(), _panelStandard);
071        _tabbedPane.addTab(Addressing.All.toString(), _panelAll);
072        _tabbedPane.addTab(Addressing.UserSpecified.toString(), _panelUserSpecified);
073
074
075        if (selectCharset != null) {
076            switch (selectCharset.getAddressing()) {
077                case Standard: _tabbedPane.setSelectedComponent(_panelStandard); break;
078                case All: _tabbedPane.setSelectedComponent(_panelAll); break;
079                case UserSpecified: _tabbedPane.setSelectedComponent(_panelUserSpecified); break;
080                default: throw new IllegalArgumentException("invalid _addressing state: " + selectCharset.getAddressing().name());
081            }
082            if (selectCharset.getStandardValue() != null) {
083                _standardValueComboBox.setSelectedItem(selectCharset.getStandardValue());
084            }
085            if (selectCharset.getAllValue() != null) {
086                _allValueComboBox.setSelectedItem(selectCharset.getAllValue());
087            }
088        }
089
090        panel.add(_tabbedPane);
091        return panel;
092    }
093
094    public boolean validate(
095            @Nonnull LogixNG_SelectCharset selectCharset,
096            @Nonnull List<String> errorMessages) {
097
098        _selectUserSpecified.validate(selectCharset.getSelectUserSpecified(), errorMessages);
099
100        return errorMessages.isEmpty();
101    }
102
103    public void updateObject(@Nonnull LogixNG_SelectCharset selectCharset) {
104
105        if (_tabbedPane.getSelectedComponent() == _panelStandard) {
106            selectCharset.setAddressing(Addressing.Standard);
107            selectCharset.setStandardValue(_standardValueComboBox.getItemAt(_standardValueComboBox.getSelectedIndex()));
108        } else if (_tabbedPane.getSelectedComponent() == _panelAll) {
109            selectCharset.setAddressing(Addressing.All);
110            selectCharset.setAllValue(_allValueComboBox.getItemAt(_allValueComboBox.getSelectedIndex()));
111        } else if (_tabbedPane.getSelectedComponent() == _panelUserSpecified) {
112            selectCharset.setAddressing(Addressing.UserSpecified);
113        } else {
114            throw new IllegalArgumentException("_tabbedPaneEnum has unknown selection");
115        }
116
117        _selectUserSpecified.updateObject(selectCharset.getSelectUserSpecified());
118    }
119
120    public void dispose() {
121        _selectUserSpecified.dispose();
122    }
123
124}