summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/VerifiableTextField.java
blob: 41a2484013f892feceb4be8c2709fe04b30acdbb (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
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.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);
	}

	{
		addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				setErrorState(false);
			}
		});
	}

	public void setErrorState(boolean b) {
		this.hasError = b;
		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);
		}
	}

}