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.menubar.JMenuItemWithMargin; | |
16 | import com.jsql.view.swing.util.I18nViewUtil; | |
17 | import com.jsql.view.swing.util.MediatorHelper; | |
18 | import org.apache.logging.log4j.LogManager; | |
19 | import org.apache.logging.log4j.Logger; | |
20 | ||
21 | import javax.swing.*; | |
22 | import java.awt.*; | |
23 | import java.awt.event.*; | |
24 | import java.util.AbstractMap.SimpleEntry; | |
25 | import java.util.Arrays; | |
26 | import java.util.stream.Stream; | |
27 | ||
28 | /** | |
29 | * A Mouse action to display a popupmenu on a JList. | |
30 | */ | |
31 | public class MouseAdapterMenuAction extends MouseAdapter { | |
32 | | |
33 | /** | |
34 | * Log4j logger sent to view. | |
35 | */ | |
36 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
37 | | |
38 | /** | |
39 | * JList to add popupmenu. | |
40 | */ | |
41 | private final DnDList dndList; | |
42 | | |
43 | /** | |
44 | * Create a popup menu for current JList item. | |
45 | * @param dndList List with action | |
46 | */ | |
47 | public MouseAdapterMenuAction(DnDList dndList) { | |
48 | this.dndList = dndList; | |
49 | } | |
50 | | |
51 | /** | |
52 | * Displays a popup menu for JList. | |
53 | * @param mouseEvent Mouse event | |
54 | */ | |
55 | @SuppressWarnings("unchecked") | |
56 | public void showPopup(final MouseEvent mouseEvent) { | |
57 | | |
58 |
1
1. showPopup : negated conditional → NO_COVERAGE |
if (mouseEvent.isPopupTrigger()) { |
59 | | |
60 | JList<ItemList> list = (JList<ItemList>) mouseEvent.getSource(); | |
61 | ||
62 | JPopupMenu popupMenuList = this.initializeMenu(mouseEvent); | |
63 | | |
64 |
1
1. showPopup : removed call to javax/swing/JPopupMenu::applyComponentOrientation → NO_COVERAGE |
popupMenuList.applyComponentOrientation(ComponentOrientation.getOrientation(I18nUtil.getLocaleDefault())); |
65 | ||
66 | // Fix #26274: IllegalComponentStateException on show() | |
67 | try { | |
68 |
1
1. showPopup : removed call to javax/swing/JPopupMenu::show → NO_COVERAGE |
popupMenuList.show( |
69 | list, | |
70 |
1
1. showPopup : negated conditional → NO_COVERAGE |
ComponentOrientation.RIGHT_TO_LEFT.equals(ComponentOrientation.getOrientation(I18nUtil.getLocaleDefault())) |
71 |
1
1. showPopup : Replaced integer subtraction with addition → NO_COVERAGE |
? mouseEvent.getX() - popupMenuList.getWidth() |
72 | : mouseEvent.getX(), | |
73 | mouseEvent.getY() | |
74 | ); | |
75 | } catch (IllegalComponentStateException e) { | |
76 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
77 | } | |
78 | | |
79 |
1
1. showPopup : removed call to javax/swing/JPopupMenu::setLocation → NO_COVERAGE |
popupMenuList.setLocation( |
80 |
1
1. showPopup : negated conditional → NO_COVERAGE |
ComponentOrientation.RIGHT_TO_LEFT.equals(ComponentOrientation.getOrientation(I18nUtil.getLocaleDefault())) |
81 |
1
1. showPopup : Replaced integer subtraction with addition → NO_COVERAGE |
? mouseEvent.getXOnScreen() - popupMenuList.getWidth() |
82 | : mouseEvent.getXOnScreen(), | |
83 | mouseEvent.getYOnScreen() | |
84 | ); | |
85 | } | |
86 | } | |
87 | ||
88 | private JPopupMenu initializeMenu(final MouseEvent mouseEvent) { | |
89 | | |
90 | var popupMenuList = new JPopupMenu(); | |
91 | | |
92 | boolean isAsian = I18nUtil.isAsian(I18nUtil.getLocaleDefault()); | |
93 | | |
94 | JMenuItem mnImport = new JMenuItemWithMargin(); | |
95 | JMenuItem mnExport = new JMenuItemWithMargin(); | |
96 | JMenuItem mnCut = new JMenuItemWithMargin(); | |
97 | JMenuItem mnCopy = new JMenuItemWithMargin(); | |
98 | JMenuItem mnPaste = new JMenuItemWithMargin(); | |
99 | JMenuItem mnDelete = new JMenuItemWithMargin(); | |
100 | JMenuItem mnNew = new JMenuItemWithMargin(); | |
101 | JMenuItem mnRestoreDefault = new JMenuItemWithMargin(); | |
102 | JMenuItem mnSelectAll = new JMenuItemWithMargin(); | |
103 | | |
104 | Stream.of( | |
105 | new SimpleEntry<>(mnImport, "LIST_IMPORT_CONFIRM_TITLE"), | |
106 | new SimpleEntry<>(mnExport, "LIST_EXPORT_TITLE"), | |
107 | new SimpleEntry<>(mnCut, "LIST_CUT"), | |
108 | new SimpleEntry<>(mnCopy, "CONTEXT_MENU_COPY"), | |
109 | new SimpleEntry<>(mnPaste, "LIST_PASTE"), | |
110 | new SimpleEntry<>(mnDelete, "LIST_DELETE"), | |
111 | new SimpleEntry<>(mnNew, "LIST_NEW_VALUE"), | |
112 | new SimpleEntry<>(mnRestoreDefault, "LIST_RESTORE_DEFAULT"), | |
113 | new SimpleEntry<>(mnSelectAll, "CONTEXT_MENU_SELECT_ALL") | |
114 | ) | |
115 |
1
1. initializeMenu : removed call to java/util/stream/Stream::forEach → NO_COVERAGE |
.forEach(entry -> { |
116 | | |
117 |
1
1. lambda$initializeMenu$0 : removed call to javax/swing/JMenuItem::setText → NO_COVERAGE |
entry.getKey().setText( |
118 |
1
1. lambda$initializeMenu$0 : negated conditional → NO_COVERAGE |
isAsian |
119 | ? I18nViewUtil.valueByKey(entry.getValue()) | |
120 | : I18nUtil.valueByKey(entry.getValue()) | |
121 | ); | |
122 |
1
1. lambda$initializeMenu$0 : removed call to javax/swing/JMenuItem::setName → NO_COVERAGE |
entry.getKey().setName(entry.getValue()); |
123 | | |
124 |
1
1. lambda$initializeMenu$0 : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE |
I18nViewUtil.addComponentForKey(entry.getValue(), entry.getKey()); |
125 | }); | |
126 | ||
127 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE |
mnCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK)); |
128 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE |
mnCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK)); |
129 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE |
mnPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK)); |
130 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE |
mnSelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK)); |
131 | | |
132 | //Create a file chooser | |
133 | final var importFileDialog = new JFileChooser(MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile()); | |
134 |
1
1. initializeMenu : removed call to javax/swing/JFileChooser::setDialogTitle → NO_COVERAGE |
importFileDialog.setDialogTitle(I18nUtil.valueByKey("LIST_IMPORT_CONFIRM_TITLE")); |
135 |
1
1. initializeMenu : removed call to javax/swing/JFileChooser::setMultiSelectionEnabled → NO_COVERAGE |
importFileDialog.setMultiSelectionEnabled(true); |
136 | ||
137 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnNew.addActionListener(new MenuActionNewValue(this.dndList)); |
138 | ||
139 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnImport.addActionListener(actionEvent -> { |
140 | | |
141 | var choice = 0; | |
142 | | |
143 | // Fix #1896: NullPointerException on showOpenDialog() | |
144 | // Fix #42831: ClassCastException on showOpenDialog() | |
145 | try { | |
146 | choice = importFileDialog.showOpenDialog(this.dndList.getTopLevelAncestor()); | |
147 | } catch (ClassCastException | NullPointerException e) { | |
148 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
149 | } | |
150 | | |
151 |
1
1. lambda$initializeMenu$1 : negated conditional → NO_COVERAGE |
if (choice == JFileChooser.APPROVE_OPTION) { |
152 | | |
153 |
1
1. lambda$initializeMenu$1 : removed call to com/jsql/view/swing/list/DnDList::dropPasteFile → NO_COVERAGE |
this.dndList.dropPasteFile( |
154 | Arrays.asList(importFileDialog.getSelectedFiles()), | |
155 | this.dndList.locationToIndex(mouseEvent.getPoint()) | |
156 | ); | |
157 | } | |
158 | }); | |
159 | ||
160 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnCopy.addActionListener(actionEvent -> { |
161 | | |
162 | var action = this.dndList.getActionMap().get(TransferHandler.getCopyAction().getValue(Action.NAME)); | |
163 | | |
164 |
1
1. lambda$initializeMenu$2 : negated conditional → NO_COVERAGE |
if (action != null) { |
165 |
1
1. lambda$initializeMenu$2 : removed call to javax/swing/Action::actionPerformed → NO_COVERAGE |
action.actionPerformed( |
166 | new ActionEvent(this.dndList, ActionEvent.ACTION_PERFORMED, null) | |
167 | ); | |
168 | } | |
169 | }); | |
170 | ||
171 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnCut.addActionListener(actionEvent -> { |
172 | | |
173 | var action = this.dndList.getActionMap().get(TransferHandler.getCutAction().getValue(Action.NAME)); | |
174 | | |
175 |
1
1. lambda$initializeMenu$3 : negated conditional → NO_COVERAGE |
if (action != null) { |
176 |
1
1. lambda$initializeMenu$3 : removed call to javax/swing/Action::actionPerformed → NO_COVERAGE |
action.actionPerformed( |
177 | new ActionEvent(this.dndList, ActionEvent.ACTION_PERFORMED, null) | |
178 | ); | |
179 | } | |
180 | }); | |
181 | ||
182 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnPaste.addActionListener(actionEvent -> { |
183 | | |
184 | var action = this.dndList.getActionMap().get(TransferHandler.getPasteAction().getValue(Action.NAME)); | |
185 | | |
186 |
1
1. lambda$initializeMenu$4 : negated conditional → NO_COVERAGE |
if (action != null) { |
187 |
1
1. lambda$initializeMenu$4 : removed call to javax/swing/Action::actionPerformed → NO_COVERAGE |
action.actionPerformed( |
188 | new ActionEvent(this.dndList, ActionEvent.ACTION_PERFORMED, null) | |
189 | ); | |
190 | } | |
191 | }); | |
192 | ||
193 |
2
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE 2. lambda$initializeMenu$5 : removed call to com/jsql/view/swing/list/DnDList::removeSelectedItem → NO_COVERAGE |
mnDelete.addActionListener(actionEvent -> this.dndList.removeSelectedItem()); |
194 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnExport.addActionListener(new MenuActionExport(this.dndList)); |
195 |
2
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE 2. lambda$initializeMenu$6 : removed call to com/jsql/view/swing/list/DnDList::restore → NO_COVERAGE |
mnRestoreDefault.addActionListener(actionEvent -> this.dndList.restore()); |
196 |
1
1. initializeMenu : removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE |
mnSelectAll.addActionListener(actionEvent -> { |
197 | | |
198 | var start = 0; | |
199 |
1
1. lambda$initializeMenu$7 : Replaced integer subtraction with addition → NO_COVERAGE |
int end = this.dndList.getModel().getSize() - 1; |
200 | | |
201 |
2
1. lambda$initializeMenu$7 : negated conditional → NO_COVERAGE 2. lambda$initializeMenu$7 : changed conditional boundary → NO_COVERAGE |
if (end >= 0) { |
202 |
1
1. lambda$initializeMenu$7 : removed call to com/jsql/view/swing/list/DnDList::setSelectionInterval → NO_COVERAGE |
this.dndList.setSelectionInterval(start, end); |
203 | } | |
204 | }); | |
205 | ||
206 | popupMenuList.add(mnNew); | |
207 | popupMenuList.add(new JSeparator()); | |
208 | popupMenuList.add(mnCut); | |
209 | popupMenuList.add(mnCopy); | |
210 | popupMenuList.add(mnPaste); | |
211 | popupMenuList.add(mnDelete); | |
212 | popupMenuList.add(new JSeparator()); | |
213 | popupMenuList.add(mnSelectAll); | |
214 | popupMenuList.add(new JSeparator()); | |
215 | popupMenuList.add(mnImport); | |
216 | popupMenuList.add(mnExport); | |
217 | popupMenuList.add(new JSeparator()); | |
218 | popupMenuList.add(mnRestoreDefault); | |
219 | | |
220 |
1
1. initializeMenu : replaced return value with null for com/jsql/view/swing/list/MouseAdapterMenuAction::initializeMenu → NO_COVERAGE |
return popupMenuList; |
221 | } | |
222 | ||
223 | @Override | |
224 | public void mousePressed(MouseEvent e) { | |
225 | | |
226 |
1
1. mousePressed : negated conditional → NO_COVERAGE |
if (SwingUtilities.isRightMouseButton(e)) { |
227 | | |
228 | int clickIndex = this.dndList.locationToIndex(e.getPoint()); | |
229 | var containsIndex = false; | |
230 | | |
231 | for (int currentIndex: this.dndList.getSelectedIndices()) { | |
232 |
1
1. mousePressed : negated conditional → NO_COVERAGE |
if (currentIndex == clickIndex) { |
233 | | |
234 | containsIndex = true; | |
235 | break; | |
236 | } | |
237 | } | |
238 | | |
239 |
1
1. mousePressed : negated conditional → NO_COVERAGE |
if (!containsIndex) { |
240 |
1
1. mousePressed : removed call to com/jsql/view/swing/list/DnDList::setSelectedIndex → NO_COVERAGE |
this.dndList.setSelectedIndex(clickIndex); |
241 | } | |
242 | } | |
243 | | |
244 |
1
1. mousePressed : removed call to com/jsql/view/swing/list/MouseAdapterMenuAction::showPopup → NO_COVERAGE |
this.showPopup(e); |
245 | } | |
246 | ||
247 | @Override | |
248 | public void mouseReleased(MouseEvent e) { | |
249 |
1
1. mouseReleased : removed call to com/jsql/view/swing/list/MouseAdapterMenuAction::showPopup → NO_COVERAGE |
this.showPopup(e); |
250 | } | |
251 | } | |
Mutations | ||
58 |
1.1 |
|
64 |
1.1 |
|
68 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
81 |
1.1 |
|
115 |
1.1 |
|
117 |
1.1 |
|
118 |
1.1 |
|
122 |
1.1 |
|
124 |
1.1 |
|
127 |
1.1 |
|
128 |
1.1 |
|
129 |
1.1 |
|
130 |
1.1 |
|
134 |
1.1 |
|
135 |
1.1 |
|
137 |
1.1 |
|
139 |
1.1 |
|
151 |
1.1 |
|
153 |
1.1 |
|
160 |
1.1 |
|
164 |
1.1 |
|
165 |
1.1 |
|
171 |
1.1 |
|
175 |
1.1 |
|
176 |
1.1 |
|
182 |
1.1 |
|
186 |
1.1 |
|
187 |
1.1 |
|
193 |
1.1 2.2 |
|
194 |
1.1 |
|
195 |
1.1 2.2 |
|
196 |
1.1 |
|
199 |
1.1 |
|
201 |
1.1 2.2 |
|
202 |
1.1 |
|
220 |
1.1 |
|
226 |
1.1 |
|
232 |
1.1 |
|
239 |
1.1 |
|
240 |
1.1 |
|
244 |
1.1 |
|
249 |
1.1 |