001package jmri.jmrit.logixng.expressions.swing;
002
003import java.time.LocalTime;
004import java.time.format.DateTimeFormatter;
005
006import java.util.List;
007
008import javax.annotation.CheckForNull;
009import javax.annotation.Nonnull;
010import javax.swing.*;
011
012import jmri.InstanceManager;
013import jmri.jmrit.logixng.*;
014import jmri.jmrit.logixng.expressions.ExpressionClock;
015import jmri.jmrit.logixng.expressions.ExpressionClock.Type;
016import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
017import jmri.util.swing.JComboBoxUtil;
018
019/**
020 * Configures an ExpressionClock object with a Swing JPanel.
021 *
022 * @author Daniel Bergqvist Copyright 2021
023 * @author Dave Sand Copyright 2021
024 */
025public class ExpressionClockSwing extends AbstractDigitalExpressionSwing {
026
027    private JComboBox<Type> _stateComboBox;
028    private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox;
029    private JTextField _beginTextField;
030    private JTextField _endTextField;
031
032    private int _beginMinutes;
033    private int _endMinutes;
034
035    @Override
036    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
037        ExpressionClock expression = (ExpressionClock) object;
038        panel = new JPanel();
039
040        _stateComboBox = new JComboBox<>();
041        for (Type e : Type.values()) {
042            _stateComboBox.addItem(e);
043        }
044        JComboBoxUtil.setupComboBoxMaxRows(_stateComboBox);
045
046        _is_IsNot_ComboBox = new JComboBox<>();
047        for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) {
048            _is_IsNot_ComboBox.addItem(e);
049        }
050        JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox);
051
052        _beginTextField = new JTextField(4);
053        _beginTextField.setText(ExpressionClock.formatTime(0));
054
055
056        _endTextField = new JTextField(4);
057        _endTextField.setText(ExpressionClock.formatTime(0));
058
059        if (expression != null) {
060            _stateComboBox.setSelectedItem(expression.getType());
061            _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot());
062            _beginTextField.setText(ExpressionClock.formatTime(expression.getBeginTime()));
063            _endTextField.setText(ExpressionClock.formatTime(expression.getEndTime()));
064        }
065
066        JComponent[] components = new JComponent[]{
067            _stateComboBox,
068            _is_IsNot_ComboBox,
069            _beginTextField,
070            _endTextField
071        };
072
073        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
074                Bundle.getMessage("ExpressionClock_Components"), components);
075
076        for (JComponent c : componentList) panel.add(c);
077    }
078
079    /** {@inheritDoc} */
080    @Override
081    public boolean validate(@Nonnull List<String> errorMessages) {
082        _beginMinutes = validateTime(errorMessages, _beginTextField);
083        _endMinutes = validateTime(errorMessages, _endTextField);
084
085        if (!errorMessages.isEmpty()) return false;
086        return true;
087    }
088
089    private int validateTime(List<String> errorMessages, JTextField timeField) {
090        int minutes = 0;
091        try {
092            LocalTime newHHMM = LocalTime.parse(timeField.getText().trim(), DateTimeFormatter.ofPattern("H:mm"));
093            minutes = newHHMM.getHour() * 60 + newHHMM.getMinute();
094            if (minutes < 0 || minutes > 1439) {
095                errorMessages.add(Bundle.getMessage("Clock_RangeError"));
096            }
097        } catch (java.time.format.DateTimeParseException ex) {
098            errorMessages.add(Bundle.getMessage("Clock_ParseError", ex.getParsedString()));
099        }
100        return minutes;
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
106        ExpressionClock expression = new ExpressionClock(systemName, userName);
107        expression.setType(ExpressionClock.Type.FastClock);
108        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public void updateObject(@Nonnull Base object) {
114        if (! (object instanceof ExpressionClock)) {
115            throw new IllegalArgumentException("object must be an ExpressionClock but is a: "+object.getClass().getName());
116        }
117        ExpressionClock expression = (ExpressionClock) object;
118        expression.setType(_stateComboBox.getItemAt(_stateComboBox.getSelectedIndex()));
119        expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem());
120        expression.setRange(_beginMinutes, _endMinutes);
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public String toString() {
126        return Bundle.getMessage("Clock_Short");
127    }
128
129    @Override
130    public void dispose() {
131    }
132
133
134//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrueSwing.class);
135
136}