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