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 +
069                    " " +
070                    Bundle.getMessage("TitleGroup") +
071                    " " +
072                    Bundle.getMessage("TitleEntries", rosterGroup);
073        }
074        HardcopyWriter writer;
075        try {
076            writer = new HardcopyWriter(mFrame, title, null, null, 10, 
077                .5 * 72, .5 * 72, .5 * 72, .5 * 72, isPreview, null, null, null, null, null);
078        } catch (HardcopyWriter.PrintCanceledException ex) {
079            log.debug("Print cancelled");
080            return;
081        }
082
083        // Write out the decoder pro logo
084        writer.writeDecoderProIcon();
085
086        // Loop through the Roster, printing as needed
087        List<RosterEntry> l = r.matchingList(null, null, null, null, null, null, null); // take all
088        log.debug("Roster list size: {}", l.size());
089        for (RosterEntry re : l) {
090            if (rosterGroup != null) {
091                if (re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)) != null &&
092                        re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)).equals("yes")) {
093                    re.printEntry(writer);
094                }
095            } else {
096                re.printEntry(writer);
097            }
098        }
099
100        // and force completion of the printing
101        writer.close();
102    }
103
104    // never invoked, because we overrode actionPerformed above
105    @Override
106    public jmri.util.swing.JmriPanel makePanel() {
107        throw new IllegalArgumentException("Should not be invoked");
108    }
109
110    @Override
111    public void setParameter(String parameter, String value) {
112        parameter = parameter.toLowerCase();
113        value = value.toLowerCase();
114        if (parameter.equals("ispreview")) {
115            isPreview = value.equals("true");
116        }
117    }
118
119    private final static Logger log = LoggerFactory.getLogger(PrintRosterAction.class);
120
121}