View Javadoc
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   * Textfield with information text displayed when empty.
16   */
17  public class JTextPanePlaceholder extends JTextPane {
18      
19      /**
20       * Log4j logger sent to view.
21       */
22      private static final Logger LOGGER = LogManager.getRootLogger();
23      
24      /**
25       * Text to display when empty.
26       */
27      private String placeholderText;
28  
29      /**
30       * Create a textfield with hint.
31       * @param placeholder Text displayed when empty
32       */
33      public JTextPanePlaceholder(String placeholder) {
34          this.placeholderText = placeholder;
35          UiUtil.init(this);
36      }
37  
38      @Override
39      public void paint(Graphics g) {
40          // Fix #4012: ArrayIndexOutOfBoundsException on paint()
41          // Fix #38546: ConcurrentModificationException on getText()
42          // Fix #37872: IndexOutOfBoundsException on getText()
43          // Fix #48915: ClassCastException on paint()
44          // Unhandled IllegalArgumentException #91471 on paint()
45          try {
46              super.paint(g);
47              if (StringUtils.isEmpty(Jsoup.parse(this.getText()).text().trim())) {
48                  UiUtil.drawPlaceholder(this, g, this.placeholderText);
49              }
50          } catch (IllegalArgumentException | ConcurrentModificationException | IndexOutOfBoundsException | ClassCastException e) {
51              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
52          }
53      }
54  
55      public void setPlaceholderText(String placeholderText) {
56          this.placeholderText = placeholderText;
57      }
58  }