blob: 928372c33321f17b5bf129e4daf0de11f2d97843 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package cuchaz.enigma.gui.elements;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class VerifiableTextField extends JTextField {
private boolean hasError;
public VerifiableTextField() {
}
public VerifiableTextField(String text) {
super(text);
}
public VerifiableTextField(int columns) {
super(columns);
}
public VerifiableTextField(String text, int columns) {
super(text, columns);
}
public VerifiableTextField(Document doc, String text, int columns) {
super(doc, text, columns);
}
{
getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
clearErrorState();
}
@Override
public void removeUpdate(DocumentEvent e) {
clearErrorState();
}
@Override
public void changedUpdate(DocumentEvent e) {
clearErrorState();
}
});
}
@Override
public void setText(String t) {
super.setText(t);
}
public void clearErrorState() {
this.hasError = false;
repaint();
}
public void addError(String message) {
this.hasError = true;
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (hasError) {
g.setColor(Color.RED);
int x1 = getWidth() - 9;
int x2 = getWidth() - 2;
int y1 = 1;
int y2 = 8;
g.fillPolygon(new int[]{x1, x2, x2}, new int[]{y1, y1, y2}, 3);
}
}
}
|