001package jmri.jmrit.roster; 002 003import java.awt.Frame; 004import java.awt.event.ActionEvent; 005import java.io.IOException; 006import java.util.List; 007import javax.swing.ImageIcon; 008import javax.swing.JLabel; 009import jmri.beans.BeanUtil; 010import jmri.jmrit.roster.rostergroup.RosterGroupSelector; 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 the Roster contents. 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 PrintRosterAction extends jmri.util.swing.JmriAbstractAction { 026 027 public PrintRosterAction(String s, jmri.util.swing.WindowInterface wi) { 028 super(s, wi); 029 isPreview = true; 030 } 031 032 public PrintRosterAction(String s, javax.swing.Icon i, jmri.util.swing.WindowInterface wi) { 033 super(s, i, wi); 034 isPreview = true; 035 } 036 037 public PrintRosterAction(String actionName, Frame frame, boolean preview) { 038 super(actionName); 039 mFrame = frame; 040 isPreview = preview; 041 } 042 043 public void setPreview(boolean preview) { 044 isPreview = preview; 045 } 046 047 /** 048 * Frame hosting the printing 049 */ 050 Frame mFrame = new Frame(); 051 052 /** 053 * Variable to set whether this is to be printed or previewed 054 */ 055 boolean isPreview; 056 057 @Override 058 public void actionPerformed(ActionEvent e) { 059 // obtain a HardcopyWriter to do this 060 Roster r = Roster.getDefault(); 061 String title = Bundle.getMessage("TitleDecoderProRoster"); 062 String rosterGroup = r.getDefaultRosterGroup(); 063 // rosterGroup may legitimately be null 064 // but getProperty returns null if the property cannot be found, so 065 // we test that the property exists before attempting to get its value 066 if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) { 067 rosterGroup = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP); 068 } 069 if (rosterGroup == null) { 070 title = title + " " + Bundle.getMessage("ALLENTRIES"); 071 } else { 072 title = title + " " + Bundle.getMessage("TitleGroup") + " " + Bundle.getMessage("TitleEntries", rosterGroup); 073 } 074 HardcopyWriter writer; 075 try { 076 writer = new HardcopyWriter(mFrame, title, 10, .5, .5, .5, .5, isPreview); 077 } catch (HardcopyWriter.PrintCanceledException ex) { 078 log.debug("Print cancelled"); 079 return; 080 } 081 082 // add the image 083 ImageIcon icon = new ImageIcon(FileUtil.findURL("resources/decoderpro.gif", FileUtil.Location.INSTALLED)); 084 // we use an ImageIcon because it's guaranteed to have been loaded when ctor is complete 085 writer.write(icon.getImage(), new JLabel(icon)); 086 //Add a number of blank lines, so that the roster entry starts below the decoderpro logo 087 int height = icon.getImage().getHeight(null); 088 int blanks = (height - writer.getLineAscent()) / writer.getLineHeight(); 089 090 try { 091 for (int i = 0; i < blanks; i++) { 092 String s = "\n"; 093 writer.write(s, 0, s.length()); 094 } 095 } catch (IOException ex) { 096 log.warn("error during printing", ex); 097 } 098 099 // Loop through the Roster, printing as needed 100 List<RosterEntry> l = r.matchingList(null, null, null, null, null, null, null); // take all 101 log.debug("Roster list size: {}", l.size()); 102 for (RosterEntry re : l) { 103 if (rosterGroup != null) { 104 if (re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)) != null 105 && re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)).equals("yes")) { 106 re.printEntry(writer); 107 } 108 } else { 109 re.printEntry(writer); 110 } 111 } 112 113 // and force completion of the printing 114 writer.close(); 115 } 116 117 // never invoked, because we overrode actionPerformed above 118 @Override 119 public jmri.util.swing.JmriPanel makePanel() { 120 throw new IllegalArgumentException("Should not be invoked"); 121 } 122 123 @Override 124 public void setParameter(String parameter, String value) { 125 parameter = parameter.toLowerCase(); 126 value = value.toLowerCase(); 127 if (parameter.equals("ispreview")) { 128 isPreview = value.equals("true"); 129 } 130 } 131 132 private final static Logger log = LoggerFactory.getLogger(PrintRosterAction.class); 133 134}