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