| 1 | package com.jsql.view.swing.text; | |
| 2 | ||
| 3 | import com.jsql.util.LogLevelUtil; | |
| 4 | import com.jsql.view.swing.util.UiUtil; | |
| 5 | import org.apache.logging.log4j.LogManager; | |
| 6 | import org.apache.logging.log4j.Logger; | |
| 7 | ||
| 8 | import javax.swing.*; | |
| 9 | import java.awt.*; | |
| 10 | ||
| 11 | /** | |
| 12 | * Textfield with information text displayed when empty. | |
| 13 | */ | |
| 14 | public class JTextFieldPlaceholder extends JTextField implements JPlaceholder { | |
| 15 | | |
| 16 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
| 17 | | |
| 18 | /** | |
| 19 | * Text to display when empty. | |
| 20 | */ | |
| 21 | private String placeholderText; | |
| 22 | private int xOffset; | |
| 23 | ||
| 24 | /** | |
| 25 | * Create a textfield with hint and default value. | |
| 26 | * @param placeholder Text displayed when empty | |
| 27 | * @param value Default value | |
| 28 | */ | |
| 29 | public JTextFieldPlaceholder(String placeholder, String value) { | |
| 30 | this(placeholder); | |
| 31 |
1
1. <init> : removed call to com/jsql/view/swing/text/JTextFieldPlaceholder::setText → NO_COVERAGE |
this.setText(value); |
| 32 | } | |
| 33 | | |
| 34 | /** | |
| 35 | * Create a textfield with hint. | |
| 36 | * @param placeholder Text displayed when empty | |
| 37 | */ | |
| 38 | public JTextFieldPlaceholder(String placeholder) { | |
| 39 | this.placeholderText = placeholder; | |
| 40 | } | |
| 41 | ||
| 42 | public JTextFieldPlaceholder(String placeholder, int xOffset) { | |
| 43 | this(placeholder); | |
| 44 | this.xOffset = xOffset; | |
| 45 | } | |
| 46 | ||
| 47 | @Override | |
| 48 | public void paint(Graphics g) { | |
| 49 | try { | |
| 50 |
1
1. paint : removed call to javax/swing/JTextField::paint → NO_COVERAGE |
super.paint(g); |
| 51 | } catch (ClassCastException e) { // Fix #4301, ClassCastException: sun.awt.image.BufImgSurfaceData cannot be cast to sun.java2d.xr.XRSurfaceData | |
| 52 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
| 53 | } | |
| 54 |
1
1. paint : negated conditional → NO_COVERAGE |
if (this.getText().isEmpty()) { |
| 55 | int h = this.getHeight(); | |
| 56 | var fm = g.getFontMetrics(); | |
| 57 |
5
1. paint : Replaced integer division with multiplication → NO_COVERAGE 2. paint : removed call to com/jsql/view/swing/util/UiUtil::drawPlaceholder → NO_COVERAGE 3. paint : Replaced integer subtraction with addition → NO_COVERAGE 4. paint : Replaced integer division with multiplication → NO_COVERAGE 5. paint : Replaced integer addition with subtraction → NO_COVERAGE |
UiUtil.drawPlaceholder(this, g, this.placeholderText, this.xOffset, h / 2 + fm.getAscent() / 2 - 1); |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | @Override | |
| 62 | public void setPlaceholderText(String placeholderText) { | |
| 63 | this.placeholderText = placeholderText; | |
| 64 | } | |
| 65 | } | |
Mutations | ||
| 31 |
1.1 |
|
| 50 |
1.1 |
|
| 54 |
1.1 |
|
| 57 |
1.1 2.2 3.3 4.4 5.5 |