001package jmri.jmrit.throttle;
002
003import java.awt.event.ActionEvent;
004import java.util.*;
005
006import javax.swing.AbstractAction;
007import javax.swing.ActionMap;
008
009import jmri.DccThrottle;
010import jmri.InstanceManager;
011
012
013/**
014 *
015 * @author Lionel Jeanson
016 */
017public class ThrottleWindowActionsFactory extends ThrottleWindowActions {
018   
019    private final List<String> actionStrings = new ArrayList<>(Arrays.asList(
020        "accelerate", "decelerate", "accelerateMore", "decelerateMore", "idle", "stop",
021        "forward", "reverse", "switchDirection",
022        "nextJInternalFrame", "previousJInternalFrame", "showControlPanel", "showFunctionPanel", "showAddressPanel",
023        "nextThrottleFrame", "previousThrottleFrame", "nextRunningThrottleFrame", "previousRunningThrottleFrame",
024        "nextThrottleWindow", "previousThrottleWindow"
025    ) );
026
027    public ThrottleWindowActionsFactory(ThrottleWindow tw) {
028        super(tw);
029        completeActionStrings();
030    }
031        
032    private void completeActionStrings() {
033        for (int i=0; i < tpwkc.getNbFunctionsKeys(); i++) {
034            actionStrings.add("fn_"+i+"_Pressed");
035            actionStrings.add("fn_"+i+"_Released");
036        }
037    }
038    
039    public ActionMap buildActionMap() {
040        ActionMap ret = new ActionMap();
041        
042        // Throttle commands
043        ret.put("accelerate", new AbstractAction() {
044            @Override
045            public void actionPerformed(ActionEvent e) {
046                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
047                incrementSpeed(throttle, throttle.getSpeedIncrement());
048            }            
049        });
050        ret.put("decelerate", new AbstractAction() {
051            @Override
052            public void actionPerformed(ActionEvent e) {
053                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
054                incrementSpeed(throttle, -throttle.getSpeedIncrement());
055            }            
056        });
057        ret.put("accelerateMore", new AbstractAction() {
058            @Override
059            public void actionPerformed(ActionEvent e) {
060                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
061                incrementSpeed(throttle, throttle.getSpeedIncrement()*tpwkc.getMoreSpeedMultiplier());
062            }            
063        });
064        ret.put("decelerateMore", new AbstractAction() {
065            @Override
066            public void actionPerformed(ActionEvent e) {
067                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
068                incrementSpeed(throttle, -throttle.getSpeedIncrement()*tpwkc.getMoreSpeedMultiplier());
069            }            
070        });        
071        ret.put("idle", new AbstractAction() {
072            @Override
073            public void actionPerformed(ActionEvent e) {
074                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
075                if (throttle!=null) throttle.setSpeedSetting(0);
076            }            
077        });
078        ret.put("stop", new AbstractAction() {
079            @Override
080            public void actionPerformed(ActionEvent e) {
081                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
082                if (throttle!=null) throttle.setSpeedSetting(-1);
083            }            
084        });
085        ret.put("forward", new AbstractAction() {
086            @Override
087            public void actionPerformed(ActionEvent e) {
088                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
089                if (throttle!=null) throttle.setIsForward(true);
090            }            
091        });
092        ret.put("reverse", new AbstractAction() {
093            @Override
094            public void actionPerformed(ActionEvent e) {
095                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
096                if (throttle!=null) throttle.setIsForward(false);
097            }            
098        });
099        ret.put("switchDirection", new AbstractAction() {
100            @Override
101            public void actionPerformed(ActionEvent e) {
102                DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getThrottle();
103                if (throttle!=null) throttle.setIsForward(!throttle.getIsForward());
104            }            
105        });         
106        
107        // function buttons
108        for (int i=0; i < tpwkc.getNbFunctionsKeys(); i++) {
109            ret.put("fn_"+i+"_Pressed", new FnActionPressed(i));
110            ret.put("fn_"+i+"_Released", new FnActionReleased(i));
111        }
112        
113        // Throttle inner window cycling
114        ret.put("nextJInternalFrame", new AbstractAction() {
115            @Override
116            public void actionPerformed(ActionEvent e) {
117                tw.getCurrentThrottleFrame().activateNextJInternalFrame();
118            }            
119        });
120        ret.put("previousJInternalFrame", new AbstractAction() {
121            @Override
122            public void actionPerformed(ActionEvent e) {
123                tw.getCurrentThrottleFrame().activatePreviousJInternalFrame();
124            }            
125        });
126        ret.put("showControlPanel", new AbstractAction() {
127            @Override
128            public void actionPerformed(ActionEvent e) {
129               toFront(tw.getCurrentThrottleFrame().getControlPanel());
130            }            
131        });
132        ret.put("showFunctionPanel", new AbstractAction() {
133            @Override
134            public void actionPerformed(ActionEvent e) {
135               toFront(tw.getCurrentThrottleFrame().getFunctionPanel());
136            }            
137        });
138        ret.put("showAddressPanel", new AbstractAction() {
139            @Override
140            public void actionPerformed(ActionEvent e) {
141                toFront(tw.getCurrentThrottleFrame().getAddressPanel());
142            }            
143        });
144        
145        // Throttle frames control
146        ret.put("nextThrottleFrame", new AbstractAction() {
147            @Override
148            public void actionPerformed(ActionEvent e) {
149                tw.nextThrottleFrame();
150            }            
151        });
152        ret.put("previousThrottleFrame", new AbstractAction() {
153            @Override
154            public void actionPerformed(ActionEvent e) {
155                tw.previousThrottleFrame();
156            }            
157        }); 
158        ret.put("nextRunningThrottleFrame", new AbstractAction() {
159            @Override
160            public void actionPerformed(ActionEvent e) {
161                tw.nextRunningThrottleFrame();
162            }            
163        });
164        ret.put("previousRunningThrottleFrame", new AbstractAction() {
165            @Override
166            public void actionPerformed(ActionEvent e) {
167                tw.previousRunningThrottleFrame();
168            }            
169        });        
170        // Throttle windows control
171        ret.put("nextThrottleWindow", new AbstractAction() {
172            @Override
173            public void actionPerformed(ActionEvent e) {
174                InstanceManager.getDefault(ThrottleFrameManager.class).requestFocusForNextThrottleWindow();
175            }            
176        });
177        ret.put("previousThrottleWindow", new AbstractAction() {
178            @Override
179            public void actionPerformed(ActionEvent e) {
180                InstanceManager.getDefault(ThrottleFrameManager.class).requestFocusForPreviousThrottleWindow();
181            }            
182        });         
183        return ret;        
184    }
185    
186    private class FnActionPressed extends AbstractAction {
187        private final int fn;
188
189        FnActionPressed(int fn) {
190            this.fn = fn;
191        }
192        
193        @Override
194        public void actionPerformed(ActionEvent e) {
195            DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getFunctionThrottle();
196            if ((throttle!=null) && (throttle.getFunctionMomentary(fn) || ( !tw.getCurrentThrottleFrame().getFunctionPanel().getFunctionButtons()[fn].getIsLockable()))) {
197                throttle.setFunction(fn, true );
198            } 
199        }                
200    }
201    
202    private class FnActionReleased extends AbstractAction {
203        private final int fn;
204
205        FnActionReleased(int fn) {
206            this.fn = fn;
207        }
208        
209        @Override
210        public void actionPerformed(ActionEvent e) {
211            DccThrottle throttle = tw.getCurrentThrottleFrame().getAddressPanel().getFunctionThrottle();
212            if (throttle!=null) throttle.setFunction(fn, ! throttle.getFunction(fn));
213        }                
214    }
215}