1 /***
2 * Copyright (C) 2006 Philipp Mpalampanis
3 *
4 * License: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Alternatively, the contents of this file may be used under the terms of
17 * either the GNU General Public License Version 2 or later (the "GPL"), or
18 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19 * in which case the provisions of the GPL or the LGPL are applicable instead
20 * of those above. If you wish to allow use of your version of this file only
21 * under the terms of either the GPL or the LGPL, and not to allow others to
22 * use your version of this file under the terms of the MPL, indicate your
23 * decision by deleting the provisions above and replace them with the notice
24 * and other provisions required by the GPL or the LGPL. If you do not delete
25 * the provisions above, a recipient may use your version of this file under
26 * the terms of any one of the MPL, the GPL or the LGPL.
27 */
28 package net.sf.echobinding.binding;
29
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import net.sf.echobinding.decorator.Decorator;
34 import net.sf.echobinding.decorator.DefaultDecorator;
35 import net.sf.echobinding.format.Format;
36 import net.sf.echobinding.validation.*;
37
38 /***
39 * @see net.sf.echobinding.binding.PropertyAdapter
40 */
41 public abstract class AbstractPropertyAdapter implements PropertyAdapter {
42
43 private Set<Validator> _validators;
44 private Format _format;
45 private Decorator _decorator;
46 private String _label;
47 private String _id;
48 private ValidationHandler _validationHandler;
49 private BindingContext _subContext;
50
51 protected Object format(Object value) {
52 if(getFormat()!=null)
53 return getFormat().format(value);
54 return value;
55 }
56
57 protected Object parse(Object value) {
58 if(getFormat()!=null)
59 return getFormat().parse((String)value);
60 return value;
61 }
62
63 public ValidationReport validate(Object bean, Object value) {
64
65 ValidationReport validationReport = new ValidationReport(true, null);
66
67 if(getValidators()!=null) {
68 for(Validator validator : getValidators() ) {
69 validationReport = validator.validate( bean, parse( value ) );
70 if(validationReport != null && !validationReport.isValid())
71 return validationReport;
72 }
73 }
74
75 return validationReport;
76 }
77
78
79 public PropertyAdapter setValidators(Set<Validator> validators) {
80 _validators = validators;
81 return this;
82 }
83
84 public PropertyAdapter addValidator(Validator validator) {
85 if(_validators==null)
86 _validators = new HashSet<Validator>();
87 _validators.add(validator);
88 return this;
89 }
90
91 public PropertyAdapter setFormat(Format format) {
92 _format = format;
93 return this;
94 }
95
96 public PropertyAdapter setDecorator(Decorator decorator) {
97 _decorator = decorator;
98 return this;
99 }
100
101 public Decorator getDecorator() {
102 if(_decorator == null)
103 return new DefaultDecorator();
104 return _decorator;
105 }
106
107 public String getLabel() {
108 return _label;
109 }
110
111
112 public PropertyAdapter setLabel(String label) {
113 _label = label;
114 return this;
115 }
116
117 public String getId() {
118 return _id;
119 }
120
121 public PropertyAdapter setId(String id) {
122 _id = id;
123 return this;
124 }
125
126 public Format getFormat() {
127 return _format;
128 }
129
130 /***
131 * @return Returns the validator.
132 */
133 public Set<Validator> getValidators() {
134 return _validators;
135 }
136
137 public PropertyAdapter setValidationHandler(ValidationHandler validationHandler) {
138 _validationHandler = validationHandler;
139 return this;
140 }
141
142 public ValidationHandler getValidationHandler() {
143 return _validationHandler;
144 }
145
146 public PropertyAdapter setSubContext(BindingContext context) {
147 _subContext = context;
148 return this;
149 }
150
151 public BindingContext getSubContext() {
152 return _subContext;
153 }
154
155 }