| 1 | /******************************************************************************* | |
| 2 | * Copyhacked (H) 2012-2025. | |
| 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 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.table.PanelTable; | |
| 16 | import com.jsql.view.swing.util.MediatorHelper; | |
| 17 | import org.apache.logging.log4j.LogManager; | |
| 18 | import org.apache.logging.log4j.Logger; | |
| 19 | ||
| 20 | import javax.swing.*; | |
| 21 | import javax.swing.text.JTextComponent; | |
| 22 | import java.awt.event.ActionEvent; | |
| 23 | import java.awt.event.InputEvent; | |
| 24 | import java.awt.event.KeyEvent; | |
| 25 | import java.io.BufferedWriter; | |
| 26 | import java.io.FileWriter; | |
| 27 | import java.io.IOException; | |
| 28 | import java.nio.charset.StandardCharsets; | |
| 29 | import java.nio.file.InvalidPathException; | |
| 30 | ||
| 31 | /** | |
| 32 | * Save the content of tab in a file. | |
| 33 | */ | |
| 34 | public class ActionSaveTab extends AbstractAction { | |
| 35 | | |
| 36 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
| 37 | | |
| 38 | private ReplaceFileChooser replaceFileChooser; | |
| 39 | ||
| 40 | public ActionSaveTab() { | |
| 41 | // Unhandled NoSuchMethodError #82561 on constructor: NoSuchMethodError | |
| 42 | // Unhandled InternalError #93015 on constructor: InvocationTargetException | |
| 43 | // Unhandled NullPointerException #95805 on constructor: desktop null on Windows | |
| 44 | // Unhandled IllegalArgumentException #95985 on constructor: Comparison method violates its general contract! | |
| 45 | try { | |
| 46 | this.replaceFileChooser = new ReplaceFileChooser( | |
| 47 | MediatorHelper.model().getMediatorUtils().preferencesUtil().getPathFile() | |
| 48 | ); | |
| 49 | } catch (IllegalArgumentException | NoSuchMethodError | InternalError | NullPointerException e) { | |
| 50 | LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Internal error in JFileChooser, verify your system and see stacktrace in tab Java: {}", e.getMessage()); | |
| 51 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
| 52 | } | |
| 53 |
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)); |
| 54 |
1
1. <init> : removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE |
this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S); |
| 55 |
1
1. <init> : removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE |
this.putValue(Action.NAME, "Save Tab As..."); |
| 56 | } | |
| 57 | | |
| 58 | @Override | |
| 59 | public void actionPerformed(ActionEvent e) { | |
| 60 |
1
1. actionPerformed : removed call to com/jsql/view/swing/dialog/ReplaceFileChooser::setDialogTitle → NO_COVERAGE |
this.replaceFileChooser.setDialogTitle("Save Tab As"); |
| 61 | var componentResult = MediatorHelper.tabResults().getSelectedComponent(); | |
| 62 | switch (componentResult) { | |
| 63 |
1
1. actionPerformed : removed call to com/jsql/view/swing/action/ActionSaveTab::saveToFile → NO_COVERAGE |
case PanelTable panelTable -> this.saveToFile(panelTable.getTableValues()); |
| 64 | case JScrollPane jScrollPane | |
| 65 |
2
1. actionPerformed : negated conditional → NO_COVERAGE 2. actionPerformed : removed call to com/jsql/view/swing/action/ActionSaveTab::saveToFile → NO_COVERAGE |
when jScrollPane.getViewport().getView() instanceof JTextComponent textarea -> this.saveToFile(textarea); |
| 66 | default -> LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Nothing to save"); | |
| 67 | } | |
| 68 | } | |
| 69 | | |
| 70 | private void saveToFile(JComponent jComponent) { | |
| 71 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
if (jComponent == null) { |
| 72 | return; | |
| 73 | } | |
| 74 | ||
| 75 |
1
1. saveToFile : removed call to com/jsql/view/swing/dialog/ReplaceFileChooser::updateUI → NO_COVERAGE |
this.replaceFileChooser.updateUI(); // required when changing dark/light mode |
| 76 | try { // Fix #96109: InvalidPathException on showSaveDialog() | |
| 77 | int stateSave = this.replaceFileChooser.showSaveDialog(MediatorHelper.frame()); | |
| 78 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
if (stateSave == JFileChooser.APPROVE_OPTION) { |
| 79 | var folderSelectedFile = this.replaceFileChooser.getCurrentDirectory().toString(); | |
| 80 |
1
1. saveToFile : removed call to com/jsql/util/PreferencesUtil::set → NO_COVERAGE |
MediatorHelper.model().getMediatorUtils().preferencesUtil().set(folderSelectedFile); |
| 81 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
if (jComponent instanceof JTextComponent jTextComponent) { |
| 82 |
1
1. saveToFile : removed call to com/jsql/view/swing/action/ActionSaveTab::saveTextToFile → NO_COVERAGE |
this.saveTextToFile(jTextComponent); |
| 83 |
1
1. saveToFile : negated conditional → NO_COVERAGE |
} else if (jComponent instanceof JTable jTable) { |
| 84 |
1
1. saveToFile : removed call to com/jsql/view/swing/action/ActionSaveTab::saveTableToFile → NO_COVERAGE |
this.saveTableToFile(jTable); |
| 85 | } | |
| 86 | } | |
| 87 | } catch (InvalidPathException e) { | |
| 88 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | private void saveTableToFile(JTable tableResults) { | |
| 93 | var fileSelected = this.replaceFileChooser.getSelectedFile(); | |
| 94 | | |
| 95 | try (var fileWriter = new FileWriter(fileSelected, StandardCharsets.UTF_8)) { | |
| 96 | var tableModel = tableResults.getModel(); | |
| 97 |
2
1. saveTableToFile : changed conditional boundary → NO_COVERAGE 2. saveTableToFile : negated conditional → NO_COVERAGE |
for (var i = 2 ; i < tableModel.getColumnCount() ; i++) { |
| 98 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriter.write(tableModel.getColumnName(i) + "\t"); |
| 99 | } | |
| 100 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriter.write("\n"); |
| 101 | | |
| 102 |
2
1. saveTableToFile : changed conditional boundary → NO_COVERAGE 2. saveTableToFile : negated conditional → NO_COVERAGE |
for (var i = 0 ; i < tableModel.getRowCount() ; i++) { |
| 103 |
2
1. saveTableToFile : negated conditional → NO_COVERAGE 2. saveTableToFile : changed conditional boundary → NO_COVERAGE |
for (var j = 2 ; j < tableModel.getColumnCount() ; j++) { |
| 104 | // Cell empty when string was too long to be injected (columnTooLong|cellEmpty|cellEmpty). | |
| 105 |
1
1. saveTableToFile : negated conditional → NO_COVERAGE |
if (tableModel.getValueAt(i, j) == null) { |
| 106 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriter.write("\t"); |
| 107 | } else { | |
| 108 | var line = tableModel.getValueAt(i, j).toString(); // Encode line break. | |
| 109 | line = line | |
| 110 | .replace("\n", "\\n") | |
| 111 | .replace("\t", "\\t"); | |
| 112 | line = line + "\t"; | |
| 113 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriter.write(line); |
| 114 | } | |
| 115 | } | |
| 116 |
1
1. saveTableToFile : removed call to java/io/FileWriter::write → NO_COVERAGE |
fileWriter.write("\n"); |
| 117 | } | |
| 118 | } catch (IOException e) { | |
| 119 | LOGGER.log( | |
| 120 | LogLevelUtil.CONSOLE_ERROR, | |
| 121 | String.format("Error writing to %s", fileSelected.getName()), | |
| 122 | e.getMessage() // full stacktrace not required | |
| 123 | ); | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | private void saveTextToFile(JTextComponent textarea) { | |
| 128 | var fileSelected = this.replaceFileChooser.getSelectedFile(); | |
| 129 | try ( | |
| 130 | var fileWriter = new FileWriter(fileSelected, StandardCharsets.UTF_8); | |
| 131 | var fileOut = new BufferedWriter(fileWriter) | |
| 132 | ) { | |
| 133 |
1
1. saveTextToFile : removed call to javax/swing/text/JTextComponent::write → NO_COVERAGE |
textarea.write(fileOut); |
| 134 | } catch (IOException e) { | |
| 135 | LOGGER.log( | |
| 136 | LogLevelUtil.CONSOLE_ERROR, | |
| 137 | String.format("Error writing to %s", fileSelected.getName()), | |
| 138 | e | |
| 139 | ); | |
| 140 | } | |
| 141 | } | |
| 142 | } | |
Mutations | ||
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 60 |
1.1 |
|
| 63 |
1.1 |
|
| 65 |
1.1 2.2 |
|
| 71 |
1.1 |
|
| 75 |
1.1 |
|
| 78 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 84 |
1.1 |
|
| 97 |
1.1 2.2 |
|
| 98 |
1.1 |
|
| 100 |
1.1 |
|
| 102 |
1.1 2.2 |
|
| 103 |
1.1 2.2 |
|
| 105 |
1.1 |
|
| 106 |
1.1 |
|
| 113 |
1.1 |
|
| 116 |
1.1 |
|
| 133 |
1.1 |