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