KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > util > bean > PropertyEditorContext


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 package com.nightlabs.util.bean;
32
33 import java.beans.PropertyEditor JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37
38 public class PropertyEditorContext
39 {
40     protected Map JavaDoc targetClass2PropertyEditorClass;
41     protected Map JavaDoc propertyName2PropertyEditor;
42     
43     public PropertyEditorContext()
44     {
45         targetClass2PropertyEditorClass = new HashMap JavaDoc();
46         propertyName2PropertyEditor = new HashMap JavaDoc();
47     }
48         
49     public void registerEditor(Class JavaDoc targetType, Class JavaDoc editorClass)
50     {
51         if (targetType == null)
52             throw new NullPointerException JavaDoc("Param targetType must not be null!");
53         
54         if (editorClass == null)
55             throw new NullPointerException JavaDoc("Param editorClass must not be null!");
56         
57         targetClass2PropertyEditorClass.put(targetType, editorClass);
58     }
59     
60     public void registerEditor(Object JavaDoc target, Class JavaDoc editorClass)
61     {
62         if (target == null)
63             throw new IllegalArgumentException JavaDoc("Param target must not be null!");
64     
65         Class JavaDoc targetClass = target.getClass();
66         registerEditor(targetClass, editorClass);
67     }
68     
69     public void registerEditor(String JavaDoc propertyName, Class JavaDoc editorClass)
70     {
71         if(propertyName == null)
72             throw new NullPointerException JavaDoc("Param propertyName must not be null!");
73         
74         if (editorClass == null)
75             throw new NullPointerException JavaDoc("Param editorClass must not be null!");
76         
77         propertyName2PropertyEditor.put(propertyName, editorClass);
78     }
79
80     public PropertyEditor JavaDoc getEditor(Object JavaDoc o)
81     throws IllegalAccessException JavaDoc,
82                  InstantiationException JavaDoc
83     {
84         if (o == null)
85             throw new IllegalArgumentException JavaDoc("Param o must not be null!");
86         
87         Class JavaDoc targetClass = o.getClass();
88         PropertyEditor JavaDoc pe = getEditor(targetClass);
89         if (pe != null) {
90             return pe;
91         } else {
92             for (Iterator JavaDoc it = targetClass2PropertyEditorClass.keySet().iterator(); it.hasNext(); ) {
93                 Class JavaDoc targetType = (Class JavaDoc) it.next();
94                 if (targetType.isAssignableFrom(targetClass)) {
95                     return (PropertyEditor JavaDoc) targetClass2PropertyEditorClass.get(targetType);
96                 }
97             }
98         }
99         return null;
100     }
101     
102     public PropertyEditor JavaDoc getEditor(Class JavaDoc targetType)
103     throws InstantiationException JavaDoc,
104                  IllegalAccessException JavaDoc
105     {
106         if (targetClass2PropertyEditorClass.containsKey(targetType))
107         {
108             Class JavaDoc pecClass = (Class JavaDoc) targetClass2PropertyEditorClass.get(targetType);
109             PropertyEditor JavaDoc pe = (PropertyEditor JavaDoc) pecClass.newInstance();
110             return pe;
111         }
112         return null;
113     }
114
115     public PropertyEditor JavaDoc getEditor(String JavaDoc propertyName)
116     {
117         if (propertyName2PropertyEditor.containsKey(propertyName))
118         {
119             PropertyEditor JavaDoc pe = (PropertyEditor JavaDoc) propertyName2PropertyEditor.get(propertyName);
120             return pe;
121         }
122         return null;
123     }
124     
125 }
126
Popular Tags