MenuActionNewValue.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.jsql.util.I18nUtil;
14
import com.jsql.util.LogLevelUtil;
15
import com.jsql.view.swing.manager.ManagerScan;
16
import com.jsql.view.swing.text.JPopupTextArea;
17
import com.jsql.view.swing.util.I18nViewUtil;
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
22
import javax.swing.*;
23
import java.awt.*;
24
import java.awt.event.ActionEvent;
25
import java.awt.event.ActionListener;
26
import java.awt.event.MouseAdapter;
27
import java.awt.event.MouseEvent;
28
import java.util.List;
29
30
/**
31
 * Action to add a new item to a JList.
32
 */
33
public class MenuActionNewValue implements ActionListener {
34
    
35
    /**
36
     * Log4j logger sent to view.
37
     */
38
    private static final Logger LOGGER = LogManager.getRootLogger();
39
    
40
    /**
41
     * List to add new items.
42
     */
43
    private final DnDList myList;
44
    
45
    /**
46
     * Create action to add new item list.
47
     * @param myList List to add new items.
48
     */
49
    public MenuActionNewValue(DnDList myList) {
50
        this.myList = myList;
51
    }
52
    
53
    @Override
54
    public void actionPerformed(ActionEvent actionEvent) {
55
        var panel = new JPanel(new BorderLayout());
56
        final JTextArea textarea = new JPopupTextArea(new JTextArea()).getProxy();
57
        var labelAddValue = new JLabel(I18nUtil.valueByKey("LIST_ADD_VALUE_LABEL") + ":");
58 1 1. actionPerformed : removed call to javax/swing/JPanel::add → NO_COVERAGE
        panel.add(labelAddValue, BorderLayout.NORTH);
59 1 1. actionPerformed : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
        I18nViewUtil.addComponentForKey("LIST_ADD_VALUE_LABEL", labelAddValue);
60
        panel.add(new JScrollPane(textarea));
61
62 1 1. actionPerformed : removed call to javax/swing/JPanel::setPreferredSize → NO_COVERAGE
        panel.setPreferredSize(new Dimension(600, 400));
63 1 1. actionPerformed : removed call to javax/swing/JPanel::setMinimumSize → NO_COVERAGE
        panel.setMinimumSize(new Dimension(600, 400));
64
        
65 1 1. actionPerformed : removed call to javax/swing/JTextArea::addMouseListener → NO_COVERAGE
        textarea.addMouseListener(new MouseAdapter() {
66
            @Override
67
            public void mousePressed(MouseEvent e) {
68 1 1. mousePressed : removed call to java/awt/event/MouseAdapter::mousePressed → NO_COVERAGE
                super.mousePressed(e);
69
                textarea.requestFocusInWindow();
70
            }
71
        });
72
73
        int result = -1;
74
               
75
        // Unhandled NullPointerException #92858 on showOptionDialog()
76
        // Unhandled IllegalArgumentException #92859 on showOptionDialog()
77
        // Fix #70832: ClassCastException on showOptionDialog()
78
        try {
79
            result = JOptionPane.showOptionDialog(
80
                this.myList.getTopLevelAncestor(),
81
                panel,
82
                I18nUtil.valueByKey("LIST_ADD_VALUE_TITLE"),
83
                JOptionPane.OK_CANCEL_OPTION,
84
                JOptionPane.QUESTION_MESSAGE,
85
                null,
86
                new String[] {
87
                    I18nUtil.valueByKey("LIST_ADD_VALUE_OK"),
88
                    I18nUtil.valueByKey("LIST_ADD_VALUE_CANCEL")
89
                },
90
                I18nUtil.valueByKey("LIST_ADD_VALUE_CANCEL")
91
            );
92
        } catch (NullPointerException | IllegalArgumentException | ClassCastException e) {
93
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
94
        }
95
96 2 1. actionPerformed : negated conditional → NO_COVERAGE
2. actionPerformed : negated conditional → NO_COVERAGE
        if (StringUtils.isEmpty(textarea.getText()) || result != JOptionPane.YES_OPTION) {
97
            return;
98
        }
99
100
        var lastIndex = Math.max(this.myList.getSelectedIndex(), 0);
101
        int firstIndex = lastIndex;
102
        
103 1 1. actionPerformed : negated conditional → NO_COVERAGE
        if (ManagerScan.NAME.equals(this.myList.getName())) {
104
            lastIndex = this.addToScanList(textarea, lastIndex);
105
        } else {
106
            lastIndex = this.addToList(textarea, lastIndex);
107
        }
108
109 2 1. actionPerformed : removed call to com/jsql/view/swing/list/DnDList::setSelectionInterval → NO_COVERAGE
2. actionPerformed : Replaced integer subtraction with addition → NO_COVERAGE
        this.myList.setSelectionInterval(firstIndex, lastIndex - 1);
110 1 1. actionPerformed : removed call to com/jsql/view/swing/list/DnDList::scrollRectToVisible → NO_COVERAGE
        this.myList.scrollRectToVisible(
111
            this.myList.getCellBounds(
112
                this.myList.getMinSelectionIndex(),
113
                this.myList.getMaxSelectionIndex()
114
            )
115
        );
116 1 1. actionPerformed : removed call to javax/swing/JTextArea::setText → NO_COVERAGE
        textarea.setText(null);
117
    }
118
119
    private int addToList(final JTextArea textarea, int index) {
120
        int lastIndex = index;
121
        
122
        for (String newItem: textarea.getText().split("\\n")) {
123 1 1. addToList : negated conditional → NO_COVERAGE
            if (StringUtils.isNotEmpty(newItem)) {
124 2 1. addToList : Changed increment from 1 to -1 → NO_COVERAGE
2. addToList : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
                ((DefaultListModel<ItemList>) this.myList.getModel()).add(
125
                    lastIndex++,
126
                    new ItemList(newItem.replace("\\", "/"))
127
                );
128
            }
129
        }
130 1 1. addToList : replaced int return with 0 for com/jsql/view/swing/list/MenuActionNewValue::addToList → NO_COVERAGE
        return lastIndex;
131
    }
132
133
    private int addToScanList(final JTextArea textarea, int index) {
134
        int lastIndex = index;
135
        List<ItemListScan> listParsedItems = ListTransfertHandlerScan.parse(textarea.getText().replace("\\", "/"));
136
        
137
        for (ItemListScan item: listParsedItems) {
138 2 1. addToScanList : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
2. addToScanList : Changed increment from 1 to -1 → NO_COVERAGE
            ((DefaultListModel<ItemList>) this.myList.getModel()).add(lastIndex++, item);
139
        }
140 1 1. addToScanList : replaced int return with 0 for com/jsql/view/swing/list/MenuActionNewValue::addToScanList → NO_COVERAGE
        return lastIndex;
141
    }
142
}

Mutations

58

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/JPanel::add → NO_COVERAGE

59

1.1
Location : actionPerformed
Killed by : none
removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE

62

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/JPanel::setPreferredSize → NO_COVERAGE

63

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/JPanel::setMinimumSize → NO_COVERAGE

65

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/JTextArea::addMouseListener → NO_COVERAGE

68

1.1
Location : mousePressed
Killed by : none
removed call to java/awt/event/MouseAdapter::mousePressed → NO_COVERAGE

96

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

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

103

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

109

1.1
Location : actionPerformed
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::setSelectionInterval → NO_COVERAGE

2.2
Location : actionPerformed
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

110

1.1
Location : actionPerformed
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::scrollRectToVisible → NO_COVERAGE

116

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/JTextArea::setText → NO_COVERAGE

123

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

124

1.1
Location : addToList
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

2.2
Location : addToList
Killed by : none
removed call to javax/swing/DefaultListModel::add → NO_COVERAGE

130

1.1
Location : addToList
Killed by : none
replaced int return with 0 for com/jsql/view/swing/list/MenuActionNewValue::addToList → NO_COVERAGE

138

1.1
Location : addToScanList
Killed by : none
removed call to javax/swing/DefaultListModel::add → NO_COVERAGE

2.2
Location : addToScanList
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

140

1.1
Location : addToScanList
Killed by : none
replaced int return with 0 for com/jsql/view/swing/list/MenuActionNewValue::addToScanList → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1