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.ExpressionSection;
012import jmri.jmrit.logixng.expressions.ExpressionSection.SectionState;
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 ExpressionSection object with a Swing JPanel.
020 *
021 * @author Dave Sand         Copyright 2023
022 */
023public class ExpressionSectionSwing extends AbstractDigitalExpressionSwing {
024
025    private LogixNG_SelectNamedBeanSwing<Section> _selectNamedBeanSwing;
026    private LogixNG_SelectEnumSwing<SectionState> _selectSectionStateSwing;
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        ExpressionSection expression = (ExpressionSection)object;
034
035        panel = new JPanel();
036
037        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
038                InstanceManager.getDefault(SectionManager.class), getJDialog(), this);
039
040        _selectSectionStateSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
041
042        JPanel tabbedPaneNamedBean;
043        JPanel tabbedPaneSectionState;
044
045        if (expression != null) {
046            tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(expression.getSelectNamedBean());
047            tabbedPaneSectionState = _selectSectionStateSwing.createPanel(expression.getSelectEnum(), SectionState.values());
048        } else {
049            tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
050            tabbedPaneSectionState = _selectSectionStateSwing.createPanel(null, SectionState.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        _selectSectionStateSwing.addAddressingListener(e -> { setDataPanelState(); });
060        _selectSectionStateSwing.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            tabbedPaneSectionState};
072
073        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
074                Bundle.getMessage("ExpressionSection_Components"), components);
075
076        for (JComponent c : componentList) panel.add(c);
077    }
078
079    private void setDataPanelState() {
080//         boolean newState = (_selectSectionStateSwing.getAddressing() != NamedBeanAddressing.Direct)
081//                 || (_selectSectionStateSwing.getEnum() == BlockState.ValueMatches);
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public boolean validate(@Nonnull List<String> errorMessages) {
087        ExpressionSection expression = new ExpressionSection("IQDE1", null);
088        _selectNamedBeanSwing.validate(expression.getSelectNamedBean(), errorMessages);
089        _selectSectionStateSwing.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        ExpressionSection expression = new ExpressionSection(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 ExpressionSection)) {
111            throw new IllegalArgumentException("object must be an ExpressionBlock but is a: "+object.getClass().getName());
112        }
113
114        ExpressionSection expression = (ExpressionSection) object;
115
116        _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean());
117        _selectSectionStateSwing.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("Section_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(ExpressionSectionSwing.class);
134}