001package jmri.jmrit.operations.trains.tools;
002
003import java.awt.Frame;
004import java.io.IOException;
005
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009import jmri.InstanceManager;
010import jmri.jmrit.operations.rollingstock.cars.*;
011import jmri.jmrit.operations.routes.RouteLocation;
012import jmri.jmrit.operations.setup.Control;
013import jmri.jmrit.operations.trains.Train;
014import jmri.jmrit.operations.trains.TrainCommon;
015import jmri.util.davidflanagan.HardcopyWriter;
016
017/**
018 * Print the cars in the train.
019 * <p>
020 * This uses the older style printing, for compatibility with Java 1.1.8 in
021 * Macintosh MRJ
022 *
023 * @author Daniel Boudreau Copyright (C) 2025
024 */
025public class PrintShowCarsInTrain {
026
027    static final String NEW_LINE = "\n"; // NOI18N
028
029    CarManager carManager = InstanceManager.getDefault(CarManager.class);
030    static int fieldSize =
031            InstanceManager.getDefault(CarRoads.class).getMaxNameLength() + Control.max_len_string_road_number;
032    static final String TAB = padString("", fieldSize);
033
034    public PrintShowCarsInTrain(Train train, boolean isPreview) {
035        super();
036        printCarsInTrain(train, isPreview);
037    }
038
039    private void printCarsInTrain(Train train, boolean isPreview) {
040        if (train.isBuilt()) {
041            // obtain a HardcopyWriter to do this
042            try (HardcopyWriter writer =
043                    new HardcopyWriter(new Frame(), Bundle.getMessage("TitleShowCarsInTrain", train.getName()),
044                            Control.reportFontSize, .5,
045                            .5, .5, .5, isPreview);) {
046
047                printCarsAtLocation(writer, train, train.getCurrentRouteLocation());
048
049            } catch (HardcopyWriter.PrintCanceledException ex) {
050                log.debug("Print cancelled");
051            } catch (IOException ex) {
052                log.error("Error printing car roster: {}", ex.getLocalizedMessage());
053            }
054        }
055    }
056
057    private void printCarsAtLocation(HardcopyWriter writer, Train train, RouteLocation rl) throws IOException {
058        if (rl != null) {
059            // print location name followed by header
060            writer.write(rl.getName() + NEW_LINE);
061            writer.write(getHeader());
062            for (RouteLocation rld : train.getRoute().getLocationsBySequenceList()) {
063                for (Car car : carManager.getByTrainDestinationList(train)) {
064                    if (TrainCommon.isNextCar(car, rl, rld, true)) {
065                        log.debug("car ({}) routelocation ({}) track ({}) route destination ({})",
066                                car.toString(), car
067                                        .getRouteLocation().getName(),
068                                car.getTrackName(), car.getRouteDestination().getName());
069                        String s = car.getRoadName().split(TrainCommon.HYPHEN)[0] +
070                                " " +
071                                TrainCommon.splitString(car.getNumber()) +
072                                NEW_LINE;
073                        if (car.getRouteDestination() == rl) {
074                            writer.write(TAB + TAB + s); // set out
075                        } else if (car.getRouteLocation() == rl && car.getTrack() != null) {
076                            writer.write(s); // pick up
077                        } else {
078                            writer.write(TAB + s); // in train
079                        }
080                    }
081                }
082            }
083        }
084    }
085
086    private String getHeader() {
087        int fieldSize =
088                InstanceManager.getDefault(CarRoads.class).getMaxNameLength() + Control.max_len_string_road_number;
089        String header = padString(Bundle.getMessage("Pickup"), fieldSize) +
090                padString(Bundle.getMessage("InTrain"), fieldSize) +
091                padString(Bundle.getMessage("SetOut"), fieldSize) +
092                NEW_LINE;
093        return header;
094    }
095
096    private static String padString(String s, int fieldSize) {
097        return TrainCommon.padString(s, fieldSize);
098    }
099
100    private final static Logger log = LoggerFactory.getLogger(PrintShowCarsInTrain.class);
101}