KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyEditorSupport JavaDoc;
19
20 import org.springframework.binding.format.Formatter;
21
22 /**
23  * Adapts a formatter to the property editor interface.
24  * @author Keith Donald
25  */

26 public class FormatterPropertyEditor extends PropertyEditorSupport JavaDoc {
27
28     /**
29      * The formatter
30      */

31     private Formatter formatter;
32
33     /**
34      * The target value class (may be null).
35      */

36     private Class JavaDoc targetClass;
37
38     /**
39      * Creates a formatter property editor.
40      * @param formatter the formatter to adapt
41      */

42     public FormatterPropertyEditor(Formatter formatter) {
43         this.formatter = formatter;
44     }
45
46     /**
47      * Creates a formatter property editor.
48      *
49      * @param formatter the formatter to adapt
50      * @param targetClass the target class for "setAsText" conversions.
51      */

52     public FormatterPropertyEditor(Formatter formatter, Class JavaDoc targetClass) {
53         this.formatter = formatter;
54         this.targetClass = targetClass;
55     }
56
57     public String JavaDoc getAsText() {
58         return formatter.formatValue(getValue());
59     }
60
61     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
62         setValue(formatter.parseValue(text, targetClass));
63     }
64 }
Popular Tags