KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > format > support > PropertyEditorFormatter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.format.support;
17
18 import java.beans.PropertyEditor JavaDoc;
19
20 import org.springframework.util.Assert;
21
22 /**
23  * Adapts a property editor to the type converter interface.
24  * @author Keith Donald
25  */

26 public class PropertyEditorFormatter extends AbstractFormatter {
27
28     private PropertyEditor JavaDoc propertyEditor;
29
30     public PropertyEditorFormatter(PropertyEditor JavaDoc propertyEditor) {
31         super();
32         Assert.notNull(propertyEditor, "Property editor is required");
33         this.propertyEditor = propertyEditor;
34     }
35
36     public PropertyEditor JavaDoc getPropertyEditor() {
37         return propertyEditor;
38     }
39
40     protected String JavaDoc doFormatValue(Object JavaDoc value) {
41         propertyEditor.setValue(value);
42         return propertyEditor.getAsText();
43     }
44
45     protected Object JavaDoc doParseValue(String JavaDoc formattedValue, Class JavaDoc targetClass) {
46         propertyEditor.setAsText(formattedValue);
47         return propertyEditor.getValue();
48     }
49 }
Popular Tags