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.ExpressionPower; 012import jmri.jmrit.logixng.expressions.ExpressionPower.PowerState; 013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface; 014import jmri.util.swing.JComboBoxUtil; 015 016/** 017 * Configures an ExpressionPower object with a Swing JPanel. 018 * 019 * @author Daniel Bergqvist Copyright (C) 2021 020 */ 021public class ExpressionPowerSwing extends AbstractDigitalExpressionSwing { 022 023 private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox; 024 private JComboBox<PowerState> _stateComboBox; 025 private JCheckBox _ignoreUnknownStateCheckBox; 026 027 028 @Override 029 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 030 ExpressionPower expression = (ExpressionPower)object; 031 032 panel = new JPanel(); 033 panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 034 035 JPanel powerPanel = new JPanel(); 036 037 _is_IsNot_ComboBox = new JComboBox<>(); 038 for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) { 039 _is_IsNot_ComboBox.addItem(e); 040 } 041 JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox); 042 043 _stateComboBox = new JComboBox<>(); 044 for (PowerState e : PowerState.values()) { 045 _stateComboBox.addItem(e); 046 } 047 JComboBoxUtil.setupComboBoxMaxRows(_stateComboBox); 048 049 _ignoreUnknownStateCheckBox = new JCheckBox(Bundle.getMessage("Power_IgnoreUnknownState")); 050 051 if (expression != null) { 052 _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot()); 053 _stateComboBox.setSelectedItem(expression.getBeanState()); 054 _ignoreUnknownStateCheckBox.setSelected(expression.isIgnoreUnknownState()); 055 } 056 057 JComponent[] components = new JComponent[]{ 058 _is_IsNot_ComboBox, 059 _stateComboBox}; 060 061 List<JComponent> componentList = SwingConfiguratorInterface.parseMessage( 062 Bundle.getMessage("ExpressionPower_Components"), components); 063 064 for (JComponent c : componentList) powerPanel.add(c); 065 066 panel.add(powerPanel); 067 JPanel ignoreUnknownStatePanel = new JPanel(); 068 ignoreUnknownStatePanel.add(_ignoreUnknownStateCheckBox); 069 070 panel.add(ignoreUnknownStatePanel); 071 072 JPanel messagePanel = new JPanel(); 073 messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.PAGE_AXIS)); 074 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation1"))); 075 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation2"))); 076 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation3"))); 077 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation4"))); 078 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation5"))); 079 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation6"))); 080 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation7"))); 081 messagePanel.add(new JLabel(Bundle.getMessage("ExpressionPower_IgnoreUnknownStateExplanation8"))); 082 panel.add(messagePanel); 083 } 084 085 /** {@inheritDoc} */ 086 @Override 087 public boolean validate(@Nonnull List<String> errorMessages) { 088 return true; 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public String getAutoSystemName() { 094 return InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName(); 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 100 ExpressionPower expression = new ExpressionPower(systemName, userName); 101 updateObject(expression); 102 return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression); 103 } 104 105 /** {@inheritDoc} */ 106 @Override 107 public void updateObject(@Nonnull Base object) { 108 if (! (object instanceof ExpressionPower)) { 109 throw new IllegalArgumentException("object must be an ExpressionPower but is a: "+object.getClass().getName()); 110 } 111 ExpressionPower expression = (ExpressionPower)object; 112 expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem()); 113 expression.setBeanState((PowerState)_stateComboBox.getSelectedItem()); 114 expression.setIgnoreUnknownState(_ignoreUnknownStateCheckBox.isSelected()); 115 } 116 117 /** {@inheritDoc} */ 118 @Override 119 public String toString() { 120 return Bundle.getMessage("Power_Short"); 121 } 122 123 @Override 124 public void dispose() { 125 // Do nothing 126 } 127 128 129// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionPowerSwing.class); 130 131}