001package jmri.jmrix.loconet.logixng.swing;
002
003import java.awt.Color;
004import java.util.*;
005
006import javax.annotation.CheckForNull;
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.*;
011import jmri.jmrit.logixng.Base;
012import jmri.jmrit.logixng.DigitalActionManager;
013import jmri.jmrit.logixng.MaleSocket;
014import jmri.jmrit.logixng.actions.swing.AbstractDigitalActionSwing;
015import jmri.jmrix.loconet.logixng.SetSpeedZero;
016import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
017
018/**
019 * Configures an ExpressionTurnout object with a Swing JPanel.
020 *
021 * @author Daniel Bergqvist Copyright 2020
022 */
023public class SetSpeedZeroSwing extends AbstractDigitalActionSwing {
024
025    private JComboBox<LocoNetConnection> _locoNetConnection;
026
027    @Override
028    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
029        if ((object != null) && !(object instanceof SetSpeedZero)) {
030            throw new IllegalArgumentException("object must be an SetSpeedZero but is a: "+object.getClass().getName());
031        }
032
033        SetSpeedZero action = (SetSpeedZero)object;
034
035        panel = new JPanel();
036        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
037
038        panel.add(new JLabel(Bundle.getMessage("SetSpeedZeroInfo1")));
039        panel.add(new JLabel(Bundle.getMessage("SetSpeedZeroInfo2")));
040        panel.add(new JLabel(Bundle.getMessage("SetSpeedZeroInfo3")));
041        panel.add(new JLabel(Bundle.getMessage("SetSpeedZeroInfo4")));
042        panel.add(new JLabel(Bundle.getMessage("SetSpeedZeroInfo5")));
043
044        JPanel queryPanel = new JPanel();
045        queryPanel.setBorder(BorderFactory.createLineBorder(Color.black));
046
047        JPanel locoNetPanel = new JPanel();
048        locoNetPanel.add(new JLabel(Bundle.getMessage("LocoNetConnection")));
049
050        _locoNetConnection = new JComboBox<>();
051        List<LocoNetSystemConnectionMemo> systemConnections =
052                jmri.InstanceManager.getList(LocoNetSystemConnectionMemo.class);
053        for (LocoNetSystemConnectionMemo connection : systemConnections) {
054            LocoNetConnection c = new LocoNetConnection(connection);
055            _locoNetConnection.addItem(c);
056            if ((action != null) && (action.getMemo() == connection)) {
057                _locoNetConnection.setSelectedItem(c);
058            }
059        }
060        locoNetPanel.add(_locoNetConnection);
061
062        panel.add(locoNetPanel);
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public boolean validate(@Nonnull List<String> errorMessages) {
068        return true;
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
074        LocoNetSystemConnectionMemo memo =
075                _locoNetConnection.getItemAt(_locoNetConnection.getSelectedIndex())._memo;
076
077        SetSpeedZero action = new SetSpeedZero(systemName, userName, memo);
078        updateObject(action);
079
080        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public void updateObject(@Nonnull Base object) {
086        if (! (object instanceof SetSpeedZero)) {
087            throw new IllegalArgumentException("object must be an ExpressionTurnout but is a: "+object.getClass().getName());
088        }
089
090        SetSpeedZero action = (SetSpeedZero)object;
091
092        action.setMemo(_locoNetConnection.getItemAt(_locoNetConnection.getSelectedIndex())._memo);
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public String toString() {
098        return Bundle.getMessage("SetSpeedZero_Short");
099    }
100
101    @Override
102    public void dispose() {
103    }
104
105
106
107    private static class LocoNetConnection {
108
109        private LocoNetSystemConnectionMemo _memo;
110
111        public LocoNetConnection(LocoNetSystemConnectionMemo memo) {
112            _memo = memo;
113        }
114
115        @Override
116        public String toString() {
117            return _memo.getUserName();
118        }
119    }
120
121//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SetSpeedZeroSwing.class);
122
123}