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