MouseAdapterMenuAction.java

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
Location : showPopup
Killed by : none
negated conditional → NO_COVERAGE

64

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

68

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

70

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

71

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

79

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

80

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

81

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

115

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

117

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

118

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

122

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

124

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

127

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

128

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

129

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

130

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

134

1.1
Location : initializeMenu
Killed by : none
removed call to javax/swing/JFileChooser::setDialogTitle → NO_COVERAGE

135

1.1
Location : initializeMenu
Killed by : none
removed call to javax/swing/JFileChooser::setMultiSelectionEnabled → NO_COVERAGE

137

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

139

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

151

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

153

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

160

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

164

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

165

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

171

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

175

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

176

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

182

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

186

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

187

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

193

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

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

194

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

195

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

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

196

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

199

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

201

1.1
Location : lambda$initializeMenu$7
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : lambda$initializeMenu$7
Killed by : none
changed conditional boundary → NO_COVERAGE

202

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

220

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

226

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

232

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

239

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

240

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

244

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

249

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.16.1