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   
9   import javax.swing.*;
10  import java.awt.*;
11  
12  /**
13   * Textfield with information text displayed when empty.
14   */
15  public class JTextAreaPlaceholder extends JTextArea implements JPlaceholder {
16      
17      private static final Logger LOGGER = LogManager.getRootLogger();
18      
19      /**
20       * Text to display when empty.
21       */
22      private String placeholderText;
23      
24      /**
25       * Create a textfield with hint.
26       * @param placeholder Text displayed when empty
27       */
28      public JTextAreaPlaceholder(String placeholder) {
29          this.placeholderText = placeholder;
30          UiUtil.init(this);
31      }
32  
33      @Override
34      public void paint(Graphics g) {
35          // Fix #6350: ArrayIndexOutOfBoundsException on paint()
36          // Fix #90822: IllegalArgumentException on paint()
37          // Fix #90761: StateInvariantError on paint()
38          // StateInvariantError possible on jdk 8 when WrappedPlainView.drawLine in paint()
39          try {
40              super.paint(g);
41              if (StringUtils.isEmpty(this.getText())) {
42                  UiUtil.drawPlaceholder(this, g, this.placeholderText);
43              }
44          } catch (IllegalArgumentException | NullPointerException | ArrayIndexOutOfBoundsException e) {
45              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
46          }
47      }
48  
49      @Override
50      public void setPlaceholderText(String placeholderText) {
51          this.placeholderText = placeholderText;
52      }
53  }