~tapaal-contributor/tapaal/weight-values-fix-1770637

« back to all changes in this revision

Viewing changes to src/pipe/gui/ExportBatchDialog.java

merged in branch lp:~tapaal-contributor/tapaal/Batch-export-PNML-XML-queries-1754675
adding batch export of files to PNML and XML

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package pipe.gui;
 
2
 
 
3
import java.awt.BorderLayout;
 
4
import java.awt.Color;
 
5
import java.awt.Component;
 
6
import java.awt.Dimension;
 
7
import java.awt.FlowLayout;
 
8
import java.awt.Frame;
 
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;
 
19
import java.io.File;
 
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;
 
64
 
 
65
public class ExportBatchDialog extends JDialog {
 
66
 
 
67
        private static final long serialVersionUID = 8346966786414688380L;
 
68
 
 
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";
 
78
 
 
79
        
 
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;
 
96
        
 
97
        private Thread progressBarThread;
 
98
        private JProgressBar progressBar;
 
99
        private JDialog progressBarContainer;
 
100
        static boolean noOrphanTransitions = false;
 
101
 
 
102
        static ExportBatchDialog exportBatchDialog;
 
103
        ModelLoader loader = new ModelLoader(new DrawingSurfaceImpl(new DataLayer()));
 
104
        
 
105
        public static boolean isDialogVisible() {
 
106
                return exportBatchDialog.isVisible();
 
107
        }
 
108
        
 
109
        public static void setNoOrphanTransitions(boolean value) {
 
110
                noOrphanTransitions = value;
 
111
        }
 
112
        
 
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);
 
121
                }
 
122
                exportBatchDialog.setVisible(true);
 
123
        }
 
124
        
 
125
        private ExportBatchDialog(Frame frame, String title, boolean modal) {
 
126
                super(frame, title, modal);
 
127
                initComponents();
 
128
        }
 
129
        
 
130
        private void initComponents() {
 
131
                setLayout(new FlowLayout());
 
132
                mainPanel = new JPanel(new GridBagLayout());
 
133
                
 
134
                initFileList();
 
135
                initChooserPanel();
 
136
                initProgressPanel();
 
137
                
 
138
                setContentPane(mainPanel);
 
139
        }
 
140
        
 
141
        private void initProgressPanel() {
 
142
                JPanel resultPanel = new JPanel(new GridBagLayout());
 
143
                resultPanel.setBorder(BorderFactory.createTitledBorder("Exported Files"));
 
144
                
 
145
                tableModel = new ExportBatchResultTableModel();
 
146
                final JTable resultTable = new JTable(tableModel){
 
147
                        private static final long serialVersionUID = 8524549736351991872L;
 
148
 
 
149
                        public String getToolTipText(MouseEvent e) {
 
150
                                String tip= null;
 
151
                                java.awt.Point point = e.getPoint();
 
152
                                int rowIndex = rowAtPoint(point);
 
153
                                int colIndex = columnAtPoint(point);
 
154
                                
 
155
                                try {
 
156
                                        tip = getValueAt(rowIndex, colIndex).toString();
 
157
                                }
 
158
                                catch (RuntimeException e1) {
 
159
                                        tip = "";
 
160
                                }
 
161
                                return tip;
 
162
                        }
 
163
                };
 
164
                //for coloring cells
 
165
                resultTable.getColumn("Status").setCellRenderer(new ExportResultTableCellRenderer(true));
 
166
                //for tooltips
 
167
                resultTable.getColumn("Destination").setCellRenderer(new ExportResultTableCellRenderer(true));
 
168
                                
 
169
                // Enable sorting
 
170
                Comparator<Object> comparator = new StringComparator();
 
171
                
 
172
                TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(resultTable.getModel());
 
173
                for(int i = 0; i < resultTable.getColumnCount(); i++){
 
174
                        sorter.setComparator(i, comparator);
 
175
                }
 
176
                resultTable.setRowSorter(sorter);
 
177
                
 
178
                JScrollPane scrollPane = new JScrollPane(resultTable);
 
179
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
 
180
                scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 
181
 
 
182
                GridBagConstraints gbc = new GridBagConstraints();
 
183
                gbc.gridx = 0;
 
184
                gbc.gridy = 0;
 
185
                gbc.weightx = 1.0;
 
186
                gbc.weighty = 1.0;
 
187
                gbc.fill = GridBagConstraints.BOTH;
 
188
                gbc.insets = new Insets(0, 0, 0, 0);
 
189
                gbc.anchor = GridBagConstraints.NORTHWEST;
 
190
                resultPanel.add(scrollPane, gbc);
 
191
                
 
192
                
 
193
                gbc = new GridBagConstraints();
 
194
                gbc.gridx = 1;
 
195
                gbc.gridy = 1;
 
196
                gbc.weightx = 1.0;
 
197
                gbc.weighty = 1.0;
 
198
                gbc.fill = GridBagConstraints.BOTH;
 
199
                gbc.insets = new Insets(0, 0, 10, 5);
 
200
                gbc.anchor = GridBagConstraints.NORTHWEST;
 
201
                
 
202
                mainPanel.add(resultPanel, gbc);
 
203
        }
 
204
        
 
205
        private void initChooserPanel() {
 
206
                chooserPanel = new JPanel(new GridBagLayout());
 
207
                chooserPanel.setBorder(BorderFactory.createTitledBorder("Export to destination"));
 
208
                
 
209
                destinationPathField = new JTextField("", 30);
 
210
                destinationPathField.setEditable(true);
 
211
                destinationPathField.getDocument().addDocumentListener(new DocumentListener() {                 
 
212
                        @Override
 
213
                        public void changedUpdate(DocumentEvent e) {
 
214
                                textFieldChanged();
 
215
                        }
 
216
 
 
217
                        @Override
 
218
                        public void insertUpdate(DocumentEvent e) {
 
219
                                textFieldChanged();
 
220
                        }
 
221
 
 
222
                        @Override
 
223
                        public void removeUpdate(DocumentEvent e) {
 
224
                                textFieldChanged();                             
 
225
                        }
 
226
                });
 
227
                GridBagConstraints gbc = new GridBagConstraints();
 
228
                gbc.gridx = 0;
 
229
                gbc.gridy = 0;
 
230
                gbc.weightx = 1.0;
 
231
                gbc.weighty = 1.0;
 
232
                gbc.fill = GridBagConstraints.BOTH;
 
233
                gbc.anchor = GridBagConstraints.WEST;
 
234
                chooserPanel.add(destinationPathField, gbc);
 
235
                
 
236
                gbc = new GridBagConstraints();
 
237
                gbc.gridx = 1;
 
238
                gbc.gridy = 0;
 
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());
 
246
                                enableButtons();
 
247
                        }
 
248
                });
 
249
                chooserPanel.add(destinationPathSelector, gbc);
 
250
                
 
251
 
 
252
                uniqueQueryNames = new JCheckBox("Use unique query names", true);
 
253
                uniqueQueryNames.setToolTipText(TOOL_TIP_UniqueQueryNamesCheckbox);
 
254
                
 
255
                gbc = new GridBagConstraints();
 
256
                gbc.gridx = 0;
 
257
                gbc.gridy = 1;
 
258
                gbc.anchor = GridBagConstraints.NORTHWEST;
 
259
                gbc.insets = new Insets(10, 0, 0, 0);
 
260
                chooserPanel.add(uniqueQueryNames, gbc);
 
261
                
 
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) {
 
267
                                tableModel.clear();
 
268
                                exportFiles();
 
269
                                enableButtons();
 
270
                        }
 
271
                });
 
272
                gbc = new GridBagConstraints();
 
273
                gbc.gridx = 1;
 
274
                gbc.gridy = 1;
 
275
                gbc.anchor = GridBagConstraints.WEST;
 
276
                gbc.insets = new Insets(10, 10, 0, 0);
 
277
 
 
278
                chooserPanel.add(exportFilesButton, gbc);
 
279
                
 
280
                gbc = new GridBagConstraints();
 
281
                gbc.gridx = 1;
 
282
                gbc.gridy = 0;
 
283
                gbc.weightx = 0;
 
284
                gbc.weighty = 0;
 
285
                gbc.insets = new Insets(10, 0, 10, 5);
 
286
                gbc.fill = GridBagConstraints.HORIZONTAL;
 
287
                gbc.anchor = GridBagConstraints.NORTHWEST;
 
288
                
 
289
                mainPanel.add(chooserPanel, gbc);
 
290
        }
 
291
        
 
292
        private void initFileList() {
 
293
                JPanel fileListPanel = new JPanel(new GridBagLayout());
 
294
                fileListPanel.setBorder(BorderFactory.createTitledBorder("Files"));
 
295
 
 
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() {
 
301
                        @Override
 
302
                        public void keyPressed(KeyEvent e) {
 
303
                                if (e.getKeyCode() == KeyEvent.VK_DELETE) {
 
304
                                        removeSelectedFiles();
 
305
                                }
 
306
                        }
 
307
                });
 
308
 
 
309
                fileList.setCellRenderer(new FileNameCellRenderer());
 
310
                GridBagConstraints gbc = new GridBagConstraints();
 
311
 
 
312
                JScrollPane scrollpane = new JScrollPane(fileList);
 
313
                scrollpane.setMinimumSize(new Dimension(175, 375));
 
314
                scrollpane.setPreferredSize(new Dimension(250, 375));
 
315
 
 
316
                gbc = new GridBagConstraints();
 
317
                gbc.gridx = 0;
 
318
                gbc.gridy = 0;
 
319
                gbc.fill = GridBagConstraints.BOTH;
 
320
                gbc.gridwidth = 3;
 
321
                gbc.weightx = 1.0;
 
322
                gbc.weighty = 1.0;
 
323
                gbc.insets = new Insets(0, 0, 10, 0);
 
324
                fileListPanel.add(scrollpane, gbc);
 
325
 
 
326
                filesButtonsPanel = new JPanel(new GridBagLayout());
 
327
                
 
328
                addFilesButton = new JButton("Add models");
 
329
                addFilesButton.setToolTipText(TOOL_TIP_AddFilesButton);
 
330
                addFilesButton.addActionListener(new ActionListener() {
 
331
                        public void actionPerformed(ActionEvent arg0) {
 
332
                                addFiles();
 
333
                                enableButtons();
 
334
                        }
 
335
                });
 
336
 
 
337
                gbc = new GridBagConstraints();
 
338
                gbc.anchor = GridBagConstraints.NORTHWEST;
 
339
                gbc.gridx = 0;
 
340
                gbc.gridy = 0;
 
341
                gbc.weightx = 1.0;
 
342
                gbc.insets = new Insets(0, 0, 0, 5);
 
343
                filesButtonsPanel.add(addFilesButton, gbc);
 
344
                
 
345
                removeFileButton = new JButton("Remove models");
 
346
                removeFileButton.setToolTipText(TOOL_TIP_RemoveFilesButton);
 
347
                removeFileButton.setEnabled(false);
 
348
                removeFileButton.addActionListener(new ActionListener() {
 
349
 
 
350
                        public void actionPerformed(ActionEvent arg0) {
 
351
                                removeSelectedFiles();
 
352
                                enableButtons();
 
353
                        }
 
354
                });
 
355
                gbc = new GridBagConstraints();
 
356
                gbc.gridx = 1;
 
357
                gbc.gridy = 0;
 
358
                gbc.anchor = GridBagConstraints.WEST;
 
359
                gbc.insets = new Insets(0, 0, 0, 5);
 
360
                filesButtonsPanel.add(removeFileButton, gbc);
 
361
                
 
362
                clearFilesButton = new JButton("Clear");
 
363
                clearFilesButton.setToolTipText(TOOL_TIP_ClearFilesButton);
 
364
                clearFilesButton.setEnabled(false);
 
365
                clearFilesButton.addActionListener(new ActionListener() {
 
366
 
 
367
                        public void actionPerformed(ActionEvent e) {
 
368
                                clearFiles();
 
369
                                enableButtons();
 
370
                        }
 
371
                });
 
372
                
 
373
                gbc = new GridBagConstraints();
 
374
                gbc.gridx = 2;
 
375
                gbc.gridy = 0;
 
376
                gbc.anchor = GridBagConstraints.WEST;
 
377
                filesButtonsPanel.add(clearFilesButton, gbc);
 
378
                
 
379
                gbc = new GridBagConstraints();
 
380
                gbc.gridx = 0;
 
381
                gbc.gridy = 2;
 
382
                gbc.fill = GridBagConstraints.HORIZONTAL;
 
383
                gbc.anchor = GridBagConstraints.NORTHEAST;
 
384
                fileListPanel.add(filesButtonsPanel, gbc);
 
385
                
 
386
                gbc = new GridBagConstraints();
 
387
                gbc.gridx = 0;
 
388
                gbc.gridy = 0;
 
389
                gbc.weightx = 1;
 
390
                gbc.fill = GridBagConstraints.BOTH;
 
391
                gbc.anchor = GridBagConstraints.NORTHEAST;
 
392
                gbc.gridheight = 2;
 
393
                gbc.insets = new Insets(10, 5, 10, 5);
 
394
                
 
395
                mainPanel.add(fileListPanel, gbc);
 
396
        }
 
397
        
 
398
        private void initProgressBar() {
 
399
                progressBarContainer = new JDialog(exportBatchDialog, "Exporting...", true);
 
400
                progressBar = new JProgressBar(0, fileList.getModel().getSize());
 
401
                
 
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);
 
410
                
 
411
                progressBarThread = new Thread(new Runnable() {
 
412
                        public void run() {
 
413
                                progressBarContainer.setVisible(true);
 
414
                        }
 
415
                });
 
416
        }
 
417
 
 
418
        private void addFiles() {
 
419
                FileBrowser browser = new FileBrowser("Timed-Arc Petri Nets",".xml", lastSelectPath);
 
420
                
 
421
                File[] filesArray = browser.openFiles();
 
422
                if (filesArray.length>0) {
 
423
                        for (File file : filesArray) {
 
424
                                lastSelectPath = file.getParent();
 
425
                                if (!files.contains(file)) {
 
426
                                        files.add(file);
 
427
                                        listModel.addElement(file);
 
428
                                }
 
429
                        }
 
430
 
 
431
                        enableButtons();
 
432
                }
 
433
        }
 
434
 
 
435
        private void removeSelectedFiles() {
 
436
                for (Object o : fileList.getSelectedValuesList()) {
 
437
                        File file = (File) o;
 
438
                        files.remove(file);
 
439
                        listModel.removeElement(file);
 
440
                }
 
441
                enableButtons();
 
442
        }
 
443
        
 
444
        private void enableButtons() {
 
445
                fileList.setEnabled(true);
 
446
                uniqueQueryNames.setEnabled(true);
 
447
                addFilesButton.setEnabled(true);
 
448
 
 
449
                if (listModel.size() > 0) {
 
450
                        clearFilesButton.setEnabled(true);
 
451
                        removeFileButton.setEnabled(true);
 
452
                } else {
 
453
                        clearFilesButton.setEnabled(false);
 
454
                        removeFileButton.setEnabled(false);
 
455
                }
 
456
                if(listModel.size() > 0 && destinationFile != null && new File(destinationPathField.getText()).exists()) {
 
457
                        exportFilesButton.setEnabled(true);
 
458
                }
 
459
                else
 
460
                        exportFilesButton.setEnabled(false);
 
461
 
 
462
        }
 
463
        private void clearFiles() {
 
464
                files.clear();
 
465
                listModel.removeAllElements();
 
466
        }
 
467
        
 
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;
 
473
                }
 
474
                else return;
 
475
        }
 
476
        
 
477
        private void exportFiles() {
 
478
                //loading bar
 
479
                initProgressBar();
 
480
                
 
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", ""));
 
487
                        try {
 
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});
 
493
                                }
 
494
                                else {
 
495
                                        tableModel.addResult(new String[]{file.getName(), path.toString(), NAME_FailStringFolderExists});
 
496
                                }
 
497
                        }
 
498
                        catch(Exception e){
 
499
                                tableModel.addResult(new String[]{file.getName(), path.toString() , NAME_FailStringParseError});
 
500
                }
 
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);
 
510
                        }
 
511
                }       
 
512
                }
 
513
                else if(destinationFile == null) {
 
514
                        new MessengerImpl().displayErrorMessage("Please choose a folder for exporting");
 
515
                }
 
516
                else if(!(destinationFile.exists())) {
 
517
                        new MessengerImpl().displayErrorMessage("The chosen path does not exist");
 
518
                }
 
519
    }
 
520
        public void textFieldChanged() {
 
521
                destinationFile = new File(destinationPathField.getText());
 
522
                enableButtons();
 
523
        }
 
524
        
 
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());
 
530
                        else {
 
531
                                Export.toQueryXML(loadedModel.network(), path.toString() + "/query.xml", renameQueries(file.getName(), loadedModel.queries()));
 
532
                        }
 
533
        }
 
534
        
 
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());
 
540
                }
 
541
                PNMLWriter pnmlWriter = new PNMLWriter(loadedModel.network(), guiModel);
 
542
                pnmlWriter.savePNML(f);
 
543
        }
 
544
        
 
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>(); 
 
547
                int index = 1;
 
548
                
 
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);
 
553
                        index++;
 
554
                }
 
555
                return renamedQueries;
 
556
                
 
557
        }
 
558
        private class ExportResultTableCellRenderer extends JLabel implements
 
559
        TableCellRenderer {
 
560
        private static final long serialVersionUID = -1054925029991763163L;
 
561
        Border unselectedBorder = null;
 
562
        Border selectedBorder = null;
 
563
        boolean isBordered = true;
 
564
        
 
565
        public ExportResultTableCellRenderer(boolean isBordered) {
 
566
                this.isBordered = isBordered;
 
567
        }
 
568
        
 
569
                public Component getTableCellRendererComponent(JTable table,
 
570
                                Object value, boolean isSelected, boolean hasFocus, int row,
 
571
                                int column) {
 
572
                        if (isBordered) {
 
573
                                if (isSelected) {
 
574
                                        setBackground(table.getSelectionBackground());
 
575
                                        setForeground(table.getSelectionForeground());
 
576
                
 
577
                                        if (selectedBorder == null) {
 
578
                                                selectedBorder = BorderFactory.createMatteBorder(2, 5,
 
579
                                                                2, 5, table.getSelectionBackground());
 
580
                                        }
 
581
                                        setBorder(selectedBorder);
 
582
                                } else {
 
583
                                        boolean isResultColumn = table.getColumnName(column)
 
584
                                                        .equals("Status");
 
585
                                        if (value != null) {
 
586
                                                if ((isResultColumn && (value.toString().equals(NAME_SuccesString)) || value.toString().equals(NAME_SuccesStringOrphanTransitionsRemoved))) {
 
587
                                                        setBackground(new Color(91, 255, 91)); // light green
 
588
                                                }
 
589
                                                else if (isResultColumn && value.toString().equals(NAME_FailStringParseError)) {
 
590
                                                        setBackground(new Color(255, 91, 91)); // light  red
 
591
                                                }
 
592
                                                else if (isResultColumn && value.toString().equals(NAME_FailStringFolderExists)) {
 
593
                                                        setBackground(new Color(255, 255, 120)); // light yellow
 
594
                                                }
 
595
                                                else
 
596
                                                        setBackground(table.getBackground());
 
597
                                        }
 
598
                                        setForeground(table.getForeground());
 
599
                                        if (unselectedBorder == null) {
 
600
                                                unselectedBorder = BorderFactory.createMatteBorder(2,
 
601
                                                                5, 2, 5, table.getBackground());
 
602
                                        }
 
603
                                        setBorder(unselectedBorder);
 
604
                                }
 
605
                        }
 
606
                
 
607
                        setEnabled(table.isEnabled());
 
608
                        setFont(table.getFont());
 
609
                        setOpaque(true);
 
610
                        
 
611
                        if (value != null) {
 
612
                                if (table.getColumnName(column).equals(
 
613
                                                "Destination")) {
 
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));
 
621
                                        }
 
622
                                        setToolTipText(result != null ? generateDestinationTooltip(result) : value.toString());
 
623
                                } else {
 
624
                                        setToolTipText(value.toString());
 
625
                                        setText(value.toString());
 
626
                                }
 
627
                        } else {
 
628
                                setToolTipText("");
 
629
                                setText("");
 
630
                        }
 
631
                
 
632
                        return this;
 
633
                }
 
634
                
 
635
                private String generateDestinationTooltip(String[] result) {
 
636
                        String fullFilePath = result[1];
 
637
                        
 
638
                        return fullFilePath;
 
639
                }
 
640
        }
 
641
}
 
642
 
 
643