View Javadoc
1   package com.jsql.view.swing.text;
2   
3   import com.jsql.util.LogLevelUtil;
4   import com.jsql.view.swing.popupmenu.JPopupMenuComponent;
5   import com.jsql.view.swing.util.UiUtil;
6   import org.apache.commons.lang3.StringUtils;
7   import org.apache.logging.log4j.LogManager;
8   import org.apache.logging.log4j.Logger;
9   import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
10  import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
11  import org.fife.ui.rsyntaxtextarea.Token;
12  
13  import java.awt.*;
14  import java.awt.event.FocusAdapter;
15  import java.awt.event.FocusEvent;
16  
17  public class SyntaxTextArea extends RSyntaxTextArea {
18  
19      /**
20       * Log4j logger sent to view.
21       */
22      private static final Logger LOGGER = LogManager.getRootLogger();
23  
24      private String placeholderText;
25  
26      public SyntaxTextArea() {
27          this(StringUtils.EMPTY);
28      }
29  
30      public SyntaxTextArea(String text) {
31          this.placeholderText = text;
32  
33          this.setPopupMenu(new JPopupMenuComponent(this));
34          this.getCaret().setBlinkRate(0);
35          this.addFocusListener(new FocusAdapter() {
36              @Override
37              public void focusGained(FocusEvent focusEvent) {
38                  SyntaxTextArea.this.getCaret().setVisible(true);
39                  SyntaxTextArea.this.getCaret().setSelectionVisible(true);
40              }
41          });
42          this.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
43          this.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
44          this.setMarkOccurrences(true);
45          this.setMarkOccurrencesDelay(200);
46      }
47  
48      @Override
49      public void paint(Graphics g) {
50          // Fix #6350: ArrayIndexOutOfBoundsException on paint()
51          // Fix #90822: IllegalArgumentException on paint()
52          // Fix #90761: StateInvariantError on paint()
53          // StateInvariantError possible on jdk 8 when WrappedPlainView.drawLine in paint()
54          try {
55              super.paint(g);
56              if (StringUtils.isEmpty(this.getText()) && StringUtils.isNotEmpty(this.placeholderText)) {
57                  UiUtil.drawPlaceholder(this, g, this.placeholderText);
58              }
59          } catch (IllegalArgumentException | NullPointerException | ArrayIndexOutOfBoundsException e) {
60              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
61          }
62      }
63  
64      @Override
65      public Font getFont() {
66          return UiUtil.FONT_MONO_NON_ASIAN;
67      }
68      @Override
69      public Font getFontForToken(Token token) {
70          return UiUtil.FONT_MONO_NON_ASIAN;
71      }
72      @Override
73      public Font getFontForTokenType(int type) {
74          return UiUtil.FONT_MONO_NON_ASIAN;
75      }
76  
77      public void setPlaceholderText(String placeholderText) {
78          this.placeholderText = placeholderText;
79      }
80  }