001package jmri.jmrit.logixng.actions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.JComboBox;
008import javax.swing.JPanel;
009
010import jmri.InstanceManager;
011import jmri.jmrit.logixng.Base;
012import jmri.jmrit.logixng.DigitalBooleanActionManager;
013import jmri.jmrit.logixng.MaleSocket;
014import jmri.jmrit.logixng.actions.DigitalBooleanLogixAction;
015import jmri.jmrit.logixng.actions.DigitalBooleanLogixAction.When;
016import jmri.util.swing.JComboBoxUtil;
017
018/**
019 * Configures an ActionTurnout object with a Swing JPanel.
020 *
021 * @author Daniel Bergqvist Copyright 2021
022 */
023public class DigitalBooleanLogixActionSwing extends AbstractBooleanActionSwing {
024
025    DigitalBooleanLogixAction.When type = DigitalBooleanLogixAction.When.Either;
026    private JComboBox<DigitalBooleanLogixAction.When> _triggerComboBox;
027
028    @Override
029    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
030        DigitalBooleanLogixAction action = (DigitalBooleanLogixAction)object;
031
032        panel = new JPanel();
033        _triggerComboBox = new JComboBox<>();
034        for (When e : When.values()) {
035            _triggerComboBox.addItem(e);
036        }
037        JComboBoxUtil.setupComboBoxMaxRows(_triggerComboBox);
038        panel.add(_triggerComboBox);
039        if (action != null) {
040            _triggerComboBox.setSelectedItem(action.getTrigger());
041        }
042    }
043
044    /** {@inheritDoc} */
045    @Override
046    public boolean validate(@Nonnull List<String> errorMessages) {
047        return true;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
053        DigitalBooleanLogixAction action = new DigitalBooleanLogixAction(systemName, userName, type);
054        updateObject(action);
055        return InstanceManager.getDefault(DigitalBooleanActionManager.class).registerAction(action);
056    }
057
058    /** {@inheritDoc} */
059    @Override
060    public void updateObject(@Nonnull Base object) {
061        if (! (object instanceof DigitalBooleanLogixAction)) {
062            throw new IllegalArgumentException("object must be an DigitalBooleanLogixAction but is a: "+object.getClass().getName());
063        }
064        DigitalBooleanLogixAction action = (DigitalBooleanLogixAction)object;
065        action.setTrigger(_triggerComboBox.getItemAt(_triggerComboBox.getSelectedIndex()));
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public String toString() {
071        return Bundle.getMessage("DigitalBooleanLogixAction_Short");
072    }
073
074    @Override
075    public void dispose() {
076    }
077
078}