001package jmri.jmrit.operations.trains.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.rollingstock.cars.CarTypes;
015import jmri.jmrit.operations.setup.Control;
016import jmri.jmrit.operations.trains.*;
017import jmri.jmrit.operations.trains.trainbuilder.TrainCommon;
018import jmri.util.davidflanagan.HardcopyWriter;
019
020/**
021 * Action to print a summary of trains that service specific car 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 PrintTrainsByCarTypesAction extends AbstractAction {
031
032    static final String NEW_LINE = "\n"; // NOI18N
033    static final String TAB = "\t"; // NOI18N
034
035    public PrintTrainsByCarTypesAction(boolean isPreview) {
036        super(isPreview ? Bundle.getMessage("MenuItemPreviewByType") : Bundle.getMessage("MenuItemPrintByType"));
037        _isPreview = isPreview;
038    }
039
040    /**
041     * Variable to set whether this is to be printed or previewed
042     */
043    boolean _isPreview;
044    HardcopyWriter writer;
045    int max_name_length = Control.max_len_string_train_name + 1;
046
047    @Override
048    public void actionPerformed(ActionEvent e) {
049        // obtain a HardcopyWriter
050        try {
051            writer = new HardcopyWriter(new Frame(), Bundle.getMessage("TitleTrainsByType"), Control.reportFontSize, .5,
052                    .5, .5, .5,
053                    _isPreview);
054        } catch (HardcopyWriter.PrintCanceledException ex) {
055            log.debug("Print cancelled");
056            return;
057        }
058
059        // Loop through the car types showing which trains will service that car
060        // type
061        String carTypes[] = InstanceManager.getDefault(CarTypes.class).getNames();
062        List<Train> trains = InstanceManager.getDefault(TrainManager.class).getTrainsByNameList();
063
064        try {
065            // title line
066            String s = Bundle.getMessage("Type") +
067                    TAB +
068                    TrainCommon.padString(Bundle.getMessage("Trains"),
069                            max_name_length) +
070                    Bundle.getMessage("Description") +
071                    NEW_LINE;
072            writer.write(s);
073
074            for (String type : carTypes) {
075                s = type + NEW_LINE;
076                writer.write(s);
077
078                for (Train train : trains) {
079                    if (train.isTypeNameAccepted(type)) {
080                        s = TAB +
081                                TrainCommon.padString(train.getName(), max_name_length) +
082                                train.getDescription() +
083                                NEW_LINE;
084                        writer.write(s);
085                    }
086                }
087            }
088            // and force completion of the printing
089            writer.close();
090        } catch (IOException we) {
091            log.error("Error printing PrintLocationAction: {}", we.getLocalizedMessage());
092        }
093    }
094
095    private final static Logger log = LoggerFactory.getLogger(PrintTrainsByCarTypesAction.class);
096}