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.JsonDecode;
012import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
013
014/**
015 * Configures an JsonDecode object with a Swing JPanel.
016 *
017 * @author Daniel Bergqvist Copyright 2024
018 */
019public class JsonDecodeSwing extends AbstractDigitalActionSwing {
020
021    private JTextField _jsonLocalVariableTextField;
022    private JTextField _resultLocalVariableTextField;
023
024
025    @Override
026    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
027        JsonDecode action = (JsonDecode)object;
028
029        panel = new JPanel();
030
031        _jsonLocalVariableTextField = new JTextField(20);
032        _resultLocalVariableTextField = new JTextField(20);
033
034        if (action != null) {
035            if (action.getResultLocalVariable() != null) {
036                _jsonLocalVariableTextField.setText(action.getJsonLocalVariable());
037            }
038            if (action.getResultLocalVariable() != null) {
039                _resultLocalVariableTextField.setText(action.getResultLocalVariable());
040            }
041        }
042
043        JComponent[] components = new JComponent[]{
044            _jsonLocalVariableTextField,
045            _resultLocalVariableTextField
046        };
047
048        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
049                Bundle.getMessage("JsonDecode_Components"), components);
050
051        for (JComponent c : componentList) panel.add(c);
052    }
053
054    /** {@inheritDoc} */
055    @Override
056    public boolean validate(@Nonnull List<String> errorMessages) {
057        return true;
058    }
059
060    /** {@inheritDoc} */
061    @Override
062    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
063        JsonDecode action = new JsonDecode(systemName, userName);
064        updateObject(action);
065        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public void updateObject(@Nonnull Base object) {
071        if (! (object instanceof JsonDecode)) {
072            throw new IllegalArgumentException("object must be an JsonDecode but is a: "+object.getClass().getName());
073        }
074        JsonDecode action = (JsonDecode)object;
075
076        action.setJsonLocalVariable(_jsonLocalVariableTextField.getText());
077        action.setResultLocalVariable(_resultLocalVariableTextField.getText());
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public String toString() {
083        return Bundle.getMessage("JsonDecode_Short");
084    }
085
086    @Override
087    public void dispose() {
088    }
089
090//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JsonDecodeSwing.class);
091
092}