001package jmri.jmrit.throttle.actions;
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;
011import jmri.jmrit.throttle.ThrottleFrameManager;
012import jmri.jmrit.throttle.ThrottleWindow;
013import jmri.jmrit.throttle.implementation.ThrottleFrame;
014import jmri.jmrit.throttle.interfaces.ThrottleControllersUIContainer;
015
016/**
017 * Actions implementation for a Throttle Window
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
033public class ThrottleWindowActionsFactory extends ThrottleWindowActions {
034   
035    private final List<String> actionStrings = new ArrayList<>(Arrays.asList(
036        "accelerate", "decelerate", "accelerateMore", "decelerateMore", "idle", "stop",
037        "forward", "reverse", "switchDirection",
038        "nextJInternalFrame", "previousJInternalFrame", "showControlPanel", "showFunctionPanel", "showAddressPanel",
039        "nextThrottleFrame", "previousThrottleFrame", "nextRunningThrottleFrame", "previousRunningThrottleFrame",
040        "nextThrottleWindow", "previousThrottleWindow"
041    ) );
042
043    public ThrottleWindowActionsFactory(ThrottleControllersUIContainer tw) {
044        super(tw);
045        completeActionStrings();
046    }
047        
048    private void completeActionStrings() {
049        for (int i=0; i < tpwkc.getNbFunctionsKeys(); i++) {
050            actionStrings.add("fn_"+i+"_Pressed");
051            actionStrings.add("fn_"+i+"_Released");
052        }
053    }
054    
055    public ActionMap buildActionMap() {
056        ActionMap ret = new ActionMap();
057        
058        // Throttle commands
059        ret.put("accelerate", new AbstractAction() {
060            @Override
061            public void actionPerformed(ActionEvent e) {
062                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
063                incrementSpeed(throttle, throttle.getSpeedIncrement());
064            }            
065        });
066        ret.put("decelerate", new AbstractAction() {
067            @Override
068            public void actionPerformed(ActionEvent e) {
069                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
070                incrementSpeed(throttle, -throttle.getSpeedIncrement());
071            }            
072        });
073        ret.put("accelerateMore", new AbstractAction() {
074            @Override
075            public void actionPerformed(ActionEvent e) {
076                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
077                incrementSpeed(throttle, throttle.getSpeedIncrement()*tpwkc.getMoreSpeedMultiplier());
078            }            
079        });
080        ret.put("decelerateMore", new AbstractAction() {
081            @Override
082            public void actionPerformed(ActionEvent e) {
083                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
084                incrementSpeed(throttle, -throttle.getSpeedIncrement()*tpwkc.getMoreSpeedMultiplier());
085            }            
086        });        
087        ret.put("idle", new AbstractAction() {
088            @Override
089            public void actionPerformed(ActionEvent e) {
090                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
091                if (throttle!=null) throttle.setSpeedSetting(0);
092            }            
093        });
094        ret.put("stop", new AbstractAction() {
095            @Override
096            public void actionPerformed(ActionEvent e) {
097                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
098                if (throttle!=null) throttle.setSpeedSetting(-1);
099            }            
100        });
101        ret.put("forward", new AbstractAction() {
102            @Override
103            public void actionPerformed(ActionEvent e) {
104                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
105                if (throttle!=null) throttle.setIsForward(true);
106            }            
107        });
108        ret.put("reverse", new AbstractAction() {
109            @Override
110            public void actionPerformed(ActionEvent e) {
111                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
112                if (throttle!=null) throttle.setIsForward(false);
113            }            
114        });
115        ret.put("switchDirection", new AbstractAction() {
116            @Override
117            public void actionPerformed(ActionEvent e) {
118                DccThrottle throttle = tw.getCurentThrottleController().getThrottle();
119                if (throttle!=null) throttle.setIsForward(!throttle.getIsForward());
120            }            
121        });         
122        
123        // function buttons
124        for (int i=0; i < tpwkc.getNbFunctionsKeys(); i++) {
125            ret.put("fn_"+i+"_Pressed", new FnActionPressed(i));
126            ret.put("fn_"+i+"_Released", new FnActionReleased(i));
127        }
128        
129        // Throttle inner window cycling
130        ret.put("nextJInternalFrame", new AbstractAction() {
131            @Override
132            public void actionPerformed(ActionEvent e) {
133                if (tw.getCurentThrottleController() instanceof ThrottleFrame) {
134                    ((ThrottleFrame)tw.getCurentThrottleController()).activateNextJInternalFrame();
135                }                
136            }            
137        });
138        ret.put("previousJInternalFrame", new AbstractAction() {
139            @Override
140            public void actionPerformed(ActionEvent e) {
141                if (tw.getCurentThrottleController() instanceof ThrottleFrame) {
142                    ((ThrottleFrame)tw.getCurentThrottleController()).activatePreviousJInternalFrame();
143                }                
144            }            
145        });
146        ret.put("showControlPanel", new AbstractAction() {
147            @Override
148            public void actionPerformed(ActionEvent e) {
149                if (tw.getCurentThrottleController() instanceof ThrottleFrame) {
150                    toFront(((ThrottleFrame)tw.getCurentThrottleController()).getControlPanelJIF());
151                }
152            }            
153        });
154        ret.put("showFunctionPanel", new AbstractAction() {
155            @Override
156            public void actionPerformed(ActionEvent e) {
157                if (tw.getCurentThrottleController() instanceof ThrottleFrame) {
158                    toFront(((ThrottleFrame)tw.getCurentThrottleController()).getFunctionPanelJIF());
159                }
160            }            
161        });
162        ret.put("showAddressPanel", new AbstractAction() {
163            @Override
164            public void actionPerformed(ActionEvent e) {
165                if (tw.getCurentThrottleController() instanceof ThrottleFrame) {
166                    toFront(((ThrottleFrame)tw.getCurentThrottleController()).getAddressPanelJIF());
167                }
168            }            
169        });
170        
171        // Throttle frames control
172        ret.put("nextThrottleFrame", new AbstractAction() {
173            @Override
174            public void actionPerformed(ActionEvent e) {
175                if (tw instanceof ThrottleWindow) {
176                    ((ThrottleWindow)tw).nextThrottleFrame();
177                }
178            }            
179        });
180        ret.put("previousThrottleFrame", new AbstractAction() {
181            @Override
182            public void actionPerformed(ActionEvent e) {
183                if (tw instanceof ThrottleWindow) {
184                    ((ThrottleWindow)tw).previousThrottleFrame();
185                }
186            }            
187        }); 
188        ret.put("nextRunningThrottleFrame", new AbstractAction() {
189            @Override
190            public void actionPerformed(ActionEvent e) {
191                if (tw instanceof ThrottleWindow) {
192                    ((ThrottleWindow)tw).nextRunningThrottleFrame();
193                }
194            }            
195        });
196        ret.put("previousRunningThrottleFrame", new AbstractAction() {
197            @Override
198            public void actionPerformed(ActionEvent e) {
199                if (tw instanceof ThrottleWindow) {
200                    ((ThrottleWindow)tw).previousRunningThrottleFrame();
201                }
202            }            
203        });        
204        // Throttle windows control
205        ret.put("nextThrottleWindow", new AbstractAction() {
206            @Override
207            public void actionPerformed(ActionEvent e) {
208                InstanceManager.getDefault(ThrottleFrameManager.class).requestFocusForNextThrottleWindow();
209            }            
210        });
211        ret.put("previousThrottleWindow", new AbstractAction() {
212            @Override
213            public void actionPerformed(ActionEvent e) {
214                InstanceManager.getDefault(ThrottleFrameManager.class).requestFocusForPreviousThrottleWindow();
215            }            
216        });         
217        return ret;        
218    }
219    
220    private class FnActionPressed extends AbstractAction {
221        private final int fn;
222
223        FnActionPressed(int fn) {
224            this.fn = fn;
225        }
226        
227        @Override
228        public void actionPerformed(ActionEvent e) {
229            DccThrottle throttle = tw.getCurentThrottleController().getFunctionThrottle();
230            if ((throttle!=null) && (throttle.getFunctionMomentary(fn))) {
231                throttle.setFunction(fn, true );
232            } 
233        }                
234    }
235    
236    private class FnActionReleased extends AbstractAction {
237        private final int fn;
238
239        FnActionReleased(int fn) {
240            this.fn = fn;
241        }
242        
243        @Override
244        public void actionPerformed(ActionEvent e) {
245            DccThrottle throttle = tw.getCurentThrottleController().getFunctionThrottle();
246            if (throttle!=null) {
247                throttle.setFunction(fn, ! throttle.getFunction(fn));
248            }
249        }                
250    }
251}