View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    *
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   *******************************************************************************/
11  package com.jsql.view.swing.text;
12  
13  import com.jsql.util.LogLevelUtil;
14  import org.apache.commons.lang3.StringUtils;
15  import org.apache.logging.log4j.LogManager;
16  import org.apache.logging.log4j.Logger;
17  
18  import javax.swing.*;
19  import java.awt.*;
20  import java.awt.event.FocusAdapter;
21  import java.awt.event.FocusEvent;
22  
23  /**
24   * A JTextArea decorated with popup menu and border.
25   */
26  public class JPopupTextArea extends JPopupTextComponent<JTextArea> implements DecoratorJComponent<JTextArea> {
27  
28      private static final Logger LOGGER = LogManager.getRootLogger();
29  
30      /**
31       * Build new instance of readonly JTextArea to decorate.
32       */
33      public JPopupTextArea() {
34          this(StringUtils.EMPTY);
35      }
36      
37      /**
38       * Build new instance of readonly JTextArea to decorate
39       * with a default placeholder.
40       */
41      public JPopupTextArea(String placeholder) {
42          this(new JTextAreaPlaceholder(placeholder) {
43              @Override
44              public boolean isEditable() {
45                  return false;
46              }
47          });
48      }
49  
50      /**
51       * Build new instance of JTextArea to decorate.
52       */
53      public JPopupTextArea(JTextArea proxy) {
54          super(proxy);
55  
56          // Side effect: disable caret blink, editable texts must restore blink rate
57          this.getProxy().addFocusListener(new FocusAdapter() {
58              @Override
59              public void focusGained(FocusEvent focusEvent) {
60                  // Fix #95769: IllegalArgumentException on setVisible()
61                  try {
62                      JPopupTextArea.this.getProxy().getCaret().setVisible(true);
63                  } catch (IllegalArgumentException e) {
64                      LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e);
65                  }
66                  JPopupTextArea.this.getProxy().getCaret().setSelectionVisible(true);
67              }
68          });
69  
70          this.getProxy().setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
71          this.getProxy().setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
72      }
73  }