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.ExpressionConditional;
012import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
013import jmri.jmrit.logixng.util.parser.ParserException;
014import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
015import jmri.util.swing.JComboBoxUtil;
016
017/**
018 * Configures an ExpressionTurnout object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2021
021 */
022public class ExpressionConditionalSwing extends AbstractDigitalExpressionSwing {
023
024    private LogixNG_SelectNamedBeanSwing<Conditional> _selectNamedBeanSwing;
025
026    private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox;
027
028    private JTabbedPane _tabbedPaneConditionalState;
029    private JComboBox<ExpressionConditional.ConditionalState> _stateComboBox;
030    private JPanel _panelConditionalStateDirect;
031    private JPanel _panelConditionalStateReference;
032    private JPanel _panelConditionalStateLocalVariable;
033    private JPanel _panelConditionalStateFormula;
034    private JTextField _conditionalStateReferenceTextField;
035    private JTextField _conditionalStateLocalVariableTextField;
036    private JTextField _conditionalStateFormulaTextField;
037
038
039    @Override
040    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
041        ExpressionConditional expression = (ExpressionConditional)object;
042
043        panel = new JPanel();
044
045        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
046                InstanceManager.getDefault(ConditionalManager.class), getJDialog(), this);
047
048        JPanel _tabbedPaneNamedBean;
049        if (expression != null) {
050            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(expression.getSelectNamedBean());
051        } else {
052            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
053        }
054
055
056        _is_IsNot_ComboBox = new JComboBox<>();
057        for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) {
058            _is_IsNot_ComboBox.addItem(e);
059        }
060        JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox);
061
062
063        _tabbedPaneConditionalState = new JTabbedPane();
064        _panelConditionalStateDirect = new javax.swing.JPanel();
065        _panelConditionalStateReference = new javax.swing.JPanel();
066        _panelConditionalStateLocalVariable = new javax.swing.JPanel();
067        _panelConditionalStateFormula = new javax.swing.JPanel();
068
069        _tabbedPaneConditionalState.addTab(NamedBeanAddressing.Direct.toString(), _panelConditionalStateDirect);
070        _tabbedPaneConditionalState.addTab(NamedBeanAddressing.Reference.toString(), _panelConditionalStateReference);
071        _tabbedPaneConditionalState.addTab(NamedBeanAddressing.LocalVariable.toString(), _panelConditionalStateLocalVariable);
072        _tabbedPaneConditionalState.addTab(NamedBeanAddressing.Formula.toString(), _panelConditionalStateFormula);
073
074        _stateComboBox = new JComboBox<>();
075        for (ExpressionConditional.ConditionalState e : ExpressionConditional.ConditionalState.values()) {
076            _stateComboBox.addItem(e);
077        }
078        JComboBoxUtil.setupComboBoxMaxRows(_stateComboBox);
079
080        _panelConditionalStateDirect.add(_stateComboBox);
081
082        _conditionalStateReferenceTextField = new JTextField();
083        _conditionalStateReferenceTextField.setColumns(30);
084        _panelConditionalStateReference.add(_conditionalStateReferenceTextField);
085
086        _conditionalStateLocalVariableTextField = new JTextField();
087        _conditionalStateLocalVariableTextField.setColumns(30);
088        _panelConditionalStateLocalVariable.add(_conditionalStateLocalVariableTextField);
089
090        _conditionalStateFormulaTextField = new JTextField();
091        _conditionalStateFormulaTextField.setColumns(30);
092        _panelConditionalStateFormula.add(_conditionalStateFormulaTextField);
093
094
095        if (expression != null) {
096            _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot());
097
098            switch (expression.getStateAddressing()) {
099                case Direct: _tabbedPaneConditionalState.setSelectedComponent(_panelConditionalStateDirect); break;
100                case Reference: _tabbedPaneConditionalState.setSelectedComponent(_panelConditionalStateReference); break;
101                case LocalVariable: _tabbedPaneConditionalState.setSelectedComponent(_panelConditionalStateLocalVariable); break;
102                case Formula: _tabbedPaneConditionalState.setSelectedComponent(_panelConditionalStateFormula); break;
103                default: throw new IllegalArgumentException("invalid _addressing state: " + expression.getStateAddressing().name());
104            }
105            _stateComboBox.setSelectedItem(expression.getConditionalState());
106            _conditionalStateReferenceTextField.setText(expression.getStateReference());
107            _conditionalStateLocalVariableTextField.setText(expression.getStateLocalVariable());
108            _conditionalStateFormulaTextField.setText(expression.getStateFormula());
109        }
110
111        JComponent[] components = new JComponent[]{
112            _tabbedPaneNamedBean,
113            _is_IsNot_ComboBox,
114            _tabbedPaneConditionalState};
115
116        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
117                Bundle.getMessage("ExpressionConditional_Components"), components);
118
119        for (JComponent c : componentList) panel.add(c);
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public boolean validate(@Nonnull List<String> errorMessages) {
125        // Create a temporary expression to test formula
126        ExpressionConditional expression = new ExpressionConditional("IQDE1", null);
127
128        _selectNamedBeanSwing.validate(expression.getSelectNamedBean(), errorMessages);
129
130        try {
131            if (_tabbedPaneConditionalState.getSelectedComponent() == _panelConditionalStateReference) {
132                expression.setStateReference(_conditionalStateReferenceTextField.getText());
133            }
134        } catch (IllegalArgumentException e) {
135            errorMessages.add(e.getMessage());
136        }
137
138        return errorMessages.isEmpty();
139    }
140
141    /** {@inheritDoc} */
142    @Override
143    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
144        ExpressionConditional expression = new ExpressionConditional(systemName, userName);
145        updateObject(expression);
146        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
147    }
148
149    /** {@inheritDoc} */
150    @Override
151    public void updateObject(@Nonnull Base object) {
152        if (! (object instanceof ExpressionConditional)) {
153            throw new IllegalArgumentException("object must be an ExpressionConditional but is a: "+object.getClass().getName());
154        }
155        ExpressionConditional expression = (ExpressionConditional)object;
156
157        _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean());
158
159        try {
160            expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem());
161
162            if (_tabbedPaneConditionalState.getSelectedComponent() == _panelConditionalStateDirect) {
163                expression.setStateAddressing(NamedBeanAddressing.Direct);
164                expression.setConditionalState((ExpressionConditional.ConditionalState)_stateComboBox.getSelectedItem());
165            } else if (_tabbedPaneConditionalState.getSelectedComponent() == _panelConditionalStateReference) {
166                expression.setStateAddressing(NamedBeanAddressing.Reference);
167                expression.setStateReference(_conditionalStateReferenceTextField.getText());
168            } else if (_tabbedPaneConditionalState.getSelectedComponent() == _panelConditionalStateLocalVariable) {
169                expression.setStateAddressing(NamedBeanAddressing.LocalVariable);
170                expression.setStateLocalVariable(_conditionalStateLocalVariableTextField.getText());
171            } else if (_tabbedPaneConditionalState.getSelectedComponent() == _panelConditionalStateFormula) {
172                expression.setStateAddressing(NamedBeanAddressing.Formula);
173                expression.setStateFormula(_conditionalStateFormulaTextField.getText());
174            } else {
175                throw new IllegalArgumentException("_tabbedPaneConditionalState has unknown selection");
176            }
177        } catch (ParserException e) {
178            throw new RuntimeException("ParserException: "+e.getMessage(), e);
179        }
180    }
181
182    /** {@inheritDoc} */
183    @Override
184    public String toString() {
185        return Bundle.getMessage("Conditional_Short");
186    }
187
188    @Override
189    public void dispose() {
190    }
191
192
193//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionConditionalSwing.class);
194
195}