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.ExpressionSensor; 012import jmri.jmrit.logixng.expressions.ExpressionSensor.SensorState; 013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface; 014import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing; 015import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing; 016import jmri.util.swing.JComboBoxUtil; 017 018/** 019 * Configures an ExpressionSensor object with a Swing JPanel. 020 * 021 * @author Daniel Bergqvist Copyright 2021 022 */ 023public class ExpressionSensorSwing extends AbstractDigitalExpressionSwing { 024 025 private LogixNG_SelectNamedBeanSwing<Sensor> _selectNamedBeanSwing; 026 private LogixNG_SelectEnumSwing<SensorState> _selectEnumSwing; 027 028 private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox; 029 030 031 public ExpressionSensorSwing() { 032 } 033 034 public ExpressionSensorSwing(JDialog dialog) { 035 super.setJDialog(dialog); 036 } 037 038 @Override 039 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 040 ExpressionSensor expression = (ExpressionSensor)object; 041 042 panel = new JPanel(); 043 044 _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>( 045 InstanceManager.getDefault(SensorManager.class), getJDialog(), this); 046 047 _selectEnumSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this); 048 049 JPanel _tabbedPaneNamedBean; 050 JPanel _tabbedPaneEnum; 051 052 if (expression != null) { 053 _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(expression.getSelectNamedBean()); 054 _tabbedPaneEnum = _selectEnumSwing.createPanel(expression.getSelectEnum(), SensorState.values()); 055 } else { 056 _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null); 057 _tabbedPaneEnum = _selectEnumSwing.createPanel(null, SensorState.values()); 058 } 059 060 061 _is_IsNot_ComboBox = new JComboBox<>(); 062 for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) { 063 _is_IsNot_ComboBox.addItem(e); 064 } 065 JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox); 066 067 068 if (expression != null) { 069 _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot()); 070 } 071 072 JComponent[] components = new JComponent[]{ 073 _tabbedPaneNamedBean, 074 _is_IsNot_ComboBox, 075 _tabbedPaneEnum}; 076 077 List<JComponent> componentList = SwingConfiguratorInterface.parseMessage( 078 Bundle.getMessage("ExpressionSensor_Components"), components); 079 080 for (JComponent c : componentList) panel.add(c); 081 } 082 083 /** {@inheritDoc} */ 084 @Override 085 public boolean validate(@Nonnull List<String> errorMessages) { 086 // Create a temporary expression to test formula 087 ExpressionSensor expression = new ExpressionSensor("IQDE1", null); 088 089 _selectNamedBeanSwing.validate(expression.getSelectNamedBean(), errorMessages); 090 _selectEnumSwing.validate(expression.getSelectEnum(), errorMessages); 091 092 return errorMessages.isEmpty(); 093 } 094 095 /** {@inheritDoc} */ 096 @Override 097 public String getAutoSystemName() { 098 return InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName(); 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 104 ExpressionSensor expression = new ExpressionSensor(systemName, userName); 105 updateObject(expression); 106 return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression); 107 } 108 109 /** {@inheritDoc} */ 110 @Override 111 public void updateObject(@Nonnull Base object) { 112 if (! (object instanceof ExpressionSensor)) { 113 throw new IllegalArgumentException("object must be an ExpressionSensor but is a: "+object.getClass().getName()); 114 } 115 ExpressionSensor expression = (ExpressionSensor)object; 116 117 _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean()); 118 _selectEnumSwing.updateObject(expression.getSelectEnum()); 119 120 expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem()); 121 } 122 123 /** {@inheritDoc} */ 124 @Override 125 public String toString() { 126 return Bundle.getMessage("Sensor_Short"); 127 } 128 129 @Override 130 public void dispose() { 131 _selectNamedBeanSwing.dispose(); 132 } 133 134 135// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSensorSwing.class); 136 137}