KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
36
37 public class PropertyEditorMan
38 {
39     private static Map JavaDoc class2Pec = new HashMap JavaDoc();
40     
41     public PropertyEditorMan()
42     {
43     }
44         
45     public static void registerEditorContext(Class JavaDoc propertyEditorContextClass)
46     throws InstantiationException JavaDoc,
47                  IllegalAccessException JavaDoc
48     {
49         PropertyEditorContext pec = (PropertyEditorContext) propertyEditorContextClass.newInstance();
50         class2Pec.put(propertyEditorContextClass, pec);
51     }
52     
53     public static void registerPropertyEditor(Class JavaDoc targetClass, Class JavaDoc propertyEditorClass, Class JavaDoc propertyEditorContextClass)
54     {
55         if (class2Pec.containsKey(propertyEditorContextClass))
56         {
57             PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass);
58             pec.registerEditor(targetClass, propertyEditorClass);
59         }
60     }
61     
62     public static void registerPropertyEditor(Class JavaDoc targetClass, Class JavaDoc propertyEditorClass)
63     {
64         registerPropertyEditor(targetClass, propertyEditorClass, DefaultPropertyEditorContext.class);
65     }
66     
67     public static PropertyEditor JavaDoc getPropertyEditor(Class JavaDoc propertyEditorContextClass, Class JavaDoc targetType)
68 // throws IllegalAccessException,
69
// InstantiationException
70
{
71         if (class2Pec.containsKey(propertyEditorContextClass))
72         {
73             PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass);
74 // return pec.getEditor(targetType);
75
try {
76                 return pec.getEditor(targetType);
77             } catch (InstantiationException JavaDoc e) {
78                 e.printStackTrace();
79             } catch (IllegalAccessException JavaDoc e) {
80                 e.printStackTrace();
81             }
82         }
83         return null;
84     }
85
86     public static PropertyEditor JavaDoc getPropertyEditor(Class JavaDoc targetType)
87 // throws IllegalAccessException,
88
// InstantiationException
89
{
90         return getPropertyEditor(DefaultPropertyEditorContext.class, targetType);
91     }
92     
93     public static PropertyEditor JavaDoc getPropertyEditor(Class JavaDoc propertyEditorContextClass, Object JavaDoc target)
94 // throws IllegalAccessException,
95
// InstantiationException
96
{
97         if (target == null)
98             throw new IllegalArgumentException JavaDoc("Param target must not be null!");
99         
100         Class JavaDoc targetType = target.getClass();
101         PropertyEditor JavaDoc pe = getPropertyEditor(propertyEditorContextClass, targetType);
102         if (pe != null) {
103             return pe;
104         } else {
105             if (class2Pec.containsKey(propertyEditorContextClass)) {
106                 PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass);
107                 try {
108                     return pec.getEditor(target);
109                 } catch (IllegalAccessException JavaDoc e) {
110                     e.printStackTrace();
111                 } catch (InstantiationException JavaDoc e) {
112                     e.printStackTrace();
113                 }
114             }
115         }
116         return null;
117     }
118     
119     public static PropertyEditor JavaDoc getPropertyEditor(Class JavaDoc propertyEditorContextClass, String JavaDoc propertyName)
120     {
121         if (class2Pec.containsKey(propertyEditorContextClass))
122         {
123             PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass);
124             PropertyEditor JavaDoc pe = pec.getEditor(propertyName);
125             return pe;
126         }
127         return null;
128     }
129     
130 }
131
Popular Tags