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