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_SelectDouble;
012import jmri.jmrit.logixng.util.parser.ParserException;
013import jmri.util.swing.BeanSelectPanel;
014
015/**
016 * Swing class for jmri.jmrit.logixng.util.LogixNG_SelectDouble.
017 *
018 * @author Daniel Bergqvist (C) 2022
019 */
020public class LogixNG_SelectDoubleSwing {
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_SelectDoubleSwing(
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_SelectDouble selectDouble) {
048
049        LogixNG_SelectDouble.FormatterParserValidator _formatterParserValidator
050                = selectDouble.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(selectDouble.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 (selectDouble.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: " + selectDouble.getAddressing().name());
102        }
103        _valueTextField.setText(_formatterParserValidator.format(selectDouble.getValue()));
104        _referenceTextField.setText(selectDouble.getReference());
105        _memoryPanel.setDefaultNamedBean(selectDouble.getMemory());
106        _listenToMemoryCheckBox.setSelected(selectDouble.getListenToMemory());
107        _localVariableTextField.setText(selectDouble.getLocalVariable());
108        _formulaTextField.setText(selectDouble.getFormula());
109
110        panel.add(_tabbedPane);
111        return panel;
112    }
113
114    public boolean validate(
115            @Nonnull LogixNG_SelectDouble selectDouble,
116            @Nonnull List<String> errorMessages) {
117
118        if (_tabbedPane.getSelectedComponent() == _panelDirect) {
119            String result = selectDouble.getFormatterParserValidator()
120                    .validate(_valueTextField.getText());
121            if (result != null) errorMessages.add(result);
122        }
123
124        try {
125            if (_tabbedPane.getSelectedComponent() == _panelReference) {
126                selectDouble.setReference(_referenceTextField.getText());
127            }
128        } catch (IllegalArgumentException e) {
129            errorMessages.add(e.getMessage());
130            return false;
131        }
132
133        try {
134            selectDouble.setFormula(_formulaTextField.getText());
135            if (_tabbedPane.getSelectedComponent() == _panelDirect) {
136                selectDouble.setAddressing(NamedBeanAddressing.Direct);
137            } else if (_tabbedPane.getSelectedComponent() == _panelReference) {
138                selectDouble.setAddressing(NamedBeanAddressing.Reference);
139            } else if (_tabbedPane.getSelectedComponent() == _panelMemory) {
140                selectDouble.setAddressing(NamedBeanAddressing.Memory);
141            } else if (_tabbedPane.getSelectedComponent() == _panelLocalVariable) {
142                selectDouble.setAddressing(NamedBeanAddressing.LocalVariable);
143            } else if (_tabbedPane.getSelectedComponent() == _panelFormula) {
144                selectDouble.setAddressing(NamedBeanAddressing.Formula);
145            } else if (_tabbedPane.getSelectedComponent() == _panelTable) {
146                selectDouble.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(selectDouble.getSelectTable(), errorMessages);
156
157        return errorMessages.isEmpty();
158    }
159
160    public void updateObject(@Nonnull LogixNG_SelectDouble selectDouble) {
161
162        if (_tabbedPane.getSelectedComponent() == _panelDirect) {
163            selectDouble.setValue(selectDouble.getFormatterParserValidator()
164                    .parse(_valueTextField.getText()));
165        }
166
167        try {
168            if (_tabbedPane.getSelectedComponent() == _panelDirect) {
169                selectDouble.setAddressing(NamedBeanAddressing.Direct);
170            } else if (_tabbedPane.getSelectedComponent() == _panelReference) {
171                selectDouble.setAddressing(NamedBeanAddressing.Reference);
172                selectDouble.setReference(_referenceTextField.getText());
173            } else if (_tabbedPane.getSelectedComponent() == _panelMemory) {
174                selectDouble.setAddressing(NamedBeanAddressing.Memory);
175                selectDouble.setMemory(_memoryPanel.getNamedBean());
176                selectDouble.setListenToMemory(_listenToMemoryCheckBox.isSelected());
177            } else if (_tabbedPane.getSelectedComponent() == _panelLocalVariable) {
178                selectDouble.setAddressing(NamedBeanAddressing.LocalVariable);
179                selectDouble.setLocalVariable(_localVariableTextField.getText());
180            } else if (_tabbedPane.getSelectedComponent() == _panelFormula) {
181                selectDouble.setAddressing(NamedBeanAddressing.Formula);
182                selectDouble.setFormula(_formulaTextField.getText());
183            } else if (_tabbedPane.getSelectedComponent() == _panelTable) {
184                selectDouble.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(selectDouble.getSelectTable());
193    }
194
195    public void dispose() {
196        _selectTableSwing.dispose();
197    }
198
199}