001package jmri.jmrit.operations.rollingstock; 002 003import java.io.*; 004import java.nio.charset.StandardCharsets; 005import java.util.Arrays; 006 007import javax.swing.BoxLayout; 008import javax.swing.JFileChooser; 009import javax.swing.JLabel; 010import javax.swing.JPanel; 011import javax.swing.filechooser.FileNameExtensionFilter; 012 013import org.apache.commons.csv.CSVFormat; 014import org.apache.commons.csv.CSVParser; 015import org.apache.commons.csv.CSVRecord; 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018 019import jmri.jmrit.operations.setup.Control; 020 021/** 022 * Provides common routes for importing cars and locomotives 023 * 024 * @author Dan Boudreau Copyright (C) 2013 025 * 026 */ 027public abstract class ImportRollingStock extends Thread { 028 029 protected static final String NEW_LINE = "\n"; // NOI18N 030 031 protected JLabel lineNumber = new JLabel(); 032 protected JLabel importLine = new JLabel(); 033 034 protected static final String LOCATION_TRACK_SEPARATOR = "-"; 035 036 protected jmri.util.JmriJFrame fstatus; 037 038 // Get file to read from 039 protected File getFile() { 040 JFileChooser fc = new jmri.util.swing.JmriJFileChooser(jmri.jmrit.operations.OperationsXml.getFileLocation()); 041 fc.setFileFilter(new FileNameExtensionFilter(Bundle.getMessage("Text&CSV"), "txt", "csv")); // NOI18N 042 int retVal = fc.showOpenDialog(null); 043 if (retVal != JFileChooser.APPROVE_OPTION) { 044 return null; // canceled 045 } 046 log.info("Importing from file: {}", fc.getSelectedFile()); 047 return fc.getSelectedFile(); 048 } 049 050 protected BufferedReader getBufferedReader(File file) { 051 try { 052 return new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); 053 } catch (FileNotFoundException e) { 054 return null; 055 } 056 } 057 058 // create a status frame showing line number and imported text 059 protected void createStatusFrame(String title) { 060 JPanel ps = new JPanel(); 061 ps.setLayout(new BoxLayout(ps, BoxLayout.Y_AXIS)); 062 fstatus = new jmri.util.JmriJFrame(title); 063 fstatus.setLocation(10, 10); 064 fstatus.setSize(Control.panelWidth700, 100); 065 066 ps.add(lineNumber); 067 ps.add(importLine); 068 069 fstatus.getContentPane().add(ps); 070 fstatus.setVisible(true); 071 } 072 073 /* 074 * Needs to handle empty lines 075 */ 076 protected String[] parseCommaLine(String line) { 077 String[] outLine = new String[0]; 078 try { 079 CSVRecord record = CSVParser.parse(line, CSVFormat.DEFAULT).getRecords().get(0); 080 outLine = new String[record.size()]; 081 // load output array to prevent NPE 082 for (int i = 0; i < outLine.length; i++) { 083 outLine[i] = record.get(i); 084 } 085 } catch (IndexOutOfBoundsException e) { 086 // do nothing blank line 087 } catch (IOException ex) { 088 log.error("Error parsing CSV: {}", line, ex); 089 Arrays.fill(outLine, ""); // NOI18N 090 } 091 return outLine; 092 } 093 094 private final static Logger log = LoggerFactory.getLogger(ImportRollingStock.class); 095}