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