AbstractColoredConsole.java

1
package com.jsql.view.swing.console;
2
3
import com.jsql.util.LogLevelUtil;
4
import com.jsql.view.swing.text.JPopupTextPane;
5
import com.jsql.view.swing.util.MediatorHelper;
6
import com.jsql.view.swing.util.UiUtil;
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
10
11
import javax.swing.*;
12
import javax.swing.text.SimpleAttributeSet;
13
import java.awt.*;
14
import java.awt.event.FocusAdapter;
15
import java.awt.event.FocusEvent;
16
17
/**
18
 * A JTextPane which displays colored strings.
19
 */
20
public abstract class AbstractColoredConsole extends JPopupTextPane {
21
    
22
    /**
23
     * Log4j logger sent to view.
24
     */
25
    private static final Logger LOGGER = LogManager.getRootLogger();
26
27
    /**
28
     * Text name of tab.
29
     */
30
    private final String tabName;
31
32
    /**
33
     * Create a JTextPane which displays colored strings.
34
     * @param tabName Text name of tab
35
     */
36
    protected AbstractColoredConsole(final String tabName, String placeholder) {
37
        
38
        super(placeholder);
39
        
40
        this.tabName = tabName;
41
        
42 1 1. <init> : removed call to com/jsql/view/swing/console/AbstractColoredConsole::addFocusListener → NO_COVERAGE
        this.addFocusListener(new FocusAdapter() {
43
            
44
            @Override
45
            public void focusGained(FocusEvent arg0) {
46
                
47 1 1. focusGained : removed call to javax/swing/text/Caret::setVisible → NO_COVERAGE
                AbstractColoredConsole.this.getProxy().getCaret().setVisible(true);
48 1 1. focusGained : removed call to javax/swing/text/Caret::setSelectionVisible → NO_COVERAGE
                AbstractColoredConsole.this.getProxy().getCaret().setSelectionVisible(true);
49
            }
50
        });
51
52 1 1. <init> : removed call to com/jsql/view/swing/console/AbstractColoredConsole::setCursor → NO_COVERAGE
        this.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
53
    }
54
    
55
    protected abstract SimpleAttributeSet getColorAttribute();
56
57
    /**
58
     * Add a string to the end of JTextPane.
59
     * @param message Text to add
60
     * @param attribut Font
61
     */
62
    public void append(String message, SimpleAttributeSet attribut) {
63
        try {
64 1 1. append : negated conditional → NO_COVERAGE
            boolean isCaretAtEnd = this.getProxy().getCaretPosition() == this.getProxy().getDocument().getLength();
65
            
66
            JScrollPane scrollPane = (JScrollPane) this.getProxy().getParent().getParent();
67
            JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
68
            int extent = scrollBar.getModel().getExtent();
69 3 1. append : Replaced integer subtraction with addition → NO_COVERAGE
2. append : changed conditional boundary → NO_COVERAGE
3. append : negated conditional → NO_COVERAGE
            boolean isScrollBarAtEnd = scrollBar.getValue() >= scrollBar.getMaximum() - extent;
70
            
71
            var logMessage = message.substring(15);
72
            var logTimestamp = message.substring(0, 15);
73
            
74 1 1. append : removed call to javax/swing/text/Document::insertString → NO_COVERAGE
            this.getProxy().getDocument().insertString(
75
                this.getProxy().getDocument().getLength(),
76 1 1. append : negated conditional → NO_COVERAGE
                (this.getProxy().getDocument().getLength() == 0 ? StringUtils.EMPTY : "\n") + logTimestamp,
77
                this.getColorAttribute()
78
            );
79
            
80 1 1. append : removed call to javax/swing/text/Document::insertString → NO_COVERAGE
            this.getProxy().getDocument().insertString(
81
                this.getProxy().getDocument().getLength(),
82
                logMessage,
83
                attribut
84
            );
85
            
86 2 1. append : negated conditional → NO_COVERAGE
2. append : negated conditional → NO_COVERAGE
            if (isCaretAtEnd || isScrollBarAtEnd) {
87 2 1. append : removed call to javax/swing/JScrollBar::setValue → NO_COVERAGE
2. append : Replaced integer addition with subtraction → NO_COVERAGE
                scrollBar.setValue(scrollBar.getMaximum() + 1);
88
            }
89
90
            var foregroundColor = Color.BLACK;
91
            
92 1 1. append : negated conditional → NO_COVERAGE
            if (attribut == JTextPaneAppender.ATTRIBUTE_WARN) {
93
                foregroundColor = Color.RED;
94 1 1. append : negated conditional → NO_COVERAGE
            } else if (attribut == JTextPaneAppender.ATTRIBUTE_SUCCESS) {
95
                foregroundColor = UiUtil.COLOR_GREEN;
96
            }
97
            
98
            int tabIndex = MediatorHelper.tabConsoles().indexOfTab(this.tabName);
99
            
100 4 1. append : changed conditional boundary → NO_COVERAGE
2. append : changed conditional boundary → NO_COVERAGE
3. append : negated conditional → NO_COVERAGE
4. append : negated conditional → NO_COVERAGE
            if (0 <= tabIndex && tabIndex < MediatorHelper.tabConsoles().getTabCount()) {
101
                
102
                var tabHeader = MediatorHelper.tabConsoles().getTabComponentAt(tabIndex);
103
                
104 1 1. append : negated conditional → NO_COVERAGE
                if (MediatorHelper.tabConsoles().getSelectedIndex() != tabIndex) {
105
                    
106 1 1. append : removed call to java/awt/Component::setFont → NO_COVERAGE
                    tabHeader.setFont(tabHeader.getFont().deriveFont(Font.BOLD));
107
108 1 1. append : negated conditional → NO_COVERAGE
                    if (foregroundColor != Color.BLACK) {  // keep tab colored until user clicks
109 1 1. append : removed call to java/awt/Component::setForeground → NO_COVERAGE
                        tabHeader.setForeground(foregroundColor);
110
                    }
111
                }
112
            }
113
        } catch (Exception e) {
114
            // Report #863: exception during report of exception
115
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, message, e);
116
        }
117
    }
118
}

Mutations

42

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/console/AbstractColoredConsole::addFocusListener → NO_COVERAGE

47

1.1
Location : focusGained
Killed by : none
removed call to javax/swing/text/Caret::setVisible → NO_COVERAGE

48

1.1
Location : focusGained
Killed by : none
removed call to javax/swing/text/Caret::setSelectionVisible → NO_COVERAGE

52

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/console/AbstractColoredConsole::setCursor → NO_COVERAGE

64

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

69

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

2.2
Location : append
Killed by : none
changed conditional boundary → NO_COVERAGE

3.3
Location : append
Killed by : none
negated conditional → NO_COVERAGE

74

1.1
Location : append
Killed by : none
removed call to javax/swing/text/Document::insertString → NO_COVERAGE

76

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

80

1.1
Location : append
Killed by : none
removed call to javax/swing/text/Document::insertString → NO_COVERAGE

86

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

2.2
Location : append
Killed by : none
negated conditional → NO_COVERAGE

87

1.1
Location : append
Killed by : none
removed call to javax/swing/JScrollBar::setValue → NO_COVERAGE

2.2
Location : append
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

92

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

94

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

100

1.1
Location : append
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : append
Killed by : none
changed conditional boundary → NO_COVERAGE

3.3
Location : append
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : append
Killed by : none
negated conditional → NO_COVERAGE

104

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

106

1.1
Location : append
Killed by : none
removed call to java/awt/Component::setFont → NO_COVERAGE

108

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

109

1.1
Location : append
Killed by : none
removed call to java/awt/Component::setForeground → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.16.1