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

Mutations

54

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

58

1.1
Location : showPopup
Killed by : none
removed call to javax/swing/JPopupMenu::applyComponentOrientation → NO_COVERAGE

61

1.1
Location : showPopup
Killed by : none
removed call to javax/swing/JPopupMenu::show → NO_COVERAGE

63

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

64

1.1
Location : showPopup
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

72

1.1
Location : showPopup
Killed by : none
removed call to javax/swing/JPopupMenu::setLocation → NO_COVERAGE

73

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

74

1.1
Location : showPopup
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

107

1.1
Location : initMenu
Killed by : none
removed call to java/util/stream/Stream::forEach → NO_COVERAGE

108

1.1
Location : lambda$initMenu$0
Killed by : none
removed call to javax/swing/JMenuItem::setText → NO_COVERAGE

109

1.1
Location : lambda$initMenu$0
Killed by : none
negated conditional → NO_COVERAGE

113

1.1
Location : lambda$initMenu$0
Killed by : none
removed call to javax/swing/JMenuItem::setName → NO_COVERAGE

114

1.1
Location : lambda$initMenu$0
Killed by : none
removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE

117

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE

118

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE

119

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE

120

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE

123

1.1
Location : initMenu
Killed by : none
removed call to com/formdev/flatlaf/util/SystemFileChooser::setDialogTitle → NO_COVERAGE

124

1.1
Location : initMenu
Killed by : none
removed call to com/formdev/flatlaf/util/SystemFileChooser::setMultiSelectionEnabled → NO_COVERAGE

126

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

128

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

137

1.1
Location : lambda$initMenu$1
Killed by : none
negated conditional → NO_COVERAGE

138

1.1
Location : lambda$initMenu$1
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::dropPasteFile → NO_COVERAGE

145

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

147

1.1
Location : lambda$initMenu$2
Killed by : none
negated conditional → NO_COVERAGE

148

1.1
Location : lambda$initMenu$2
Killed by : none
removed call to javax/swing/Action::actionPerformed → NO_COVERAGE

154

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

156

1.1
Location : lambda$initMenu$3
Killed by : none
negated conditional → NO_COVERAGE

157

1.1
Location : lambda$initMenu$3
Killed by : none
removed call to javax/swing/Action::actionPerformed → NO_COVERAGE

163

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

165

1.1
Location : lambda$initMenu$4
Killed by : none
negated conditional → NO_COVERAGE

166

1.1
Location : lambda$initMenu$4
Killed by : none
removed call to javax/swing/Action::actionPerformed → NO_COVERAGE

172

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

2.2
Location : lambda$initMenu$5
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::removeSelectedItem → NO_COVERAGE

173

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

174

1.1
Location : lambda$initMenu$6
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::restore → NO_COVERAGE

2.2
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

175

1.1
Location : initMenu
Killed by : none
removed call to javax/swing/JMenuItem::addActionListener → NO_COVERAGE

177

1.1
Location : lambda$initMenu$7
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

178

1.1
Location : lambda$initMenu$7
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : lambda$initMenu$7
Killed by : none
negated conditional → NO_COVERAGE

179

1.1
Location : lambda$initMenu$7
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::setSelectionInterval → NO_COVERAGE

196

1.1
Location : initMenu
Killed by : none
replaced return value with null for com/jsql/view/swing/list/MouseAdapterMenuAction::initMenu → NO_COVERAGE

201

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

205

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

210

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

211

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

214

1.1
Location : mousePressed
Killed by : none
removed call to com/jsql/view/swing/list/MouseAdapterMenuAction::showPopup → NO_COVERAGE

219

1.1
Location : mouseReleased
Killed by : none
removed call to com/jsql/view/swing/list/MouseAdapterMenuAction::showPopup → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.22.0