001package jmri.jmrix.mqtt.logixng.swing;
002
003import jmri.jmrit.logixng.actions.swing.AbstractDigitalActionSwing;
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.mqtt.MqttSystemConnectionMemo;
014import jmri.jmrix.mqtt.logixng.Publish;
015import jmri.jmrix.mqtt.logixng.Publish.Retain;
016import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
017import jmri.jmrit.logixng.util.swing.LogixNG_SelectStringSwing;
018import jmri.util.swing.JComboBoxUtil;
019
020/**
021 * Configures an Publish object with a Swing JPanel.
022 *
023 * @author Daniel Bergqvist Copyright 2022
024 */
025public class PublishSwing extends AbstractDigitalActionSwing {
026
027    private JComboBox<MqttConnection> _mqttConnection;
028
029    private LogixNG_SelectStringSwing _selectTopicSwing;
030    private LogixNG_SelectStringSwing _selectMessageSwing;
031
032    private JComboBox<Retain> _retainComboBox;
033
034
035    public PublishSwing() {
036    }
037
038    public PublishSwing(JDialog dialog) {
039        super.setJDialog(dialog);
040    }
041
042    @Override
043    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
044        Publish action = (Publish) object;
045        if (action == null) {
046            // Create a temporary action
047            action = new Publish("IQDA1", null, null);
048        }
049
050        _selectTopicSwing = new LogixNG_SelectStringSwing(getJDialog(), this);
051        _selectMessageSwing = new LogixNG_SelectStringSwing(getJDialog(), this);
052
053        panel = new JPanel();
054        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
055
056        JPanel tabbedPaneTopic;
057        JPanel tabbedPaneData;
058
059        tabbedPaneTopic = _selectTopicSwing.createPanel(action.getSelectTopic());
060        tabbedPaneData = _selectMessageSwing.createPanel(action.getSelectMessage());
061
062
063        JPanel internalPanel = new JPanel();
064
065        JComponent[] components = new JComponent[]{
066            tabbedPaneTopic,
067            tabbedPaneData};
068
069        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
070                Bundle.getMessage("Publish_Components"), components);
071
072        for (JComponent c : componentList) internalPanel.add(c);
073
074        JPanel mqttPanel = new JPanel();
075        mqttPanel.add(new JLabel(Bundle.getMessage("MqttConnection")));
076
077        _mqttConnection = new JComboBox<>();
078        List<MqttSystemConnectionMemo> systemConnections =
079                jmri.InstanceManager.getList(MqttSystemConnectionMemo.class);
080        for (MqttSystemConnectionMemo connection : systemConnections) {
081            MqttConnection c = new MqttConnection(connection);
082            _mqttConnection.addItem(c);
083            if (action.getMemo() == connection) {
084                _mqttConnection.setSelectedItem(c);
085            }
086        }
087        mqttPanel.add(_mqttConnection);
088
089        JPanel retainPanel = new JPanel();
090        retainPanel.add(new JLabel(Bundle.getMessage("PublishSwing_Retain")));
091
092        _retainComboBox = new JComboBox<>();
093        for (Retain e : Retain.values()) {
094            _retainComboBox.addItem(e);
095        }
096        JComboBoxUtil.setupComboBoxMaxRows(_retainComboBox);
097        _retainComboBox.setSelectedItem(action.getRetain());
098        retainPanel.add(_retainComboBox);
099
100        panel.add(internalPanel);
101        panel.add(retainPanel);
102        panel.add(mqttPanel);
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public boolean validate(@Nonnull List<String> errorMessages) {
108        // Create a temporary action to test formula
109        Publish action = new Publish("IQDA1", null, null);
110
111        _selectTopicSwing.validate(action.getSelectTopic(), errorMessages);
112        _selectMessageSwing.validate(action.getSelectMessage(), errorMessages);
113
114        return errorMessages.isEmpty();
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public String getAutoSystemName() {
120        return InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName();
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
126        MqttSystemConnectionMemo memo =
127                _mqttConnection.getItemAt(_mqttConnection.getSelectedIndex())._memo;
128
129        Publish action = new Publish(systemName, userName, memo);
130        updateObject(action);
131        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
132    }
133
134    /** {@inheritDoc} */
135    @Override
136    public void updateObject(@Nonnull Base object) {
137        if (! (object instanceof Publish)) {
138            throw new IllegalArgumentException("object must be an Publish but is a: "+object.getClass().getName());
139        }
140        Publish action = (Publish) object;
141
142        _selectTopicSwing.updateObject(action.getSelectTopic());
143        _selectMessageSwing.updateObject(action.getSelectMessage());
144
145        action.setMemo(_mqttConnection.getItemAt(_mqttConnection.getSelectedIndex())._memo);
146        action.setRetain(_retainComboBox.getItemAt(_retainComboBox.getSelectedIndex()));
147    }
148
149    /** {@inheritDoc} */
150    @Override
151    public String toString() {
152        return Bundle.getMessage("Publish_Short");
153    }
154
155    @Override
156    public void dispose() {
157        _selectTopicSwing.dispose();
158        _selectMessageSwing.dispose();
159    }
160
161
162    private static class MqttConnection {
163
164        private MqttSystemConnectionMemo _memo;
165
166        public MqttConnection(MqttSystemConnectionMemo memo) {
167            _memo = memo;
168        }
169
170        @Override
171        public String toString() {
172            return _memo.getUserName();
173        }
174    }
175
176//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PublishSwing.class);
177
178}