001package jmri.jmrit.operations.routes.tools;
002
003import java.awt.Frame;
004import java.io.IOException;
005import java.util.List;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import jmri.InstanceManager;
011import jmri.jmrit.operations.routes.*;
012import jmri.jmrit.operations.setup.Control;
013import jmri.jmrit.operations.trains.TrainCommon;
014import jmri.util.davidflanagan.HardcopyWriter;
015
016/**
017 * Prints a summary of a route or all routes.
018 * <p>
019 * This uses the older style printing, for compatibility with Java 1.1.8 in
020 * Macintosh MRJ
021 *
022 * @author Bob Jacobsen Copyright (C) 2003
023 * @author Dennis Miller Copyright (C) 2005
024 * @author Daniel Boudreau Copyright (C) 2009, 2012, 2023
025 */
026public class PrintRoutes {
027
028    static final String NEW_LINE = "\n"; // NOI18N
029    static final String TAB = "\t"; // NOI18N
030    static final String SPACE = " ";
031    private static final char FORM_FEED = '\f';
032
033    private static final int MAX_NAME_LENGTH = Control.max_len_string_location_name - 5;
034
035    boolean _isPreview;
036
037    /**
038     * Prints or previews a summary of all routes
039     * 
040     * @param isPreview true if print preview
041     */
042    public PrintRoutes(boolean isPreview) {
043        _isPreview = isPreview;
044        printRoutes();
045    }
046
047    /**
048     * Prints or previews a summary of a route
049     * 
050     * @param isPreview true if print preview
051     * @param route     The route to be printed
052     */
053    public PrintRoutes(boolean isPreview, Route route) {
054        _isPreview = isPreview;
055        printRoute(route);
056    }
057
058    private void printRoutes() {
059        // obtain a HardcopyWriter to do this
060        try (HardcopyWriter writer = new HardcopyWriter(new Frame(), Bundle.getMessage("TitleRoutesTable"),
061                Control.reportFontSize, .5, .5, .5, .5, _isPreview)) {
062
063            writer.write(SPACE); // prevents exception when using Preview and no routes
064            List<Route> routes = InstanceManager.getDefault(RouteManager.class).getRoutesByNameList();
065            for (int i = 0; i < routes.size(); i++) {
066                Route route = routes.get(i);
067                writer.write(route.getName() + NEW_LINE);
068                printRoute(writer, route);
069                if (i != routes.size() - 1) {
070                    writer.write(FORM_FEED);
071                }
072            }
073        } catch (HardcopyWriter.PrintCanceledException ex) {
074            log.debug("Print cancelled");
075        } catch (IOException e1) {
076            log.error("Exception in print routes: {}", e1.getLocalizedMessage());
077        }
078    }
079
080    private void printRoute(Route route) {
081        if (route == null) {
082            return;
083        }
084        // obtain a HardcopyWriter to do this
085        try (HardcopyWriter writer = new HardcopyWriter(new Frame(), Bundle.getMessage("TitleRoute", route.getName()),
086                Control.reportFontSize, .5, .5, .5, .5, _isPreview)) {
087
088            printRoute(writer, route);
089        } catch (HardcopyWriter.PrintCanceledException ex) {
090            log.debug("Print cancelled");
091        } catch (IOException e1) {
092            log.error("Exception in print routes: {}", e1.getLocalizedMessage());
093        }
094    }
095
096    private void printRoute(HardcopyWriter writer, Route route) throws IOException {
097        writer.write(route.getComment() + NEW_LINE);
098        String s = Bundle.getMessage("Location") +
099                TAB +
100                "    " +
101                Bundle.getMessage("Direction") +
102                SPACE +
103                Bundle.getMessage("MaxMoves") +
104                SPACE +
105                Bundle.getMessage("Pickups") +
106                SPACE +
107                Bundle.getMessage("Drops") +
108                SPACE +
109                Bundle.getMessage("Travel") +
110                TAB +
111                Bundle.getMessage("Length") +
112                TAB +
113                Bundle.getMessage("Grade") +
114                TAB +
115                Bundle.getMessage("X") +
116                "    " +
117                Bundle.getMessage("Y") +
118                NEW_LINE;
119        writer.write(s);
120        List<RouteLocation> routeList = route.getLocationsBySequenceList();
121        for (RouteLocation rl : routeList) {
122            String name = rl.getName();
123            name = padAndTruncate(name);
124            String pad = SPACE;
125            if (rl.getTrainIconX() < 10) {
126                pad = "    ";
127            } else if (rl.getTrainIconX() < 100) {
128                pad = "   ";
129            } else if (rl.getTrainIconX() < 1000) {
130                pad = "  ";
131            }
132            s = name +
133                    TAB +
134                    rl.getTrainDirectionString() +
135                    TAB +
136                    rl.getMaxCarMoves() +
137                    TAB +
138                    (rl.isPickUpAllowed() ? Bundle.getMessage("yes") : Bundle.getMessage("no")) +
139                    TAB +
140                    (rl.isDropAllowed() ? Bundle.getMessage("yes") : Bundle.getMessage("no")) +
141                    TAB +
142                    rl.getWait() +
143                    TAB +
144                    rl.getMaxTrainLength() +
145                    TAB +
146                    rl.getGrade() +
147                    TAB +
148                    rl.getTrainIconX() +
149                    pad +
150                    rl.getTrainIconY() +
151                    NEW_LINE;
152            writer.write(s);
153        }
154        s = NEW_LINE +
155                Bundle.getMessage("Location") +
156                TAB +
157                Bundle.getMessage("DepartTime") +
158                TAB +
159                Bundle.getMessage("Comment") +
160                NEW_LINE;
161        writer.write(s);
162        for (RouteLocation rl : routeList) {
163            String name = rl.getName();
164            name = padAndTruncate(name);
165            s = name + TAB + rl.getDepartureTime() + TAB + rl.getComment() + NEW_LINE;
166            writer.write(s);
167        }
168    }
169
170    private String padAndTruncate(String string) {
171        return TrainCommon.padAndTruncate(string, MAX_NAME_LENGTH);
172    }
173
174    private final static Logger log = LoggerFactory.getLogger(PrintRoutes.class);
175}