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.commons.lang3.StringUtils;
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
8 import org.jsoup.Jsoup;
9
10 import javax.swing.*;
11 import java.awt.*;
12 import java.util.ConcurrentModificationException;
13
14
15
16
17 public class JTextPanePlaceholder extends JTextPane implements JPlaceholder {
18
19 private static final Logger LOGGER = LogManager.getRootLogger();
20
21
22
23
24 private String placeholderText;
25
26
27
28
29
30 public JTextPanePlaceholder(String placeholder) {
31 this.placeholderText = placeholder;
32 UiUtil.init(this);
33 }
34
35 @Override
36 public void paint(Graphics g) {
37
38
39
40
41
42 try {
43 super.paint(g);
44 if (StringUtils.isEmpty(Jsoup.parse(this.getText()).text().trim())) {
45 UiUtil.drawPlaceholder(this, g, this.placeholderText);
46 }
47 } catch (IllegalArgumentException | ConcurrentModificationException | IndexOutOfBoundsException | ClassCastException e) {
48 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
49 }
50 }
51
52 @Override
53 public void setPlaceholderText(String placeholderText) {
54 this.placeholderText = placeholderText;
55 }
56 }