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.AnalogExpressionManager;
013import jmri.jmrit.logixng.MaleSocket;
014import jmri.jmrit.logixng.expressions.AnalogExpressionAnalogIO;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
016
017/**
018 * Configures an AnalogExpressionAnalogIO object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2021
021 */
022public class AnalogExpressionAnalogIOSwing extends AbstractAnalogExpressionSwing {
023
024    private LogixNG_SelectNamedBeanSwing<AnalogIO> _selectNamedBeanSwing;
025
026
027    @Override
028    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
029        AnalogExpressionAnalogIO action = (AnalogExpressionAnalogIO)object;
030
031        panel = new JPanel();
032
033        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
034                InstanceManager.getDefault(AnalogIOManager.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("BeanNameAnalogIO")));
044        panel.add(_tabbedPaneNamedBean);
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public boolean validate(@Nonnull List<String> errorMessages) {
050        AnalogExpressionAnalogIO expression = new AnalogExpressionAnalogIO("IQAE1", 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        AnalogExpressionAnalogIO expression = new AnalogExpressionAnalogIO(systemName, userName);
059        updateObject(expression);
060        return InstanceManager.getDefault(AnalogExpressionManager.class).registerExpression(expression);
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public void updateObject(@Nonnull Base object) {
066        if (! (object instanceof AnalogExpressionAnalogIO)) {
067            throw new IllegalArgumentException("object must be an AnalogExpressionAnalogIO but is a: "+object.getClass().getName());
068        }
069        AnalogExpressionAnalogIO expression = (AnalogExpressionAnalogIO)object;
070        _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean());
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    public String toString() {
076        return Bundle.getMessage("AnalogExpressionAnalogIO_Short");
077    }
078
079    @Override
080    public void dispose() {
081    }
082
083
084//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogExpressionAnalogIOSwing.class);
085
086}