001package jmri.jmrit.logixng.util.swing;
002
003import java.util.List;
004
005import javax.annotation.Nonnull;
006import javax.swing.*;
007
008import jmri.*;
009import jmri.jmrit.logixng.*;
010import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
011import jmri.jmrit.logixng.util.LogixNG_SelectInteger;
012import jmri.jmrit.logixng.util.parser.ParserException;
013import jmri.util.swing.BeanSelectPanel;
014
015/**
016 * Swing class for jmri.jmrit.logixng.util.LogixNG_SelectInteger.
017 *
018 * @author Daniel Bergqvist (C) 2022
019 */
020public class LogixNG_SelectIntegerSwing {
021
022    private final JDialog _dialog;
023    private final LogixNG_SelectTableSwing _selectTableSwing;
024
025    private JTabbedPane _tabbedPane;
026    private JTextField _valueTextField;
027    private JPanel _panelDirect;
028    private JPanel _panelReference;
029    private JPanel _panelMemory;
030    private JPanel _panelLocalVariable;
031    private JPanel _panelFormula;
032    private JPanel _panelTable;
033    private JTextField _referenceTextField;
034    private BeanSelectPanel<Memory> _memoryPanel;
035    private JCheckBox _listenToMemoryCheckBox;
036    private JTextField _localVariableTextField;
037    private JTextField _formulaTextField;
038
039
040    public LogixNG_SelectIntegerSwing(
041            @Nonnull JDialog dialog,
042            @Nonnull SwingConfiguratorInterface swi) {
043        _dialog = dialog;
044        _selectTableSwing = new LogixNG_SelectTableSwing(_dialog, swi);
045    }
046
047    public JPanel createPanel(@Nonnull LogixNG_SelectInteger selectInteger) {
048
049        LogixNG_SelectInteger.FormatterParserValidator _formatterParserValidator =
050                selectInteger.getFormatterParserValidator();
051
052        JPanel panel = new JPanel();
053
054        _tabbedPane = new JTabbedPane();
055        _panelDirect = new javax.swing.JPanel();
056        _panelReference = new javax.swing.JPanel();
057        _panelMemory = new JPanel();
058        _panelLocalVariable = new javax.swing.JPanel();
059        _panelFormula = new javax.swing.JPanel();
060        _panelTable = _selectTableSwing.createPanel(selectInteger.getSelectTable());
061
062        _memoryPanel = new BeanSelectPanel<>(InstanceManager.getDefault(MemoryManager.class), null);
063        _listenToMemoryCheckBox = new JCheckBox(Bundle.getMessage("ListenToMemory"));
064
065        _panelMemory.setLayout(new BoxLayout(_panelMemory, BoxLayout.Y_AXIS));
066        _panelMemory.add(_memoryPanel);
067        _panelMemory.add(_listenToMemoryCheckBox);
068
069        _tabbedPane.addTab(NamedBeanAddressing.Direct.toString(), _panelDirect);
070        _tabbedPane.addTab(NamedBeanAddressing.Reference.toString(), _panelReference);
071        _tabbedPane.addTab(NamedBeanAddressing.Memory.toString(), _panelMemory);
072        _tabbedPane.addTab(NamedBeanAddressing.LocalVariable.toString(), _panelLocalVariable);
073        _tabbedPane.addTab(NamedBeanAddressing.Formula.toString(), _panelFormula);
074        _tabbedPane.addTab(NamedBeanAddressing.Table.toString(), _panelTable);
075
076        _valueTextField = new JTextField(30);
077        _valueTextField.setText(_formatterParserValidator.format(
078                _formatterParserValidator.getInitialValue()));
079        _panelDirect.add(_valueTextField);
080
081        _referenceTextField = new JTextField();
082        _referenceTextField.setColumns(30);
083        _panelReference.add(_referenceTextField);
084
085        _localVariableTextField = new JTextField();
086        _localVariableTextField.setColumns(30);
087        _panelLocalVariable.add(_localVariableTextField);
088
089        _formulaTextField = new JTextField();
090        _formulaTextField.setColumns(30);
091        _panelFormula.add(_formulaTextField);
092
093
094        switch (selectInteger.getAddressing()) {
095            case Direct: _tabbedPane.setSelectedComponent(_panelDirect); break;
096            case Reference: _tabbedPane.setSelectedComponent(_panelReference); break;
097            case Memory: _tabbedPane.setSelectedComponent(_panelMemory); break;
098            case LocalVariable: _tabbedPane.setSelectedComponent(_panelLocalVariable); break;
099            case Formula: _tabbedPane.setSelectedComponent(_panelFormula); break;
100            case Table: _tabbedPane.setSelectedComponent(_panelTable); break;
101            default: throw new IllegalArgumentException("invalid _addressing state: " + selectInteger.getAddressing().name());
102        }
103        _valueTextField.setText(_formatterParserValidator.format(selectInteger.getValue()));
104        _referenceTextField.setText(selectInteger.getReference());
105        _memoryPanel.setDefaultNamedBean(selectInteger.getMemory());
106        _listenToMemoryCheckBox.setSelected(selectInteger.getListenToMemory());
107        _localVariableTextField.setText(selectInteger.getLocalVariable());
108        _formulaTextField.setText(selectInteger.getFormula());
109
110        panel.add(_tabbedPane);
111        return panel;
112    }
113
114    public boolean validate(
115            @Nonnull LogixNG_SelectInteger selectInteger,
116            @Nonnull List<String> errorMessages) {
117
118        if (_tabbedPane.getSelectedComponent() == _panelDirect) {
119            String result = selectInteger.getFormatterParserValidator()
120                    .validate(_valueTextField.getText());
121            if (result != null) errorMessages.add(result);
122        }
123
124        try {
125            if (_tabbedPane.getSelectedComponent() == _panelReference) {
126                selectInteger.setReference(_referenceTextField.getText());
127            }
128        } catch (IllegalArgumentException e) {
129            errorMessages.add(e.getMessage());
130            return false;
131        }
132
133        try {
134            selectInteger.setFormula(_formulaTextField.getText());
135            if (_tabbedPane.getSelectedComponent() == _panelDirect) {
136                selectInteger.setAddressing(NamedBeanAddressing.Direct);
137            } else if (_tabbedPane.getSelectedComponent() == _panelReference) {
138                selectInteger.setAddressing(NamedBeanAddressing.Reference);
139            } else if (_tabbedPane.getSelectedComponent() == _panelMemory) {
140                selectInteger.setAddressing(NamedBeanAddressing.Memory);
141            } else if (_tabbedPane.getSelectedComponent() == _panelLocalVariable) {
142                selectInteger.setAddressing(NamedBeanAddressing.LocalVariable);
143            } else if (_tabbedPane.getSelectedComponent() == _panelFormula) {
144                selectInteger.setAddressing(NamedBeanAddressing.Formula);
145            } else if (_tabbedPane.getSelectedComponent() == _panelTable) {
146                selectInteger.setAddressing(NamedBeanAddressing.Table);
147            } else {
148                throw new IllegalArgumentException("_tabbedPane has unknown selection");
149            }
150        } catch (ParserException e) {
151            errorMessages.add("Cannot parse formula: " + e.getMessage());
152            return false;
153        }
154
155        _selectTableSwing.validate(selectInteger.getSelectTable(), errorMessages);
156
157        return errorMessages.isEmpty();
158    }
159
160    public void updateObject(@Nonnull LogixNG_SelectInteger selectInteger) {
161
162        if (_tabbedPane.getSelectedComponent() == _panelDirect) {
163            selectInteger.setValue(selectInteger.getFormatterParserValidator()
164                    .parse(_valueTextField.getText()));
165        }
166
167        try {
168            if (_tabbedPane.getSelectedComponent() == _panelDirect) {
169                selectInteger.setAddressing(NamedBeanAddressing.Direct);
170            } else if (_tabbedPane.getSelectedComponent() == _panelReference) {
171                selectInteger.setAddressing(NamedBeanAddressing.Reference);
172                selectInteger.setReference(_referenceTextField.getText());
173            } else if (_tabbedPane.getSelectedComponent() == _panelMemory) {
174                selectInteger.setAddressing(NamedBeanAddressing.Memory);
175                selectInteger.setMemory(_memoryPanel.getNamedBean());
176                selectInteger.setListenToMemory(_listenToMemoryCheckBox.isSelected());
177            } else if (_tabbedPane.getSelectedComponent() == _panelLocalVariable) {
178                selectInteger.setAddressing(NamedBeanAddressing.LocalVariable);
179                selectInteger.setLocalVariable(_localVariableTextField.getText());
180            } else if (_tabbedPane.getSelectedComponent() == _panelFormula) {
181                selectInteger.setAddressing(NamedBeanAddressing.Formula);
182                selectInteger.setFormula(_formulaTextField.getText());
183            } else if (_tabbedPane.getSelectedComponent() == _panelTable) {
184                selectInteger.setAddressing(NamedBeanAddressing.Table);
185            } else {
186                throw new IllegalArgumentException("_tabbedPaneEnum has unknown selection");
187            }
188        } catch (ParserException e) {
189            throw new RuntimeException("ParserException: "+e.getMessage(), e);
190        }
191
192        _selectTableSwing.updateObject(selectInteger.getSelectTable());
193    }
194
195    public void setEnabled(boolean enabled) {
196        _tabbedPane.setEnabled(enabled);
197        _valueTextField.setEnabled(enabled);
198        _referenceTextField.setEnabled(enabled);
199        _memoryPanel.getBeanCombo().setEnabled(enabled);
200        _listenToMemoryCheckBox.setEnabled(enabled);
201        _localVariableTextField.setEnabled(enabled);
202        _formulaTextField.setEnabled(enabled);
203        _selectTableSwing.setEnabled(enabled);
204    }
205
206    public void dispose() {
207        _selectTableSwing.dispose();
208    }
209
210}