Child: [9eb143] (diff)

Download this file

ExportView.java    445 lines (384 with data), 16.1 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
/*
* Copyright 2013 TecO - Karlsruhe Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package squirrel.view;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
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.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
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 net.miginfocom.swing.MigLayout;
import squirrel.controller.ExportController;
import squirrel.controller.TimestampFormat;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSink;
import squirrel.model.io.DataSource;
import squirrel.model.io.DataColumn.Type;
/**
* Export UI
*/
public class ExportView extends WarningView {
/**
* Controller of UI
*/
private ExportController controller;
private JDialog dialog;
/**
* Tabs to switch between different file types
*/
private JTabbedPane tabbedPane;
/**
* UI to export CSV file
*/
private CSVPanel csvPnlMain;
/**
* UI Elements
*/
private JButton btnCancel, btnImport;
private JFileChooser fchBrowse;
private JLabel info;
/**
* Constructor of Export View
* @param controller - that handels user input
*/
public ExportView(ExportController controller) {
this.controller = controller;
controller.setValidationController(DataSource.Type.CSV);
initGUI();
controller.setExportView(this);
}
/**
* Initializes the UI
*/
private void initGUI() {
// Set up the tabbed pane and main panels and buttons
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
btnCancel = new JButton("Cancel");
btnCancel.setMnemonic('C');
btnImport = new JButton("Export");
btnImport.setMnemonic('E');
csvPnlMain = new CSVPanel();
csvPnlMain.initGUI();
info = new JLabel("You can only change to Ignore and add Timestamps");
tabbedPane.addTab("CSV", csvPnlMain);
ImportActionListener csvAl = new ImportActionListener();
btnImport.addActionListener(csvAl);
btnCancel.addActionListener(csvAl);
this.setLayout(new MigLayout(""));
this.add(info, "wrap");
this.add(tabbedPane, "grow, push, span, wrap");
this.add(btnCancel, "tag cancel");
this.add(btnImport, "tag apply");
fchBrowse = new JFileChooser(System.getProperty("user.home"));
fchBrowse.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
}
/**
* Return the selected source type to export
* @return
*/
public DataSource.Type getSelectedSourceType() {
switch (tabbedPane.getSelectedIndex()) {
case 0:
return DataSource.Type.CSV;
default:
throw new IllegalStateException("The selected tab/source is not supported.");
}
}
public void showAsDialog(Frame owner) {
controller.defaultValues();
dialog = new JDialog(owner, "Export Dialog", true);
dialog.add(this, BorderLayout.CENTER);
dialog.pack();
dialog.setVisible(true);
}
public void disposeDialog() {
if (this.dialog != null)
dialog.dispose();
}
/**
* For testing...
*/
public void setCSVValues(String lineSeparator, String elemSeparator,
String location,
DefaultTableModel fieldsTblModel, TableCellEditor typeEditor, String date,
String units, String unitsSeparator, String from, String to) {
csvPnlMain.txfLineSeparator.setText(lineSeparator);
csvPnlMain.txfElemSeparator.setText(elemSeparator);
csvPnlMain.txfExportLocation.setText(location);
csvPnlMain.columnDesciptionPanel.tblModel = fieldsTblModel;
csvPnlMain.columnDesciptionPanel.table.setModel(fieldsTblModel);
csvPnlMain.columnDesciptionPanel.table.getColumnModel().getColumn(2)
.setCellEditor(typeEditor);
}
private boolean nonEmpty(String s) {
return s != null && s.length() > 0;
}
private class ImportActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnImport) {
switch (getSelectedSourceType()) {
case CSV:
controller.setValidationController(DataSource.Type.CSV);
csvPnlMain.validateAll(controller);
controller.doExport(DataSink.Type.CSV);
break;
}
} else if (e.getSource() == btnCancel) {
dialog.dispose();
}
}
}
private class CSVPanel extends JPanel {
JTextField txfLineSeparator, txfElemSeparator, txfExportLocation;
JLabel lblLineSeparator, lblElemSeparator, lblExportLocation;
JButton btnBrowse;
ColumnDescriptionPanel columnDesciptionPanel;
void initGUI() {
lblLineSeparator = new JLabel("Line Separator:");
lblElemSeparator = new JLabel("Element Separator:");
lblExportLocation = new JLabel("Location:");
txfLineSeparator = new JTextField(3);
txfLineSeparator.setText("\n");
txfElemSeparator = new JTextField(3);
txfElemSeparator.setText(",");
txfExportLocation = new JTextField(20);
btnBrowse = new JButton("Browse");
this.setLayout(new MigLayout("", "", "[nogrid][nogrid][]"));
// These following components share one cell and constitute a row
this.add(lblExportLocation, "align label");
this.add(txfExportLocation, "pushx, growx");
this.add(btnBrowse, "gapleft unrel, align right, wrap");
// 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");
columnDesciptionPanel = new ColumnDescriptionPanel(
new String[] { "#", "Name", "Type" }, 2);
columnDesciptionPanel.initGUI();
JPanel splitPane = columnDesciptionPanel;
this.add(splitPane, "pushx, growx, wrap");
ActionListener al = new ActionListenerImpl();
btnBrowse.addActionListener(al);
}
void validateAll(ExportController controller) {
controller.clearValidations();
controller.validateLocation(txfExportLocation.getText());
controller.validateCSVElemSep(txfElemSeparator.getText());
controller.validateCSVLineSep(txfLineSeparator.getText());
controller.validateCSVColumnDescriptions(columnDesciptionPanel.tblModel, true);
Map<Integer, TimestampFormat> formats = new HashMap<>();
for (Integer key : columnDesciptionPanel.timePanels.keySet()) {
TimespecPanel panel = columnDesciptionPanel.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 class ActionListenerImpl implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnBrowse) {
int result = fchBrowse.showSaveDialog(ExportView.this);
if (result == JFileChooser.APPROVE_OPTION) {
File fileToOpen = fchBrowse.getSelectedFile();
txfExportLocation.setText("file://" + fileToOpen.getPath() + ".csv");
}
}
}
}
}
private class ColumnDescriptionPanel extends JPanel {
JTable table;
JScrollPane scrollPane;
JButton btnAdd, btnRemove;
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");
tblModel.setValueAt(0, 0, 0);
tblModel.setValueAt(1, 1, 0);
table = new JTable(tblModel);
table.getColumnModel()
.getColumn(2)
.setCellEditor(
new DefaultCellEditor(new JComboBox<DataColumn.Type>(DataColumn.Type
.values())));
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, wrap, 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);
}
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, growx, wrap");
}
validate();
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) {
Type colType =
(Type) tblModel.getValueAt(table.getSelectedRow(), typeIndex);
switch (getSelectedSourceType()) {
case CSV:
if (colType != null && colType.isTimeStamp()) {
if (!timePanels.containsKey(min)) {
timePanels.put(min, new TimespecPanel());
}
showTimePanel(min);
} else {
showTimePanel(-1);
}
break;
}
}
}
}
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), "",
DataColumn.Type.IGNORE, "" });
} else if (e.getSource() == btnRemove) {
CellEditor editor = table.getCellEditor();
if (editor != null)
editor.stopCellEditing();
maybeRemoveTimePanel(table.getSelectedRow());
if (controller.validateDeleteColumn(table.getSelectedRow())) {
tblModel.removeRow(table.getSelectedRow());
}
}
}
}
}
class TimespecPanel extends JPanel {
JCheckBox chbApplyRegex;
JRadioButton rdbDate, rdbCustDate;
ButtonGroup btnGroup;
JTextField txfDate, txfCustUnits,
txfCustSeparator;
public TimespecPanel() {
initGUI();
}
void initGUI() {
this.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Timestamp specifications"));
chbApplyRegex = new JCheckBox("Apply Regex");
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("Units:");
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(rdbDate, "span 2");
this.add(rdbCustDate, "span 2, wrap");
this.add(lblDate);
this.add(txfDate);
this.add(lblCustUnits);
this.add(txfCustUnits);
this.add(lblCustSeparator);
this.add(txfCustSeparator, "wrap");
}
}
public Dialog getDialog() {
return this.dialog;
}
}