001package jmri.jmrit.logix; 002 003import java.awt.Dimension; 004import java.awt.event.ActionEvent; 005import java.awt.event.WindowAdapter; 006import java.awt.event.WindowEvent; 007 008import javax.swing.ButtonGroup; 009import javax.swing.JDesktopPane; 010import javax.swing.JMenu; 011import javax.swing.JMenuBar; 012import javax.swing.JMenuItem; 013import javax.swing.JRadioButtonMenuItem; 014 015import jmri.DccThrottle; 016import jmri.InstanceManager; 017import jmri.JmriException; 018import jmri.PowerManager; 019import jmri.SpeedStepMode; 020import jmri.jmrit.throttle.FunctionButton; 021import jmri.util.JmriJFrame; 022 023/** 024 * A JFrame to contain throttle elements such as speed control, function panel. 025 * It keeps a record of the throttle commands for playback later. 026 * <p> 027 * 028 * Modeled on package jmri.jmrit.throttle by 029 * 030 * @author Glen Oberhauser 031 * @author Bob Jacobsen Copyright 2008 032 * 033 * @author Pete Cressman Copyright 2009, 2020 034 */ 035public class LearnThrottleFrame extends JmriJFrame { 036 037 private final WarrantFrame _warrantFrame; 038 private PowerManager powerMgr = null; 039 private LearnControlPanel _controlPanel; 040 private LearnFunctionPanel _functionPanel; 041 private LearnSpeedPanel _speedPanel; 042 043 /** 044 * Default constructor 045 * @param warrantFrame caller 046 */ 047 public LearnThrottleFrame(WarrantFrame warrantFrame) { 048 super(false, false); 049 _warrantFrame = warrantFrame; 050 powerMgr = InstanceManager.getNullableDefault(jmri.PowerManager.class); 051 if (powerMgr == null) { 052 log.info("No power manager instance found, panel not active"); 053 } 054 initGUI(); 055 setVisible(true); 056 } 057 058 /** 059 * Get notification that a throttle has been found as you requested. 060 * 061 * @param t An instantiation of the DccThrottle with the address requested. 062 */ 063 public void notifyThrottleFound(DccThrottle t) { 064 if (log.isDebugEnabled()) { 065 log.debug("notifyThrottleFound address= {} class= {}",t.getLocoAddress(),t.getClass().getName()); 066 } 067 _controlPanel.notifyAddressThrottleFound(t); 068 _functionPanel.notifyAddressThrottleFound(t); 069 _speedPanel.notifyAddressThrottleFound(t); 070 setSpeedSetting(0.0f); // be sure loco is stopped. 071 setButtonForward(t.getIsForward()); 072 String name = _warrantFrame.getTrainName(); 073 if (name == null || name.isEmpty()) { 074 jmri.jmrit.roster.RosterEntry re = _warrantFrame._speedUtil.getRosterEntry(); 075 if (re != null) { 076 name = re.getId(); 077 } else { 078 name = t.getLocoAddress().toString(); 079 } 080 } else { 081 name =name +" - " + t.getLocoAddress().toString(); 082 } 083 setTitle(name); 084 } 085 086 private void initGUI() { 087 setTitle(Bundle.getMessage("ThrottleTitle")); 088 this.addWindowListener(new WindowAdapter() { 089 @Override 090 public void windowClosing(WindowEvent e) { 091 _warrantFrame.close(); 092 dispose(); 093 } 094 }); 095 initializeMenu(); 096 097 _controlPanel = new LearnControlPanel(this); 098 _controlPanel.setVisible(true); 099 _controlPanel.setEnabled(false); 100 _controlPanel.setTitle(Bundle.getMessage("speed")); 101 _controlPanel.setSize(_controlPanel.getPreferredSize()); 102 103 int width = 3 * (FunctionButton.getButtonWidth()) + 2 * 3 * 5 + 11; // = 192 104 int height = 9 * (FunctionButton.getButtonHeight()) + 2 * 6 * 5 + 20; // FunctionButton.BUT_IMG_SIZE = 45 105 _functionPanel = new LearnFunctionPanel(this); 106 _functionPanel.setSize(width, height); 107 _functionPanel.setVisible(true); 108 _functionPanel.setEnabled(false); 109 _functionPanel.setTitle(Bundle.getMessage("setFunction")); 110 111 _speedPanel = new LearnSpeedPanel(_warrantFrame.getWarrant()); 112 _speedPanel.setSize(_functionPanel.getWidth(), _controlPanel.getHeight() - _functionPanel.getHeight()); 113 _speedPanel.setVisible(true); 114 _speedPanel.setClosable(true); 115 _speedPanel.setTitle(java.util.ResourceBundle.getBundle("jmri/jmrit/throttle/ThrottleBundle").getString("ThrottleMenuViewSpeedPanel")); 116 117 _controlPanel.setLocation(0, 0); 118 _functionPanel.setLocation(_controlPanel.getWidth(), 0); 119 _speedPanel.setLocation(_controlPanel.getWidth(), _functionPanel.getHeight()); 120 121 JDesktopPane desktop = new JDesktopPane(); 122 getContentPane().add(desktop); 123 desktop.add(_controlPanel); 124 desktop.add(_functionPanel); 125 desktop.add(_speedPanel); 126 127 desktop.setPreferredSize(new Dimension( 128 _controlPanel.getWidth() + _functionPanel.getWidth(), _controlPanel.getHeight())); 129 130 setResizable(false); 131 jmri.util.PlaceWindow.getDefault().nextTo(_warrantFrame, null, this); 132 pack(); 133 } 134 135 /** 136 * Set up View, Edit and Power Menus 137 */ 138 private void initializeMenu() { 139 JMenu speedControl = new JMenu(Bundle.getMessage("SpeedControl")); 140 ButtonGroup buttonGroup = new ButtonGroup(); 141 JRadioButtonMenuItem displaySlider = new JRadioButtonMenuItem(Bundle.getMessage("ButtonDisplaySpeedSlider")); 142 displaySlider.addActionListener((ActionEvent e)-> 143 _controlPanel.setSpeedController(jmri.jmrit.throttle.ControlPanel.SLIDERDISPLAYCONTINUOUS)); 144 displaySlider.setSelected(true); 145 buttonGroup.add(displaySlider); 146 speedControl.add(displaySlider); 147 JRadioButtonMenuItem displaySteps = new JRadioButtonMenuItem(Bundle.getMessage("ButtonDisplaySpeedSteps")); 148 displaySteps.addActionListener((ActionEvent e)-> 149 _controlPanel.setSpeedController(jmri.jmrit.throttle.ControlPanel.STEPDISPLAY)); 150 buttonGroup.add(displaySteps); 151 speedControl.add(displaySteps); 152 this.setJMenuBar(new JMenuBar()); 153 this.getJMenuBar().add(speedControl); 154 155 if (powerMgr != null) { 156 JMenu powerMenu = new JMenu(Bundle.getMessage("ThrottleMenuPower")); 157 JMenuItem powerOn = new JMenuItem(Bundle.getMessage("ThrottleMenuPowerOn")); 158 powerMenu.add(powerOn); 159 powerOn.addActionListener((ActionEvent e) -> { 160 try { 161 powerMgr.setPower(PowerManager.ON); 162 } catch (JmriException e1) { 163 log.error("Error when setting power", e1); 164 } 165 }); 166 167 JMenuItem powerOff = new JMenuItem(Bundle.getMessage("ThrottleMenuPowerOff")); 168 powerMenu.add(powerOff); 169 powerOff.addActionListener((ActionEvent e) -> { 170 try { 171 powerMgr.setPower(PowerManager.OFF); 172 } catch (JmriException e1) { 173 log.error("Error when setting power", e1); 174 } 175 }); 176 177 this.getJMenuBar().add(powerMenu); 178 } 179 // add help selection 180 addHelpMenu("package.jmri.jmrit.throttle.ThrottleFrame", true); 181 } 182 183 @Override 184 public void dispose() { 185 _controlPanel.destroy(); 186 _functionPanel.destroy(); 187 _speedPanel.destroy(); 188 _controlPanel.dispose(); 189 _functionPanel.dispose(); 190 super.dispose(); 191 } 192 193 /* Record throttle commands that have been sent to the throttle from ControlPanel */ 194 195 protected void setSpeedSetting(float speed) { 196 _warrantFrame.setSpeedCommand(speed); 197 } 198 199 protected void setSpeedStepMode(SpeedStepMode speedStep) { 200 _warrantFrame.setThrottleCommand("SpeedStep", speedStep.name); 201 } 202 203 protected void setFunctionState(String fNum, boolean isSet) { 204 _warrantFrame.setThrottleCommand(fNum, Boolean.toString(isSet)); 205 } 206 207 protected void setFunctionLock(String fMom, boolean isLockable) { 208 _warrantFrame.setThrottleCommand(fMom, Boolean.toString(isLockable)); 209 } 210 211 protected void setButtonForward(boolean isForward) { 212 _warrantFrame.setThrottleCommand("Forward", Boolean.toString(isForward)); 213 } 214 215 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LearnThrottleFrame.class); 216 217}