001package jmri.jmrit.logixng.expressions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.*;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.expressions.ExpressionTransit;
012import jmri.jmrit.logixng.expressions.ExpressionTransit.TransitState;
013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
014import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
016import jmri.util.swing.JComboBoxUtil;
017
018/**
019 * Configures an ExpressionTransit object with a Swing JPanel.
020 *
021 * @author Dave Sand         Copyright 2023
022 */
023public class ExpressionTransitSwing extends AbstractDigitalExpressionSwing {
024
025    private LogixNG_SelectNamedBeanSwing<Transit> _selectNamedBeanSwing;
026    private LogixNG_SelectEnumSwing<TransitState> _selectTransitStateSwing;
027
028    private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox;
029
030
031    @Override
032    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
033        ExpressionTransit expression = (ExpressionTransit)object;
034
035        panel = new JPanel();
036
037        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
038                InstanceManager.getDefault(TransitManager.class), getJDialog(), this);
039
040        _selectTransitStateSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
041
042        JPanel tabbedPaneNamedBean;
043        JPanel tabbedPaneTransitState;
044
045        if (expression != null) {
046            tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(expression.getSelectNamedBean());
047            tabbedPaneTransitState = _selectTransitStateSwing.createPanel(expression.getSelectEnum(), TransitState.values());
048        } else {
049            tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
050            tabbedPaneTransitState = _selectTransitStateSwing.createPanel(null, TransitState.values());
051        }
052
053        _is_IsNot_ComboBox = new JComboBox<>();
054        for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) {
055            _is_IsNot_ComboBox.addItem(e);
056        }
057        JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox);
058
059        _selectTransitStateSwing.addAddressingListener(e -> { setDataPanelState(); });
060        _selectTransitStateSwing.addEnumListener(e -> { setDataPanelState(); });
061
062        setDataPanelState();
063
064        if (expression != null) {
065            _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot());
066        }
067
068        JComponent[] components = new JComponent[]{
069            tabbedPaneNamedBean,
070            _is_IsNot_ComboBox,
071            tabbedPaneTransitState};
072
073        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
074                Bundle.getMessage("ExpressionTransit_Components"), components);
075
076        for (JComponent c : componentList) panel.add(c);
077    }
078
079    private void setDataPanelState() {
080//         boolean newState = (_selectTransitStateSwing.getAddressing() != NamedBeanAddressing.Direct)
081//                 || (_selectTransitStateSwing.getEnum() == BlockState.ValueMatches);
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public boolean validate(@Nonnull List<String> errorMessages) {
087        ExpressionTransit expression = new ExpressionTransit("IQDE1", null);
088        _selectNamedBeanSwing.validate(expression.getSelectNamedBean(), errorMessages);
089        _selectTransitStateSwing.validate(expression.getSelectEnum(), errorMessages);
090        return errorMessages.isEmpty();
091    }
092
093    /** {@inheritDoc} */
094    @Override
095    public String getAutoSystemName() {
096        return InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName();
097    }
098
099    /** {@inheritDoc} */
100    @Override
101    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
102        ExpressionTransit expression = new ExpressionTransit(systemName, userName);
103        updateObject(expression);
104        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public void updateObject(@Nonnull Base object) {
110        if (! (object instanceof ExpressionTransit)) {
111            throw new IllegalArgumentException("object must be an ExpressionBlock but is a: "+object.getClass().getName());
112        }
113
114        ExpressionTransit expression = (ExpressionTransit) object;
115
116        _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean());
117        _selectTransitStateSwing.updateObject(expression.getSelectEnum());
118
119        expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem());
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public String toString() {
125        return Bundle.getMessage("Transit_Short");
126    }
127
128    @Override
129    public void dispose() {
130        _selectNamedBeanSwing.dispose();
131    }
132
133//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionTransitSwing.class);
134}