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