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