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 implements JPlaceholder {
18
19 private static final Logger LOGGER = LogManager.getRootLogger();
20
21 private String placeholderText;
22
23 public SyntaxTextArea() {
24 this(StringUtils.EMPTY);
25 }
26
27 public SyntaxTextArea(String text) {
28 this.placeholderText = text;
29
30 this.setPopupMenu(new JPopupMenuComponent(this));
31 this.getCaret().setBlinkRate(0);
32 this.addFocusListener(new FocusAdapter() {
33 @Override
34 public void focusGained(FocusEvent focusEvent) {
35 SyntaxTextArea.this.getCaret().setVisible(true);
36 SyntaxTextArea.this.getCaret().setSelectionVisible(true);
37 }
38 });
39 this.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
40 this.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
41 this.setMarkOccurrences(true);
42 this.setMarkOccurrencesDelay(200);
43 }
44
45 @Override
46 public void paint(Graphics g) {
47
48
49
50
51 try {
52 super.paint(g);
53 if (StringUtils.isEmpty(this.getText()) && StringUtils.isNotEmpty(this.placeholderText)) {
54 UiUtil.drawPlaceholder(this, g, this.placeholderText);
55 }
56 } catch (IllegalArgumentException | NullPointerException | ArrayIndexOutOfBoundsException e) {
57 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
58 }
59 }
60
61 @Override
62 public Font getFont() {
63 return UiUtil.FONT_MONO_NON_ASIAN;
64 }
65 @Override
66 public Font getFontForToken(Token token) {
67 return UiUtil.FONT_MONO_NON_ASIAN;
68 }
69 @Override
70 public Font getFontForTokenType(int type) {
71 return UiUtil.FONT_MONO_NON_ASIAN;
72 }
73 @Override
74 public void setPlaceholderText(String placeholderText) {
75 this.placeholderText = placeholderText;
76 }
77 }