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