|
1 | | -/** |
| 1 | +/* |
2 | 2 | * Copyright 2019 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
18 | 18 | import java.awt.Graphics; |
19 | 19 | import java.awt.event.FocusEvent; |
20 | 20 | import java.awt.event.FocusListener; |
| 21 | + |
21 | 22 | import javax.swing.JTextPane; |
22 | 23 | import javax.swing.UIManager; |
23 | | -import javax.swing.text.Caret; |
24 | 24 |
|
25 | | -@SuppressWarnings("all") |
26 | 25 | public class RunnerTextPane extends JTextPane implements FocusListener { |
27 | | - public RunnerTextPane() { |
28 | | - super(); |
29 | | - this.addFocusListener(this); |
30 | | - } |
31 | | - |
32 | | - @Override |
33 | | - public void paintComponent(final Graphics g) { |
34 | | - boolean _isOpaque = this.isOpaque(); |
35 | | - boolean _not = (!_isOpaque); |
36 | | - if (_not) { |
37 | | - super.paintComponent(g); |
38 | | - return; |
| 26 | + private static final long serialVersionUID = 1089473481444949272L; |
| 27 | + |
| 28 | + public RunnerTextPane() { |
| 29 | + super(); |
| 30 | + addFocusListener(this); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void paintComponent(final Graphics g) { |
| 35 | + // default for non-opaque components |
| 36 | + if (isOpaque()) { |
| 37 | + super.paintComponent(g); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // use value of JTextField for consistency |
| 42 | + g.setColor(UIManager.getColor("TextField.inactiveBackground")); |
| 43 | + g.fillRect(0, 0, getWidth(), getHeight()); |
| 44 | + setOpaque(false); |
| 45 | + |
| 46 | + // do rest, changing opaque to ensure background is not overwritten |
| 47 | + super.paintComponent(g); |
| 48 | + setOpaque(true); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void focusGained(final FocusEvent e) { |
| 53 | + getCaret().setVisible(true); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void focusLost(final FocusEvent e) { |
| 58 | + getCaret().setVisible(false); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setText(final String t) { |
| 63 | + super.setText(t); |
| 64 | + // ensure left parts of long lines are always visible |
| 65 | + setCaretPosition(0); |
39 | 66 | } |
40 | | - g.setColor(UIManager.getColor("TextField.inactiveBackground")); |
41 | | - g.fillRect(0, 0, this.getWidth(), this.getHeight()); |
42 | | - this.setOpaque(false); |
43 | | - super.paintComponent(g); |
44 | | - this.setOpaque(true); |
45 | | - } |
46 | | - |
47 | | - @Override |
48 | | - public void focusGained(final FocusEvent e) { |
49 | | - Caret _caret = this.getCaret(); |
50 | | - _caret.setVisible(true); |
51 | | - } |
52 | | - |
53 | | - @Override |
54 | | - public void focusLost(final FocusEvent e) { |
55 | | - Caret _caret = this.getCaret(); |
56 | | - _caret.setVisible(false); |
57 | | - } |
58 | | - |
59 | | - @Override |
60 | | - public void setText(final String t) { |
61 | | - super.setText(t); |
62 | | - this.setCaretPosition(0); |
63 | | - } |
64 | 67 | } |
0 commit comments