001package jmri.jmrit.logixng.actions.swing; 002 003import java.awt.Color; 004import java.util.List; 005 006import javax.annotation.CheckForNull; 007import javax.annotation.Nonnull; 008import javax.swing.*; 009 010import jmri.*; 011import jmri.jmrit.logixng.Base; 012import jmri.jmrit.logixng.DigitalActionManager; 013import jmri.jmrit.logixng.MaleSocket; 014import jmri.jmrit.logixng.actions.ActionThrottle; 015 016/** 017 * Configures an ActionThrottle object with a Swing JPanel. 018 * 019 * @author Daniel Bergqvist Copyright 2021 020 */ 021public class ActionThrottleSwing extends AbstractDigitalActionSwing { 022 023 private JComboBox<Connection> _connection; 024 private JCheckBox _stopLocoWhenSwitchingLoco; 025 private JCheckBox _waitForThrottle; 026 027 @Override 028 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 029 if ((object != null) && !(object instanceof ActionThrottle)) { 030 throw new IllegalArgumentException("object must be an ActionThrottle but is a: "+object.getClass().getName()); 031 } 032 033 ActionThrottle action = (ActionThrottle)object; 034 035 panel = new JPanel(); 036 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 037 038 JPanel queryPanel = new JPanel(); 039 queryPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 040 041 JPanel connectionPanel = new JPanel(); 042 connectionPanel.add(new JLabel(Bundle.getMessage("ActionThrottleSwing_Connection"))); 043 044 _connection = new JComboBox<>(); 045 _connection.addItem(new Connection(null)); 046 List<SystemConnectionMemo> systemConnections = 047 jmri.InstanceManager.getList(SystemConnectionMemo.class); 048 for (SystemConnectionMemo connection : systemConnections) { 049 if (!connection.provides(ThrottleManager.class)) continue; 050 Connection c = new Connection(connection); 051 _connection.addItem(c); 052 if ((action != null) && (action.getMemo() == connection)) { 053 _connection.setSelectedItem(c); 054 } 055 } 056 connectionPanel.add(_connection); 057 panel.add(connectionPanel); 058 059 _stopLocoWhenSwitchingLoco = new JCheckBox(Bundle.getMessage("ActionThrottleSwing_StopLocoWhenSwitchingLoco")); 060 _waitForThrottle = new JCheckBox(Bundle.getMessage("ActionThrottleSwing_WaitForThrottle")); 061 _waitForThrottle.addActionListener((e) -> { 062 setEnabledStopLocoWhenSwitchingLoco(); 063 }); 064 if (action != null) { 065 _stopLocoWhenSwitchingLoco.setSelected(action.isStopLocoWhenSwitchingLoco()); 066 _waitForThrottle.setSelected(action.isWaitForThrottle()); 067 } else { 068 _stopLocoWhenSwitchingLoco.setSelected(true); 069 _waitForThrottle.setSelected(false); 070 } 071 setEnabledStopLocoWhenSwitchingLoco(); 072 panel.add(_stopLocoWhenSwitchingLoco); 073 panel.add(_waitForThrottle); 074 } 075 076 private void setEnabledStopLocoWhenSwitchingLoco() { 077 _stopLocoWhenSwitchingLoco.setEnabled(!_waitForThrottle.isSelected()); 078 if (_waitForThrottle.isSelected()) { 079 _stopLocoWhenSwitchingLoco.setSelected(false); 080 } 081 } 082 083 /** {@inheritDoc} */ 084 @Override 085 public boolean validate(@Nonnull List<String> errorMessages) { 086 return true; 087 } 088 089 /** {@inheritDoc} */ 090 @Override 091 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 092 ActionThrottle action = new ActionThrottle(systemName, userName); 093 updateObject(action); 094 095 return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action); 096 } 097 098 /** {@inheritDoc} */ 099 @Override 100 public void updateObject(@Nonnull Base object) { 101 if (! (object instanceof ActionThrottle)) { 102 throw new IllegalArgumentException("object must be an ActionThrottle but is a: "+object.getClass().getName()); 103 } 104 105 ActionThrottle action = (ActionThrottle)object; 106 107 action.setMemo(_connection.getItemAt(_connection.getSelectedIndex())._memo); 108 action.setStopLocoWhenSwitchingLoco(_stopLocoWhenSwitchingLoco.isSelected()); 109 action.setWaitForThrottle(_waitForThrottle.isSelected()); 110 } 111 112 /** {@inheritDoc} */ 113 @Override 114 public String toString() { 115 return Bundle.getMessage("ActionThrottle_Short"); 116 } 117 118 @Override 119 public void dispose() { 120 } 121 122 123 124 private static class Connection { 125 126 private SystemConnectionMemo _memo; 127 128 public Connection(SystemConnectionMemo memo) { 129 _memo = memo; 130 } 131 132 @Override 133 public String toString() { 134 if (_memo == null) { 135 return Bundle.getMessage("ActionThrottleSwing_DefaultConnection"); 136 } 137 return _memo.getUserName(); 138 } 139 } 140 141}