package timeflow.app.ui; import timeflow.data.time.*; import timeflow.data.db.*; import timeflow.format.field.*; import timeflow.format.file.DelimitedFormat; import timeflow.model.*; import timeflow.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.ParseException; import java.util.*; import java.io.*; import java.net.MalformedURLException; import java.net.URL; public class ImportDelimitedPanel extends JFrame { String fileName; SchemaPanel schemaPanel; boolean exitOnClose=false; // for testing! JScrollPane scroller; TFModel model; JLabel numLinesLabel=new JLabel(); // for testing: public static void main(String[] args) throws Exception { System.out.println("Starting test of ImportEditor"); String file="data/probate.tsv"; String[][] data=DelimitedFormat.readArrayGuessDelim(file, System.out); ImportDelimitedPanel editor=new ImportDelimitedPanel(new TFModel()); editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); editor.setFileName(file); editor.setData(data); editor.setBounds(50,50,900,800); editor.setVisible(true); editor.exitOnClose=true; } public ImportDelimitedPanel(final TFModel model) { super("Import File"); this.model=model; setBackground(Color.white); setLayout(new BorderLayout()); JPanel top=new JPanel(); add(top, BorderLayout.NORTH); top.setLayout(new FlowLayout(FlowLayout.LEFT)); top.setBackground(Color.lightGray); top.add(numLinesLabel); final JTextField source=new JTextField(12); JButton done=new JButton("Import This"); top.add(done); done.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { model.setDB(schemaPanel.makeDB(source.getText()), fileName, true, this); setVisible(false); if (exitOnClose) System.exit(0); }}); JButton cancel=new JButton("Cancel"); top.add(cancel); cancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); if (exitOnClose) System.exit(0); }}); top.add(new JLabel(" Enter A Source:")); top.add(source); schemaPanel=new SchemaPanel(); schemaPanel.setBackground(Color.white); scroller=new JScrollPane(schemaPanel); add(scroller, BorderLayout.CENTER); } public void scrollToTop() { scroller.getViewport().setViewPosition(new Point(0,0)); } public void setFileName(String fileName) { this.fileName=fileName; } public void setData(String[][] data) { numLinesLabel.setText((data.length-1)+" records read. "); schemaPanel.display(data); } class SchemaPanel extends JPanel { int numFields, rows; String[][] data; ArrayList panels=new ArrayList(); ActDB makeDB(String source) { // count number of fields that are not ignored. int n=0; for (FieldPanel fp: panels) if (!fp.ignore.isSelected()) n++; Class[] types=new Class[n]; String[] fieldNames=new String[n]; int[] index=new int[n]; if (source.trim().length()==0) source="[source unspecified]"; int i=0, j=0; for (FieldPanel fp: panels) { if (!fp.ignore.isSelected()) { fieldNames[i]=fp.fieldName; String typeChoice=(String)fp.typeChoices.getSelectedItem(); Class type=FieldFormatCatalog.javaClass(typeChoice); System.out.println("Type: "+type+" for: "+typeChoice+" from "+fp.fieldName); types[i]=type; index[i]=j; i++; } j++; } ActDB db= new ArrayDB(fieldNames, types, source); HashMap errors=new HashMap(); for (i=1; i0) { Field error=db.addField("UNPARSED FIELDS", String.class); for (int row:errors.keySet()) { db.get(row).set(error, errors.get(row).toString()); } } for (j=0; j h=new HashSet(); for (int i=0; i vals=new Bag(); for (int j=1; j top=vals.listTop(5); int n=top.size(); String[] samples=new String[n]; for (int j=0; j