001package jmri.jmrit.decoderdefn;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import java.util.List;
006import javax.swing.AbstractAction;
007import jmri.InstanceManager;
008import jmri.Version;
009import jmri.util.davidflanagan.HardcopyWriter;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Action to print a summary of available decoder definitions
015 * <p>
016 * This uses the older style printing, for compatibility with Java 1.1.8 in
017 * Macintosh MRJ
018 *
019 * @author Bob Jacobsen Copyright (C) 2003
020 * @author Dennis Miller Copyright (C) 2005
021 */
022public class PrintDecoderListAction extends AbstractAction {
023
024    public PrintDecoderListAction(String actionName, Frame frame, boolean preview) {
025        super(actionName);
026        mFrame = frame;
027        isPreview = preview;
028    }
029
030    /**
031     * Frame hosting the printing
032     */
033    Frame mFrame;
034    /**
035     * Variable to set whether this is to be printed or previewed
036     */
037    boolean isPreview;
038
039    @Override
040    public void actionPerformed(ActionEvent e) {
041
042        // obtain a HardcopyWriter to do this
043        HardcopyWriter writer = null;
044        try {
045            writer = new HardcopyWriter(mFrame, "DecoderPro V" + Version.name() + " Decoder Definitions", 10, .5, .5, .5, .5, isPreview);
046        } catch (HardcopyWriter.PrintCanceledException ex) {
047            log.debug("Print cancelled");
048            return;
049        }
050
051        // add the icon
052        writer.writeDecoderProIcon(true);
053
054        // Loop through the decoder index, printing as needed
055        String lastMfg = "";
056        String lastFamily = "";
057
058        DecoderIndexFile f = InstanceManager.getDefault(DecoderIndexFile.class);
059        List<DecoderFile> l = f.matchingDecoderList(null, null, null, null, null, null); // take all
060        int i = -1;
061        log.debug("Roster list size: {}", l.size());
062        for (i = 0; i < l.size(); i++) {
063            DecoderFile d = l.get(i);
064            if (!d.getMfg().equals(lastMfg)) {
065                printMfg(d, writer);
066                lastMfg = d.getMfg();
067                lastFamily = "";
068            }
069            if (!d.getFamily().equals(lastFamily)) {
070                printFamily(d, writer);
071                lastFamily = d.getFamily();
072            }
073            if (!d.getFamily().equals(d.getModel())) {
074                printEntry(d, writer);
075            }
076        }
077
078        // and force completion of the printing
079        writer.close();
080    }
081
082    void printEntry(DecoderFile d, HardcopyWriter w) {
083        try {
084            String s = "\n                       " + d.getModel();
085            w.write(s, 0, s.length());
086        } catch (java.io.IOException e) {
087            log.error("Error printing", e);
088        }
089    }
090
091    void printMfg(DecoderFile d, HardcopyWriter w) {
092        try {
093            String s = "\n\n" + d.getMfg();
094            w.write(s, 0, s.length());
095        } catch (java.io.IOException e) {
096            log.error("Error printing", e);
097        }
098    }
099
100    void printFamily(DecoderFile d, HardcopyWriter w) {
101        try {
102            String s = "\n           " + d.getFamily();
103            w.write(s, 0, s.length());
104        } catch (java.io.IOException e) {
105            log.error("Error printing", e);
106        }
107    }
108
109    private final static Logger log = LoggerFactory.getLogger(PrintDecoderListAction.class);
110}