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