1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.ActionListener;
|
---|
7 | import java.awt.event.FocusEvent;
|
---|
8 | import java.awt.event.FocusListener;
|
---|
9 | import java.beans.PropertyChangeEvent;
|
---|
10 | import java.beans.PropertyChangeListener;
|
---|
11 | import java.util.Objects;
|
---|
12 |
|
---|
13 | import javax.swing.BorderFactory;
|
---|
14 | import javax.swing.UIManager;
|
---|
15 | import javax.swing.border.Border;
|
---|
16 | import javax.swing.event.DocumentEvent;
|
---|
17 | import javax.swing.event.DocumentListener;
|
---|
18 | import javax.swing.text.JTextComponent;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This is an abstract class for a validator on a text component.
|
---|
24 | *
|
---|
25 | * Subclasses implement {@link #validate()}. {@link #validate()} is invoked whenever
|
---|
26 | * <ul>
|
---|
27 | * <li>the content of the text component changes (the validator is a {@link DocumentListener})</li>
|
---|
28 | * <li>the text component loses focus (the validator is a {@link FocusListener})</li>
|
---|
29 | * <li>the text component is a {@link JosmTextField} and an {@link ActionEvent} is detected</li>
|
---|
30 | * </ul>
|
---|
31 | *
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public abstract class AbstractTextComponentValidator implements ActionListener, FocusListener, DocumentListener, PropertyChangeListener {
|
---|
35 | private static final Border ERROR_BORDER = BorderFactory.createLineBorder(Color.RED, 1);
|
---|
36 | private static final Color ERROR_BACKGROUND = new Color(255, 224, 224);
|
---|
37 |
|
---|
38 | private JTextComponent tc;
|
---|
39 | /** remembers whether the content of the text component is currently valid or not; null means,
|
---|
40 | * we don't know yet
|
---|
41 | */
|
---|
42 | private Boolean valid;
|
---|
43 | // remember the message
|
---|
44 | private String msg;
|
---|
45 |
|
---|
46 | protected void feedbackInvalid(String msg) {
|
---|
47 | if (valid == null || valid || !Objects.equals(msg, this.msg)) {
|
---|
48 | // only provide feedback if the validity has changed. This avoids unnecessary UI updates.
|
---|
49 | tc.setBorder(ERROR_BORDER);
|
---|
50 | tc.setBackground(ERROR_BACKGROUND);
|
---|
51 | tc.setToolTipText(msg);
|
---|
52 | valid = Boolean.FALSE;
|
---|
53 | this.msg = msg;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected void feedbackDisabled() {
|
---|
58 | feedbackValid(null);
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void feedbackValid(String msg) {
|
---|
62 | if (valid == null || !valid || !Objects.equals(msg, this.msg)) {
|
---|
63 | // only provide feedback if the validity has changed. This avoids unnecessary UI updates.
|
---|
64 | tc.setBorder(UIManager.getBorder("TextField.border"));
|
---|
65 | tc.setBackground(UIManager.getColor("TextField.background"));
|
---|
66 | tc.setToolTipText(msg == null ? "" : msg);
|
---|
67 | valid = Boolean.TRUE;
|
---|
68 | this.msg = msg;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Replies the decorated text component
|
---|
74 | *
|
---|
75 | * @return the decorated text component
|
---|
76 | */
|
---|
77 | public JTextComponent getComponent() {
|
---|
78 | return tc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Creates the validator and weires it to the text component <code>tc</code>.
|
---|
83 | *
|
---|
84 | * @param tc the text component. Must not be null.
|
---|
85 | * @throws IllegalArgumentException if tc is null
|
---|
86 | */
|
---|
87 | protected AbstractTextComponentValidator(JTextComponent tc) {
|
---|
88 | this(tc, true);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Alternative constructor that allows to turn off the actionListener.
|
---|
93 | * This can be useful if the enter key stroke needs to be forwarded to the default button in a dialog.
|
---|
94 | * @param tc text component
|
---|
95 | * @param addActionListener {@code true} to add the action listener
|
---|
96 | */
|
---|
97 | protected AbstractTextComponentValidator(JTextComponent tc, boolean addActionListener) {
|
---|
98 | this(tc, true, true, addActionListener);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Constructs a new {@code AbstractTextComponentValidator}.
|
---|
103 | * @param tc text component
|
---|
104 | * @param addFocusListener {@code true} to add the focus listener
|
---|
105 | * @param addDocumentListener {@code true} to add the document listener
|
---|
106 | * @param addActionListener {@code true} to add the action listener
|
---|
107 | */
|
---|
108 | protected AbstractTextComponentValidator(
|
---|
109 | JTextComponent tc, boolean addFocusListener, boolean addDocumentListener, boolean addActionListener) {
|
---|
110 | CheckParameterUtil.ensureParameterNotNull(tc, "tc");
|
---|
111 | this.tc = tc;
|
---|
112 | if (addFocusListener) {
|
---|
113 | tc.addFocusListener(this);
|
---|
114 | }
|
---|
115 | if (addDocumentListener) {
|
---|
116 | tc.getDocument().addDocumentListener(this);
|
---|
117 | }
|
---|
118 | if (addActionListener && tc instanceof JosmTextField) {
|
---|
119 | ((JosmTextField) tc).addActionListener(this);
|
---|
120 | }
|
---|
121 | tc.addPropertyChangeListener("enabled", this);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Implement in subclasses to validate the content of the text component.
|
---|
126 | *
|
---|
127 | */
|
---|
128 | public abstract void validate();
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Replies true if the current content of the decorated text component is valid;
|
---|
132 | * false otherwise
|
---|
133 | *
|
---|
134 | * @return true if the current content of the decorated text component is valid
|
---|
135 | */
|
---|
136 | public abstract boolean isValid();
|
---|
137 |
|
---|
138 | /* -------------------------------------------------------------------------------- */
|
---|
139 | /* interface FocusListener */
|
---|
140 | /* -------------------------------------------------------------------------------- */
|
---|
141 | @Override
|
---|
142 | public void focusGained(FocusEvent arg0) {}
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public void focusLost(FocusEvent arg0) {
|
---|
146 | validate();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* -------------------------------------------------------------------------------- */
|
---|
150 | /* interface ActionListener */
|
---|
151 | /* -------------------------------------------------------------------------------- */
|
---|
152 | @Override
|
---|
153 | public void actionPerformed(ActionEvent arg0) {
|
---|
154 | validate();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* -------------------------------------------------------------------------------- */
|
---|
158 | /* interface DocumentListener */
|
---|
159 | /* -------------------------------------------------------------------------------- */
|
---|
160 | @Override
|
---|
161 | public void changedUpdate(DocumentEvent arg0) {
|
---|
162 | validate();
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void insertUpdate(DocumentEvent arg0) {
|
---|
167 | validate();
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public void removeUpdate(DocumentEvent arg0) {
|
---|
172 | validate();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* -------------------------------------------------------------------------------- */
|
---|
176 | /* interface PropertyChangeListener */
|
---|
177 | /* -------------------------------------------------------------------------------- */
|
---|
178 | @Override
|
---|
179 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
180 | if ("enabled".equals(evt.getPropertyName())) {
|
---|
181 | boolean enabled = (Boolean) evt.getNewValue();
|
---|
182 | if (enabled) {
|
---|
183 | validate();
|
---|
184 | } else {
|
---|
185 | feedbackDisabled();
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|