3
import java.awt.BorderLayout;
5
import java.awt.Component;
6
import java.awt.Dimension;
7
import java.awt.FlowLayout;
9
import java.awt.GridBagConstraints;
10
import java.awt.GridBagLayout;
11
import java.awt.Insets;
12
import java.awt.Point;
13
import java.awt.Rectangle;
14
import java.awt.event.ActionEvent;
15
import java.awt.event.ActionListener;
16
import java.awt.event.KeyAdapter;
17
import java.awt.event.KeyEvent;
18
import java.awt.event.MouseEvent;
20
import java.io.IOException;
21
import java.nio.file.Files;
22
import java.nio.file.Path;
23
import java.nio.file.Paths;
24
import java.util.ArrayList;
25
import java.util.Collection;
26
import java.util.Comparator;
27
import java.util.HashMap;
28
import java.util.List;
29
import javax.swing.BorderFactory;
30
import javax.swing.DefaultListModel;
31
import javax.swing.JButton;
32
import javax.swing.JCheckBox;
33
import javax.swing.JDialog;
34
import javax.swing.JLabel;
35
import javax.swing.JList;
36
import javax.swing.JPanel;
37
import javax.swing.JProgressBar;
38
import javax.swing.JScrollPane;
39
import javax.swing.JTable;
40
import javax.swing.JTextField;
41
import javax.swing.ListSelectionModel;
42
import javax.swing.border.Border;
43
import javax.swing.event.DocumentEvent;
44
import javax.swing.event.DocumentListener;
45
import javax.swing.table.TableCellRenderer;
46
import javax.swing.table.TableModel;
47
import javax.swing.table.TableRowSorter;
48
import javax.xml.parsers.ParserConfigurationException;
49
import javax.xml.transform.TransformerConfigurationException;
50
import javax.xml.transform.TransformerException;
51
import org.w3c.dom.DOMException;
52
import dk.aau.cs.gui.FileNameCellRenderer;
53
import dk.aau.cs.gui.components.BatchProcessingResultsTableModel;
54
import dk.aau.cs.gui.components.ExportBatchResultTableModel;
55
import dk.aau.cs.io.LoadedModel;
56
import dk.aau.cs.io.ModelLoader;
57
import dk.aau.cs.io.PNMLWriter;
58
import dk.aau.cs.model.tapn.TimedArcPetriNet;
59
import dk.aau.cs.util.StringComparator;
60
import dk.aau.cs.verification.batchProcessing.BatchProcessingVerificationResult;
61
import pipe.dataLayer.DataLayer;
62
import pipe.dataLayer.TAPNQuery;
63
import pipe.gui.widgets.FileBrowser;
65
public class ExportBatchDialog extends JDialog {
67
private static final long serialVersionUID = 8346966786414688380L;
69
private final static String TOOL_TIP_AddFilesButton = "Press to add nets to batch export";
70
private final static String TOOL_TIP_RemoveFilesButton = "Press to remove the currently selected nets";
71
private final static String TOOL_TIP_ClearFilesButton = "Press to remove all nets from list";
72
private final static String TOOL_TIP_ExportFilesButton = "Press to export all nets in PNML and XML format";
73
private final static String TOOL_TIP_UniqueQueryNamesCheckbox = "Give queries unique names when exporting";
74
private final static String NAME_SuccesString = "Succeeded";
75
private final static String NAME_SuccesStringOrphanTransitionsRemoved = "Succeeded, orphan transitions removed";
76
private final static String NAME_FailStringFolderExists = "Failed as the subfolder already exists";
77
private final static String NAME_FailStringParseError = "Failed due to net/query parsing error";
80
private JPanel filesButtonsPanel;
81
private JPanel mainPanel;
82
private JButton addFilesButton;
83
private JButton clearFilesButton;
84
private JButton removeFileButton;
85
private JButton exportFilesButton;
86
private JPanel chooserPanel;
87
private JTextField destinationPathField;
88
private JList fileList;
89
private DefaultListModel listModel;
90
private List<File> files = new ArrayList<File>();
91
private String lastExportPath;
92
private String lastSelectPath;
93
private JCheckBox uniqueQueryNames;
94
private File destinationFile;
95
private ExportBatchResultTableModel tableModel;
97
private Thread progressBarThread;
98
private JProgressBar progressBar;
99
private JDialog progressBarContainer;
100
static boolean noOrphanTransitions = false;
102
static ExportBatchDialog exportBatchDialog;
103
ModelLoader loader = new ModelLoader(new DrawingSurfaceImpl(new DataLayer()));
105
public static boolean isDialogVisible() {
106
return exportBatchDialog.isVisible();
109
public static void setNoOrphanTransitions(boolean value) {
110
noOrphanTransitions = value;
113
public static void ShowExportBatchDialog(){
114
if(exportBatchDialog == null){
115
exportBatchDialog = new ExportBatchDialog(CreateGui.getApp(), "Batch Export", true);
116
exportBatchDialog.pack();
117
exportBatchDialog.setPreferredSize(exportBatchDialog.getSize());
118
exportBatchDialog.setMinimumSize(new Dimension(exportBatchDialog.getWidth(), exportBatchDialog.getHeight()));
119
exportBatchDialog.setLocationRelativeTo(null);
120
exportBatchDialog.setResizable(true);
122
exportBatchDialog.setVisible(true);
125
private ExportBatchDialog(Frame frame, String title, boolean modal) {
126
super(frame, title, modal);
130
private void initComponents() {
131
setLayout(new FlowLayout());
132
mainPanel = new JPanel(new GridBagLayout());
138
setContentPane(mainPanel);
141
private void initProgressPanel() {
142
JPanel resultPanel = new JPanel(new GridBagLayout());
143
resultPanel.setBorder(BorderFactory.createTitledBorder("Exported Files"));
145
tableModel = new ExportBatchResultTableModel();
146
final JTable resultTable = new JTable(tableModel){
147
private static final long serialVersionUID = 8524549736351991872L;
149
public String getToolTipText(MouseEvent e) {
151
java.awt.Point point = e.getPoint();
152
int rowIndex = rowAtPoint(point);
153
int colIndex = columnAtPoint(point);
156
tip = getValueAt(rowIndex, colIndex).toString();
158
catch (RuntimeException e1) {
165
resultTable.getColumn("Status").setCellRenderer(new ExportResultTableCellRenderer(true));
167
resultTable.getColumn("Destination").setCellRenderer(new ExportResultTableCellRenderer(true));
170
Comparator<Object> comparator = new StringComparator();
172
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(resultTable.getModel());
173
for(int i = 0; i < resultTable.getColumnCount(); i++){
174
sorter.setComparator(i, comparator);
176
resultTable.setRowSorter(sorter);
178
JScrollPane scrollPane = new JScrollPane(resultTable);
179
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
180
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
182
GridBagConstraints gbc = new GridBagConstraints();
187
gbc.fill = GridBagConstraints.BOTH;
188
gbc.insets = new Insets(0, 0, 0, 0);
189
gbc.anchor = GridBagConstraints.NORTHWEST;
190
resultPanel.add(scrollPane, gbc);
193
gbc = new GridBagConstraints();
198
gbc.fill = GridBagConstraints.BOTH;
199
gbc.insets = new Insets(0, 0, 10, 5);
200
gbc.anchor = GridBagConstraints.NORTHWEST;
202
mainPanel.add(resultPanel, gbc);
205
private void initChooserPanel() {
206
chooserPanel = new JPanel(new GridBagLayout());
207
chooserPanel.setBorder(BorderFactory.createTitledBorder("Export to destination"));
209
destinationPathField = new JTextField("", 30);
210
destinationPathField.setEditable(true);
211
destinationPathField.getDocument().addDocumentListener(new DocumentListener() {
213
public void changedUpdate(DocumentEvent e) {
218
public void insertUpdate(DocumentEvent e) {
223
public void removeUpdate(DocumentEvent e) {
227
GridBagConstraints gbc = new GridBagConstraints();
232
gbc.fill = GridBagConstraints.BOTH;
233
gbc.anchor = GridBagConstraints.WEST;
234
chooserPanel.add(destinationPathField, gbc);
236
gbc = new GridBagConstraints();
239
gbc.anchor = GridBagConstraints.WEST;
240
gbc.insets = new Insets(0, 10, 0, 0);
241
JButton destinationPathSelector = new JButton("Select destination folder");
242
destinationPathSelector.addActionListener(new ActionListener() {
243
public void actionPerformed(ActionEvent e) {
244
selectDestinationPath();
245
destinationPathField.setText(destinationFile.getParent());
249
chooserPanel.add(destinationPathSelector, gbc);
252
uniqueQueryNames = new JCheckBox("Use unique query names", true);
253
uniqueQueryNames.setToolTipText(TOOL_TIP_UniqueQueryNamesCheckbox);
255
gbc = new GridBagConstraints();
258
gbc.anchor = GridBagConstraints.NORTHWEST;
259
gbc.insets = new Insets(10, 0, 0, 0);
260
chooserPanel.add(uniqueQueryNames, gbc);
262
exportFilesButton = new JButton("Export All Nets and Queries");
263
exportFilesButton.setToolTipText(TOOL_TIP_ExportFilesButton);
264
exportFilesButton.setEnabled(false);
265
exportFilesButton.addActionListener(new ActionListener() {
266
public void actionPerformed(ActionEvent e) {
272
gbc = new GridBagConstraints();
275
gbc.anchor = GridBagConstraints.WEST;
276
gbc.insets = new Insets(10, 10, 0, 0);
278
chooserPanel.add(exportFilesButton, gbc);
280
gbc = new GridBagConstraints();
285
gbc.insets = new Insets(10, 0, 10, 5);
286
gbc.fill = GridBagConstraints.HORIZONTAL;
287
gbc.anchor = GridBagConstraints.NORTHWEST;
289
mainPanel.add(chooserPanel, gbc);
292
private void initFileList() {
293
JPanel fileListPanel = new JPanel(new GridBagLayout());
294
fileListPanel.setBorder(BorderFactory.createTitledBorder("Files"));
296
listModel = new DefaultListModel();
297
fileList = new JList(listModel);
298
fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
299
fileList.setSelectedIndex(0);
300
fileList.addKeyListener(new KeyAdapter() {
302
public void keyPressed(KeyEvent e) {
303
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
304
removeSelectedFiles();
309
fileList.setCellRenderer(new FileNameCellRenderer());
310
GridBagConstraints gbc = new GridBagConstraints();
312
JScrollPane scrollpane = new JScrollPane(fileList);
313
scrollpane.setMinimumSize(new Dimension(175, 375));
314
scrollpane.setPreferredSize(new Dimension(250, 375));
316
gbc = new GridBagConstraints();
319
gbc.fill = GridBagConstraints.BOTH;
323
gbc.insets = new Insets(0, 0, 10, 0);
324
fileListPanel.add(scrollpane, gbc);
326
filesButtonsPanel = new JPanel(new GridBagLayout());
328
addFilesButton = new JButton("Add models");
329
addFilesButton.setToolTipText(TOOL_TIP_AddFilesButton);
330
addFilesButton.addActionListener(new ActionListener() {
331
public void actionPerformed(ActionEvent arg0) {
337
gbc = new GridBagConstraints();
338
gbc.anchor = GridBagConstraints.NORTHWEST;
342
gbc.insets = new Insets(0, 0, 0, 5);
343
filesButtonsPanel.add(addFilesButton, gbc);
345
removeFileButton = new JButton("Remove models");
346
removeFileButton.setToolTipText(TOOL_TIP_RemoveFilesButton);
347
removeFileButton.setEnabled(false);
348
removeFileButton.addActionListener(new ActionListener() {
350
public void actionPerformed(ActionEvent arg0) {
351
removeSelectedFiles();
355
gbc = new GridBagConstraints();
358
gbc.anchor = GridBagConstraints.WEST;
359
gbc.insets = new Insets(0, 0, 0, 5);
360
filesButtonsPanel.add(removeFileButton, gbc);
362
clearFilesButton = new JButton("Clear");
363
clearFilesButton.setToolTipText(TOOL_TIP_ClearFilesButton);
364
clearFilesButton.setEnabled(false);
365
clearFilesButton.addActionListener(new ActionListener() {
367
public void actionPerformed(ActionEvent e) {
373
gbc = new GridBagConstraints();
376
gbc.anchor = GridBagConstraints.WEST;
377
filesButtonsPanel.add(clearFilesButton, gbc);
379
gbc = new GridBagConstraints();
382
gbc.fill = GridBagConstraints.HORIZONTAL;
383
gbc.anchor = GridBagConstraints.NORTHEAST;
384
fileListPanel.add(filesButtonsPanel, gbc);
386
gbc = new GridBagConstraints();
390
gbc.fill = GridBagConstraints.BOTH;
391
gbc.anchor = GridBagConstraints.NORTHEAST;
393
gbc.insets = new Insets(10, 5, 10, 5);
395
mainPanel.add(fileListPanel, gbc);
398
private void initProgressBar() {
399
progressBarContainer = new JDialog(exportBatchDialog, "Exporting...", true);
400
progressBar = new JProgressBar(0, fileList.getModel().getSize());
402
progressBarContainer.add(BorderLayout.CENTER, progressBar);
403
progressBarContainer.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
404
progressBarContainer.setSize(400, 100);
405
progressBarContainer.setLocationRelativeTo(exportBatchDialog);
406
progressBarContainer.setVisible(false);
407
progressBar.setStringPainted(true);
408
progressBar.setString("Exported Nets: 0 of " + fileList.getModel().getSize());
409
progressBar.setValue(0);
411
progressBarThread = new Thread(new Runnable() {
413
progressBarContainer.setVisible(true);
418
private void addFiles() {
419
FileBrowser browser = new FileBrowser("Timed-Arc Petri Nets",".xml", lastSelectPath);
421
File[] filesArray = browser.openFiles();
422
if (filesArray.length>0) {
423
for (File file : filesArray) {
424
lastSelectPath = file.getParent();
425
if (!files.contains(file)) {
427
listModel.addElement(file);
435
private void removeSelectedFiles() {
436
for (Object o : fileList.getSelectedValuesList()) {
437
File file = (File) o;
439
listModel.removeElement(file);
444
private void enableButtons() {
445
fileList.setEnabled(true);
446
uniqueQueryNames.setEnabled(true);
447
addFilesButton.setEnabled(true);
449
if (listModel.size() > 0) {
450
clearFilesButton.setEnabled(true);
451
removeFileButton.setEnabled(true);
453
clearFilesButton.setEnabled(false);
454
removeFileButton.setEnabled(false);
456
if(listModel.size() > 0 && destinationFile != null && new File(destinationPathField.getText()).exists()) {
457
exportFilesButton.setEnabled(true);
460
exportFilesButton.setEnabled(false);
463
private void clearFiles() {
465
listModel.removeAllElements();
468
private void selectDestinationPath() {
469
String chosenFile = new FileBrowser("Select an export folder", ".", lastExportPath).saveFile("Export");
470
if(chosenFile != null) {
471
destinationFile = new File(chosenFile);
472
lastExportPath = chosenFile;
477
private void exportFiles() {
481
if(destinationFile != null && destinationFile.exists()) {
482
String destPath = destinationFile.isFile() ? destinationFile.getParent() : destinationFile.getAbsolutePath();
483
lastExportPath = destPath;
484
progressBarThread.start();
485
for(File file : files) {
486
Path path = Paths.get(destPath + "/" + file.getName().replaceAll(".xml", ""));
488
if(!(Files.exists(path))) {
489
Files.createDirectories(path);
490
exportModel(file, path);
491
tableModel.addResult(noOrphanTransitions == false ? new String[]{file.getName(), path.toString(), NAME_SuccesString}
492
: new String[]{file.getName(), path.toString(), NAME_SuccesStringOrphanTransitionsRemoved});
495
tableModel.addResult(new String[]{file.getName(), path.toString(), NAME_FailStringFolderExists});
499
tableModel.addResult(new String[]{file.getName(), path.toString() , NAME_FailStringParseError});
501
//For the loading bar
502
progressBar.setString("Exported Nets: " + files.indexOf(file) + " of " + files.size());
503
progressBar.setValue(files.indexOf(file));
504
progressBar.paintImmediately(new Rectangle(0, 0, progressBar.getWidth(), progressBar.getHeight()));
505
noOrphanTransitions = false;
506
//reset loading bar when done
507
if(progressBar.getValue() == files.size()-1) {
508
progressBarContainer.setVisible(false);
509
progressBar.setValue(0);
513
else if(destinationFile == null) {
514
new MessengerImpl().displayErrorMessage("Please choose a folder for exporting");
516
else if(!(destinationFile.exists())) {
517
new MessengerImpl().displayErrorMessage("The chosen path does not exist");
520
public void textFieldChanged() {
521
destinationFile = new File(destinationPathField.getText());
525
private void exportModel(File file, Path path) throws Exception {
526
LoadedModel loadedModel = loader.load(file);
527
exportPNML(path, loadedModel);
528
if(!uniqueQueryNames.isSelected())
529
Export.toQueryXML(loadedModel.network(), path.toString() + "/query.xml", loadedModel.queries());
531
Export.toQueryXML(loadedModel.network(), path.toString() + "/query.xml", renameQueries(file.getName(), loadedModel.queries()));
535
private void exportPNML(Path path, LoadedModel loadedModel) throws DOMException, TransformerConfigurationException, IOException, ParserConfigurationException, TransformerException {
536
File f = new File(path.toString() + "/model.pnml");
537
HashMap<TimedArcPetriNet, DataLayer> guiModel = new HashMap<TimedArcPetriNet, DataLayer>();
538
for(pipe.dataLayer.Template template : loadedModel.templates()) {
539
guiModel.put(template.model(), template.guiModel());
541
PNMLWriter pnmlWriter = new PNMLWriter(loadedModel.network(), guiModel);
542
pnmlWriter.savePNML(f);
545
private Collection<pipe.dataLayer.TAPNQuery> renameQueries(String fileName, Collection<pipe.dataLayer.TAPNQuery> queries){
546
Collection<pipe.dataLayer.TAPNQuery> renamedQueries = new ArrayList<pipe.dataLayer.TAPNQuery>();
549
for(pipe.dataLayer.TAPNQuery query : queries) {
550
pipe.dataLayer.TAPNQuery copy = query;
551
copy.setName((fileName.replaceAll(".xml", "") + "." + query.getName() + "-" + index).replaceAll(" ", "_"));
552
renamedQueries.add(copy);
555
return renamedQueries;
558
private class ExportResultTableCellRenderer extends JLabel implements
560
private static final long serialVersionUID = -1054925029991763163L;
561
Border unselectedBorder = null;
562
Border selectedBorder = null;
563
boolean isBordered = true;
565
public ExportResultTableCellRenderer(boolean isBordered) {
566
this.isBordered = isBordered;
569
public Component getTableCellRendererComponent(JTable table,
570
Object value, boolean isSelected, boolean hasFocus, int row,
574
setBackground(table.getSelectionBackground());
575
setForeground(table.getSelectionForeground());
577
if (selectedBorder == null) {
578
selectedBorder = BorderFactory.createMatteBorder(2, 5,
579
2, 5, table.getSelectionBackground());
581
setBorder(selectedBorder);
583
boolean isResultColumn = table.getColumnName(column)
586
if ((isResultColumn && (value.toString().equals(NAME_SuccesString)) || value.toString().equals(NAME_SuccesStringOrphanTransitionsRemoved))) {
587
setBackground(new Color(91, 255, 91)); // light green
589
else if (isResultColumn && value.toString().equals(NAME_FailStringParseError)) {
590
setBackground(new Color(255, 91, 91)); // light red
592
else if (isResultColumn && value.toString().equals(NAME_FailStringFolderExists)) {
593
setBackground(new Color(255, 255, 120)); // light yellow
596
setBackground(table.getBackground());
598
setForeground(table.getForeground());
599
if (unselectedBorder == null) {
600
unselectedBorder = BorderFactory.createMatteBorder(2,
601
5, 2, 5, table.getBackground());
603
setBorder(unselectedBorder);
607
setEnabled(table.isEnabled());
608
setFont(table.getFont());
612
if (table.getColumnName(column).equals(
614
setText(".../"+new File(value.toString()).getName());
615
Point mousePos = table.getMousePosition();
616
String[] result = null;
617
if (mousePos != null) {
618
result = ((ExportBatchResultTableModel) table
619
.getModel()).getResult(table
620
.rowAtPoint(mousePos));
622
setToolTipText(result != null ? generateDestinationTooltip(result) : value.toString());
624
setToolTipText(value.toString());
625
setText(value.toString());
635
private String generateDestinationTooltip(String[] result) {
636
String fullFilePath = result[1];