1 | /******************************************************************************* | |
2 | * Copyhacked (H) 2012-2020. | |
3 | * This program and the accompanying materials | |
4 | * are made available under no term at all, use it like | |
5 | * you want, but share and discuss about it | |
6 | * every time possible with every body. | |
7 | * | |
8 | * Contributors: | |
9 | * ron190 at ymail dot com - initial implementation | |
10 | *******************************************************************************/ | |
11 | package com.jsql.view.swing.list; | |
12 | ||
13 | import com.jsql.util.I18nUtil; | |
14 | import com.jsql.util.LogLevelUtil; | |
15 | import com.jsql.view.swing.util.MediatorHelper; | |
16 | import org.apache.logging.log4j.LogManager; | |
17 | import org.apache.logging.log4j.Logger; | |
18 | ||
19 | import javax.swing.*; | |
20 | import java.awt.event.ActionEvent; | |
21 | import java.awt.event.ActionListener; | |
22 | import java.io.FileOutputStream; | |
23 | import java.io.IOException; | |
24 | import java.io.PrintStream; | |
25 | import java.nio.charset.StandardCharsets; | |
26 | ||
27 | /** | |
28 | * Action to export a JList. | |
29 | */ | |
30 | public class MenuActionExport implements ActionListener { | |
31 | | |
32 | /** | |
33 | * Log4j logger sent to view. | |
34 | */ | |
35 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
36 | ||
37 | /** | |
38 | * List to export. | |
39 | */ | |
40 | private final DnDList myList; | |
41 | | |
42 | /** | |
43 | * Create action to export a list. | |
44 | * @param myList List to export. | |
45 | */ | |
46 | public MenuActionExport(DnDList myList) { | |
47 | | |
48 | this.myList = myList; | |
49 | } | |
50 | ||
51 | @Override | |
52 | public void actionPerformed(ActionEvent arg0) { | |
53 | | |
54 | final JFileChooser importFileDialog = new JFileChooser(MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile()) { | |
55 | | |
56 | @Override | |
57 | public void approveSelection() { | |
58 | | |
59 | var file = this.getSelectedFile(); | |
60 | | |
61 | if ( | |
62 |
1
1. approveSelection : negated conditional → NO_COVERAGE |
file.exists() |
63 |
1
1. approveSelection : negated conditional → NO_COVERAGE |
&& this.getDialogType() == JFileChooser.SAVE_DIALOG |
64 | ) { | |
65 | | |
66 | int replace = JOptionPane.showConfirmDialog( | |
67 | this, | |
68 | String.format( | |
69 | "%s %s", | |
70 | file.getName(), | |
71 | I18nUtil.valueByKey("LIST_EXPORT_CONFIRM_LABEL") | |
72 | ), | |
73 | I18nUtil.valueByKey("LIST_EXPORT_CONFIRM_TITLE"), | |
74 | JOptionPane.YES_NO_OPTION | |
75 | ); | |
76 | | |
77 | switch (replace) { | |
78 | | |
79 | case JOptionPane.YES_OPTION: | |
80 |
1
1. approveSelection : removed call to javax/swing/JFileChooser::approveSelection → NO_COVERAGE |
super.approveSelection(); |
81 | return; | |
82 | | |
83 | case JOptionPane.NO_OPTION: | |
84 | case JOptionPane.CLOSED_OPTION: | |
85 | return; | |
86 | | |
87 | case JOptionPane.CANCEL_OPTION: | |
88 |
1
1. approveSelection : removed call to com/jsql/view/swing/list/MenuActionExport$1::cancelSelection → NO_COVERAGE |
this.cancelSelection(); |
89 | return; | |
90 | | |
91 | default: | |
92 | break; | |
93 | } | |
94 | } else { | |
95 |
1
1. approveSelection : removed call to javax/swing/JFileChooser::approveSelection → NO_COVERAGE |
super.approveSelection(); |
96 | } | |
97 | } | |
98 | }; | |
99 | | |
100 |
1
1. actionPerformed : removed call to javax/swing/JFileChooser::setDialogTitle → NO_COVERAGE |
importFileDialog.setDialogTitle(I18nUtil.valueByKey("LIST_EXPORT_TITLE")); |
101 | int choice = importFileDialog.showSaveDialog(this.myList.getTopLevelAncestor()); | |
102 | | |
103 |
1
1. actionPerformed : negated conditional → NO_COVERAGE |
if (choice != JFileChooser.APPROVE_OPTION) { |
104 | return; | |
105 | } | |
106 | ||
107 | try ( | |
108 | var file = new FileOutputStream(importFileDialog.getSelectedFile()); | |
109 | var out = new PrintStream(file, false, StandardCharsets.UTF_8) | |
110 | ) { | |
111 | int len = this.myList.getModel().getSize(); | |
112 | | |
113 |
2
1. actionPerformed : changed conditional boundary → NO_COVERAGE 2. actionPerformed : negated conditional → NO_COVERAGE |
for (var i = 0 ; i < len ; i++) { |
114 |
1
1. actionPerformed : removed call to java/io/PrintStream::println → NO_COVERAGE |
out.println(this.myList.getModel().getElementAt(i).toString()); |
115 | } | |
116 | } catch (IOException e) { | |
117 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
118 | } | |
119 | } | |
120 | } | |
Mutations | ||
62 |
1.1 |
|
63 |
1.1 |
|
80 |
1.1 |
|
88 |
1.1 |
|
95 |
1.1 |
|
100 |
1.1 |
|
103 |
1.1 |
|
113 |
1.1 2.2 |
|
114 |
1.1 |