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.setup.Control;
015import jmri.jmrit.operations.setup.Setup;
016import jmri.jmrit.operations.trains.*;
017
018/**
019 * Action to print a train's build report that has been saved.
020 *
021 * @author Daniel Boudreau Copyright (C) 2022
022 */
023public class PrintSavedBuildReportAction extends AbstractAction implements java.beans.PropertyChangeListener {
024
025    private final static Logger log = LoggerFactory.getLogger(PrintSavedBuildReportAction.class);
026
027    public PrintSavedBuildReportAction(boolean isPreview, Train train) {
028        super(isPreview ? Bundle.getMessage("MenuItemPreviewSavedBuildReport")
029                : Bundle.getMessage("MenuItemPrintSavedBuildReport"));
030        _isPreview = isPreview;
031        _train = train;
032        setEnabled(Setup.isSaveTrainManifestsEnabled());
033        Setup.getDefault().addPropertyChangeListener(this);
034    }
035
036    /**
037     * Variable to set whether this is to be printed or previewed
038     */
039    boolean _isPreview;
040    Train _train;
041
042    @Override
043    public void propertyChange(java.beans.PropertyChangeEvent e) {
044        if (Control.SHOW_PROPERTY) {
045            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
046                    e.getNewValue());
047        }
048        if (e.getPropertyName().equals(Setup.SAVE_TRAIN_MANIFEST_PROPERTY_CHANGE)) {
049            setEnabled(Setup.isSaveTrainManifestsEnabled());
050        }
051    }
052
053    @Override
054    public void actionPerformed(ActionEvent e) {
055        File file = getFile();
056        if (file == null || !file.exists()) {
057            log.debug("User didn't select a file");
058            return;
059        }
060        if (_isPreview && Setup.isBuildReportEditorEnabled()) {
061            TrainPrintUtilities.editReport(file, _train.getName()); 
062            return;
063        }
064        TrainPrintUtilities.printReport(file,
065                Bundle.getMessage("buildReport", _train.getDescription()),
066                _isPreview, Train.NONE, true, Train.NONE, Train.NONE, Setup.PORTRAIT, Setup.getBuildReportFontSize(), true);
067        return;
068    }
069
070    // Get file to read from
071    protected File getFile() {
072        String pathName = InstanceManager.getDefault(TrainManagerXml.class).getBackupBuildStatusDirectoryName();
073        if (_train != null) {
074            pathName = InstanceManager.getDefault(TrainManagerXml.class)
075                    .getBackupBuildStatusDirectoryName(_train.getName());
076        }
077        JFileChooser fc = new jmri.util.swing.JmriJFileChooser(pathName);
078        fc.setFileFilter(new FileNameExtensionFilter(Bundle.getMessage("TextFiles"), "txt"));
079        int retVal = fc.showOpenDialog(null);
080        if (retVal != JFileChooser.APPROVE_OPTION) {
081            return null; // canceled
082        }
083        return fc.getSelectedFile();
084    }
085}