001package jmri.jmrit.timetable.swing;
002
003import java.awt.*;
004import java.awt.print.*;
005
006/**
007 * Print a timetable graph.
008 * @author Dave Sand Copyright (c) 2019
009 */
010public class TimeTablePrintGraph  implements Printable {
011
012    /**
013     * Initialize the data used by the printing methods
014     * @param segmentId The segment to be displayed.  For multiple segment
015     * layouts separate graphs are required.
016     * @param scheduleId The schedule to be used for this graph.
017     * @param showTrainTimes When true, include the minutes portion of the
018     * train times at each station.
019     * @param twoPage When true, format the output for two pages.
020     */
021    public TimeTablePrintGraph(int segmentId, int scheduleId, boolean showTrainTimes, boolean twoPage) {
022        _segmentId = segmentId;
023        _scheduleId = scheduleId;
024        _showTrainTimes = showTrainTimes;
025        _twoPage = twoPage;
026    }
027
028    final int _segmentId;
029    final int _scheduleId;
030    final boolean _showTrainTimes;
031    final boolean _twoPage;
032
033    /**
034     * Setup the printer selection and start the print job.
035     */
036    void printGraph() {
037         PrinterJob job = PrinterJob.getPrinterJob();
038         job.setPrintable(this);
039         boolean ok = job.printDialog();
040         if (ok) {
041             try {
042                job.print();
043             } catch (PrinterException ex) {
044                log.warn("A printer exception occurred: ", ex);
045             }
046         }
047    }
048
049    @Override
050    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
051        int pageCount = _twoPage ? 2 : 1;
052        if (page > pageCount - 1) {     // Page number is zero based
053            return NO_SUCH_PAGE;
054        }
055        pf.setOrientation(PageFormat.LANDSCAPE);
056
057        /* User (0,0) is typically outside the imageable area, so we must
058         * translate by the X and Y values in the PageFormat to avoid clipping
059         */
060        Graphics2D g2d;
061        if (g instanceof Graphics2D) {
062            g2d = (Graphics2D) g;
063        } else {
064            throw new IllegalArgumentException();
065        }
066
067
068
069
070//         Graphics2D g2d = (Graphics2D) g;
071
072
073
074        g2d.translate(pf.getImageableX(), pf.getImageableY());
075
076        TimeTableGraphCommon graph = new TimeTableGraphCommon();
077        graph.init(_segmentId, _scheduleId, _showTrainTimes, pf.getImageableHeight(), pf.getImageableWidth() * pageCount, false);
078        if (page == 1) {
079            // Move the image left to hide the first page
080            Double w = pf.getImageableWidth();
081            g2d.translate(-w.intValue(), 0);
082        }
083        graph.doPaint(g2d);
084
085        return PAGE_EXISTS;
086    }
087
088    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TimeTablePrintGraph.class);
089}