001package jmri.jmrit.throttle;
002
003import java.beans.*;
004
005import javax.swing.JInternalFrame;
006
007import jmri.DccThrottle;
008import jmri.InstanceManager;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 *
015 * @author lionel
016 */
017public abstract class ThrottleWindowActions implements PropertyChangeListener {
018    
019    protected final ThrottleWindow tw;
020    protected ThrottlesPreferencesWindowKeyboardControls tpwkc;
021    
022    ThrottleWindowActions(ThrottleWindow tw) {
023        this.tw = tw;
024        resetTpwkc();
025    }
026        
027    private void resetTpwkc() {
028        tpwkc = InstanceManager.getDefault(ThrottlesPreferences.class).getThrottlesKeyboardControls();        
029    }
030              
031    protected void toFront(JInternalFrame jif) {
032        if (jif == null) {
033            return;
034        }
035        if (!jif.isVisible()) {
036            jif.setVisible(true);
037        }        
038        if (jif.isIcon()) {
039            try {
040                jif.setIcon(false);
041            } catch (PropertyVetoException ex) {
042                log.debug("JInternalFrame uniconify, vetoed");
043            }
044        }
045        jif.requestFocus();
046        jif.toFront();
047        try {
048            jif.setSelected(true);
049        } catch (java.beans.PropertyVetoException ex) {
050            log.debug("JInternalFrame selection, vetoed");
051        }
052    }
053    
054    protected void incrementSpeed(DccThrottle throttle, float increment) {
055        if (throttle == null) {
056            return;
057        }
058        float speed;
059        float curSpeed = throttle.getSpeedSetting();
060        if (curSpeed < 0) {
061            curSpeed = 0; // restart from 0 if was on emergency stop
062        }
063        if (tw.getCurrentThrottleFrame().getControlPanel().getDisplaySlider() == ControlPanel.SLIDERDISPLAYCONTINUOUS ) {
064            if (throttle.getIsForward()) {
065                speed = curSpeed + increment;
066                if (speed > -throttle.getSpeedIncrement()/2 && speed < throttle.getSpeedIncrement()/2 ) {
067                    speed = 0;
068                }
069                if (speed < 0) {
070                    throttle.setIsForward(false);
071                    speed = -speed;
072                }
073            } else {
074                speed = -curSpeed + increment;
075                if (speed > -throttle.getSpeedIncrement()/2 && speed < throttle.getSpeedIncrement()/2 ) {
076                    speed = 0;
077                }                
078                if (speed > 0) {
079                    throttle.setIsForward(true);
080                } else {                        
081                    speed = -speed;
082                }
083            }            
084        } else {
085            speed = curSpeed + increment;
086        }
087        if ( speed < throttle.getSpeedIncrement()/2 || speed <0 ) { // force 0 bellow minimum speed
088            speed = 0;
089        } else if (speed > 1) {
090            speed = 1;
091        }
092        throttle.setSpeedSetting( speed );               
093    }
094    
095    @Override
096    public void propertyChange(PropertyChangeEvent evt) {
097        if ((evt == null) || (evt.getPropertyName() == null)) {
098            return;
099        }
100        if (evt.getPropertyName().compareTo("ThrottlePreferences") == 0) {
101           resetTpwkc();
102        }               
103    }
104    
105    private final static Logger log = LoggerFactory.getLogger(ThrottleWindowActions.class);    
106}