001package jmri.jmrit.logixng.actions.swing;
002
003import java.awt.Color;
004import java.util.List;
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.ActionThrottle;
015
016/**
017 * Configures an ActionThrottle object with a Swing JPanel.
018 *
019 * @author Daniel Bergqvist Copyright 2021
020 */
021public class ActionThrottleSwing extends AbstractDigitalActionSwing {
022
023    private JComboBox<Connection> _connection;
024    private JCheckBox _stopLocoWhenSwitchingLoco;
025
026    @Override
027    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
028        if ((object != null) && !(object instanceof ActionThrottle)) {
029            throw new IllegalArgumentException("object must be an ActionThrottle but is a: "+object.getClass().getName());
030        }
031
032        ActionThrottle action = (ActionThrottle)object;
033
034        panel = new JPanel();
035        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
036
037        JPanel queryPanel = new JPanel();
038        queryPanel.setBorder(BorderFactory.createLineBorder(Color.black));
039
040        JPanel connectionPanel = new JPanel();
041        connectionPanel.add(new JLabel(Bundle.getMessage("ActionThrottleSwing_Connection")));
042
043        _connection = new JComboBox<>();
044        _connection.addItem(new Connection(null));
045        List<SystemConnectionMemo> systemConnections =
046                jmri.InstanceManager.getList(SystemConnectionMemo.class);
047        for (SystemConnectionMemo connection : systemConnections) {
048            if (!connection.provides(ThrottleManager.class)) continue;
049            Connection c = new Connection(connection);
050            _connection.addItem(c);
051            if ((action != null) && (action.getMemo() == connection)) {
052                _connection.setSelectedItem(c);
053            }
054        }
055        connectionPanel.add(_connection);
056        panel.add(connectionPanel);
057
058        _stopLocoWhenSwitchingLoco = new JCheckBox(Bundle.getMessage("ActionThrottleSwing_StopLocoWhenSwitchingLoco"));
059        if (action != null) {
060            _stopLocoWhenSwitchingLoco.setSelected(action.isStopLocoWhenSwitchingLoco());
061        } else {
062            _stopLocoWhenSwitchingLoco.setSelected(true);
063        }
064        panel.add(_stopLocoWhenSwitchingLoco);
065    }
066
067    /** {@inheritDoc} */
068    @Override
069    public boolean validate(@Nonnull List<String> errorMessages) {
070        return true;
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
076        ActionThrottle action = new ActionThrottle(systemName, userName);
077        updateObject(action);
078
079        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public void updateObject(@Nonnull Base object) {
085        if (! (object instanceof ActionThrottle)) {
086            throw new IllegalArgumentException("object must be an ActionThrottle but is a: "+object.getClass().getName());
087        }
088
089        ActionThrottle action = (ActionThrottle)object;
090
091        action.setMemo(_connection.getItemAt(_connection.getSelectedIndex())._memo);
092        action.setStopLocoWhenSwitchingLoco(_stopLocoWhenSwitchingLoco.isSelected());
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public String toString() {
098        return Bundle.getMessage("ActionThrottle_Short");
099    }
100
101    @Override
102    public void dispose() {
103    }
104
105
106
107    private static class Connection {
108
109        private SystemConnectionMemo _memo;
110
111        public Connection(SystemConnectionMemo memo) {
112            _memo = memo;
113        }
114
115        @Override
116        public String toString() {
117            if (_memo == null) {
118                return Bundle.getMessage("ActionThrottleSwing_DefaultConnection");
119            }
120            return _memo.getUserName();
121        }
122    }
123
124}