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.ActionClockRate;
012import jmri.jmrit.logixng.actions.ActionClockRate.ClockState;
013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
014import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectDoubleSwing;
016
017/**
018 * Configures an ActionClockRate object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2021
021 * @author Dave Sand Copyright 2021
022 */
023public class ActionClockRateSwing extends AbstractDigitalActionSwing {
024
025    private LogixNG_SelectEnumSwing<ClockState> _selectEnumSwing;
026    private LogixNG_SelectDoubleSwing _selectSpeedSwing;
027
028    private final JLabel labelToWith = new JLabel(Bundle.getMessage("ActionClockRate_LabelTo"));
029
030
031    public ActionClockRateSwing() {
032    }
033
034    public ActionClockRateSwing(JDialog dialog) {
035        super.setJDialog(dialog);
036    }
037
038    @Override
039    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
040        ActionClockRate action = (ActionClockRate) object;
041        if (action == null) {
042            // Create a temporary action
043            action = new ActionClockRate("IQDA1", null);
044        }
045
046        _selectEnumSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
047        _selectSpeedSwing = new LogixNG_SelectDoubleSwing(getJDialog(), this);
048
049        panel = new JPanel();
050        JPanel tabbedPaneClockState;
051        JPanel tabbedPaneSpeed;
052
053        tabbedPaneClockState = _selectEnumSwing.createPanel(action.getSelectEnum(), ClockState.values());
054        tabbedPaneSpeed = _selectSpeedSwing.createPanel(action.getSelectSpeed());
055
056        JComponent[] components = new JComponent[]{
057            tabbedPaneClockState,
058            labelToWith,
059            tabbedPaneSpeed};
060
061        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
062                Bundle.getMessage("ActionClock_Components"), components);
063
064        for (JComponent c : componentList) panel.add(c);
065    }
066
067    /** {@inheritDoc} */
068    @Override
069    public boolean validate(@Nonnull List<String> errorMessages) {
070        // Create a temporary action to test formula
071        ActionClockRate action = new ActionClockRate("IQDA1", null);
072
073        _selectEnumSwing.validate(action.getSelectEnum(), errorMessages);
074        _selectSpeedSwing.validate(action.getSelectSpeed(), errorMessages);
075
076        return errorMessages.isEmpty();
077    }
078
079    /** {@inheritDoc} */
080    @Override
081    public String getAutoSystemName() {
082        return InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName();
083    }
084
085    /** {@inheritDoc} */
086    @Override
087    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
088        ActionClockRate action = new ActionClockRate(systemName, userName);
089        updateObject(action);
090        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
091    }
092
093    /** {@inheritDoc} */
094    @Override
095    public void updateObject(@Nonnull Base object) {
096        if (! (object instanceof ActionClockRate)) {
097            throw new IllegalArgumentException("object must be an ActionClockRate but is a: "+object.getClass().getName());
098        }
099        ActionClockRate action = (ActionClockRate) object;
100
101        _selectEnumSwing.updateObject(action.getSelectEnum());
102        _selectSpeedSwing.updateObject(action.getSelectSpeed());
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public String toString() {
108        return Bundle.getMessage("ActionClockRate_Short");
109    }
110
111    @Override
112    public void dispose() {
113        _selectEnumSwing.dispose();
114        _selectSpeedSwing.dispose();
115    }
116
117
118//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionClockRateSwing.class);
119
120}