001package jmri.jmrit.logixng.expressions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009
010import jmri.*;
011import jmri.jmrit.logixng.Base;
012import jmri.jmrit.logixng.StringExpressionManager;
013import jmri.jmrit.logixng.MaleSocket;
014import jmri.jmrit.logixng.expressions.StringExpressionStringIO;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
016
017/**
018 * Configures an StringExpressionStringIO object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2025
021 */
022public class StringExpressionStringIOSwing extends AbstractStringExpressionSwing {
023
024    private LogixNG_SelectNamedBeanSwing<StringIO> _selectNamedBeanSwing;
025
026
027    @Override
028    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
029        StringExpressionStringIO action = (StringExpressionStringIO)object;
030
031        panel = new JPanel();
032
033        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
034                InstanceManager.getDefault(StringIOManager.class), getJDialog(), this);
035
036        JPanel _tabbedPaneNamedBean;
037        if (action != null) {
038            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
039        } else {
040            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
041        }
042
043        panel.add(new JLabel(Bundle.getMessage("StringExpressionStringIO_Short")));
044        panel.add(_tabbedPaneNamedBean);
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public boolean validate(@Nonnull List<String> errorMessages) {
050        StringExpressionStringIO expression = new StringExpressionStringIO("IQSE1", null);
051        _selectNamedBeanSwing.validate(expression.getSelectNamedBean(), errorMessages);
052        return errorMessages.isEmpty();
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
058        StringExpressionStringIO expression = new StringExpressionStringIO(systemName, userName);
059        updateObject(expression);
060        return InstanceManager.getDefault(StringExpressionManager.class).registerExpression(expression);
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public void updateObject(@Nonnull Base object) {
066        if (! (object instanceof StringExpressionStringIO)) {
067            throw new IllegalArgumentException("object must be an StringExpressionStringIO but is a: "+object.getClass().getName());
068        }
069        StringExpressionStringIO expression = (StringExpressionStringIO)object;
070        _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean());
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    public String toString() {
076        return Bundle.getMessage("StringExpressionStringIO_Short");
077    }
078
079    @Override
080    public void dispose() {
081    }
082
083
084//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StringExpressionStringIOSwing.class);
085
086}