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