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