Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConstraintManager |
|
| 1.6;1.6 |
1 | package org.jtheque.core.utils.ui.constraints; | |
2 | ||
3 | import org.jtheque.core.managers.error.JThequeError; | |
4 | import org.jtheque.utils.ui.DocumentLengthFilterAvert; | |
5 | ||
6 | import javax.swing.JTextField; | |
7 | import javax.swing.text.AbstractDocument; | |
8 | import javax.swing.text.Document; | |
9 | import javax.swing.text.DocumentFilter; | |
10 | import java.util.Collection; | |
11 | import java.util.HashMap; | |
12 | import java.util.Map; | |
13 | ||
14 | /* | |
15 | * This file is part of JTheque. | |
16 | * | |
17 | * JTheque is free software: you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation, either version 3 of the License. | |
20 | * | |
21 | * JTheque is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
28 | */ | |
29 | ||
30 | /** | |
31 | * A constraint manager. | |
32 | * | |
33 | * @author Baptiste Wicht | |
34 | */ | |
35 | public final class ConstraintManager { | |
36 | 0 | private static final Map<String, Constraint> CONSTRAINTS = new HashMap<String, Constraint>(20); |
37 | ||
38 | /** | |
39 | * Construct a new ConstraintManager. This class isn't instanciable. | |
40 | */ | |
41 | private ConstraintManager() { | |
42 | 0 | super(); |
43 | 0 | } |
44 | ||
45 | /** | |
46 | * Add a constraint. | |
47 | * | |
48 | * @param fieldName The name of the field. | |
49 | * @param constraint The constraint. | |
50 | */ | |
51 | public static void addConstraint(String fieldName, Constraint constraint) { | |
52 | 0 | CONSTRAINTS.put(fieldName, constraint); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Return the constraint for a specific field. | |
57 | * | |
58 | * @param fieldName The name of the field to get the constraint for. | |
59 | * @return The constraint for the field. | |
60 | */ | |
61 | public static Constraint getConstraint(String fieldName) { | |
62 | 0 | return CONSTRAINTS.get(fieldName); |
63 | } | |
64 | ||
65 | /** | |
66 | * Validate the field with the constraint. | |
67 | * | |
68 | * @param fieldName The name of the field. | |
69 | * @param field The field. | |
70 | * @param errors The errors list to fill. | |
71 | */ | |
72 | public static void validate(String fieldName, Object field, Collection<JThequeError> errors) { | |
73 | 0 | if (CONSTRAINTS.containsKey(fieldName)) { |
74 | 0 | CONSTRAINTS.get(fieldName).validate(field, errors); |
75 | } | |
76 | 0 | } |
77 | ||
78 | /** | |
79 | * Configure a JTextField with the constraint. | |
80 | * | |
81 | * @param field The field to configure. | |
82 | * @param fieldName The name of the field. | |
83 | */ | |
84 | public static void configure(JTextField field, String fieldName) { | |
85 | 0 | if (CONSTRAINTS.containsKey(fieldName) && CONSTRAINTS.get(fieldName).mustControlLength()) { |
86 | 0 | DocumentFilter filter = new DocumentLengthFilterAvert(CONSTRAINTS.get(fieldName).maxLength(), field); |
87 | 0 | Document document = field.getDocument(); |
88 | 0 | ((AbstractDocument) document).setDocumentFilter(filter); |
89 | } | |
90 | 0 | } |
91 | } |