ActionSaveTab.java

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

Mutations

55

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE

56

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE

57

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::putValue → NO_COVERAGE

62

1.1
Location : actionPerformed
Killed by : none
removed call to com/jsql/view/swing/dialog/ReplaceFileChooser::setDialogTitle → NO_COVERAGE

64

1.1
Location : actionPerformed
Killed by : none
negated conditional → NO_COVERAGE

66

1.1
Location : actionPerformed
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::saveToFile → NO_COVERAGE

67

1.1
Location : actionPerformed
Killed by : none
negated conditional → NO_COVERAGE

69

1.1
Location : actionPerformed
Killed by : none
negated conditional → NO_COVERAGE

72

1.1
Location : actionPerformed
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::saveToFile → NO_COVERAGE

77

1.1
Location : saveToFile
Killed by : none
negated conditional → NO_COVERAGE

81

1.1
Location : saveToFile
Killed by : none
removed call to com/jsql/view/swing/dialog/ReplaceFileChooser::updateUI → NO_COVERAGE

83

1.1
Location : saveToFile
Killed by : none
negated conditional → NO_COVERAGE

85

1.1
Location : saveToFile
Killed by : none
removed call to com/jsql/util/PreferencesUtil::set → NO_COVERAGE

86

1.1
Location : saveToFile
Killed by : none
negated conditional → NO_COVERAGE

87

1.1
Location : saveToFile
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::saveTextToFile → NO_COVERAGE

88

1.1
Location : saveToFile
Killed by : none
negated conditional → NO_COVERAGE

89

1.1
Location : saveToFile
Killed by : none
removed call to com/jsql/view/swing/action/ActionSaveTab::saveTableToFile → NO_COVERAGE

99

1.1
Location : saveTableToFile
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : saveTableToFile
Killed by : none
negated conditional → NO_COVERAGE

100

1.1
Location : saveTableToFile
Killed by : none
removed call to java/io/FileWriter::write → NO_COVERAGE

102

1.1
Location : saveTableToFile
Killed by : none
removed call to java/io/FileWriter::write → NO_COVERAGE

104

1.1
Location : saveTableToFile
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : saveTableToFile
Killed by : none
negated conditional → NO_COVERAGE

105

1.1
Location : saveTableToFile
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : saveTableToFile
Killed by : none
changed conditional boundary → NO_COVERAGE

107

1.1
Location : saveTableToFile
Killed by : none
negated conditional → NO_COVERAGE

108

1.1
Location : saveTableToFile
Killed by : none
removed call to java/io/FileWriter::write → NO_COVERAGE

115

1.1
Location : saveTableToFile
Killed by : none
removed call to java/io/FileWriter::write → NO_COVERAGE

118

1.1
Location : saveTableToFile
Killed by : none
removed call to java/io/FileWriter::write → NO_COVERAGE

135

1.1
Location : saveTextToFile
Killed by : none
removed call to javax/swing/text/JTextComponent::write → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1