MenuActionExport.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.list;
12
13
import com.formdev.flatlaf.util.SystemFileChooser;
14
import com.jsql.util.I18nUtil;
15
import com.jsql.util.LogLevelUtil;
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 java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.io.FileOutputStream;
24
import java.io.IOException;
25
import java.io.PrintStream;
26
import java.nio.charset.StandardCharsets;
27
28
/**
29
 * Action to export a JList.
30
 */
31
public class MenuActionExport implements ActionListener {
32
    
33
    private static final Logger LOGGER = LogManager.getRootLogger();
34
35
    /**
36
     * List to export.
37
     */
38
    private final DnDList myList;
39
    
40
    /**
41
     * Create action to export a list.
42
     * @param myList List to export.
43
     */
44
    public MenuActionExport(DnDList myList) {
45
        this.myList = myList;
46
    }
47
48
    @Override
49
    public void actionPerformed(ActionEvent actionEvent) {
50
        final SystemFileChooser importFileDialog = new SystemFileChooser(MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile());
51 1 1. actionPerformed : removed call to com/formdev/flatlaf/util/SystemFileChooser::setApproveCallback → NO_COVERAGE
        importFileDialog.setApproveCallback((selectedFiles, context) -> {
52
            var file = selectedFiles[0];
53 1 1. lambda$actionPerformed$0 : negated conditional → NO_COVERAGE
            if (file.exists()) {
54 1 1. lambda$actionPerformed$0 : replaced int return with 0 for com/jsql/view/swing/list/MenuActionExport::lambda$actionPerformed$0 → NO_COVERAGE
                return context.showMessageDialog(
55
                    JOptionPane.QUESTION_MESSAGE,
56
                    String.format("%s %s", file.getName(), I18nUtil.valueByKey("LIST_EXPORT_CONFIRM_LABEL")),
57
                    I18nUtil.valueByKey("LIST_EXPORT_CONFIRM_TITLE"),
58
                    JOptionPane.YES_NO_OPTION
59
                );
60
            } else {
61
                return SystemFileChooser.APPROVE_OPTION;
62
            }
63
        });
64
        
65 1 1. actionPerformed : removed call to com/formdev/flatlaf/util/SystemFileChooser::setDialogTitle → NO_COVERAGE
        importFileDialog.setDialogTitle(I18nUtil.valueByKey("LIST_EXPORT_TITLE"));
66
        int choice = importFileDialog.showSaveDialog(this.myList.getTopLevelAncestor());
67 1 1. actionPerformed : negated conditional → NO_COVERAGE
        if (choice != JFileChooser.APPROVE_OPTION) {
68
            return;
69
        }
70
71
        try (
72
            var file = new FileOutputStream(importFileDialog.getSelectedFile());
73
            var out = new PrintStream(file, false, StandardCharsets.UTF_8)
74
        ) {
75
            int len = this.myList.getModel().getSize();
76 2 1. actionPerformed : negated conditional → NO_COVERAGE
2. actionPerformed : changed conditional boundary → NO_COVERAGE
            for (var i = 0 ; i < len ; i++) {
77 1 1. actionPerformed : removed call to java/io/PrintStream::println → NO_COVERAGE
                out.println(this.myList.getModel().getElementAt(i).toString());
78
            }
79
            LOGGER.log(LogLevelUtil.CONSOLE_SUCCESS, "List saved: {}", importFileDialog.getSelectedFile());
80
        } catch (IOException e) {
81
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
82
        }
83
    }
84
}

Mutations

51

1.1
Location : actionPerformed
Killed by : none
removed call to com/formdev/flatlaf/util/SystemFileChooser::setApproveCallback → NO_COVERAGE

53

1.1
Location : lambda$actionPerformed$0
Killed by : none
negated conditional → NO_COVERAGE

54

1.1
Location : lambda$actionPerformed$0
Killed by : none
replaced int return with 0 for com/jsql/view/swing/list/MenuActionExport::lambda$actionPerformed$0 → NO_COVERAGE

65

1.1
Location : actionPerformed
Killed by : none
removed call to com/formdev/flatlaf/util/SystemFileChooser::setDialogTitle → NO_COVERAGE

67

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

76

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

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

77

1.1
Location : actionPerformed
Killed by : none
removed call to java/io/PrintStream::println → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.22.0