001package jmri.jmrit.decoderdefn;
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.Version;
011import jmri.util.FileUtil;
012import jmri.util.davidflanagan.HardcopyWriter;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Action to print a summary of available decoder definitions
018 * <p>
019 * This uses the older style printing, for compatibility with Java 1.1.8 in
020 * Macintosh MRJ
021 *
022 * @author Bob Jacobsen Copyright (C) 2003
023 * @author Dennis Miller Copyright (C) 2005
024 */
025public class PrintDecoderListAction extends AbstractAction {
026
027    public PrintDecoderListAction(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, "DecoderPro V" + Version.name() + " Decoder Definitions", 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 decoder index, printing as needed
060        String lastMfg = "";
061        String lastFamily = "";
062
063        DecoderIndexFile f = InstanceManager.getDefault(DecoderIndexFile.class);
064        List<DecoderFile> l = f.matchingDecoderList(null, null, null, null, null, null); // take all
065        int i = -1;
066        log.debug("Roster list size: {}", l.size());
067        for (i = 0; i < l.size(); i++) {
068            DecoderFile d = l.get(i);
069            if (!d.getMfg().equals(lastMfg)) {
070                printMfg(d, writer);
071                lastMfg = d.getMfg();
072                lastFamily = "";
073            }
074            if (!d.getFamily().equals(lastFamily)) {
075                printFamily(d, writer);
076                lastFamily = d.getFamily();
077            }
078            if (!d.getFamily().equals(d.getModel())) {
079                printEntry(d, writer);
080            }
081        }
082
083        // and force completion of the printing
084        writer.close();
085    }
086
087    void printEntry(DecoderFile d, HardcopyWriter w) {
088        try {
089            String s = "\n                       " + d.getModel();
090            w.write(s, 0, s.length());
091        } catch (java.io.IOException e) {
092            log.error("Error printing", e);
093        }
094    }
095
096    void printMfg(DecoderFile d, HardcopyWriter w) {
097        try {
098            String s = "\n\n" + d.getMfg();
099            w.write(s, 0, s.length());
100        } catch (java.io.IOException e) {
101            log.error("Error printing", e);
102        }
103    }
104
105    void printFamily(DecoderFile d, HardcopyWriter w) {
106        try {
107            String s = "\n           " + d.getFamily();
108            w.write(s, 0, s.length());
109        } catch (java.io.IOException e) {
110            log.error("Error printing", e);
111        }
112    }
113
114    private final static Logger log = LoggerFactory.getLogger(PrintDecoderListAction.class);
115}