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, 2025 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 = 065 new HardcopyWriter(new Frame(), Bundle.getMessage("TitleCarLoads"), null, null, 066 Control.reportFontSize, .5 * 72, .5 * 72, .5 * 72, .5 * 72, _isPreview, "", false, true, 067 null, null)) { 068 069 writer.write(getHeader()); 070 071 // Loop through the Roster, printing as needed 072 String[] carTypes = InstanceManager.getDefault(CarTypes.class).getNames(); 073 Hashtable<String, List<CarLoad>> list = InstanceManager.getDefault(CarLoads.class).getList(); 074 075 for (String carType : carTypes) { 076 List<CarLoad> carLoads = list.get(carType); 077 if (carLoads == null) { 078 continue; 079 } 080 boolean printType = true; 081 for (CarLoad carLoad : carLoads) { 082 // don't print out default load or empty 083 if ((carLoad.getName() 084 .equals(InstanceManager.getDefault(CarLoads.class).getDefaultEmptyName()) || 085 carLoad.getName() 086 .equals(InstanceManager.getDefault(CarLoads.class).getDefaultLoadName())) && 087 carLoad.getPickupComment().equals(CarLoad.NONE) && 088 carLoad.getDropComment().equals(CarLoad.NONE) && 089 carLoad.getPriority().equals(CarLoad.PRIORITY_LOW) && 090 !carLoad.isHazardous()) { 091 continue; 092 } 093 // print the car type once 094 if (printType) { 095 writer.write(carType + NEW_LINE); 096 printType = false; 097 } 098 StringBuffer buf = new StringBuffer(TAB); 099 buf.append(tabString(carLoad.getName(), 100 InstanceManager.getDefault(CarLoads.class).getMaxNameLength())); 101 // load type: load or empty 102 buf.append(tabString(carLoad.getLoadType(), 5)); 103 // priority low, medium, or high 104 buf.append(tabString(carLoad.getPriority(), 4)); 105 buf.append(tabString( 106 carLoad.isHazardous() ? Bundle.getMessage("ButtonYes") : Bundle.getMessage("ButtonNo"), 107 3)); 108 buf.append(tabString(carLoad.getPickupComment(), 26)); 109 buf.append(tabString(carLoad.getDropComment(), 27).trim()); 110 writer.write(buf.toString() + NEW_LINE); 111 } 112 } 113 } catch (HardcopyWriter.PrintCanceledException ex) { 114 log.debug("Print canceled"); 115 } catch (IOException ex) { 116 log.error("Error printing car roster: {}", ex.getLocalizedMessage()); 117 } 118 } 119 120 private String getHeader() { 121 return Bundle.getMessage("Type") + 122 TAB + 123 tabString(Bundle.getMessage("Load"), 124 InstanceManager.getDefault(CarLoads.class).getMaxNameLength()) + 125 tabString(Bundle.getMessage("Type"), 5) + 126 tabString(Bundle.getMessage("Priority"), 4) + 127 tabString(Bundle.getMessage("Hazardous"), 3) + 128 tabString(Bundle.getMessage("LoadPickupMessage"), 26) + 129 Bundle.getMessage("LoadDropMessage") + 130 NEW_LINE; 131 } 132 } 133 134 private static String tabString(String s, int fieldSize) { 135 return TrainCommon.padAndTruncate(s, fieldSize) + " "; 136 } 137 138 private final static Logger log = LoggerFactory.getLogger(PrintCarLoadsAction.class); 139}