| 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.jsql.util.I18nUtil; | |
| 14 | import com.jsql.util.LogLevelUtil; | |
| 15 | import com.jsql.view.swing.util.MediatorHelper; | |
| 16 | import org.apache.logging.log4j.LogManager; | |
| 17 | import org.apache.logging.log4j.Logger; | |
| 18 | ||
| 19 | import javax.swing.*; | |
| 20 | import java.awt.event.ActionEvent; | |
| 21 | import java.awt.event.ActionListener; | |
| 22 | import java.io.FileOutputStream; | |
| 23 | import java.io.IOException; | |
| 24 | import java.io.PrintStream; | |
| 25 | import java.nio.charset.StandardCharsets; | |
| 26 | ||
| 27 | /** | |
| 28 | * Action to export a JList. | |
| 29 | */ | |
| 30 | public class MenuActionExport implements ActionListener { | |
| 31 | | |
| 32 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
| 33 | ||
| 34 | /** | |
| 35 | * List to export. | |
| 36 | */ | |
| 37 | private final DnDList myList; | |
| 38 | | |
| 39 | /** | |
| 40 | * Create action to export a list. | |
| 41 | * @param myList List to export. | |
| 42 | */ | |
| 43 | public MenuActionExport(DnDList myList) { | |
| 44 | this.myList = myList; | |
| 45 | } | |
| 46 | ||
| 47 | @Override | |
| 48 | public void actionPerformed(ActionEvent actionEvent) { | |
| 49 | final JFileChooser importFileDialog = new JFileChooser(MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile()) { | |
| 50 | @Override | |
| 51 | public void approveSelection() { | |
| 52 | var file = this.getSelectedFile(); | |
| 53 |
2
1. approveSelection : negated conditional → NO_COVERAGE 2. approveSelection : negated conditional → NO_COVERAGE |
if (file.exists() && this.getDialogType() == JFileChooser.SAVE_DIALOG) { |
| 54 | int replace = JOptionPane.showConfirmDialog( | |
| 55 | MediatorHelper.frame(), | |
| 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 | | |
| 61 | switch (replace) { | |
| 62 | case JOptionPane.YES_OPTION: | |
| 63 |
1
1. approveSelection : removed call to javax/swing/JFileChooser::approveSelection → NO_COVERAGE |
super.approveSelection(); |
| 64 | return; | |
| 65 | case JOptionPane.NO_OPTION: | |
| 66 | case JOptionPane.CLOSED_OPTION: | |
| 67 | return; | |
| 68 | case JOptionPane.CANCEL_OPTION: | |
| 69 |
1
1. approveSelection : removed call to com/jsql/view/swing/list/MenuActionExport$1::cancelSelection → NO_COVERAGE |
this.cancelSelection(); |
| 70 | return; | |
| 71 | default: | |
| 72 | break; | |
| 73 | } | |
| 74 | } else { | |
| 75 |
1
1. approveSelection : removed call to javax/swing/JFileChooser::approveSelection → NO_COVERAGE |
super.approveSelection(); |
| 76 | } | |
| 77 | } | |
| 78 | }; | |
| 79 | | |
| 80 |
1
1. actionPerformed : removed call to javax/swing/JFileChooser::setDialogTitle → NO_COVERAGE |
importFileDialog.setDialogTitle(I18nUtil.valueByKey("LIST_EXPORT_TITLE")); |
| 81 | int choice = importFileDialog.showSaveDialog(this.myList.getTopLevelAncestor()); | |
| 82 |
1
1. actionPerformed : negated conditional → NO_COVERAGE |
if (choice != JFileChooser.APPROVE_OPTION) { |
| 83 | return; | |
| 84 | } | |
| 85 | ||
| 86 | try ( | |
| 87 | var file = new FileOutputStream(importFileDialog.getSelectedFile()); | |
| 88 | var out = new PrintStream(file, false, StandardCharsets.UTF_8) | |
| 89 | ) { | |
| 90 | int len = this.myList.getModel().getSize(); | |
| 91 |
2
1. actionPerformed : changed conditional boundary → NO_COVERAGE 2. actionPerformed : negated conditional → NO_COVERAGE |
for (var i = 0 ; i < len ; i++) { |
| 92 |
1
1. actionPerformed : removed call to java/io/PrintStream::println → NO_COVERAGE |
out.println(this.myList.getModel().getElementAt(i).toString()); |
| 93 | } | |
| 94 | } catch (IOException e) { | |
| 95 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
| 96 | } | |
| 97 | } | |
| 98 | } | |
Mutations | ||
| 53 |
1.1 2.2 |
|
| 63 |
1.1 |
|
| 69 |
1.1 |
|
| 75 |
1.1 |
|
| 80 |
1.1 |
|
| 82 |
1.1 |
|
| 91 |
1.1 2.2 |
|
| 92 |
1.1 |