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