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