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 implements JPlaceholder {
18      
19      private static final Logger LOGGER = LogManager.getRootLogger();
20      
21      /**
22       * Text to display when empty.
23       */
24      private String placeholderText;
25  
26      /**
27       * Create a textfield with hint.
28       * @param placeholder Text displayed when empty
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          // Fix #4012: ArrayIndexOutOfBoundsException on paint()
38          // Fix #38546: ConcurrentModificationException on getText()
39          // Fix #37872: IndexOutOfBoundsException on getText()
40          // Fix #48915: ClassCastException on paint()
41          // Unhandled IllegalArgumentException #91471 on paint()
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  }