001package jmri.jmrit.throttle.actions;
002
003import java.beans.*;
004
005import javax.swing.JInternalFrame;
006
007import jmri.DccThrottle;
008import jmri.InstanceManager;
009import jmri.jmrit.throttle.interfaces.ThrottleControllersUIContainer;
010import jmri.jmrit.throttle.preferences.ThrottlesPreferences;
011import jmri.jmrit.throttle.preferences.ThrottlesPreferencesWindowKeyboardControls;
012
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * An abstract class for ThrottleWindowActions, used by mousewheel and key listener
018 * 
019 * <hr>
020 * This file is part of JMRI.
021 * <p>
022 * JMRI is free software; you can redistribute it and/or modify it under the
023 * terms of version 2 of the GNU General Public License as published by the Free
024 * Software Foundation. See the "COPYING" file for a copy of this license.
025 * <p>
026 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
027 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
028 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
029 *
030 * @author Lionel Jeanson
031 * 
032 */
033
034public abstract class ThrottleWindowActions implements PropertyChangeListener {
035    
036    protected final ThrottleControllersUIContainer tw;
037    protected ThrottlesPreferencesWindowKeyboardControls tpwkc;
038    
039    ThrottleWindowActions(ThrottleControllersUIContainer tw) {
040        this.tw = tw;
041        resetTpwkc();
042    }
043        
044    private void resetTpwkc() {
045        tpwkc = InstanceManager.getDefault(ThrottlesPreferences.class).getThrottlesKeyboardControls();        
046    }
047              
048    protected void toFront(JInternalFrame jif) {
049        if (jif == null) {
050            return;
051        }
052        if (!jif.isVisible()) {
053            jif.setVisible(true);
054        }        
055        if (jif.isIcon()) {
056            try {
057                jif.setIcon(false);
058            } catch (PropertyVetoException ex) {
059                log.debug("JInternalFrame uniconify, vetoed");
060            }
061        }
062        jif.requestFocus();
063        jif.toFront();
064        try {
065            jif.setSelected(true);
066        } catch (java.beans.PropertyVetoException ex) {
067            log.debug("JInternalFrame selection, vetoed");
068        }
069    }
070    
071    protected void incrementSpeed(DccThrottle throttle, float increment) {
072        if (throttle == null) {
073            return;
074        }
075        float speed;
076        float curSpeed = throttle.getSpeedSetting();
077        if (curSpeed < 0) {
078            curSpeed = 0; // restart from 0 if was on emergency stop
079        }
080        if (tw.getCurentThrottleController().isSpeedDisplayContinuous()) {  
081            if (throttle.getIsForward()) {
082                speed = curSpeed + increment;
083                if (speed > -throttle.getSpeedIncrement()/2 && speed < throttle.getSpeedIncrement()/2 ) {
084                    speed = 0;
085                }
086                if (speed < 0) {
087                    throttle.setIsForward(false);
088                    speed = -speed;
089                }
090            } else {
091                speed = -curSpeed + increment;
092                if (speed > -throttle.getSpeedIncrement()/2 && speed < throttle.getSpeedIncrement()/2 ) {
093                    speed = 0;
094                }                
095                if (speed > 0) {
096                    throttle.setIsForward(true);
097                } else {                        
098                    speed = -speed;
099                }
100            }            
101        } else {
102            speed = curSpeed + increment;
103        }
104        if ( speed < throttle.getSpeedIncrement()/2 || speed <0 ) { // force 0 bellow minimum speed
105            speed = 0;
106        } else if (speed > 1) {
107            speed = 1;
108        }
109        throttle.setSpeedSetting( speed );               
110    }
111    
112    @Override
113    public void propertyChange(PropertyChangeEvent evt) {
114        if ((evt == null) || (evt.getPropertyName() == null)) {
115            return;
116        }
117        if (evt.getPropertyName().compareTo("ThrottlePreferences") == 0) {
118           resetTpwkc();
119        }               
120    }
121    
122    private static final Logger log = LoggerFactory.getLogger(ThrottleWindowActions.class);    
123}