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