JPopupTextComponent.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.text;
12
13
import com.jsql.util.LogLevelUtil;
14
import com.jsql.view.swing.popupmenu.JPopupMenuText;
15
import com.jsql.view.swing.text.action.SilentDeleteTextAction;
16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
18
19
import javax.swing.*;
20
import javax.swing.text.DefaultEditorKit;
21
import javax.swing.text.JTextComponent;
22
import javax.swing.undo.CannotRedoException;
23
import javax.swing.undo.CannotUndoException;
24
import javax.swing.undo.UndoManager;
25
import java.awt.event.ActionEvent;
26
27
/**
28
 * A swing JTextComponent with Undo/Redo functionality.
29
 * @param <T> Component like JTextField or JTextArea to decorate
30
 */
31
public class JPopupTextComponent<T extends JTextComponent> extends JPopupComponent<T> implements DecoratorJComponent<T> {
32
    
33
    /**
34
     * Log4j logger sent to view.
35
     */
36
    private static final Logger LOGGER = LogManager.getRootLogger();
37
38
    /**
39
     * Save the component to decorate, add the Undo/Redo.
40
     * @param proxy Swing component to decorate
41
     */
42
    public JPopupTextComponent(final T proxy) {
43
        super(proxy);
44
45 1 1. <init> : removed call to javax/swing/text/JTextComponent::setComponentPopupMenu → NO_COVERAGE
        this.getProxy().setComponentPopupMenu(new JPopupMenuText(this.getProxy()));
46 1 1. <init> : removed call to javax/swing/text/JTextComponent::setDragEnabled → NO_COVERAGE
        this.getProxy().setDragEnabled(true);
47
48
        var undoRedoManager = new UndoManager();
49
        var doc = this.getProxy().getDocument();
50
51
        // Listen for undo and redo events
52 1 1. <init> : removed call to javax/swing/text/Document::addUndoableEditListener → NO_COVERAGE
        doc.addUndoableEditListener(undoableEditEvent -> undoRedoManager.addEdit(undoableEditEvent.getEdit()));
53
54 1 1. <init> : removed call to com/jsql/view/swing/text/JPopupTextComponent::initUndo → NO_COVERAGE
        this.initUndo(undoRedoManager);
55 1 1. <init> : removed call to com/jsql/view/swing/text/JPopupTextComponent::initRedo → NO_COVERAGE
        this.initRedo(undoRedoManager);
56 1 1. <init> : removed call to com/jsql/view/swing/text/JPopupTextComponent::makeDeleteSilent → NO_COVERAGE
        this.makeDeleteSilent();
57
    }
58
59
    private void initUndo(final UndoManager undo) {
60
        final var undoIdentifier = "Undo";  // Create an undo action and add it to the text component
61
        
62 1 1. initUndo : removed call to javax/swing/ActionMap::put → NO_COVERAGE
        this.getProxy().getActionMap().put(undoIdentifier, new AbstractAction(undoIdentifier) {
63
            @Override
64
            public void actionPerformed(ActionEvent evt) {
65
                // Unhandled ArrayIndexOutOfBoundsException #92146 on undo()
66
                try {
67 1 1. actionPerformed : negated conditional → NO_COVERAGE
                    if (undo.canUndo()) {
68 1 1. actionPerformed : removed call to javax/swing/undo/UndoManager::undo → NO_COVERAGE
                        undo.undo();
69
                    }
70
                } catch (ArrayIndexOutOfBoundsException | CannotUndoException e) {
71
                    LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
72
                }
73
            }
74
       });
75
76
        // Bind the undo action to ctl-Z
77 1 1. initUndo : removed call to javax/swing/InputMap::put → NO_COVERAGE
        this.getProxy().getInputMap().put(KeyStroke.getKeyStroke("control Z"), undoIdentifier);
78
    }
79
80
    private void initRedo(final UndoManager undo) {
81
        final var redoIdentifier = "Redo";  // Create a redo action and add it to the text component
82
        
83 1 1. initRedo : removed call to javax/swing/ActionMap::put → NO_COVERAGE
        this.getProxy().getActionMap().put(redoIdentifier, new AbstractAction(redoIdentifier) {
84
            @Override
85
            public void actionPerformed(ActionEvent evt) {
86
                try {
87 1 1. actionPerformed : negated conditional → NO_COVERAGE
                    if (undo.canRedo()) {
88 1 1. actionPerformed : removed call to javax/swing/undo/UndoManager::redo → NO_COVERAGE
                        undo.redo();
89
                    }
90
                } catch (CannotRedoException e) {
91
                    LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
92
                }
93
            }
94
        });
95
96
        // Bind the redo action to ctl-Y
97 1 1. initRedo : removed call to javax/swing/InputMap::put → NO_COVERAGE
        this.getProxy().getInputMap().put(KeyStroke.getKeyStroke("control Y"), redoIdentifier);
98
    }
99
100
    private void makeDeleteSilent() {
101
        var actionMap = this.getProxy().getActionMap();  // Silent delete
102
103
        String key = DefaultEditorKit.deletePrevCharAction;
104 1 1. makeDeleteSilent : removed call to javax/swing/ActionMap::put → NO_COVERAGE
        actionMap.put(key, new SilentDeleteTextAction(key, actionMap.get(key)));
105
106
        key = DefaultEditorKit.deleteNextCharAction;
107 1 1. makeDeleteSilent : removed call to javax/swing/ActionMap::put → NO_COVERAGE
        actionMap.put(key, new SilentDeleteTextAction(key, actionMap.get(key)));
108
    }
109
}

Mutations

45

1.1
Location : <init>
Killed by : none
removed call to javax/swing/text/JTextComponent::setComponentPopupMenu → NO_COVERAGE

46

1.1
Location : <init>
Killed by : none
removed call to javax/swing/text/JTextComponent::setDragEnabled → NO_COVERAGE

52

1.1
Location : <init>
Killed by : none
removed call to javax/swing/text/Document::addUndoableEditListener → NO_COVERAGE

54

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/text/JPopupTextComponent::initUndo → NO_COVERAGE

55

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/text/JPopupTextComponent::initRedo → NO_COVERAGE

56

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/text/JPopupTextComponent::makeDeleteSilent → NO_COVERAGE

62

1.1
Location : initUndo
Killed by : none
removed call to javax/swing/ActionMap::put → NO_COVERAGE

67

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

68

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/undo/UndoManager::undo → NO_COVERAGE

77

1.1
Location : initUndo
Killed by : none
removed call to javax/swing/InputMap::put → NO_COVERAGE

83

1.1
Location : initRedo
Killed by : none
removed call to javax/swing/ActionMap::put → NO_COVERAGE

87

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

88

1.1
Location : actionPerformed
Killed by : none
removed call to javax/swing/undo/UndoManager::redo → NO_COVERAGE

97

1.1
Location : initRedo
Killed by : none
removed call to javax/swing/InputMap::put → NO_COVERAGE

104

1.1
Location : makeDeleteSilent
Killed by : none
removed call to javax/swing/ActionMap::put → NO_COVERAGE

107

1.1
Location : makeDeleteSilent
Killed by : none
removed call to javax/swing/ActionMap::put → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.18.2