Parent: [a7a9ff] (diff)

Download this file

SpecPanel.java    535 lines (449 with data), 18.8 kB

  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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.view.ImportWizard;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.CellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import jwf.WizardPanel;
import net.miginfocom.swing.MigLayout;
import squirrel.controller.ImportController;
import squirrel.controller.TimestampFormat;
import squirrel.controller.ValidationResult;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSource;
import squirrel.model.io.DataColumn.Type;
public class SpecPanel extends WizardPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public ImportController controller;
public JPanel gui;
public JFrame container;
public SpecPanel(ImportController controller, JFrame container) {
this.controller = controller;
this.container = container;
this.initGui();
}
private void initGui() {
this.container.setSize(new Dimension(650, 700));
this.setLayout(new MigLayout());
JLabel lablogo = new JLabel();
lablogo.setIcon(new javax.swing.ImageIcon(FileFormatPanel.class.getClassLoader()
.getResource("squirrel/resources/teco.png")));
this.add(lablogo, "wrap 50, w 390!, h 100!");
switch (controller.getSelectedSourceType()) {
case CSV:
this.gui = new CSVPanel();
break;
}
this.add(gui);
}
private class CSVPanel extends JPanel {
/**
* UI components
*/
JTextField txfLineSeparator, txfElemSeparator;
JLabel lblLineSeparator, lblElemSeparator;
ColumnDescriptionPanel columnDescriptionPanel;
public CSVPanel() {
this.initGUI();
}
/**
* Inits UI
*/
void initGUI() {
lblLineSeparator = new JLabel("Line Separator:");
lblElemSeparator = new JLabel("Element Separator:");
txfLineSeparator = new JTextField(3);
txfLineSeparator.setText("\\n");
txfLineSeparator.setVisible(false);
lblLineSeparator.setVisible(false);
txfElemSeparator = new JTextField(3);
txfElemSeparator.setText("");
this.setLayout(new MigLayout("", "", "[nogrid][nogrid][]"));
// These following components share one cell and constitute a row
this.add(lblElemSeparator, "align label");
this.add(txfElemSeparator, "gapright unrelated");
this.add(lblLineSeparator, "align label");
this.add(txfLineSeparator, "wrap, gapbottom unrel");
columnDescriptionPanel = new ColumnDescriptionPanel(
new String[] { "#", "Name", "Type", "Allowed Values" }, 2);
columnDescriptionPanel.initGUI();
columnDescriptionPanel.table
.getColumnModel()
.getColumn(2)
.setCellEditor(
new DefaultCellEditor(new JComboBox<DataColumn.Type>(DataColumn.Type
.values())));
this.add(columnDescriptionPanel, "pushx, growx, wrap");
}
/**
* Validates user imput
* @param controller - import controller
*/
void validateAll(ImportController controller) {
controller.clearErrors();
controller.validateCSVElemSep(txfElemSeparator.getText());
controller.validateCSVLineSep(txfLineSeparator.getText());
controller.validateCSVColumnDescriptions(columnDescriptionPanel.tblModel);
Map<Integer, TimestampFormat> formats = new HashMap<>();
for (Integer key : columnDescriptionPanel.timePanels.keySet()) {
TimespecPanel panel = columnDescriptionPanel.timePanels.get(key);
if (panel.rdbCustDate.isSelected()) {
formats.put(key, new TimestampFormat(panel.txfCustUnits.getText(),
panel.txfCustSeparator.getText()));
} else {
formats.put(key, new TimestampFormat(panel.txfDate.getText()));
}
}
controller.validateCSVTimestampFormats(formats);
}
}
/*
* private DBDataSource dataSource; private TimeConverter timeConverter;
* private PreparedStatement statement = null; private ResultSet resultSet =
* null; private float average = -1; private long startTimestamp = -1;
* private long endTimestamp = -1; private int amountForAverage; private
* Range statmentRange; private String pattern;
*/
// DBDataSource dataSource, int amountForAverage, Range
// statmentRange,String
// pattern
private class ColumnDescriptionPanel extends JPanel {
JTable table;
JScrollPane scrollPane;
JButton btnAdd, btnRemove, btnMoveUp, btnMoveDown;
DefaultTableModel tblModel;
Map<Integer, TimespecPanel> timePanels = new HashMap<>();
Component currentTimePanel;
JLabel lblNoTimestamp;
private int typeIndex;
public ColumnDescriptionPanel(String[] columnNames, int typeIndex) {
this.tblModel = new DefaultTableModel(columnNames, 2);
this.typeIndex = typeIndex;
}
void initGUI() {
btnAdd = new JButton("Add");
btnRemove = new JButton("Remove");
btnMoveUp = new JButton("Move Up");
btnMoveDown = new JButton("Move Down");
tblModel.setValueAt(0, 0, 0);
tblModel.setValueAt(1, 1, 0);
table = new JTable(tblModel);
table.getColumnModel().getColumn(0).setMaxWidth(20);
table.setPreferredScrollableViewportSize(
new Dimension((int) table.getPreferredSize().getHeight(),
table.getRowHeight() * 6));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new TableSelectionListener());
scrollPane = new JScrollPane(table);
lblNoTimestamp = new JLabel("No timestamp column is selected.");
lblNoTimestamp
.setFont(new Font(lblNoTimestamp.getFont().getName(),
lblNoTimestamp.getFont().getStyle(), lblNoTimestamp.getFont()
.getSize() * 2));
currentTimePanel = lblNoTimestamp;
// layout
this.setLayout(new MigLayout());
this.add(scrollPane, "push, grow");
this.add(btnAdd, "growx, flowy, aligny top, split 4");
this.add(btnRemove, "growx, gapbottom unrelated");
this.add(btnMoveUp, "growx");
this.add(btnMoveDown, "growx, wrap, gapbottom unrel");
this.add(lblNoTimestamp, "pushx, growx, wrap");
ActionListener al = new ActionListenerImpl();
btnAdd.addActionListener(al);
btnRemove.addActionListener(al);
btnMoveUp.addActionListener(al);
btnMoveDown.addActionListener(al);
}
private void showTimePanel(int index) {
this.remove(currentTimePanel);
if (index < 0) {
this.add(lblNoTimestamp, "pushx, growx, wrap");
currentTimePanel = lblNoTimestamp;
} else {
currentTimePanel = timePanels.get(index);
this.add(currentTimePanel, "pushx, pushy, growy, growx, wrap");
}
revalidate();
super.repaint();
}
private void maybeRemoveTimePanel(int index) {
Type colType = (Type) tblModel.getValueAt(table.getSelectedRow(), typeIndex);
if (colType != null && colType == Type.TIMESTAMP) {
timePanels.remove(index);
}
}
private class TableSelectionListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
int min = lsm.getMinSelectionIndex();
int max = lsm.getMaxSelectionIndex();
if (min > -1 && max > -1) {
try {
Type colType =
(Type) tblModel.getValueAt(table.getSelectedRow(), typeIndex);
switch (controller.getSelectedSourceType()) {
case CSV:
if (colType != null && colType.isTimeStamp()) {
if (!timePanels.containsKey(min)) {
timePanels.put(min, new TimespecPanel());
}
showTimePanel(min);
} else {
showTimePanel(-1);
}
break;
}
} catch (Exception e1) {
}
}
}
}
private class ActionListenerImpl implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd) {
Integer nextNum = tblModel.getRowCount();
tblModel.addRow(new Object[] { String.valueOf(nextNum), "", Type.IGNORE, "" });
} else if (e.getSource() == btnRemove) {
CellEditor editor = table.getCellEditor();
if (editor != null)
editor.stopCellEditing();
int selRowIdx = table.getSelectedRow();
if (selRowIdx > -1) {
maybeRemoveTimePanel(selRowIdx);
tblModel.removeRow(selRowIdx);
}
} else if (e.getSource() == btnMoveUp) {
// Only one of multiple selected rows is moved up
int selRow = table.getSelectedRow();
if (selRow > 0)
tblMoveRowAndSelection(selRow, selRow - 1);
else
// if this is the first row, move it to the bottom
tblMoveRowAndSelection(selRow, tblModel.getRowCount() - 1);
} else if (e.getSource() == btnMoveDown) {
// Only one of multiple selected rows is moved up
int selRow = table.getSelectedRow();
// if this is the last row, move it to the top
if (selRow == tblModel.getRowCount() - 1)
tblMoveRowAndSelection(selRow, 0);
else
tblMoveRowAndSelection(selRow, selRow + 1);
}
}
private void tblMoveRowAndSelection(int rowIdx, int to) {
tblModel.moveRow(rowIdx, rowIdx, to);
table.getSelectionModel().setSelectionInterval(to, to);
}
}
}
/**
* Panel to specify Timestamp sepcs of csv
*/
class TimespecPanel extends JPanel {
JCheckBox chbApplyRegex;
JRadioButton rdbDate, rdbCustDate;
ButtonGroup btnGroup;
JTextField txfRegexMatch, txfRegexReplace, txfDate, txfCustUnits,
txfCustSeparator;
/**
* Constructor
*/
public TimespecPanel() {
initGUI();
}
/**
* Inits UI
*/
void initGUI() {
this.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Timestamp specifications"));
chbApplyRegex = new JCheckBox("Apply Regex");
JLabel lblRegexMatch = new JLabel("Match:");
txfRegexMatch = new JTextField(20);
JLabel lblRegexReplace = new JLabel("Replace:");
txfRegexReplace = new JTextField(20);
rdbDate = new JRadioButton("Date");
rdbDate.setSelected(true);
// controller.setSelectedDate(false);
JLabel lblDate = new JLabel("Format:");
txfDate = new JTextField(10);
rdbCustDate = new JRadioButton("Custom");
JLabel lblCustUnits = new JLabel("Unit:");
txfCustUnits = new JTextField(10);
JLabel lblCustSeparator = new JLabel("Separator:");
txfCustSeparator = new JTextField(3);
btnGroup = new ButtonGroup();
btnGroup.add(rdbDate);
btnGroup.add(rdbCustDate);
// layout
this.setLayout(new MigLayout("", "[][][][][][]"));
//this.add(chbApplyRegex, "span 2");
this.add(rdbDate, "span 2");
this.add(rdbCustDate, "span 2, wrap");
//this.add(lblRegexMatch);
//this.add(txfRegexMatch);
this.add(lblDate);
this.add(txfDate);
this.add(lblCustUnits);
this.add(txfCustUnits, "wrap");
//this.add(lblRegexReplace);
//this.add(txfRegexReplace);
this.add(lblCustSeparator, "skip 2");
this.add(txfCustSeparator, "wrap");
}
}
@Override
public boolean canFinish() {
return true;
}
@Override
public void display() {
}
@Override
public void finish() {
controller.doImport();
this.setVisible(false);
}
@Override
public boolean hasNext() {
return false;
}
@Override
public WizardPanel next() {
return null;
}
@Override
public boolean validateFinish(List errors) {
boolean validation = true;
switch (controller.getSelectedSourceType()) {
case CSV:
((CSVPanel) gui).validateAll(controller);
break;
}
List<ValidationResult> errorResults = controller.getErrorValidations();
if (!errorResults.isEmpty()) {
validation = false;
for (int i = 0; i < errorResults.size(); i++) {
errors.add(errorResults.get(i).toString());
}
}
return validation;
}
@Override
public boolean validateNext(List errors) {
// Never refered
boolean validation = true;
switch (controller.getSelectedSourceType()) {
case CSV:
((CSVPanel) gui).validateAll(controller);
break;
}
List<ValidationResult> errorResults = controller.getErrorValidations();
if (!errorResults.isEmpty()) {
validation = false;
for (int i = 0; i < errorResults.size(); i++) {
errors.add(errorResults.get(i).toString());
}
}
return validation;
}
public void setCSVValues(String lineSep, String valueSep, DefaultTableModel csvModel,
TableCellEditor typeEditor, TimestampFormat[] tsfs, String previewStart,
String previewEnd) {
((CSVPanel) this.gui).txfElemSeparator.setText(valueSep);
((CSVPanel) this.gui).txfLineSeparator.setText(lineSep);
((CSVPanel) this.gui).columnDescriptionPanel.tblModel = csvModel;
((CSVPanel) this.gui).columnDescriptionPanel.table.setModel(csvModel);
((CSVPanel) this.gui).columnDescriptionPanel.table.getColumnModel().getColumn(2)
.setCellEditor(typeEditor);
for (int i = 0; i < csvModel.getRowCount(); i++) {
((CSVPanel) this.gui).columnDescriptionPanel.table.getSelectionModel()
.setSelectionInterval(i, i);
}
for (int i = 0; i < tsfs.length; i++) {
TimespecPanel tsp = ((CSVPanel) this.gui).columnDescriptionPanel.timePanels.get(i);
tsp.txfDate
.setText(
tsfs[i].date);
tsp.txfCustSeparator.setText(tsfs[i].customSeparator);
tsp.txfCustUnits.setText(tsfs[i].customUnits);
if (tsfs[i].isCustomDate)
tsp.rdbCustDate.setSelected(true);
else
tsp.rdbDate.setSelected(true);
}
}
public void showWarningDialog(List<ValidationResult> results) {
// Never Refer
final JDialog dialog = new JDialog((Frame) null, "Warning", true);
dialog.setLayout(new MigLayout("flowy"));
JLabel label0 = new JLabel("Refresh is not possible.");
JLabel label1 = new JLabel("The following requirements have not been met:");
JList<ValidationResult> list =
new JList<ValidationResult>(results.toArray(new ValidationResult[] {}));
JScrollPane scrollPane = new JScrollPane(list);
JButton buttonClose = new JButton("Close");
buttonClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
dialog.add(label0, "gap -5");
dialog.add(label1, "gapbottom unrelated");
dialog.add(scrollPane);
dialog.add(buttonClose, "tag cancel");
dialog.pack();
dialog.setVisible(true);
}
}