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      /**
29       * Log4j logger sent to view.
30       */
31      private static final Logger LOGGER = LogManager.getRootLogger();
32  
33      /**
34       * Build new instance of readonly JTextArea to decorate.
35       */
36      public JPopupTextArea() {
37          this(StringUtils.EMPTY);
38      }
39      
40      /**
41       * Build new instance of readonly JTextArea to decorate
42       * with a default placeholder.
43       */
44      public JPopupTextArea(String placeholder) {
45          this(new JTextAreaPlaceholder(placeholder) {
46              @Override
47              public boolean isEditable() {
48                  return false;
49              }
50          });
51      }
52  
53      /**
54       * Build new instance of JTextArea to decorate.
55       */
56      public JPopupTextArea(JTextArea proxy) {
57          super(proxy);
58  
59          // Side effect: disable caret blink, editable texts must restore blink rate
60          this.getProxy().addFocusListener(new FocusAdapter() {
61              @Override
62              public void focusGained(FocusEvent focusEvent) {
63                  // Fix #95769: IllegalArgumentException on setVisible()
64                  try {
65                      JPopupTextArea.this.getProxy().getCaret().setVisible(true);
66                  } catch (IllegalArgumentException e) {
67                      LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e);
68                  }
69                  JPopupTextArea.this.getProxy().getCaret().setSelectionVisible(true);
70              }
71          });
72  
73          this.getProxy().setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
74          this.getProxy().setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
75      }
76  }