001package jmri.jmrit.logixng.actions.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.InstanceManager;
011import jmri.Memory;
012import jmri.MemoryManager;
013import jmri.jmrit.logixng.Base;
014import jmri.jmrit.logixng.AnalogActionManager;
015import jmri.jmrit.logixng.MaleSocket;
016import jmri.jmrit.logixng.actions.AnalogActionMemory;
017import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
018
019/**
020 * Configures an ActionMemory object with a Swing JPanel.
021 *
022 * @author Daniel Bergqvist Copyright 2021
023 */
024public class AnalogActionMemorySwing extends AbstractAnalogActionSwing {
025
026    private LogixNG_SelectNamedBeanSwing<Memory> _selectNamedBeanSwing;
027
028
029    @Override
030    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
031        AnalogActionMemory action = (AnalogActionMemory)object;
032
033        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
034                InstanceManager.getDefault(MemoryManager.class), getJDialog(), this);
035
036        panel = new JPanel();
037        JPanel _tabbedPaneNamedBean;
038        if (action != null) {
039            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
040        } else {
041            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
042        }
043
044        panel.add(new JLabel(Bundle.getMessage("BeanNameMemory")));
045        panel.add(_tabbedPaneNamedBean);
046    }
047
048    /** {@inheritDoc} */
049    @Override
050    public boolean validate(@Nonnull List<String> errorMessages) {
051        // Create a temporary action to test formula
052        AnalogActionMemory action = new AnalogActionMemory("IQAA1", null);
053        updateObject(action);
054        return errorMessages.isEmpty();
055    }
056
057    /** {@inheritDoc} */
058    @Override
059    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
060        AnalogActionMemory action = new AnalogActionMemory(systemName, userName);
061        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
062        return InstanceManager.getDefault(AnalogActionManager.class).registerAction(action);
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public void updateObject(@Nonnull Base object) {
068        if (! (object instanceof AnalogActionMemory)) {
069            throw new IllegalArgumentException("object must be an ActionMemory but is a: "+object.getClass().getName());
070        }
071        AnalogActionMemory action = (AnalogActionMemory)object;
072        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public String toString() {
078        return Bundle.getMessage("AnalogActionMemory_Short");
079    }
080
081    @Override
082    public void dispose() {
083        // Do nothing
084    }
085
086
087//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogActionMemorySwing.class);
088
089}