001package jmri.jmrit.logixng.actions.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.actions.ExecuteAction;
013import jmri.util.swing.JComboBoxUtil;
014
015/**
016 * Configures an ExecuteAction object with a Swing JPanel.
017 *
018 * @author Daniel Bergqvist Copyright 2024
019 */
020public class ExecuteActionSwing extends AbstractDigitalActionSwing {
021
022    private JComboBox<String> _actionsComboBox;
023
024
025    @Override
026    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
027        ExecuteAction action = (ExecuteAction)object;
028
029        panel = new JPanel();
030
031        _actionsComboBox = new JComboBox<>();
032        _actionsComboBox.addItem("");
033        for (MaleDigitalActionSocket bean : InstanceManager.getDefault(DigitalActionManager.class).getNamedBeanSet()) {
034            if (bean.getUserName() != null) {
035                _actionsComboBox.addItem(bean.getDisplayName());
036                if (action != null) {
037                    NamedBeanHandle<MaleDigitalActionSocket> handle =
038                            action.getSelectNamedBean().getNamedBean();
039                    if ((handle != null) && (handle.getName().equals(bean.getDisplayName()))) {
040                        _actionsComboBox.setSelectedItem(bean.getDisplayName());
041                    }
042                }
043            }
044        }
045        JComboBoxUtil.setupComboBoxMaxRows(_actionsComboBox);
046
047        panel.add(_actionsComboBox);
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        ExecuteAction action = new ExecuteAction(systemName, userName);
060        updateObject(action);
061        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public void updateObject(@Nonnull Base object) {
067        if (! (object instanceof ExecuteAction)) {
068            throw new IllegalArgumentException("object must be an ExecuteAction but is a: "+object.getClass().getName());
069        }
070
071        ExecuteAction action = (ExecuteAction)object;
072
073        String expr = _actionsComboBox.getItemAt(_actionsComboBox.getSelectedIndex());
074        if (expr.isEmpty()) action.getSelectNamedBean().removeNamedBean();
075        else action.getSelectNamedBean().setNamedBean(expr);
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public String toString() {
081        return Bundle.getMessage("ExecuteAction_Short");
082    }
083
084    @Override
085    public void dispose() {
086    }
087
088
089//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExecuteActionSwing.class);
090
091}