001package jmri.jmrit.operations.rollingstock.cars.tools; 002 003import java.awt.event.ActionEvent; 004import java.io.*; 005import java.nio.charset.StandardCharsets; 006 007import javax.swing.AbstractAction; 008 009import jmri.InstanceManager; 010import jmri.jmrit.operations.rollingstock.cars.*; 011import jmri.jmrit.operations.router.Router; 012import jmri.jmrit.operations.setup.Setup; 013import jmri.jmrit.operations.trains.TrainPrintUtilities; 014import jmri.jmrit.operations.trains.TrainUtilities; 015import jmri.util.swing.JmriJOptionPane; 016 017/** 018 * Creates a routing report for the selected car. 019 * 020 * @author Bob Jacobsen Copyright (C) 2001 021 * @author Daniel Boudreau Copyright (C) 2023 022 */ 023public class CarRoutingReportAction extends AbstractAction { 024 025 CarSetFrame _csFrame; 026 boolean _isPreview; 027 028 public CarRoutingReportAction(CarSetFrame frame, boolean isPreview) { 029 super(isPreview ? Bundle.getMessage("MenuPreviewRoutingReport") : Bundle.getMessage("MenuPrintRoutingReport")); 030 _csFrame = frame; 031 _isPreview = isPreview; 032 } 033 034 @Override 035 public void actionPerformed(ActionEvent e) { 036 createCarRoutingReport(_csFrame._car); 037 } 038 039 private void createCarRoutingReport(Car car) { 040 if (car != null && car.getLocation() != null && car.getFinalDestination() != null) { 041 PrintWriter printWriter = getCarRouterBuildReportPrintWriter(car); 042 Router router = InstanceManager.getDefault(Router.class); 043 router.isCarRouteable(car, null, car.getFinalDestination(), car.getFinalDestinationTrack(), printWriter); 044 showCarRoutingReport(car); 045 } else { 046 JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("WarnMsgNoLocOrFD"), 047 Bundle.getMessage("WarnMsgNoLocOrFD"), JmriJOptionPane.WARNING_MESSAGE); 048 } 049 } 050 051 private PrintWriter getCarRouterBuildReportPrintWriter(Car car) { 052 // create car router report print writer 053 PrintWriter printWriter = null; 054 File file = InstanceManager.getDefault(CarManagerXml.class).createRawCarRouterReportFile(car.toString()); 055 try { 056 printWriter = new PrintWriter( 057 new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 058 true); 059 } catch (IOException e) { 060 log.error("Can not open build report file: {}", e.getLocalizedMessage()); 061 } 062 return printWriter; 063 } 064 065 private void showCarRoutingReport(Car car) { 066 CarManagerXml carManagerXml = InstanceManager.getDefault(CarManagerXml.class); 067 TrainPrintUtilities.editReport(carManagerXml.getRawCarRouterReportFile(car.toString()), 068 carManagerXml.createCarRouterReportFile(car.toString())); 069 File file = carManagerXml.getCarRouterReportFile(car.toString()); 070 if (_isPreview && Setup.isBuildReportEditorEnabled()) { 071 TrainUtilities.openDesktop(file); 072 } else { 073 TrainPrintUtilities.printReport(file, Bundle.getMessage("RoutingReportCar", car.toString()), _isPreview, 074 Car.NONE, false, Car.NONE, Car.NONE, Setup.PORTRAIT, Setup.getBuildReportFontSize(), true); 075 } 076 } 077 078 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CarRoutingReportAction.class); 079}