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