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