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.InstanceManager; 010import jmri.jmrit.logixng.*; 011import jmri.jmrit.logixng.expressions.ExpressionReference; 012import jmri.jmrit.logixng.expressions.ExpressionReference.PointsTo; 013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface; 014import jmri.util.swing.JComboBoxUtil; 015 016/** 017 * Configures an ExpressionReference object with a Swing JPanel. 018 * 019 * @author Daniel Bergqvist Copyright 2021 020 */ 021public class ExpressionReferenceSwing extends AbstractDigitalExpressionSwing { 022 023 private JTextField _sensorReferenceTextField; 024 private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox; 025 private JComboBox<PointsTo> _stateComboBox; 026 027 028 @Override 029 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 030 ExpressionReference expression = (ExpressionReference)object; 031 032 panel = new JPanel(); 033 034 _sensorReferenceTextField = new JTextField(30); 035 036 _is_IsNot_ComboBox = new JComboBox<>(); 037 for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) { 038 _is_IsNot_ComboBox.addItem(e); 039 } 040 JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox); 041 042 _stateComboBox = new JComboBox<>(); 043 for (PointsTo e : PointsTo.values()) { 044 _stateComboBox.addItem(e); 045 } 046 JComboBoxUtil.setupComboBoxMaxRows(_stateComboBox); 047 048 if (expression != null) { 049 _sensorReferenceTextField.setText(expression.getReference()); 050 _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot()); 051 _stateComboBox.setSelectedItem(expression.getPointsTo()); 052 } 053 054 JComponent[] components = new JComponent[]{ 055 _sensorReferenceTextField, 056 _is_IsNot_ComboBox, 057 _stateComboBox}; 058 059 List<JComponent> componentList = SwingConfiguratorInterface.parseMessage( 060 Bundle.getMessage("ExpressionReference_Components"), components); 061 062 for (JComponent c : componentList) panel.add(c); 063 } 064 065 /** {@inheritDoc} */ 066 @Override 067 public boolean validate(@Nonnull List<String> errorMessages) { 068 // Create a temporary expression to test formula 069 ExpressionReference expression = new ExpressionReference("IQDE1", null); 070 071 try { 072 expression.setReference(_sensorReferenceTextField.getText()); 073 } catch (IllegalArgumentException e) { 074 errorMessages.add(e.getMessage()); 075 return false; 076 } 077 078 return true; 079 } 080 081 /** {@inheritDoc} */ 082 @Override 083 public String getAutoSystemName() { 084 return InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName(); 085 } 086 087 /** {@inheritDoc} */ 088 @Override 089 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 090 ExpressionReference expression = new ExpressionReference(systemName, userName); 091 updateObject(expression); 092 return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression); 093 } 094 095 /** {@inheritDoc} */ 096 @Override 097 public void updateObject(@Nonnull Base object) { 098 if (! (object instanceof ExpressionReference)) { 099 throw new IllegalArgumentException("object must be an ExpressionReference but is a: "+object.getClass().getName()); 100 } 101 ExpressionReference expression = (ExpressionReference)object; 102 expression.setReference(_sensorReferenceTextField.getText()); 103 expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem()); 104 expression.setPointsTo((PointsTo)_stateComboBox.getSelectedItem()); 105 } 106 107 /** {@inheritDoc} */ 108 @Override 109 public String toString() { 110 return Bundle.getMessage("Reference_Short"); 111 } 112 113 @Override 114 public void dispose() { 115 // Do nothing 116 } 117 118 119// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionReferenceSwing.class); 120 121}