001package jmri.jmrix.nce.consist;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import java.util.List;
006import javax.swing.AbstractAction;
007import javax.swing.ImageIcon;
008import javax.swing.JLabel;
009import jmri.InstanceManager;
010import jmri.util.FileUtil;
011import jmri.util.davidflanagan.HardcopyWriter;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Action to print a summary of the Roster contents
017 * <p>
018 * This uses the older style printing, for compatibility with Java 1.1.8 in
019 * Macintosh MRJ
020 *
021 * @author Bob Jacobsen Copyright (C) 2003
022 * @author Dennis Miller Copyright (C) 2005
023 * @author Daniel Boudreau Copyright (C) 2008
024 */
025public class PrintNceConsistRosterAction extends AbstractAction {
026
027    public PrintNceConsistRosterAction(String actionName, Frame frame, boolean preview) {
028        super(actionName);
029        mFrame = frame;
030        isPreview = preview;
031    }
032
033    /**
034     * Frame hosting the printing
035     */
036    Frame mFrame;
037    /**
038     * Variable to set whether this is to be printed or previewed
039     */
040    boolean isPreview;
041
042    @Override
043    public void actionPerformed(ActionEvent e) {
044
045        // obtain a HardcopyWriter to do this
046        HardcopyWriter writer = null;
047        try {
048            writer = new HardcopyWriter(mFrame, Bundle.getMessage("NcePrintRosterTitle"), 10, .5, .5, .5, .5, isPreview);
049        } catch (HardcopyWriter.PrintCanceledException ex) {
050            log.debug("Print cancelled");
051            return;
052        }
053
054        // add the image
055        ImageIcon icon = new ImageIcon(FileUtil.findURL("resources/decoderpro.gif", FileUtil.Location.INSTALLED));
056        // we use an ImageIcon because it's guaranteed to have been loaded when ctor is complete
057        writer.write(icon.getImage(), new JLabel(icon));
058
059        // Loop through the Roster, printing as needed
060        NceConsistRoster r = InstanceManager.getDefault(NceConsistRoster.class);
061        List<NceConsistRosterEntry> list = r.matchingList(null, null, null, null, null, null, null, null, null, null); // take all
062
063        log.debug("Roster list size: {}", list.size());
064        for (NceConsistRosterEntry entry : list) {
065            entry.printEntry(writer);
066        }
067
068        // and force completion of the printing
069        writer.close();
070    }
071
072    private final static Logger log = LoggerFactory.getLogger(PrintNceConsistRosterAction.class);
073
074}