001package jmri.jmrit.roster;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import java.util.List;
006import jmri.beans.BeanUtil;
007import jmri.jmrit.roster.rostergroup.RosterGroupSelector;
008import jmri.util.davidflanagan.HardcopyWriter;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Action to print a summary of the Roster contents.
014 * <p>
015 * This uses the older style printing, for compatibility with Java 1.1.8 in
016 * Macintosh MRJ
017 *
018 * @author Bob Jacobsen Copyright (C) 2003
019 * @author Dennis Miller Copyright (C) 2005
020 */
021public class PrintRosterAction extends jmri.util.swing.JmriAbstractAction {
022
023    public PrintRosterAction(String s, jmri.util.swing.WindowInterface wi) {
024        super(s, wi);
025        isPreview = true;
026    }
027
028    public PrintRosterAction(String s, javax.swing.Icon i, jmri.util.swing.WindowInterface wi) {
029        super(s, i, wi);
030        isPreview = true;
031    }
032
033    public PrintRosterAction(String actionName, Frame frame, boolean preview) {
034        super(actionName);
035        mFrame = frame;
036        isPreview = preview;
037    }
038
039    public void setPreview(boolean preview) {
040        isPreview = preview;
041    }
042
043    /**
044     * Frame hosting the printing
045     */
046    Frame mFrame = new Frame();
047
048    /**
049     * Variable to set whether this is to be printed or previewed
050     */
051    boolean isPreview;
052
053    @Override
054    public void actionPerformed(ActionEvent e) {
055        // obtain a HardcopyWriter to do this
056        Roster r = Roster.getDefault();
057        String title = Bundle.getMessage("TitleDecoderProRoster");
058        String rosterGroup = r.getDefaultRosterGroup();
059        // rosterGroup may legitimately be null
060        // but getProperty returns null if the property cannot be found, so
061        // we test that the property exists before attempting to get its value
062        if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) {
063            rosterGroup = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP);
064        }
065        if (rosterGroup == null) {
066            title = title + " " + Bundle.getMessage("ALLENTRIES");
067        } else {
068            title = title + " " + Bundle.getMessage("TitleGroup") + " " + Bundle.getMessage("TitleEntries", rosterGroup);
069        }
070        HardcopyWriter writer;
071        try {
072            writer = new HardcopyWriter(mFrame, title, 10, .5, .5, .5, .5, isPreview);
073        } catch (HardcopyWriter.PrintCanceledException ex) {
074            log.debug("Print cancelled");
075            return;
076        }
077
078        // Write out the decoder pro logo
079        writer.writeDecoderProIcon();
080
081        // Loop through the Roster, printing as needed
082        List<RosterEntry> l = r.matchingList(null, null, null, null, null, null, null); // take all
083        log.debug("Roster list size: {}", l.size());
084        for (RosterEntry re : l) {
085            if (rosterGroup != null) {
086                if (re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)) != null
087                        && re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)).equals("yes")) {
088                    re.printEntry(writer);
089                }
090            } else {
091                re.printEntry(writer);
092            }
093        }
094
095        // and force completion of the printing
096        writer.close();
097    }
098
099    // never invoked, because we overrode actionPerformed above
100    @Override
101    public jmri.util.swing.JmriPanel makePanel() {
102        throw new IllegalArgumentException("Should not be invoked");
103    }
104
105    @Override
106    public void setParameter(String parameter, String value) {
107        parameter = parameter.toLowerCase();
108        value = value.toLowerCase();
109        if (parameter.equals("ispreview")) {
110            isPreview = value.equals("true");
111        }
112    }
113
114    private final static Logger log = LoggerFactory.getLogger(PrintRosterAction.class);
115
116}