001package jmri.jmrix.mqtt.logixng.swing;
002
003import jmri.jmrit.logixng.actions.swing.AbstractDigitalActionSwing;
004
005import java.awt.GridBagConstraints;
006import java.awt.GridBagLayout;
007import java.util.List;
008
009import javax.annotation.CheckForNull;
010import javax.annotation.Nonnull;
011import javax.swing.*;
012
013import jmri.InstanceManager;
014import jmri.jmrit.logixng.*;
015import jmri.jmrix.mqtt.MqttSystemConnectionMemo;
016import jmri.jmrix.mqtt.logixng.Subscribe;
017
018/**
019 * Configures an Subscribe object with a Swing JPanel.
020 *
021 * @author Daniel Bergqvist Copyright 2022
022 */
023public class SubscribeSwing extends AbstractDigitalActionSwing {
024
025    private JComboBox<MqttConnection> _mqttConnection;
026
027    JTextField _subscribeToTopicTextField;
028    JTextField _lastTopicLocalVariableTextField;
029    JCheckBox _removeChannelFromLastTopicCheckBox;
030    JTextField _lastMessageLocalVariableTextField;
031
032
033    public SubscribeSwing() {
034    }
035
036    public SubscribeSwing(JDialog dialog) {
037        super.setJDialog(dialog);
038    }
039
040    @Override
041    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
042        Subscribe action = (Subscribe) object;
043        if (action == null) {
044            // Create a temporary action
045            action = new Subscribe("IQDA1", null, null);
046            // Make "remove channel" the default for new actions
047            action.setRemoveChannelFromLastTopic(true);
048        }
049
050        panel = new JPanel();
051
052        JLabel subscribeToTopicLabel = new JLabel(Bundle.getMessage("Subscribe_subscribeToTopic"));
053        JLabel lastTopicLocalVariableLabel = new JLabel(Bundle.getMessage("Subscribe_lastTopicLocalVariable"));
054        JLabel lastMessageLocalVariableLabel = new JLabel(Bundle.getMessage("Subscribe_lastMessageLocalVariable"));
055
056        _subscribeToTopicTextField = new JTextField(40);
057        _lastTopicLocalVariableTextField = new JTextField(40);
058        _removeChannelFromLastTopicCheckBox = new JCheckBox(Bundle.getMessage("Subscribe_removeChannelFromLastTopic"));
059        _lastMessageLocalVariableTextField = new JTextField(40);
060
061        _subscribeToTopicTextField.setText(action.getSubscribeToTopic());
062        _lastTopicLocalVariableTextField.setText(action.getLastTopicLocalVariable());
063        _removeChannelFromLastTopicCheckBox.setSelected(action.getRemoveChannelFromLastTopic());
064        _lastMessageLocalVariableTextField.setText(action.getLastMessageLocalVariable());
065
066        JPanel mqttPanel = new JPanel();
067        mqttPanel.add(new JLabel(Bundle.getMessage("MqttConnection")));
068
069        _mqttConnection = new JComboBox<>();
070        List<MqttSystemConnectionMemo> systemConnections =
071                jmri.InstanceManager.getList(MqttSystemConnectionMemo.class);
072        for (MqttSystemConnectionMemo connection : systemConnections) {
073            MqttConnection c = new MqttConnection(connection);
074            _mqttConnection.addItem(c);
075            if (action.getMemo() == connection) {
076                _mqttConnection.setSelectedItem(c);
077            }
078        }
079        mqttPanel.add(_mqttConnection);
080
081        panel.setLayout(new GridBagLayout());
082        GridBagConstraints c = new GridBagConstraints();
083        c.gridwidth = 1;
084        c.gridheight = 1;
085        c.gridx = 0;
086        c.gridy = 0;
087        c.anchor = GridBagConstraints.EAST;
088        panel.add(subscribeToTopicLabel, c);
089        subscribeToTopicLabel.setLabelFor(_subscribeToTopicTextField);
090        c.gridy = 1;
091        panel.add(lastTopicLocalVariableLabel, c);
092        lastTopicLocalVariableLabel.setLabelFor(_lastTopicLocalVariableTextField);
093        c.gridy = 2;
094        panel.add(lastMessageLocalVariableLabel, c);
095        lastMessageLocalVariableLabel.setLabelFor(_lastMessageLocalVariableTextField);
096        c.gridx = 1;
097        c.gridy = 0;
098        c.anchor = GridBagConstraints.WEST;
099        panel.add(_subscribeToTopicTextField, c);
100//        _subscribeToTopicTextField.setToolTipText(Bundle.getMessage("SysNameToolTip", "Y"));
101        c.gridy = 1;
102        panel.add(_lastTopicLocalVariableTextField, c);
103//        _lastTopicLocalVariableTextField.setToolTipText(Bundle.getMessage("SysNameToolTip", "Y"));
104        c.gridy = 2;
105        panel.add(_lastMessageLocalVariableTextField, c);
106//        _lastMessageLocalVariableTextField.setToolTipText(Bundle.getMessage("SysNameToolTip", "Y"));
107        c.gridy = 3;
108        panel.add(_removeChannelFromLastTopicCheckBox, c);
109        c.gridwidth = 2;
110        c.gridheight = 1;
111        c.gridx = 0;
112        c.gridy = 4;
113        c.anchor = GridBagConstraints.CENTER;
114        panel.add(mqttPanel, c);
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public boolean validate(@Nonnull List<String> errorMessages) {
120        return true;
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public String getAutoSystemName() {
126        return InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName();
127    }
128
129    /** {@inheritDoc} */
130    @Override
131    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
132        MqttSystemConnectionMemo memo =
133                _mqttConnection.getItemAt(_mqttConnection.getSelectedIndex())._memo;
134
135        Subscribe action = new Subscribe(systemName, userName, memo);
136        updateObject(action);
137        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
138    }
139
140    /** {@inheritDoc} */
141    @Override
142    public void updateObject(@Nonnull Base object) {
143        if (! (object instanceof Subscribe)) {
144            throw new IllegalArgumentException("object must be an Subscribe but is a: "+object.getClass().getName());
145        }
146        Subscribe action = (Subscribe) object;
147
148        action.setSubscribeToTopic(_subscribeToTopicTextField.getText());
149        action.setLastTopicLocalVariable(_lastTopicLocalVariableTextField.getText());
150        action.setRemoveChannelFromLastTopic(_removeChannelFromLastTopicCheckBox.isSelected());
151        action.setLastMessageLocalVariable(_lastMessageLocalVariableTextField.getText());
152
153        action.setMemo(_mqttConnection.getItemAt(_mqttConnection.getSelectedIndex())._memo);
154    }
155
156    /** {@inheritDoc} */
157    @Override
158    public String toString() {
159        return Bundle.getMessage("Subscribe_Short");
160    }
161
162    @Override
163    public void dispose() {
164    }
165
166
167    private static class MqttConnection {
168
169        private MqttSystemConnectionMemo _memo;
170
171        public MqttConnection(MqttSystemConnectionMemo memo) {
172            _memo = memo;
173        }
174
175        @Override
176        public String toString() {
177            return _memo.getUserName();
178        }
179    }
180
181//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PublishSwing.class);
182
183}