001package jmri.jmrit.logixng.actions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.actions.IfThenElse;
012import jmri.jmrit.logixng.actions.IfThenElse.ExecuteType;
013import jmri.jmrit.logixng.actions.IfThenElse.EvaluateType;
014import jmri.util.swing.JComboBoxUtil;
015
016/**
017 * Configures an ActionTurnout object with a Swing JPanel.
018 *
019 * @author Daniel Bergqvist Copyright 2021
020 */
021public class IfThenElseSwing extends AbstractDigitalActionSwing {
022
023    private JComboBox<ExecuteType> _executeTypeComboBox;
024    private JComboBox<EvaluateType> _evaluateTypeComboBox;
025
026
027    @Override
028    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
029        if ((object != null) && !(object instanceof IfThenElse)) {
030            throw new IllegalArgumentException("object must be an IfThenElse but is a: "+object.getClass().getName());
031        }
032
033        IfThenElse action = (IfThenElse)object;
034
035        panel = new JPanel();
036        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
037
038        _executeTypeComboBox = new JComboBox<>();
039        for (ExecuteType type : ExecuteType.values()) _executeTypeComboBox.addItem(type);
040        JComboBoxUtil.setupComboBoxMaxRows(_executeTypeComboBox);
041        if (action != null) {
042            _executeTypeComboBox.setSelectedItem(action.getExecuteType());
043        } else {
044            LogixNGPreferences prefs = InstanceManager.getDefault(LogixNGPreferences.class);
045            _executeTypeComboBox.setSelectedItem(prefs.getIfThenElseExecuteTypeDefault());
046        }
047
048        _evaluateTypeComboBox = new JComboBox<>();
049        for (EvaluateType type : EvaluateType.values()) _evaluateTypeComboBox.addItem(type);
050        JComboBoxUtil.setupComboBoxMaxRows(_evaluateTypeComboBox);
051        if (action != null) _evaluateTypeComboBox.setSelectedItem(action.getEvaluateType());
052
053        JPanel typeOuterPanel = new JPanel();
054        JPanel typePanel = new JPanel();
055        java.awt.GridLayout layout = new java.awt.GridLayout(0,1);
056        typePanel.setLayout(layout);
057        layout.setVgap(15);
058        typePanel.add(_executeTypeComboBox);
059        typePanel.add(_evaluateTypeComboBox);
060        typeOuterPanel.add(typePanel);
061        panel.add(typeOuterPanel);
062
063        JPanel labelPanel = new JPanel();
064        labelPanel.add(new JLabel(Bundle.getMessage("IfThenElse_Info")));
065        panel.add(labelPanel);
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public boolean validate(@Nonnull List<String> errorMessages) {
071        return true;
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
077        IfThenElse action = new IfThenElse(systemName, userName);
078        updateObject(action);
079        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public void updateObject(@Nonnull Base object) {
085        if (!(object instanceof IfThenElse)) {
086            throw new IllegalArgumentException("object must be an IfThenElse but is a: "+object.getClass().getName());
087        }
088
089        IfThenElse action = (IfThenElse)object;
090
091        action.setExecuteType(_executeTypeComboBox.getItemAt(_executeTypeComboBox.getSelectedIndex()));
092        action.setEvaluateType(_evaluateTypeComboBox.getItemAt(_evaluateTypeComboBox.getSelectedIndex()));
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public String toString() {
098        return Bundle.getMessage("IfThenElse_Short");
099    }
100
101    @Override
102    public void dispose() {
103    }
104
105}