001package jmri.jmrit.logixng.swing; 002 003import java.awt.*; 004import java.awt.event.ActionEvent; 005 006import javax.swing.*; 007 008import jmri.NamedBean; 009import jmri.jmrit.logixng.NamedBeanAddressing; 010import jmri.util.JmriJFrame; 011import jmri.util.swing.BeanSelectPanel; 012 013/** 014 * A dialog to enter data. 015 * In LogixNG, the user can in many cases enter data in several ways. 016 * For example, a turnout can be entered directly, by using a reference, 017 * a local variable or a formula. This dialog presents a dialog for that. 018 * 019 * @author Daniel Bergqvist (C) 2022 020 */ 021public class LogixNG_DataDialog { 022 023 private final SwingConfiguratorInterface _swi; 024 private JmriJFrame _showDialogFrame; 025 026 private Runnable _runOnOk; 027 private JTabbedPane _tabbedPane; 028 private BeanSelectPanel<? extends NamedBean> _beanPanel; 029 private JComboBox<? extends Object> _beanComboBox; 030 private JTextField _beanTextField; 031 private JPanel _panelDirect; 032 private JPanel _panelReference; 033 private JPanel _panelLocalVariable; 034 private JPanel _panelFormula; 035 private JTextField _beanReferenceTextField; 036 private JTextField _beanLocalVariableTextField; 037 private JTextField _beanFormulaTextField; 038 039 040 041 public LogixNG_DataDialog(SwingConfiguratorInterface swi) { 042 _swi = swi; 043 } 044 045 public void showDialog( 046 String title, 047 NamedBeanAddressing addressing, 048 BeanSelectPanel<? extends NamedBean> beanPanel, 049 JTextField referenceTextField, 050 JTextField localVariableTextField, 051 JTextField formulaTextField, 052 Runnable runOnOk) { 053 if (checkOpenDialog()) { 054 return; 055 } 056 057 _beanPanel = beanPanel; 058 _beanComboBox = null; 059 _beanTextField = null; 060 061 showDialog(title, addressing, referenceTextField, localVariableTextField, formulaTextField, runOnOk); 062 } 063 064 public void showDialog( 065 String title, 066 NamedBeanAddressing addressing, 067 JComboBox<? extends Object> comboBox, 068 JTextField referenceTextField, 069 JTextField localVariableTextField, 070 JTextField formulaTextField, 071 Runnable runOnOk) { 072 if (checkOpenDialog()) { 073 return; 074 } 075 076 _beanPanel = null; 077 _beanComboBox = comboBox; 078 _beanTextField = null; 079 080 showDialog(title, addressing, referenceTextField, localVariableTextField, formulaTextField, runOnOk); 081 } 082 083 public void showDialog( 084 String title, 085 NamedBeanAddressing addressing, 086 JTextField textField, 087 JTextField referenceTextField, 088 JTextField localVariableTextField, 089 JTextField formulaTextField, 090 Runnable runOnOk) { 091 if (checkOpenDialog()) { 092 return; 093 } 094 095 _beanPanel = null; 096 _beanComboBox = null; 097 _beanTextField = textField; 098 showDialog(title, addressing, referenceTextField, localVariableTextField, formulaTextField, runOnOk); 099 } 100 101 private void showDialog( 102 String title, 103 NamedBeanAddressing addressing, 104 JTextField referenceTextField, 105 JTextField localVariableTextField, 106 JTextField formulaTextField, 107 Runnable runOnOk) { 108 109 _beanReferenceTextField = referenceTextField; 110 _beanLocalVariableTextField = localVariableTextField; 111 _beanFormulaTextField = formulaTextField; 112 113 _runOnOk = runOnOk; 114 115// _showDialogFrame = new JmriJFrame(Bundle.getMessage(titleId)); 116 _showDialogFrame = new JmriJFrame(title); 117// _showDialogFrame.addHelpMenu( 118// "package.jmri.jmrit.beantable.LogixNGTable", true); // NOI18N 119 _showDialogFrame.setLocation( 120 (Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - _showDialogFrame.getWidth() / 2, 121 (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - _showDialogFrame.getHeight() / 2); 122 Container contentPane = _showDialogFrame.getContentPane(); 123 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 124 125 _tabbedPane = new JTabbedPane(); 126 _panelDirect = new javax.swing.JPanel(); 127 _panelReference = new javax.swing.JPanel(); 128 _panelLocalVariable = new javax.swing.JPanel(); 129 _panelFormula = new javax.swing.JPanel(); 130 131 _tabbedPane.addTab(NamedBeanAddressing.Direct.toString(), _panelDirect); 132 _tabbedPane.addTab(NamedBeanAddressing.Reference.toString(), _panelReference); 133 _tabbedPane.addTab(NamedBeanAddressing.LocalVariable.toString(), _panelLocalVariable); 134 _tabbedPane.addTab(NamedBeanAddressing.Formula.toString(), _panelFormula); 135 136 switch (addressing) { 137 case Direct: _tabbedPane.setSelectedComponent(_panelDirect); break; 138 case Reference: _tabbedPane.setSelectedComponent(_panelReference); break; 139 case LocalVariable: _tabbedPane.setSelectedComponent(_panelLocalVariable); break; 140 case Formula: _tabbedPane.setSelectedComponent(_panelFormula); break; 141 default: throw new IllegalArgumentException("invalid addressing: " + addressing.name()); 142 } 143 144 if (_beanPanel != null) _panelDirect.add(_beanPanel); 145 else if (_beanComboBox != null) _panelDirect.add(_beanComboBox); 146 else if (_beanTextField != null) _panelDirect.add(_beanTextField); 147 148// _beanReferenceTextField = new JTextField(); 149// _beanReferenceTextField.setColumns(30); 150 _panelReference.add(_beanReferenceTextField); 151 152// _beanLocalVariableTextField = new JTextField(); 153// _beanLocalVariableTextField.setColumns(30); 154 _panelLocalVariable.add(_beanLocalVariableTextField); 155 156// _beanFormulaTextField = new JTextField(); 157// _beanFormulaTextField.setColumns(30); 158 _panelFormula.add(_beanFormulaTextField); 159 160 // set up create and cancel buttons 161 JPanel panel5 = new JPanel(); 162 panel5.setLayout(new FlowLayout()); 163 // Cancel 164 JButton cancel = new JButton(Bundle.getMessage("ButtonCancel")); // NOI18N 165 panel5.add(cancel); 166 cancel.addActionListener(this::cancelDialogPressed); 167// cancel.setToolTipText(Bundle.getMessage("CancelLogixNGButtonHint")); // NOI18N 168 169 JButton okButton = new JButton(Bundle.getMessage("ButtonOK")); // NOI18N 170 panel5.add(okButton); 171 okButton.addActionListener((ActionEvent e) -> { 172 _showDialogFrame.setVisible(false); 173 _showDialogFrame.dispose(); 174 _showDialogFrame = null; 175 if (_swi.getJDialog() != null) { 176 _swi.getJDialog().setVisible(true); 177 } 178 _runOnOk.run(); 179 }); 180 181 _showDialogFrame.add(_tabbedPane); 182 183 _showDialogFrame.add(panel5); 184 185 _showDialogFrame.pack(); 186 _showDialogFrame.setVisible(true); 187 } 188 189 public NamedBeanAddressing getAddressing() { 190 if (_tabbedPane.getSelectedComponent() == _panelDirect) { 191 return NamedBeanAddressing.Direct; 192 } else if (_tabbedPane.getSelectedComponent() == _panelReference) { 193 return NamedBeanAddressing.Reference; 194 } else if (_tabbedPane.getSelectedComponent() == _panelLocalVariable) { 195 return NamedBeanAddressing.LocalVariable; 196 } else if (_tabbedPane.getSelectedComponent() == _panelFormula) { 197 return NamedBeanAddressing.Formula; 198 } else { 199 throw new IllegalArgumentException("_tabbedPane has unknown selection"); 200 } 201 } 202 203 public boolean checkOpenDialog() { 204 if (_showDialogFrame != null) { 205 _showDialogFrame.requestFocus(); 206 return true; 207 } 208 return false; 209 } 210 211 /** 212 * Respond to the Cancel button in Add bean window. 213 * <p> 214 * Note: Also get there if the user closes the Add bean window. 215 * 216 * @param e The event heard 217 */ 218 void cancelDialogPressed(ActionEvent e) { 219 _showDialogFrame.setVisible(false); 220 _showDialogFrame.dispose(); 221 _showDialogFrame = null; 222 if (_swi.getJDialog() != null) { 223 _swi.getJDialog().setVisible(true); 224 } 225 } 226 227 public void dispose() { 228 if (_showDialogFrame != null) { 229 _showDialogFrame.setVisible(false); 230 _showDialogFrame.dispose(); 231 } 232 } 233 234}