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

Mutations

55

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

56

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

59

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

60

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

62

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

65

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

93

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

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

100

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

106

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

107

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

113

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

120

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

121

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

127

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

135

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

137

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