001package jmri.jmrit.operations.automation.actions;
002
003import jmri.jmrit.operations.routes.RouteLocation;
004import jmri.jmrit.operations.trains.Train;
005
006public class IsTrainEnRouteAction extends Action {
007
008    private static final int _code = ActionCodes.IS_TRAIN_EN_ROUTE;
009
010    @Override
011    public int getCode() {
012        return _code;
013    }
014
015    @Override
016    public String getName() {
017        return Bundle.getMessage("IsTrainEnRoute");
018    }
019
020    /**
021     * Used to determine if train is en-route. Returns true if train is built
022     * and hasn't reached the selected route location. If no route location has
023     * been entered, return train's en-route status.
024     */
025    @Override
026    public void doAction() {
027        if (getAutomationItem() != null) {
028            Train train = getAutomationItem().getTrain();
029            if (train == null || !train.isBuilt() || train.getRoute() == null) {
030                finishAction(false);
031            } else {
032                RouteLocation rl = getAutomationItem().getRouteLocation();
033                if (rl == null) {
034                    finishAction(train.isTrainEnRoute());
035                    return;
036                }
037                for (RouteLocation routeLocation : train.getRoute().getLocationsBySequenceList()) {
038                    if (routeLocation == rl) {
039                        finishAction(false);
040                        break;
041                    }
042                    if (train.getCurrentRouteLocation() == routeLocation) {
043                        finishAction(true);
044                        break;
045                    }
046                }
047            }
048        }
049    }
050
051    @Override
052    public void cancelAction() {
053        // no cancel for this action     
054    }
055
056}