JPopupMenuComponent.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.popupmenu;
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.text.JTextAreaPlaceholderConsole;
17
import com.jsql.view.swing.text.JTextPanePlaceholderConsole;
18
import com.jsql.view.swing.util.I18nViewUtil;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
22
import javax.swing.*;
23
import javax.swing.event.PopupMenuEvent;
24
import javax.swing.event.PopupMenuListener;
25
import javax.swing.text.DefaultEditorKit;
26
import javax.swing.text.JTextComponent;
27
import java.awt.*;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.InputEvent;
30
import java.awt.event.KeyEvent;
31
32
/**
33
 * Popup menu for editable text component.
34
 */
35
public class JPopupMenuComponent extends JPopupMenu {
36
    
37
    /**
38
     * Log4j logger sent to view.
39
     */
40
    private static final Logger LOGGER = LogManager.getRootLogger();
41
    
42
    private final JComponent component;
43
    
44
    /**
45
     * Create a popup menu for editable component.
46
     * @param component The component with the new menu
47
     */
48
    public JPopupMenuComponent(JComponent component) {
49
        
50
        this.component = component;
51
        
52
        JMenuItem copyItem = new JMenuItemWithMargin(component.getActionMap().get(DefaultEditorKit.copyAction));
53 1 1. <init> : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE
        copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
54 1 1. <init> : removed call to javax/swing/JMenuItem::setMnemonic → NO_COVERAGE
        copyItem.setMnemonic('C');
55 1 1. <init> : removed call to javax/swing/JMenuItem::setText → NO_COVERAGE
        copyItem.setText(I18nUtil.valueByKey("CONTEXT_MENU_COPY"));
56 1 1. <init> : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
        I18nViewUtil.addComponentForKey("CONTEXT_MENU_COPY", copyItem);
57
58
        JMenuItem selectAllItem = new JMenuItemWithMargin(component.getActionMap().get(DefaultEditorKit.selectAllAction));
59 1 1. <init> : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE
        selectAllItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK));
60 1 1. <init> : removed call to javax/swing/JMenuItem::setMnemonic → NO_COVERAGE
        selectAllItem.setMnemonic('A');
61 1 1. <init> : removed call to javax/swing/JMenuItem::setText → NO_COVERAGE
        selectAllItem.setText(I18nUtil.valueByKey("CONTEXT_MENU_SELECT_ALL"));
62 1 1. <init> : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
        I18nViewUtil.addComponentForKey("CONTEXT_MENU_SELECT_ALL", selectAllItem);
63
        
64 1 1. <init> : removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::setLightWeightPopupEnabled → NO_COVERAGE
        this.setLightWeightPopupEnabled(false);
65
        
66
        this.add(copyItem);
67 1 1. <init> : removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::addSeparator → NO_COVERAGE
        this.addSeparator();
68
        this.add(selectAllItem);
69
        
70 2 1. <init> : negated conditional → NO_COVERAGE
2. <init> : negated conditional → NO_COVERAGE
        if (
71
            component instanceof JTextAreaPlaceholderConsole
72
            || component instanceof JTextPanePlaceholderConsole
73
        ) {
74
            JMenuItem clearItem = new JMenuItemWithMargin();
75
            
76 1 1. <init> : removed call to javax/swing/JMenuItem::setAction → NO_COVERAGE
            clearItem.setAction(new AbstractAction() {
77
                
78
                @Override
79
                public void actionPerformed(ActionEvent e) {
80
                    
81 1 1. actionPerformed : removed call to javax/swing/text/JTextComponent::setText → NO_COVERAGE
                    ((JTextComponent) JPopupMenuComponent.this.component).setText(null);
82
                }
83
            });
84
            
85 1 1. <init> : removed call to javax/swing/JMenuItem::setText → NO_COVERAGE
            clearItem.setText(I18nUtil.valueByKey("CONTEXT_MENU_CLEAR"));
86 1 1. <init> : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
            I18nViewUtil.addComponentForKey("CONTEXT_MENU_CLEAR", clearItem);
87 1 1. <init> : removed call to javax/swing/JMenuItem::setAccelerator → NO_COVERAGE
            clearItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_DOWN_MASK));
88 1 1. <init> : removed call to javax/swing/JMenuItem::setMnemonic → NO_COVERAGE
            clearItem.setMnemonic('E');
89
            
90 1 1. <init> : removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::addSeparator → NO_COVERAGE
            this.addSeparator();
91
            this.add(clearItem);
92
        }
93
94 1 1. <init> : removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::addPopupMenuListener → NO_COVERAGE
        this.addPopupMenuListener(new PopupMenuOrientedListener());
95
    }
96
    
97
    private class PopupMenuOrientedListener implements PopupMenuListener {
98
        @Override
99
        public void popupMenuWillBecomeVisible(PopupMenuEvent event) {
100
            // Fix #47018: NullPointerException on getLocation()
101
            try {
102 1 1. popupMenuWillBecomeVisible : removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::setLocation → NO_COVERAGE
                JPopupMenuComponent.this.setLocation(MouseInfo.getPointerInfo().getLocation());
103
                
104 1 1. popupMenuWillBecomeVisible : removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::setLocation → NO_COVERAGE
                JPopupMenuComponent.this.setLocation(
105 1 1. popupMenuWillBecomeVisible : negated conditional → NO_COVERAGE
                    ComponentOrientation.RIGHT_TO_LEFT.equals(ComponentOrientation.getOrientation(I18nUtil.getLocaleDefault()))
106 1 1. popupMenuWillBecomeVisible : Replaced integer subtraction with addition → NO_COVERAGE
                    ? MouseInfo.getPointerInfo().getLocation().x - JPopupMenuComponent.this.getWidth()
107
                    : MouseInfo.getPointerInfo().getLocation().x,
108
                    MouseInfo.getPointerInfo().getLocation().y
109
                );
110
            } catch (NullPointerException e) {
111
                LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
112
            }
113
        }
114
        
115
        @Override
116
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
117
            // Do nothing
118
        }
119
        
120
        @Override
121
        public void popupMenuCanceled(PopupMenuEvent e) {
122
            // Do nothing
123
        }
124
    }
125
}

Mutations

53

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

54

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setMnemonic → NO_COVERAGE

55

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setText → NO_COVERAGE

56

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

59

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

60

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setMnemonic → NO_COVERAGE

61

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setText → NO_COVERAGE

62

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

64

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::setLightWeightPopupEnabled → NO_COVERAGE

67

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::addSeparator → NO_COVERAGE

70

1.1
Location : <init>
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : <init>
Killed by : none
negated conditional → NO_COVERAGE

76

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setAction → NO_COVERAGE

81

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

85

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setText → NO_COVERAGE

86

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

87

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

88

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JMenuItem::setMnemonic → NO_COVERAGE

90

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::addSeparator → NO_COVERAGE

94

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::addPopupMenuListener → NO_COVERAGE

102

1.1
Location : popupMenuWillBecomeVisible
Killed by : none
removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::setLocation → NO_COVERAGE

104

1.1
Location : popupMenuWillBecomeVisible
Killed by : none
removed call to com/jsql/view/swing/popupmenu/JPopupMenuComponent::setLocation → NO_COVERAGE

105

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

106

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

Active mutators

Tests examined


Report generated by PIT 1.16.1