001package jmri.jmrit.symbolicprog; 002 003import java.awt.event.ActionEvent; 004import java.io.*; 005 006import javax.swing.AbstractAction; 007import javax.swing.JFileChooser; 008import javax.swing.JFrame; 009 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013/** 014 * Action to export the CV values to a PR1DOS data file. 015 * <p> 016 * Note that this format is somewhat different from the PR1WIN format, and it's 017 * not clear they will interoperate. 018 * 019 * @author Bob Jacobsen Copyright (C) 2003, 2014 020 */ 021public class Pr1ExportAction extends AbstractAction { 022 023 /** 024 * Create the action 025 * 026 * @param actionName String name to be displayed in menus, etc 027 * @param pModel CvTableModel that contains the data to (eventually) be 028 * exported 029 * @param pParent JFrame that will eventually invoke the action, used to 030 * anchor a file dialog 031 */ 032 public Pr1ExportAction(String actionName, CvTableModel pModel, JFrame pParent) { 033 super(actionName); 034 mModel = pModel; 035 mParent = pParent; 036 } 037 038 JFileChooser fileChooser; 039 JFrame mParent; 040 041 /** 042 * CvTableModel to load 043 */ 044 CvTableModel mModel; 045 046 @Override 047 public void actionPerformed(ActionEvent e) { 048 049 if (fileChooser == null) { 050 fileChooser = new jmri.util.swing.JmriJFileChooser(); 051 } 052 053 int retVal = fileChooser.showSaveDialog(mParent); 054 055 if (retVal == JFileChooser.APPROVE_OPTION) { 056 File file = fileChooser.getSelectedFile(); 057 if (log.isDebugEnabled()) { 058 log.debug("start to export to PR1 file {}", file); 059 } 060 061 try ( PrintStream str = new PrintStream(new FileOutputStream(file)); ) { 062 063 str.println("[DecoderData]"); 064 for (int i = 1; i <= 256; i++) { 065 int lowCvIndex = i; 066 CvValue cv1 = mModel.allCvMap().get("" + lowCvIndex); 067 int value1 = (cv1 != null) ? cv1.getValue() : 0; 068 069 str.println("CV" + i + "=" + value1); 070 } 071 str.flush(); 072 str.close(); 073 } catch (FileNotFoundException ex) { 074 log.error("Error writing file", ex); 075 } 076 } 077 } 078 079 private final static Logger log = LoggerFactory.getLogger(Pr1ExportAction.class); 080}