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