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.Base; 011import jmri.jmrit.logixng.DigitalExpressionManager; 012import jmri.jmrit.logixng.MaleSocket; 013import jmri.jmrit.logixng.expressions.And; 014import jmri.jmrit.logixng.expressions.And.Type; 015import jmri.util.swing.JComboBoxUtil; 016 017/** 018 * Configures an ExpressionTurnout object with a Swing JPanel. 019 * 020 * @author Daniel Bergqvist Copyright 2021 021 */ 022public class AndSwing extends AbstractDigitalExpressionSwing { 023 024 private JComboBox<Type> _typeComboBox; 025 026 027 @Override 028 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 029 if ((object != null) && !(object instanceof And)) { 030 throw new IllegalArgumentException("object must be an And but is a: "+object.getClass().getName()); 031 } 032 033 And action = (And)object; 034 035 panel = new JPanel(); 036 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 037 038 _typeComboBox = new JComboBox<>(); 039 for (Type type : Type.values()) _typeComboBox.addItem(type); 040 JComboBoxUtil.setupComboBoxMaxRows(_typeComboBox); 041 if (action != null) _typeComboBox.setSelectedItem(action.getType()); 042 043 JPanel typePanel = new JPanel(); 044 typePanel.add(_typeComboBox); 045 panel.add(typePanel); 046 047 JPanel labelPanel = new JPanel(); 048 labelPanel.add(new JLabel(Bundle.getMessage("And_Info"))); 049 panel.add(labelPanel); 050 } 051 052 /** {@inheritDoc} */ 053 @Override 054 public boolean validate(@Nonnull List<String> errorMessages) { 055 return true; 056 } 057 058 /** {@inheritDoc} */ 059 @Override 060 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 061 And expression = new And(systemName, userName); 062 updateObject(expression); 063 return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression); 064 } 065 066 /** {@inheritDoc} */ 067 @Override 068 public void updateObject(@Nonnull Base object) { 069 if (!(object instanceof And)) { 070 throw new IllegalArgumentException("object must be an And but is a: "+object.getClass().getName()); 071 } 072 073 And expression = (And)object; 074 075 expression.setType(_typeComboBox.getItemAt(_typeComboBox.getSelectedIndex())); 076 } 077 078 /** {@inheritDoc} */ 079 @Override 080 public String toString() { 081 return Bundle.getMessage("And_Short"); 082 } 083 084 @Override 085 public void dispose() { 086 } 087 088 089// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AndSwing.class); 090 091}