001package jmri.jmrit.operations.rollingstock.cars.tools; 002 003import java.awt.Frame; 004import java.awt.event.ActionEvent; 005import java.io.IOException; 006import java.util.Hashtable; 007import java.util.List; 008 009import javax.swing.AbstractAction; 010 011import org.slf4j.Logger; 012import org.slf4j.LoggerFactory; 013 014import jmri.InstanceManager; 015import jmri.jmrit.operations.rollingstock.cars.*; 016import jmri.jmrit.operations.setup.Control; 017import jmri.util.davidflanagan.HardcopyWriter; 018 019/** 020 * Action to print a summary of car loads ordered by car type. 021 * <p> 022 * This uses the older style printing, for compatibility with Java 1.1.8 in 023 * Macintosh MRJ 024 * 025 * @author Bob Jacobsen Copyright (C) 2003 026 * @author Dennis Miller Copyright (C) 2005 027 * @author Daniel Boudreau Copyright (C) 2011 028 */ 029public class PrintCarLoadsAction extends AbstractAction { 030 031 public PrintCarLoadsAction(boolean isPreview) { 032 super(isPreview ? Bundle.getMessage("MenuItemCarLoadsPreview") : Bundle.getMessage("MenuItemCarLoadsPrint")); 033 _isPreview = isPreview; 034 } 035 036 /** 037 * Frame hosting the printing 038 */ 039 /** 040 * Variable to set whether this is to be printed or previewed 041 */ 042 boolean _isPreview; 043 044 @Override 045 public void actionPerformed(ActionEvent e) { 046 new CarLoadPrintOption(); 047 } 048 049 public class CarLoadPrintOption { 050 051 static final String TAB = "\t"; // NOI18N 052 static final String NEW_LINE = "\n"; // NOI18N 053 054 // no frame needed for now 055 public CarLoadPrintOption() { 056 super(); 057 printCars(); 058 } 059 060 private void printCars() { 061 062 // obtain a HardcopyWriter to do this 063 try ( HardcopyWriter writer = new HardcopyWriter(new Frame(), Bundle.getMessage("TitleCarLoads"), Control.reportFontSize, .5, 064 .5, .5, .5, _isPreview); ){ 065 066 // Loop through the Roster, printing as needed 067 String[] carTypes = InstanceManager.getDefault(CarTypes.class).getNames(); 068 Hashtable<String, List<CarLoad>> list = InstanceManager.getDefault(CarLoads.class).getList(); 069 070 String header = Bundle.getMessage("Type") + 071 TAB + 072 tabString(Bundle.getMessage("Load"), 073 InstanceManager.getDefault(CarLoads.class).getMaxNameLength() + 1) + 074 Bundle.getMessage("Type") + 075 " " + 076 Bundle.getMessage("Priority") + 077 " " + 078 tabString(Bundle.getMessage("Hazardous"), 4) + 079 Bundle.getMessage("LoadPickupMessage") + 080 " " + 081 Bundle.getMessage("LoadDropMessage") + 082 NEW_LINE; 083 writer.write(header); 084 for (String carType : carTypes) { 085 List<CarLoad> carLoads = list.get(carType); 086 if (carLoads == null) { 087 continue; 088 } 089 boolean printType = true; 090 for (CarLoad carLoad : carLoads) { 091 // don't print out default load or empty 092 if ((carLoad.getName() 093 .equals(InstanceManager.getDefault(CarLoads.class).getDefaultEmptyName()) || 094 carLoad.getName() 095 .equals(InstanceManager.getDefault(CarLoads.class).getDefaultLoadName())) && 096 carLoad.getPickupComment().equals(CarLoad.NONE) && 097 carLoad.getDropComment().equals(CarLoad.NONE) && 098 carLoad.getPriority().equals(CarLoad.PRIORITY_LOW) && 099 !carLoad.isHazardous()) { 100 continue; 101 } 102 // print the car type once 103 if (printType) { 104 writer.write(carType + NEW_LINE); 105 printType = false; 106 } 107 StringBuffer buf = new StringBuffer(TAB); 108 buf.append(tabString(carLoad.getName(), 109 InstanceManager.getDefault(CarLoads.class).getMaxNameLength() + 1)); 110 buf.append(tabString(carLoad.getLoadType(), 6)); // load 111 // or 112 // empty 113 buf.append(tabString(carLoad.getPriority(), 5)); // low 114 // med 115 // high 116 buf.append(tabString(carLoad.isHazardous()? Bundle.getMessage("ButtonYes") : Bundle.getMessage("ButtonNo"), 4)); 117 buf.append(tabString(carLoad.getPickupComment(), 27)); 118 buf.append(tabString(carLoad.getDropComment(), 27)); 119 writer.write(buf.toString() + NEW_LINE); 120 } 121 } 122 // and force completion of the printing 123// writer.close(); not needed when using try / catch 124 } catch (HardcopyWriter.PrintCanceledException ex) { 125 log.debug("Print cancelled"); 126 } catch (IOException ex) { 127 log.error("Error printing car roster: {}", ex.getLocalizedMessage()); 128 } 129 } 130 } 131 132 private static String tabString(String s, int fieldSize) { 133 if (s.length() > fieldSize) { 134 s = s.substring(0, fieldSize - 1); 135 } 136 StringBuffer buf = new StringBuffer(s + " "); 137 while (buf.length() < fieldSize) { 138 buf.append(" "); 139 } 140 return buf.toString(); 141 } 142 143 private final static Logger log = LoggerFactory.getLogger(PrintCarLoadsAction.class); 144}