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.shell; | |
12 | ||
13 | import com.jsql.util.LogLevelUtil; | |
14 | import org.apache.logging.log4j.LogManager; | |
15 | import org.apache.logging.log4j.Logger; | |
16 | ||
17 | import javax.swing.text.BadLocationException; | |
18 | import javax.swing.text.DefaultCaret; | |
19 | import javax.swing.text.JTextComponent; | |
20 | import java.awt.*; | |
21 | ||
22 | /** | |
23 | * A caret in a block shape. | |
24 | */ | |
25 | public class BlockCaret extends DefaultCaret { | |
26 | | |
27 | /** | |
28 | * Log4j logger sent to view. | |
29 | */ | |
30 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
31 | | |
32 | /** | |
33 | * Create a caret shaped for terminal. | |
34 | */ | |
35 | public BlockCaret() { | |
36 |
1
1. <init> : removed call to com/jsql/view/swing/shell/BlockCaret::setBlinkRate → NO_COVERAGE |
this.setBlinkRate(500); // half a second |
37 | } | |
38 | ||
39 | @Override | |
40 | protected synchronized void damage(Rectangle r) { | |
41 | | |
42 |
1
1. damage : negated conditional → NO_COVERAGE |
if (r == null) { |
43 | return; | |
44 | } | |
45 | ||
46 | // give values to x,y,width,height (inherited from java.awt.Rectangle) | |
47 | this.x = r.x; | |
48 | this.y = r.y; | |
49 | this.height = r.height; | |
50 | | |
51 | // A value for width was probably set by paint(), which we leave alone. | |
52 | // But the first call to damage() precedes the first call to paint(), so | |
53 | // in this case we must be prepared to set a valid width, or else | |
54 | // paint() | |
55 | // will receive a bogus clip area and caret will not get drawn properly. | |
56 |
2
1. damage : negated conditional → NO_COVERAGE 2. damage : changed conditional boundary → NO_COVERAGE |
if (this.width <= 0) { |
57 | this.width = this.getComponent().getWidth(); | |
58 | } | |
59 | ||
60 | //Calls getComponent().repaint(x, y, width, height) to erase | |
61 |
1
1. damage : removed call to com/jsql/view/swing/shell/BlockCaret::repaint → NO_COVERAGE |
this.repaint(); |
62 | | |
63 | // previous location of caret. Sometimes one call isn't enough. | |
64 |
1
1. damage : removed call to com/jsql/view/swing/shell/BlockCaret::repaint → NO_COVERAGE |
this.repaint(); |
65 | } | |
66 | ||
67 | @Override | |
68 | public void paint(Graphics g) { | |
69 | | |
70 | JTextComponent comp = this.getComponent(); | |
71 | ||
72 |
1
1. paint : negated conditional → NO_COVERAGE |
if (comp == null) { |
73 | return; | |
74 | } | |
75 | ||
76 | int dot = this.getDot(); | |
77 | Rectangle r = null; | |
78 | char dotChar; | |
79 | | |
80 | try { | |
81 | r = comp.modelToView(dot); | |
82 | | |
83 |
1
1. paint : negated conditional → NO_COVERAGE |
if (r == null) { |
84 | return; | |
85 | } | |
86 | | |
87 | dotChar = comp.getText(dot, 1).charAt(0); | |
88 | | |
89 | } catch (BadLocationException e) { | |
90 | | |
91 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
92 | return; | |
93 | } | |
94 | ||
95 |
1
1. paint : negated conditional → NO_COVERAGE |
if (Character.isWhitespace(dotChar)) { |
96 | dotChar = '_'; | |
97 | } | |
98 | ||
99 |
2
1. paint : negated conditional → NO_COVERAGE 2. paint : negated conditional → NO_COVERAGE |
if (this.x != r.x || this.y != r.y) { |
100 | | |
101 | // paint() has been called directly, without a previous call to | |
102 | // damage(), so do some cleanup. (This happens, for example, when | |
103 | // the text component is resized.) | |
104 |
1
1. paint : removed call to com/jsql/view/swing/shell/BlockCaret::damage → NO_COVERAGE |
this.damage(r); |
105 | | |
106 | return; | |
107 | } | |
108 | ||
109 |
1
1. paint : removed call to java/awt/Graphics::setColor → NO_COVERAGE |
g.setColor(new Color(0, 255, 0)); |
110 | ||
111 | // do this to draw in XOR mode | |
112 |
1
1. paint : removed call to java/awt/Graphics::setXORMode → NO_COVERAGE |
g.setXORMode(comp.getBackground()); |
113 | ||
114 | this.width = g.getFontMetrics().charWidth(dotChar); | |
115 | | |
116 |
1
1. paint : negated conditional → NO_COVERAGE |
if (this.isVisible()) { |
117 |
1
1. paint : removed call to java/awt/Graphics::fillRect → NO_COVERAGE |
g.fillRect(r.x, r.y, this.width, r.height); |
118 | } | |
119 | } | |
120 | } | |
Mutations | ||
36 |
1.1 |
|
42 |
1.1 |
|
56 |
1.1 2.2 |
|
61 |
1.1 |
|
64 |
1.1 |
|
72 |
1.1 |
|
83 |
1.1 |
|
95 |
1.1 |
|
99 |
1.1 2.2 |
|
104 |
1.1 |
|
109 |
1.1 |
|
112 |
1.1 |
|
116 |
1.1 |
|
117 |
1.1 |