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