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.action; | |
12 | ||
13 | import com.jsql.util.LogLevelUtil; | |
14 | import com.jsql.view.swing.dialog.ReplaceFileChooser; | |
15 | import com.jsql.view.swing.scrollpane.LightScrollPane; | |
16 | import com.jsql.view.swing.table.PanelTable; | |
17 | import com.jsql.view.swing.util.MediatorHelper; | |
18 | import org.apache.logging.log4j.LogManager; | |
19 | import org.apache.logging.log4j.Logger; | |
20 | ||
21 | import javax.swing.*; | |
22 | import javax.swing.text.JTextComponent; | |
23 | import java.awt.event.ActionEvent; | |
24 | import java.awt.event.InputEvent; | |
25 | import java.awt.event.KeyEvent; | |
26 | import java.io.BufferedWriter; | |
27 | import java.io.FileWriter; | |
28 | import java.io.IOException; | |
29 | import java.nio.charset.StandardCharsets; | |
30 | ||
31 | /** | |
32 | * Save the content of tab in a file. | |
33 | */ | |
34 | public class ActionSaveTab extends AbstractAction { | |
35 | | |
36 | /** | |
37 | * Log4j logger sent to view. | |
38 | */ | |
39 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
40 | | |
41 | private final ReplaceFileChooser replaceFileChooser = new ReplaceFileChooser( | |
42 | MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile() | |
43 | ); | |
44 | ||
45 | public ActionSaveTab() { | |
46 | | |
47 |
1
1. <init> : removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE |
this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK)); |
48 |
1
1. <init> : removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE |
this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S); |
49 |
1
1. <init> : removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE |
this.putValue(Action.NAME, "Save Tab As..."); |
50 | } | |
51 | | |
52 | @Override | |
53 | public void actionPerformed(ActionEvent e) { | |
54 | | |
55 |
1
1. actionPerformed : removed call to com/jsql/view/swing/dialog/ReplaceFileChooser::setDialogTitle → NO_COVERAGE |
this.replaceFileChooser.setDialogTitle("Save Tab As"); |
56 | ||
57 | var componentResult = MediatorHelper.tabResults().getSelectedComponent(); | |
58 | | |
59 |
1
1. actionPerformed : negated conditional → NO_COVERAGE |
if (componentResult instanceof PanelTable) { |
60 | | |
61 | JTable table = ((PanelTable) componentResult).getTableValues(); | |
62 |
1
1. actionPerformed : removed call to com/jsql/view/swing/action/ActionSaveTab::saveToFile → NO_COVERAGE |
this.saveToFile(table); |
63 | | |
64 |
1
1. actionPerformed : negated conditional → NO_COVERAGE |
} else if ( |
65 | componentResult instanceof LightScrollPane | |
66 |
1
1. actionPerformed : negated conditional → NO_COVERAGE |
&& ((LightScrollPane) componentResult).scrollPane.getViewport().getView() instanceof JTextComponent |
67 | ) { | |
68 | | |
69 | JTextComponent textarea = (JTextComponent) ((LightScrollPane) componentResult).scrollPane.getViewport().getView(); | |
70 |
1
1. actionPerformed : removed call to com/jsql/view/swing/action/ActionSaveTab::saveToFile → NO_COVERAGE |
this.saveToFile(textarea); |
71 | } | |
72 | } | |
73 | | |
74 | private void saveToFile(JComponent textarea) { | |
75 | | |
76 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
if (textarea == null) { |
77 | return; | |
78 | } | |
79 | | |
80 | int stateSave = this.replaceFileChooser.showSaveDialog(MediatorHelper.frame()); | |
81 | | |
82 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
if (stateSave == JFileChooser.APPROVE_OPTION) { |
83 | | |
84 | var folderSelectedFile = this.replaceFileChooser.getCurrentDirectory().toString(); | |
85 |
1
1. saveToFile : removed call to com/jsql/util/PreferencesUtil::set → NO_COVERAGE |
MediatorHelper.model().getMediatorUtils().getPreferencesUtil().set(folderSelectedFile); |
86 | | |
87 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
if (textarea instanceof JTextComponent) { |
88 |
1
1. saveToFile : removed call to com/jsql/view/swing/action/ActionSaveTab::saveTextToFile → NO_COVERAGE |
this.saveTextToFile((JTextComponent) textarea); |
89 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
} else if (textarea instanceof JTable) { |
90 |
1
1. saveToFile : removed call to com/jsql/view/swing/action/ActionSaveTab::saveTableToFile → NO_COVERAGE |
this.saveTableToFile((JTable) textarea); |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | private void saveTableToFile(JTable tableResults) { | |
96 | | |
97 | var fileSelected = this.replaceFileChooser.getSelectedFile(); | |
98 | | |
99 | try (var fileWriterExcel = new FileWriter(fileSelected, StandardCharsets.UTF_8)) { | |
100 | | |
101 | var tableModel = tableResults.getModel(); | |
102 | | |
103 |
2
1. saveTableToFile : changed conditional boundary → NO_COVERAGE 2. saveTableToFile : negated conditional → NO_COVERAGE |
for (var i = 2 ; i < tableModel.getColumnCount() ; i++) { |
104 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriterExcel.write(tableModel.getColumnName(i) + "\t"); |
105 | } | |
106 | | |
107 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriterExcel.write("\n"); |
108 | | |
109 |
2
1. saveTableToFile : changed conditional boundary → NO_COVERAGE 2. saveTableToFile : negated conditional → NO_COVERAGE |
for (var i = 0 ; i < tableModel.getRowCount() ; i++) { |
110 | | |
111 |
2
1. saveTableToFile : negated conditional → NO_COVERAGE 2. saveTableToFile : changed conditional boundary → NO_COVERAGE |
for (var j = 2 ; j < tableModel.getColumnCount() ; j++) { |
112 | | |
113 | // Cell empty when string was too long to be injected (columnTooLong|cellEmpty|cellEmpty). | |
114 |
1
1. saveTableToFile : negated conditional → NO_COVERAGE |
if (tableModel.getValueAt(i, j) == null) { |
115 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriterExcel.write("\t"); |
116 | } else { | |
117 | | |
118 | // Encode line break. | |
119 | var line = tableModel.getValueAt(i, j).toString(); | |
120 | line = line | |
121 | .replace("\n", "\\n") | |
122 | .replace("\t", "\\t"); | |
123 | line = line + "\t"; | |
124 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriterExcel.write(line); |
125 | } | |
126 | } | |
127 | | |
128 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriterExcel.write("\n"); |
129 | } | |
130 | } catch (IOException e) { | |
131 | LOGGER.log( | |
132 | LogLevelUtil.CONSOLE_ERROR, | |
133 | String.format("Error writing to %s", fileSelected.getName()), | |
134 | e | |
135 | ); | |
136 | } | |
137 | } | |
138 | ||
139 | private void saveTextToFile(JTextComponent textarea) { | |
140 | | |
141 | var fileSelected = this.replaceFileChooser.getSelectedFile(); | |
142 | | |
143 | try ( | |
144 | var fileWriter = new FileWriter(fileSelected, StandardCharsets.UTF_8); | |
145 | var fileOut = new BufferedWriter(fileWriter) | |
146 | ) { | |
147 |
1
1. saveTextToFile : removed call to javax/swing/text/JTextComponent::write → NO_COVERAGE |
textarea.write(fileOut); |
148 | } catch (IOException e) { | |
149 | LOGGER.log( | |
150 | LogLevelUtil.CONSOLE_ERROR, | |
151 | String.format("Error writing to %s", fileSelected.getName()), | |
152 | e | |
153 | ); | |
154 | } | |
155 | } | |
156 | } | |
Mutations | ||
47 |
1.1 |
|
48 |
1.1 |
|
49 |
1.1 |
|
55 |
1.1 |
|
59 |
1.1 |
|
62 |
1.1 |
|
64 |
1.1 |
|
66 |
1.1 |
|
70 |
1.1 |
|
76 |
1.1 |
|
82 |
1.1 |
|
85 |
1.1 |
|
87 |
1.1 |
|
88 |
1.1 |
|
89 |
1.1 |
|
90 |
1.1 |
|
103 |
1.1 2.2 |
|
104 |
1.1 |
|
107 |
1.1 |
|
109 |
1.1 2.2 |
|
111 |
1.1 2.2 |
|
114 |
1.1 |
|
115 |
1.1 |
|
124 |
1.1 |
|
128 |
1.1 |
|
147 |
1.1 |