001package jmri.jmrit.logixng.actions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.actions.Timeout;
012import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
013import jmri.jmrit.logixng.util.TimerUnit;
014import jmri.jmrit.logixng.util.swing.LogixNG_SelectIntegerSwing;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
016
017/**
018 * Configures an Timeout object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2021
021 */
022public class TimeoutSwing extends AbstractDigitalActionSwing {
023
024    private LogixNG_SelectIntegerSwing _selectDelaySwing;
025    private LogixNG_SelectEnumSwing<TimerUnit> _selectTimerUnitSwing;
026
027    @Override
028    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
029        Timeout action = (Timeout)object;
030        if (action == null) {
031            // Create a temporary action
032            action = new Timeout("IQDA1", null);
033        }
034
035        _selectDelaySwing = new LogixNG_SelectIntegerSwing(getJDialog(), this);
036        _selectTimerUnitSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
037
038        panel = new JPanel();
039
040        JPanel _tabbedPaneDelay;
041        JPanel _tabbedPaneTimerUnit;
042
043        _tabbedPaneDelay = _selectDelaySwing.createPanel(action.getSelectDelay());
044        _tabbedPaneTimerUnit = _selectTimerUnitSwing.createPanel(action.getSelectTimerUnit(), TimerUnit.values());
045
046        JComponent[] components = new JComponent[]{
047            _tabbedPaneDelay,
048            _tabbedPaneTimerUnit};
049
050        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
051                Bundle.getMessage("Timeout_Components"), components);
052
053        for (JComponent c : componentList) panel.add(c);
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public boolean validate(@Nonnull List<String> errorMessages) {
059        // Create a temporary action to test formula
060        Timeout action = new Timeout("IQDA1", null);
061
062        _selectDelaySwing.validate(action.getSelectDelay(), errorMessages);
063        _selectTimerUnitSwing.validate(action.getSelectTimerUnit(), errorMessages);
064
065        return errorMessages.isEmpty();
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
071        Timeout action = new Timeout(systemName, userName);
072        updateObject(action);
073        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public void updateObject(@Nonnull Base object) {
079        if (! (object instanceof Timeout)) {
080            throw new IllegalArgumentException("object must be an Timeout but is a: "+object.getClass().getName());
081        }
082        Timeout action = (Timeout)object;
083        _selectDelaySwing.updateObject(action.getSelectDelay());
084        _selectTimerUnitSwing.updateObject(action.getSelectTimerUnit());
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public String toString() {
090        return Bundle.getMessage("Timeout_Short");
091    }
092
093    @Override
094    public void dispose() {
095        _selectDelaySwing.dispose();
096        _selectTimerUnitSwing.dispose();
097    }
098
099}