001package jmri.jmrit.operations.trains.tools; 002 003import java.awt.event.ActionEvent; 004import java.io.File; 005 006import javax.swing.AbstractAction; 007import javax.swing.JFileChooser; 008import javax.swing.filechooser.FileNameExtensionFilter; 009 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013import jmri.InstanceManager; 014import jmri.jmrit.operations.locations.Location; 015import jmri.jmrit.operations.locations.LocationManager; 016import jmri.jmrit.operations.setup.Control; 017import jmri.jmrit.operations.setup.Setup; 018import jmri.jmrit.operations.trains.*; 019import jmri.util.FileUtil; 020 021/** 022 * Action to print a train's manifest that has been saved. 023 * 024 * @author Daniel Boudreau Copyright (C) 2015 025 */ 026public class PrintSavedTrainManifestAction extends AbstractAction implements java.beans.PropertyChangeListener { 027 028 private final static Logger log = LoggerFactory.getLogger(PrintSavedTrainManifestAction.class); 029 030 public PrintSavedTrainManifestAction(boolean isPreview, Train train) { 031 super(isPreview ? Bundle.getMessage("MenuItemPreviewSavedManifest") 032 : Bundle.getMessage("MenuItemPrintSavedManifest")); 033 _isPreview = isPreview; 034 _train = train; 035 setEnabled(Setup.isSaveTrainManifestsEnabled()); 036 Setup.getDefault().addPropertyChangeListener(this); 037 } 038 039 /** 040 * Variable to set whether this is to be printed or previewed 041 */ 042 boolean _isPreview; 043 Train _train; 044 045 @Override 046 public void propertyChange(java.beans.PropertyChangeEvent e) { 047 if (Control.SHOW_PROPERTY) { 048 log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), 049 e.getNewValue()); 050 } 051 if (e.getPropertyName().equals(Setup.SAVE_TRAIN_MANIFEST_PROPERTY_CHANGE)) { 052 setEnabled(Setup.isSaveTrainManifestsEnabled()); 053 } 054 } 055 056 @Override 057 public void actionPerformed(ActionEvent e) { 058 File file = getFile(); 059 if (file == null || !file.exists()) { 060 log.debug("User didn't select a file"); 061 return; 062 } 063 if (_isPreview && Setup.isManifestEditorEnabled()) { 064 TrainUtilities.openDesktop(file); 065 return; 066 } 067 String logoURL = Setup.NONE; 068 if (_train != null && !_train.getManifestLogoPathName().equals(Train.NONE)) { 069 logoURL = FileUtil.getExternalFilename(_train.getManifestLogoPathName()); 070 } else if (!Setup.getManifestLogoURL().equals(Setup.NONE)) { 071 logoURL = FileUtil.getExternalFilename(Setup.getManifestLogoURL()); 072 } 073 String printerName = Location.NONE; 074 if (_train != null) { 075 Location departs = InstanceManager.getDefault(LocationManager.class) 076 .getLocationByName(_train.getTrainDepartsName()); 077 if (departs != null) { 078 printerName = departs.getDefaultPrinterName(); 079 } 080 } 081 TrainPrintUtilities.printReport(file, file.getName(), _isPreview, Setup.getFontName(), false, logoURL, 082 printerName, Setup.getManifestOrientation(), Setup.getManifestFontSize(), 083 Setup.isPrintPageHeaderEnabled(), Setup.getPrintDuplexSides()); 084 return; 085 } 086 087 // Get file to read from 088 protected File getFile() { 089 String pathName = InstanceManager.getDefault(TrainManagerXml.class).getBackupManifestDirectoryName(); 090 if (_train != null) { 091 pathName = InstanceManager.getDefault(TrainManagerXml.class) 092 .getBackupManifestDirectoryName(_train.getName()); 093 } 094 JFileChooser fc = new jmri.util.swing.JmriJFileChooser(pathName); 095 fc.setFileFilter(new FileNameExtensionFilter(Bundle.getMessage("TextFiles"), "txt")); 096 int retVal = fc.showOpenDialog(null); 097 if (retVal != JFileChooser.APPROVE_OPTION) { 098 return null; // canceled 099 } 100 return fc.getSelectedFile(); 101 } 102}