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