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