001package jmri.jmrit.operations.locations.tools;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import java.io.IOException;
006import java.util.List;
007
008import javax.swing.AbstractAction;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013import jmri.InstanceManager;
014import jmri.jmrit.operations.locations.Location;
015import jmri.jmrit.operations.locations.LocationManager;
016import jmri.jmrit.operations.locations.Track;
017import jmri.jmrit.operations.rollingstock.cars.CarTypes;
018import jmri.jmrit.operations.setup.Control;
019import jmri.util.davidflanagan.HardcopyWriter;
020
021/**
022 * Action to print a summary of locations and tracks that service specific car
023 * types.
024 * <p>
025 * This uses the older style printing, for compatibility with Java 1.1.8 in
026 * Macintosh MRJ
027 *
028 * @author Bob Jacobsen Copyright (C) 2003
029 * @author Dennis Miller Copyright (C) 2005
030 * @author Daniel Boudreau Copyright (C) 2010
031 */
032public class PrintLocationsByCarTypesAction extends AbstractAction {
033
034    static final String NEW_LINE = "\n"; // NOI18N
035    static final String TAB = "\t"; // NOI18N
036
037    public PrintLocationsByCarTypesAction(boolean preview) {
038        super(preview ? Bundle.getMessage("MenuItemPreviewByType") : Bundle.getMessage("MenuItemPrintByType"));
039        isPreview = preview;
040    }
041
042    /**
043     * Variable to set whether this is to be printed or previewed
044     */
045    boolean isPreview;
046
047    @Override
048    public void actionPerformed(ActionEvent e) {
049        print();
050    }
051
052    private void print() {
053        // obtain a HardcopyWriter
054        try ( HardcopyWriter writer = new HardcopyWriter(new Frame(), Bundle.getMessage("TitleLocationsByType"),
055            Control.reportFontSize, .5, .5, .5, .5, isPreview); ) {
056            
057            // Loop through the car types showing which locations and tracks will
058            // service that car type
059            String carTypes[] = InstanceManager.getDefault(CarTypes.class).getNames();
060            
061            List<Location> locations = InstanceManager.getDefault(LocationManager.class).getLocationsByNameList();
062
063            // title line
064            String s = Bundle.getMessage(
065                    "Type") + TAB + Bundle.getMessage("Location") + TAB + Bundle.getMessage("Track") + NEW_LINE;
066            writer.write(s);
067            // car types
068            for (String type : carTypes) {
069                s = type + NEW_LINE;
070                writer.write(s);
071                // locations
072                for (Location location : locations) {
073                    if (location.acceptsTypeName(type)) {
074                        s = TAB + location.getName() + NEW_LINE;
075                        writer.write(s);
076                        // tracks
077                        List<Track> tracks = location.getTracksByNameList(null);
078                        for (Track track : tracks) {
079                            if (track.isTypeNameAccepted(type)) {
080                                s = TAB + TAB + TAB + track.getName() + NEW_LINE;
081                                writer.write(s);
082                            }
083                        }
084                    }
085                }
086            }
087            // and force completion of the printing
088//            writer.close(); not needed when using try / catch
089        } catch (HardcopyWriter.PrintCanceledException we) {
090            log.debug("Print cancelled");
091        } catch (IOException we) {
092            log.error("Error printing PrintLocationAction", we);
093        }
094    }
095
096    private final static Logger log = LoggerFactory.getLogger(PrintLocationsByCarTypesAction.class);
097}