Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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<FieldPanel> panels=new ArrayList<FieldPanel>();
       
       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<Integer, StringBuffer> errors=new HashMap<Integer, StringBuffer>();
           for (i=1; i<data.length; i++)
           {
               Act act=db.createAct();                
               for (int k=0; k<n; k++)
               {
                   j=index[k];
                   String s=data[i][j];
                   Field f=db.getField(fieldNames[k]);
                   FieldFormat format=FieldFormatCatalog.getFormat(types[k]);
                   try
                   {
                       Object o=format.parse(s);
                       act.set(f,o);
                   }
                   catch (Exception e)
                   {
                       StringBuffer b=errors.get(i-1);
                       if (b==null)
                       {
                           b=new StringBuffer();
                           errors.put(i-1,b);
                       }
                       else
                           b.append("; ");
                       b.append(f.getName()+":"+s);
                   }
               }
           }
           
           if (errors.size()>0)
           {
               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<n; j++)
           {
               System.out.println(db.getField(fieldNames[j]));
           }
           
           return db;
       }
       
       void display(String[][] data)
       {
           removeAll();
           
           this.data=data;
           
           // analyze data.
           Class[] guesses=FieldFormatGuesser.analyze(data, 1, 100);
           
           // go through first row, which is headers.
           String[] headers=data[0];
           
           // if there are duplicate headers, add indicators.
           HashSet<String> h=new HashSet<String>();
           for (int i=0; i<headers.length; i++)
           {
               String base=headers[i];
               int j=2;
               String name=base;
               while (h.contains(name))
               {
                   name=base+" "+j;
                   j++;
               }
               headers[i]=name;
               h.add(name);
           }
           
           numFields=headers.length;
           int cols=2;
           rows=(int)Math.ceil(numFields/2.0);
           setLayout(new GridLayout(rows, cols));
           for (int i=0; i<numFields; i++)
           {
               Bag<String> vals=new Bag<String>();
               
               for (int j=1; j<data.length; j++)
               {
                   vals.add(data[j][i]);
               }
               java.util.List<String> top=vals.listTop(5);
               int n=top.size();
               String[] samples=new String[n];
               for (int j=0; j<n; j++)
               {
                   String s=top.get(j);
                   samples[j]=(s.length()==0 ? "*MISSING*" : s)+"    ("+vals.num(s)+" times)";
               }
               
               JPanel p=new JPanel();
               add(p);
               p.setLayout(new BorderLayout());
               FieldPanel f=new FieldPanel(headers[i], samples, guesses[i]);
               panels.add(f);
               p.add(f, BorderLayout.CENTER);
               JPanel hr=new JPanel();
               hr.setPreferredSize(new Dimension(20,20));
               p.add(hr, BorderLayout.SOUTH);
           }
       }
       
       public Dimension getPreferredSize()
       {
           return new Dimension(400,150*rows);
       }
   }
   
   class FieldPanel extends JPanel
   {
       JComboBox typeChoices;
       String fieldName;
       JCheckBox ignore;
       JLabel fieldLabel;
       int x1=5, y1=20, y3=150,x2=150, x3=150, x4=375, dh=2;
 
       
       FieldPanel(String fieldName, String[] sampleValues, Class typeGuess)
       {
           // just going with a null layout here, because it's a lot simpler!
           
           setLayout(null);
           setBackground(Color.white);
           this.fieldName=fieldName;
           
           fieldLabel=new JLabel(" \""+fieldName+'"');    
           fieldLabel.setFont(model.getDisplay().big());
           add(fieldLabel);
           fieldLabel.setBounds(x1,y1,fieldLabel.getPreferredSize().width, fieldLabel.getPreferredSize().height);
           
           typeChoices=new JComboBox();
           for (String choice: FieldFormatCatalog.classNames())
               typeChoices.addItem(choice);
           typeChoices.setSelectedItem(FieldFormatCatalog.humanName(typeGuess));
           add(typeChoices);
           int y2=fieldLabel.getY()+fieldLabel.getHeight()+dh+5;
           typeChoices.setBounds(x1,y2,
                   typeChoices.getPreferredSize().width, typeChoices.getPreferredSize().height);
               
           ignore=new JCheckBox("Ignore Field");            
           add(ignore);
           ignore.setBounds(x1,typeChoices.getY()+typeChoices.getHeight()+dh,
                   ignore.getPreferredSize().width, ignore.getPreferredSize().height);
           ignore.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                   Color c=ignore.isSelected() ? Color.gray : Color.black;
                   fieldLabel.setForeground(c);
                   typeChoices.setForeground(c);
               }});
           
           JTextArea values=new JTextArea();
           values.setForeground(Color.gray);
           for (int i=0; i<sampleValues.length; i++)
               values.append(sampleValues[i]+"\n");
           add(values);
           values.setBounds(x3,y2,x4-x3,y3-y2);
       }
       
       public Dimension getPreferredSize()
       {
           return new Dimension(500,150);
       }
   }
}