001package jmri.jmrit.operations.automation.actions;
002
003import jmri.InstanceManager;
004import jmri.jmrit.operations.locations.Location;
005import jmri.jmrit.operations.locations.LocationManager;
006import jmri.jmrit.operations.trains.Train;
007import jmri.jmrit.operations.trains.TrainCsvSwitchLists;
008import jmri.jmrit.operations.trains.TrainManager;
009import jmri.jmrit.operations.trains.TrainSwitchLists;
010
011public class PrintSwitchListChangesAction extends Action {
012
013    private static final int _code = ActionCodes.PRINT_SWITCHLIST_CHANGES;
014    protected static final boolean IS_CHANGED = true;
015
016    @Override
017    public int getCode() {
018        return _code;
019    }
020
021    @Override
022    public String getName() {
023        if (InstanceManager.getDefault(TrainManager.class).isPrintPreviewEnabled()) {
024            return Bundle.getMessage("PreviewSwitchListChanges");
025        } else {
026            return Bundle.getMessage("PrintSwitchListChanges");
027        }
028    }
029
030    @Override
031    public void doAction() {
032        doAction(IS_CHANGED);
033    }
034
035    protected void doAction(boolean isChanged) {
036        if (getAutomationItem() != null) {
037            setRunning(true);
038            TrainSwitchLists trainSwitchLists = new TrainSwitchLists();
039            TrainCsvSwitchLists trainCsvSwitchLists = new TrainCsvSwitchLists();
040            for (Location location : InstanceManager.getDefault(LocationManager.class).getUniqueLocationsByNameList()) {
041                if (location.isSwitchListEnabled() &&
042                        (!isChanged || location.getStatus().equals(Location.MODIFIED))) {
043                    // also build the CSV switch lists
044                    trainCsvSwitchLists.buildSwitchList(location);
045                    trainSwitchLists.buildSwitchList(location);
046                    trainSwitchLists.printSwitchList(location,
047                            InstanceManager.getDefault(TrainManager.class).isPrintPreviewEnabled());
048                }
049            }
050            // set trains switch lists printed
051            InstanceManager.getDefault(TrainManager.class).setTrainsSwitchListStatus(Train.PRINTED);
052        }
053        finishAction(true);
054    }
055
056    @Override
057    public void cancelAction() {
058        // no cancel for this action
059    }
060}