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