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.jmrit.throttle.interfaces.ThrottleControllerUI; 014import jmri.jmrix.ConnectionConfig; 015import jmri.jmrix.ConnectionConfigManager; 016import jmri.util.swing.JmriAbstractAction; 017import jmri.util.swing.WindowInterface; 018 019/** 020 * Create a new throttle. 021 * 022 * <hr> 023 * This file is part of JMRI. 024 * <p> 025 * JMRI is free software; you can redistribute it and/or modify it under the 026 * terms of version 2 of the GNU General Public License as published by the Free 027 * Software Foundation. See the "COPYING" file for a copy of this license. 028 * <p> 029 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 030 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 031 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 032 * 033 * @author Glen Oberhauser 034 */ 035public class ThrottleCreationAction extends JmriAbstractAction { 036 037 private final ConnectionConfig connectionConfig; 038 039 public ThrottleCreationAction(String s, WindowInterface wi) { 040 super(s, wi); 041 connectionConfig = null; 042 // disable the ourselves if there is no throttle Manager 043 if (jmri.InstanceManager.getNullableDefault(ThrottleManager.class) == null) { 044 setEnabled(false); 045 } 046 } 047 048 public ThrottleCreationAction(String s, Icon i, WindowInterface wi) { 049 super(s, i, wi); 050 connectionConfig = null; 051 // disable the ourselves if there is no throttle Manager 052 if (jmri.InstanceManager.getNullableDefault(ThrottleManager.class) == null) { 053 setEnabled(false); 054 } 055 } 056 057 /** 058 * Constructor 059 * 060 * @param s Name for the action. 061 */ 062 public ThrottleCreationAction(String s) { 063 super(s); 064 connectionConfig = null; 065 // disable the ourselves if there is no throttle Manager 066 if (jmri.InstanceManager.getNullableDefault(ThrottleManager.class) == null) { 067 setEnabled(false); 068 } 069 } 070 071 /** 072 * Constructor 073 * 074 * @param s Name for the action. 075 * @param connectionConfig the connection config 076 */ 077 public ThrottleCreationAction(String s, ConnectionConfig connectionConfig) { 078 super(s); 079 this.connectionConfig = connectionConfig; 080 // disable the ourselves if there is no throttle Manager 081 if ((connectionConfig == null) 082 || !connectionConfig.getAdapter().getSystemConnectionMemo() 083 .provides(ThrottleManager.class)) { 084 super.setEnabled(false); 085 } 086 } 087 088 public ThrottleCreationAction() { 089 this(Bundle.getMessage("MenuItemNewThrottle")); 090 } 091 092 /** 093 * The action is performed. Create a new ThrottleFrame. 094 * 095 * @param e The event causing the action. 096 */ 097 @Override 098 public void actionPerformed(ActionEvent e) { 099 String group = null; 100 if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) { 101 group = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP); 102 } 103 ThrottleControllerUI tf = InstanceManager.getDefault(ThrottleFrameManager.class).createThrottleFrame(connectionConfig); 104 tf.getRosterEntrySelector().setSelectedRosterGroup(group); 105 tf.toFront(); 106 } 107 108 // never invoked, because we overrode actionPerformed above 109 @Override 110 public jmri.util.swing.JmriPanel makePanel() { 111 throw new IllegalArgumentException("Should not be invoked"); 112 } 113 114 public static void addNewThrottleItemsToThrottleMenu(@Nonnull JMenu throttleMenu) { 115 116 throttleMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction(Bundle.getMessage("MenuItemNewThrottle"))); 117 118 ConnectionConfigManager ccm = InstanceManager.getNullableDefault(ConnectionConfigManager.class); 119 if (ccm == null) return; 120 121 int numConnectionsWithThrottleManager = 0; 122 123 for (ConnectionConfig c : ccm) { 124 if (c.getAdapter().getSystemConnectionMemo().provides(ThrottleManager.class)) { 125 ThrottleManager connectionThrottleManager = 126 c.getAdapter().getSystemConnectionMemo().get(ThrottleManager.class); 127 if (connectionThrottleManager != null) numConnectionsWithThrottleManager++; 128 } 129 } 130 131 if (numConnectionsWithThrottleManager > 1) { 132 JMenu throttleConnectionMenu = new JMenu(Bundle.getMessage("MenuThrottlesForConnections")); 133 134 ThrottleManager defaultThrottleManager = InstanceManager.getDefault(ThrottleManager.class); 135 136 for (ConnectionConfig c : InstanceManager.getDefault(ConnectionConfigManager.class)) { 137 138 ThrottleManager connectionThrottleManager = c.getAdapter().getSystemConnectionMemo().get(ThrottleManager.class); 139 if (connectionThrottleManager != null) { 140 if (connectionThrottleManager == defaultThrottleManager) { 141 throttleConnectionMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction( 142 Bundle.getMessage("MenuItemNewThrottleWithConnectionDefault", c.getConnectionName()), 143 c)); 144 } else { 145 throttleConnectionMenu.add(new jmri.jmrit.throttle.ThrottleCreationAction( 146 Bundle.getMessage("MenuItemNewThrottleWithConnection", c.getConnectionName()), 147 c)); 148 } 149 } 150 } 151 152 throttleMenu.add(throttleConnectionMenu); 153 } 154 } 155}