001package jmri.jmrit.logixng.actions.swing;
002
003import java.util.*;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.*;
010import jmri.jmrit.logixng.Base;
011import jmri.jmrit.logixng.DigitalActionManager;
012import jmri.jmrit.logixng.MaleSocket;
013import jmri.jmrit.logixng.actions.ActionRequestUpdateAllSensors;
014
015/**
016 * Configures an ActionRequestUpdateAllSensors object with a Swing JPanel.
017 *
018 * @author Daniel Bergqvist Copyright 2022
019 */
020public class ActionRequestUpdateAllSensorsSwing extends AbstractDigitalActionSwing {
021
022    private JComboBox<Connection> _connection;
023
024    @Override
025    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
026        if ((object != null) && !(object instanceof ActionRequestUpdateAllSensors)) {
027            throw new IllegalArgumentException("object must be an ActionRequestUpdateAllSensors but is a: "+object.getClass().getName());
028        }
029
030        ActionRequestUpdateAllSensors action = (ActionRequestUpdateAllSensors)object;
031
032        panel = new JPanel();
033        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
034
035        JPanel connectionPanel = new JPanel();
036        connectionPanel.add(new JLabel(Bundle.getMessage("ActionRequestUpdateAllSensorsSwing_Connection")));
037
038        _connection = new JComboBox<>();
039        _connection.addItem(new Connection(null));
040        List<SystemConnectionMemo> systemConnections =
041                jmri.InstanceManager.getList(SystemConnectionMemo.class);
042        for (SystemConnectionMemo connection : systemConnections) {
043            if (!connection.provides(SensorManager.class)) continue;
044            Connection c = new Connection(connection);
045            _connection.addItem(c);
046            if ((action != null) && (action.getMemo() == connection)) {
047                _connection.setSelectedItem(c);
048            }
049        }
050        connectionPanel.add(_connection);
051
052        JPanel infoPanel = new JPanel();
053        infoPanel.add(new JLabel(Bundle.getMessage("ActionRequestUpdateAllSensorsSwing_Info")));
054
055        panel.add(connectionPanel);
056        panel.add(infoPanel);
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        SystemConnectionMemo memo = _connection.getItemAt(_connection.getSelectedIndex())._memo;
069
070        ActionRequestUpdateAllSensors action = new ActionRequestUpdateAllSensors(systemName, userName, memo);
071        updateObject(action);
072
073        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public void updateObject(@Nonnull Base object) {
079        if (! (object instanceof ActionRequestUpdateAllSensors)) {
080            throw new IllegalArgumentException("object must be an ActionRequestUpdateAllSensors but is a: "+object.getClass().getName());
081        }
082
083        ActionRequestUpdateAllSensors action = (ActionRequestUpdateAllSensors)object;
084
085        action.setMemo(_connection.getItemAt(_connection.getSelectedIndex())._memo);
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public String toString() {
091        return Bundle.getMessage("ActionRequestUpdateAllSensors_Short");
092    }
093
094    @Override
095    public void dispose() {
096    }
097
098
099
100    private static class Connection {
101
102        private SystemConnectionMemo _memo;
103
104        public Connection(SystemConnectionMemo memo) {
105            _memo = memo;
106        }
107
108        @Override
109        public String toString() {
110            if (_memo == null) {
111                return Bundle.getMessage("ActionRequestUpdateAllSensorsSwing_AllConnections");
112            }
113            return _memo.getUserName();
114        }
115    }
116
117//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionRequestUpdateAllSensorsSwing.class);
118
119}