001package jmri.jmrix.can.cbus.logixng.swing;
002
003import jmri.jmrit.logixng.actions.swing.*;
004
005import java.util.List;
006
007import javax.annotation.CheckForNull;
008import javax.annotation.Nonnull;
009import javax.swing.*;
010
011import jmri.InstanceManager;
012import jmri.jmrit.logixng.*;
013import jmri.jmrix.can.cbus.logixng.SendMergCbusEvent;
014import jmri.jmrix.can.cbus.logixng.SendMergCbusEvent.CbusEventType;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
016import jmri.jmrit.logixng.util.swing.LogixNG_SelectIntegerSwing;
017import jmri.jmrix.can.CanSystemConnectionMemo;
018import jmri.jmrix.can.cbus.logixng.CategoryMergCbus;
019
020/**
021 * Configures an SendCbusEvent object with a Swing JPanel.
022 *
023 * @author Daniel Bergqvist Copyright 2025
024 */
025public class SendMergCbusEventSwing extends AbstractDigitalActionSwing {
026
027    private LogixNG_SelectIntegerSwing _selectNodeNumberSwing;
028    private LogixNG_SelectIntegerSwing _selectEventNumberSwing;
029    private LogixNG_SelectEnumSwing<CbusEventType> _selectEventTypeSwing;
030    private JComboBox<CbusConnection> _cbusConnection;
031
032
033    public SendMergCbusEventSwing() {
034    }
035
036    public SendMergCbusEventSwing(JDialog dialog) {
037        super.setJDialog(dialog);
038    }
039
040    @Override
041    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
042        SendMergCbusEvent action = (SendMergCbusEvent) object;
043        if (action == null) {
044            // Create a temporary action
045            action = new SendMergCbusEvent("IQDA1", null, null);
046        }
047
048        _selectNodeNumberSwing = new LogixNG_SelectIntegerSwing(getJDialog(), this);
049        _selectEventNumberSwing = new LogixNG_SelectIntegerSwing(getJDialog(), this);
050        _selectEventTypeSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
051
052        panel = new JPanel(new java.awt.GridBagLayout());
053        JPanel tabbedPaneNodeNumber;
054        JPanel tabbedPaneEventNumber;
055        JPanel tabbedPaneCbusEventType;
056
057        tabbedPaneNodeNumber = _selectNodeNumberSwing.createPanel(action.getSelectNodeNumber());
058        tabbedPaneEventNumber = _selectEventNumberSwing.createPanel(action.getSelectEventNumber());
059        tabbedPaneCbusEventType = _selectEventTypeSwing.createPanel(action.getSelectEventType(), CbusEventType.values());
060
061
062        _cbusConnection = new JComboBox<>();
063        List<CanSystemConnectionMemo> systemConnections = CategoryMergCbus.getMergConnections();
064        for (CanSystemConnectionMemo connection : systemConnections) {
065            CbusConnection c = new CbusConnection(connection);
066            _cbusConnection.addItem(c);
067            if (action.getMemo() == connection) {
068                _cbusConnection.setSelectedItem(c);
069            }
070        }
071
072
073        java.awt.GridBagConstraints constraints = new java.awt.GridBagConstraints();
074        constraints.gridwidth = 1;
075        constraints.gridheight = 1;
076        constraints.gridx = 0;
077        constraints.gridy = 0;
078        constraints.anchor = java.awt.GridBagConstraints.EAST;
079        panel.add(new JLabel(Bundle.getMessage("SendCbusEventSwing_NodeNumber")), constraints);
080        constraints.gridy = 1;
081        panel.add(new JLabel(Bundle.getMessage("SendCbusEventSwing_EventNumber")), constraints);
082        constraints.gridy = 2;
083        panel.add(new JLabel(Bundle.getMessage("SendCbusEventSwing_EventType")), constraints);
084        constraints.gridy = 3;
085        panel.add(new JLabel(Bundle.getMessage("SendCbusEventSwing_Memo")), constraints);
086        constraints.gridx = 1;
087        constraints.gridy = 0;
088        constraints.anchor = java.awt.GridBagConstraints.WEST;
089        panel.add(tabbedPaneNodeNumber, constraints);
090        constraints.gridy = 1;
091        panel.add(tabbedPaneEventNumber, constraints);
092        constraints.gridy = 2;
093        panel.add(tabbedPaneCbusEventType, constraints);
094        constraints.gridy = 3;
095        panel.add(_cbusConnection, constraints);
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public boolean validate(@Nonnull List<String> errorMessages) {
101        // Create a temporary action to test formula
102        SendMergCbusEvent action = new SendMergCbusEvent("IQDA1", null, null);
103
104        _selectNodeNumberSwing.validate(action.getSelectNodeNumber(), errorMessages);
105        _selectEventNumberSwing.validate(action.getSelectEventNumber(), errorMessages);
106        _selectEventTypeSwing.validate(action.getSelectEventType(), errorMessages);
107
108        return errorMessages.isEmpty();
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public String getAutoSystemName() {
114        return InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName();
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
120        SendMergCbusEvent action = new SendMergCbusEvent(systemName, userName, null);
121        updateObject(action);
122        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    public void updateObject(@Nonnull Base object) {
128        if (! (object instanceof SendMergCbusEvent)) {
129            throw new IllegalArgumentException("object must be an SendCbusEvent but is a: "+object.getClass().getName());
130        }
131        SendMergCbusEvent action = (SendMergCbusEvent) object;
132
133        _selectNodeNumberSwing.updateObject(action.getSelectNodeNumber());
134        _selectEventNumberSwing.updateObject(action.getSelectEventNumber());
135        _selectEventTypeSwing.updateObject(action.getSelectEventType());
136
137        action.setMemo(_cbusConnection.getItemAt(_cbusConnection.getSelectedIndex())._memo);
138    }
139
140    /** {@inheritDoc} */
141    @Override
142    public String toString() {
143        return Bundle.getMessage("SendCbusEvent_Short");
144    }
145
146    @Override
147    public void dispose() {
148        _selectEventTypeSwing.dispose();
149        _selectNodeNumberSwing.dispose();
150    }
151
152
153    private static class CbusConnection {
154
155        private CanSystemConnectionMemo _memo;
156
157        public CbusConnection(CanSystemConnectionMemo memo) {
158            _memo = memo;
159        }
160
161        @Override
162        public String toString() {
163            return _memo.getUserName();
164        }
165    }
166
167//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SendCbusEventSwing.class);
168
169}