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.*;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.actions.ActionThrottleFunction;
012import jmri.jmrit.logixng.actions.ActionThrottleFunction.FunctionState;
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 ActionThrottleFunction object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2023
021 */
022public class ActionThrottleFunctionSwing extends AbstractDigitalActionSwing {
023
024    private LogixNG_SelectIntegerSwing _selectAddressSwing;
025    private LogixNG_SelectIntegerSwing _selectFunctionSwing;
026    private LogixNG_SelectEnumSwing<FunctionState> _selectOnOffSwing;
027    private JComboBox<Connection> _connection;
028
029    @Override
030    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
031        ActionThrottleFunction action = (ActionThrottleFunction)object;
032        if (action == null) {
033            // Create a temporary action
034            action = new ActionThrottleFunction("IQDA1", null);
035        }
036
037        _selectAddressSwing = new LogixNG_SelectIntegerSwing(getJDialog(), this);
038        _selectFunctionSwing = new LogixNG_SelectIntegerSwing(getJDialog(), this);
039        _selectOnOffSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
040
041        panel = new JPanel();
042        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
043
044        JPanel selectPanel = new JPanel();
045        JPanel _tabbedPaneAddress;
046        JPanel _tabbedPaneFunction;
047        JPanel _tabbedPaneOnOff;
048
049        _tabbedPaneAddress = _selectAddressSwing.createPanel(action.getSelectAddress());
050        _tabbedPaneFunction = _selectFunctionSwing.createPanel(action.getSelectFunction());
051        _tabbedPaneOnOff = _selectOnOffSwing.createPanel(action.getSelectOnOff(), FunctionState.values());
052
053        JComponent[] components = new JComponent[]{
054            _tabbedPaneAddress,
055            _tabbedPaneFunction,
056            _tabbedPaneOnOff};
057
058        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
059                Bundle.getMessage("ActionThrottleFunction_Components"), components);
060
061        for (JComponent c : componentList) selectPanel.add(c);
062
063        JPanel connectionPanel = new JPanel();
064        connectionPanel.add(new JLabel(Bundle.getMessage("ActionThrottleFunctionSwing_Connection")));
065
066        _connection = new JComboBox<>();
067        _connection.addItem(new Connection(null));
068        List<SystemConnectionMemo> systemConnections =
069                jmri.InstanceManager.getList(SystemConnectionMemo.class);
070        for (SystemConnectionMemo connection : systemConnections) {
071            if (!connection.provides(ThrottleManager.class)) continue;
072            Connection c = new Connection(connection);
073            _connection.addItem(c);
074            if ((object != null) && (action.getMemo() == connection)) {
075                _connection.setSelectedItem(c);
076            }
077        }
078        connectionPanel.add(_connection);
079
080        panel.add(selectPanel);
081        panel.add(connectionPanel);
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public boolean validate(@Nonnull List<String> errorMessages) {
087        // Create a temporary action to test formula
088        ActionThrottleFunction action = new ActionThrottleFunction("IQDA1", null);
089
090        _selectAddressSwing.validate(action.getSelectAddress(), errorMessages);
091        _selectFunctionSwing.validate(action.getSelectFunction(), errorMessages);
092        _selectOnOffSwing.validate(action.getSelectOnOff(), errorMessages);
093
094        return errorMessages.isEmpty();
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
100        ActionThrottleFunction action = new ActionThrottleFunction(systemName, userName);
101        updateObject(action);
102        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public void updateObject(@Nonnull Base object) {
108        if (! (object instanceof ActionThrottleFunction)) {
109            throw new IllegalArgumentException("object must be an ActionThrottleFunction but is a: "+object.getClass().getName());
110        }
111        ActionThrottleFunction action = (ActionThrottleFunction)object;
112        _selectAddressSwing.updateObject(action.getSelectAddress());
113        _selectFunctionSwing.updateObject(action.getSelectFunction());
114        _selectOnOffSwing.updateObject(action.getSelectOnOff());
115
116        action.setMemo(_connection.getItemAt(_connection.getSelectedIndex())._memo);
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public String toString() {
122        return Bundle.getMessage("ActionThrottleFunction_Short");
123    }
124
125    @Override
126    public void dispose() {
127        _selectAddressSwing.dispose();
128        _selectFunctionSwing.dispose();
129        _selectOnOffSwing.dispose();
130    }
131
132
133
134    private static class Connection {
135
136        private SystemConnectionMemo _memo;
137
138        public Connection(SystemConnectionMemo memo) {
139            _memo = memo;
140        }
141
142        @Override
143        public String toString() {
144            if (_memo == null) {
145                return Bundle.getMessage("ActionThrottleFunctionSwing_DefaultConnection");
146            }
147            return _memo.getUserName();
148        }
149    }
150
151}