import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
/*
* FileImport.java
*
* Created on 14. April 2004, 14:16
*/
/**
*
* @author Darimont
*/
public class FileImport extends JFrame {
private BufferedReader br;
private File importFile;
private JFileChooser chooser;
private FileImport _this;
private volatile boolean importRunning = false;
private DefaultTableModel model;
/** Creates new form FileImport */
public FileImport() {
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_this = this;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
bgPanel = new JPanel();
tablePanel = new JPanel();
scrollPane = new JScrollPane();
jTable1 = new JTable();
controlsPanel = new JPanel();
btnChoseFile = new JButton();
txtPath = new JTextField();
btnImport = new JButton();
lblPath = new JLabel();
lblDelimiter = new JLabel();
txtDelimiter = new JTextField();
getContentPane().setLayout(new java.awt.FlowLayout());
setTitle("FileImport");
bgPanel.setLayout(new java.awt.GridBagLayout());
bgPanel.setMinimumSize(new java.awt.Dimension(400, 300));
bgPanel.setPreferredSize(new java.awt.Dimension(400, 300));
tablePanel.setMinimumSize(new java.awt.Dimension(400, 200));
tablePanel.setPreferredSize(new java.awt.Dimension(400, 200));
scrollPane.setMinimumSize(new java.awt.Dimension(400, 200));
scrollPane.setPreferredSize(new java.awt.Dimension(400, 200));
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(jTable1);
tablePanel.add(scrollPane);
bgPanel.add(tablePanel, new java.awt.GridBagConstraints());
controlsPanel.setLayout(new java.awt.GridBagLayout());
controlsPanel.setMinimumSize(new java.awt.Dimension(400, 100));
controlsPanel.setPreferredSize(new java.awt.Dimension(400, 100));
btnChoseFile.setText("Chose File");
btnChoseFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnChoseFileActionPerformed(evt);
}
});
controlsPanel.add(btnChoseFile, new java.awt.GridBagConstraints());
txtPath.setColumns(20);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
controlsPanel.add(txtPath, gridBagConstraints);
btnImport.setText("Import");
btnImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportActionPerformed(evt);
}
});
controlsPanel.add(btnImport, new java.awt.GridBagConstraints());
lblPath.setText("Path: ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
controlsPanel.add(lblPath, gridBagConstraints);
lblDelimiter.setText("Delimiter: ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
controlsPanel.add(lblDelimiter, gridBagConstraints);
txtDelimiter.setColumns(3);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
controlsPanel.add(txtDelimiter, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
bgPanel.add(controlsPanel, gridBagConstraints);
getContentPane().add(bgPanel);
pack();
}
private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
if (importFile != null)
if (!importRunning) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
doImport(importFile);
}
});
} else {
System.out.println("Still importing ... ");
}
}
/**
* @param importFile
*/
protected void doImport(File importFile) {
// TODO Auto-generated method stub
String delimiter = txtDelimiter.getText();
if (delimiter == null || !delimiter.equals(""))
delimiter = ";";
try {
br = new BufferedReader(new FileReader(importFile));
ArrayList rows = new ArrayList();
Object[] clmHeaders = null;
String line = null;
StringTokenizer tokenizer = null;
int tCnt = -1;
while ((line = br.readLine()) != null) {
tokenizer = new StringTokenizer(line, delimiter);
tCnt = tokenizer.countTokens();
Object[] rowData = new Object[tCnt];
int i = 0;
while (tokenizer.hasMoreTokens())
rowData[i++] = tokenizer.nextToken();
rows.add(rowData);
}
clmHeaders = new Object[((Object[]) rows.get(0)).length];
for (int i = 0; i < clmHeaders.length; i++) {
clmHeaders[i] = "Column" + i;
}
Object[][] oArray = new Object[tCnt][];
int lstSize = rows.size();
for (int i = 0; i < lstSize; i++)
oArray[i] = (Object[]) rows.get(i);
model = new DefaultTableModel(oArray, clmHeaders);
jTable1.setModel(model);
jTable1.updateUI();
System.out.println("Import complete!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void btnChoseFileActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
if (chooser == null)
chooser =
new JFileChooser(new File(System.getProperty("user.dir")));
chooser.showDialog(this, "load");
importFile = chooser.getSelectedFile();
if (importFile != null)
txtPath.setText(importFile.getAbsolutePath());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new FileImport().show();
}
// Variables declaration - do not modify
private JButton btnChoseFile;
private JButton btnImport;
private JPanel bgPanel;
private JPanel tablePanel;
private JPanel controlsPanel;
private JScrollPane scrollPane;
private JTable jTable1;
private JLabel lblDelimiter;
private JLabel lblPath;
private JTextField txtDelimiter;
private JTextField txtPath;
// End of variables declaration
}