001package jmri.jmrit.throttle; 002 003import java.awt.event.ActionEvent; 004 005import javax.annotation.Nonnull; 006import javax.swing.Icon; 007import javax.swing.JMenu; 008 009import jmri.InstanceManager; 010import jmri.ThrottleManager; 011import jmri.beans.BeanUtil; 012import jmri.jmrit.roster.rostergroup.RosterGroupSelector; 013import jmri.jmrix.ConnectionConfig; 014import jmri.jmrix.ConnectionConfigManager; 015import jmri.util.swing.JmriAbstractAction; 016import jmri.util.swing.WindowInterface; 017 018/** 019 * Create a new throttle. 020 * 021 * @author Glen Oberhauser 022 */ 023public class ThrottleCreationAction extends JmriAbstractAction { 024 025 private final ConnectionConfig connectionConfig; 026 027 public ThrottleCreationAction(String s, WindowInterface wi) { 028 super(s, wi); 029 connectionConfig = null; 030 // disable the ourselves if there is no throttle Manager 031 if (jmri.InstanceManager.getNullableDefault(ThrottleManager.class) == null) { 032 setEnabled(false); 033 } 034 } 035 036 public ThrottleCreationAction(String s, Icon i, WindowInterface wi) { 037 super(s, i, wi); 038 connectionConfig = null; 039 // disable the ourselves if there is no throttle Manager 040 if (jmri.InstanceManager.getNullableDefault(ThrottleManager.class) == null) { 041 setEnabled(false); 042 } 043 } 044 045 /** 046 * Constructor 047 * 048 * @param s Name for the action. 049 */ 050 public ThrottleCreationAction(String s) { 051 super(s); 052 connectionConfig = null; 053 // disable the ourselves if there is no throttle Manager 054 if (jmri.InstanceManager.getNullableDefault(ThrottleManager.class) == null) { 055 setEnabled(false); 056 } 057 } 058 059 /** 060 * Constructor 061 * 062 * @param s Name for the action. 063 * @param connectionConfig the connection config 064 */ 065 public ThrottleCreationAction(String s, ConnectionConfig connectionConfig) { 066 super(s); 067 this.connectionConfig = connectionConfig; 068 // disable the ourselves if there is no throttle Manager 069 if ((connectionConfig == null) 070 || !connectionConfig.getAdapter().getSystemConnectionMemo() 071 .provides(ThrottleManager.class)) { 072 super.setEnabled(false); 073 } 074 } 075 076 public ThrottleCreationAction() { 077 this(Bundle.getMessage("MenuItemNewThrottle")); 078 } 079 080 /** 081 * The action is performed. Create a new ThrottleFrame. 082 * 083 * @param e The event causing the action. 084 */ 085 @Override 086 public void actionPerformed(ActionEvent e) { 087 String group = null; 088 if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) { 089 group = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP); 090 } 091 ThrottleFrame tf = InstanceManager.getDefault(ThrottleFrameManager.class).createThrottleFrame(connectionConfig); 092 tf.getAddressPanel().getRosterEntrySelector().setSelectedRosterGroup(group); 093 tf.toFront(); 094 } 095 096 // never invoked, because we overrode actionPerformed above 097 @Override 098 public jmri.util.swing.JmriPanel makePanel() { 099 throw new IllegalArgumentException("Should not be invoked"); 100 } 101 102 public static void addNewThrottleItemsToThrottleMenu(@Nonnull JMenu throttleMenu) { 103 104 throttleMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction(Bundle.getMessage("MenuItemNewThrottle"))); 105 106 ConnectionConfigManager ccm = InstanceManager.getNullableDefault(ConnectionConfigManager.class); 107 if (ccm == null) return; 108 109 int numConnectionsWithThrottleManager = 0; 110 111 for (ConnectionConfig c : ccm) { 112 if (c.getAdapter().getSystemConnectionMemo().provides(ThrottleManager.class)) { 113 ThrottleManager connectionThrottleManager = 114 c.getAdapter().getSystemConnectionMemo().get(ThrottleManager.class); 115 if (connectionThrottleManager != null) numConnectionsWithThrottleManager++; 116 } 117 } 118 119 if (numConnectionsWithThrottleManager > 1) { 120 JMenu throttleConnectionMenu = new JMenu(Bundle.getMessage("MenuThrottlesForConnections")); 121 122 ThrottleManager defaultThrottleManager = InstanceManager.getDefault(ThrottleManager.class); 123 124 for (ConnectionConfig c : InstanceManager.getDefault(ConnectionConfigManager.class)) { 125 126 ThrottleManager connectionThrottleManager = c.getAdapter().getSystemConnectionMemo().get(ThrottleManager.class); 127 if (connectionThrottleManager != null) { 128 if (connectionThrottleManager == defaultThrottleManager) { 129 throttleConnectionMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction( 130 Bundle.getMessage("MenuItemNewThrottleWithConnectionDefault", c.getConnectionName()), 131 c)); 132 } else { 133 throttleConnectionMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction( 134 Bundle.getMessage("MenuItemNewThrottleWithConnection", c.getConnectionName()), 135 c)); 136 } 137 } 138 } 139 140 throttleMenu.add(throttleConnectionMenu); 141 } 142 } 143}