001package jmri.jmrit.logixng.expressions.swing;
002
003import java.util.*;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.NamedBeanHandle;
011import jmri.jmrit.logixng.*;
012import jmri.jmrit.logixng.expressions.LastResultOfDigitalExpression;
013import jmri.util.swing.JComboBoxUtil;
014
015/**
016 * Configures an LastResultOfDigitalExpression object with a Swing JPanel.
017 *
018 * @author Daniel Bergqvist Copyright 2021
019 */
020public class LastResultOfDigitalExpressionSwing extends AbstractDigitalExpressionSwing {
021
022    private JComboBox<String> _expressionsComboBox;
023
024
025    @Override
026    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
027        LastResultOfDigitalExpression expression = (LastResultOfDigitalExpression)object;
028
029        panel = new JPanel();
030
031        _expressionsComboBox = new JComboBox<>();
032        _expressionsComboBox.addItem("");
033        for (MaleDigitalExpressionSocket bean : InstanceManager.getDefault(DigitalExpressionManager.class).getNamedBeanSet()) {
034            if (bean.getUserName() != null) {
035                _expressionsComboBox.addItem(bean.getDisplayName());
036                if (expression != null) {
037                    NamedBeanHandle<MaleDigitalExpressionSocket> handle =
038                            expression.getSelectNamedBean().getNamedBean();
039                    if ((handle != null) && (handle.getName().equals(bean.getDisplayName()))) {
040                        _expressionsComboBox.setSelectedItem(bean.getDisplayName());
041                    }
042                }
043            }
044        }
045        JComboBoxUtil.setupComboBoxMaxRows(_expressionsComboBox);
046
047        panel.add(_expressionsComboBox);
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public boolean validate(@Nonnull List<String> errorMessages) {
053        return true;
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
059        LastResultOfDigitalExpression expression = new LastResultOfDigitalExpression(systemName, userName);
060        updateObject(expression);
061        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public void updateObject(@Nonnull Base object) {
067        if (! (object instanceof LastResultOfDigitalExpression)) {
068            throw new IllegalArgumentException("object must be an LastResultOfDigitalExpression but is a: "+object.getClass().getName());
069        }
070
071        LastResultOfDigitalExpression expression = (LastResultOfDigitalExpression)object;
072
073        String expr = _expressionsComboBox.getItemAt(_expressionsComboBox.getSelectedIndex());
074        if (expr.isEmpty()) expression.getSelectNamedBean().removeNamedBean();
075        else expression.getSelectNamedBean().setNamedBean(expr);
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public String toString() {
081        return Bundle.getMessage("LastResultOfDigitalExpression_Short");
082    }
083
084    @Override
085    public void dispose() {
086    }
087
088
089//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LastResultOfDigitalExpressionSwing.class);
090
091}