KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > beans > CustomPropertyEditorManager


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.beans;
8
9 import java.beans.PropertyEditor JavaDoc;
10 import java.beans.PropertyEditorManager JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 /**
15  * Decorator class to enhance the functionnality of the {@link PropertyEditorManager}.
16  * If a PropertyEditor is not found, a PropertyEditor matching one of the implemented interfaces
17  * is searched.
18  *
19  * @author Laurent Etiemble
20  * @version $Revision: 1.8 $
21  */

22 public abstract class CustomPropertyEditorManager extends PropertyEditorManager JavaDoc
23 {
24    /** Description of the Field */
25    public final static String JavaDoc EDITORS_PACKAGE = "org.ejtools.adwt.editor";
26
27
28    /**
29     * Add super interfaces search when finding a PropertyEditor
30     *
31     * @param clazz The class of the property
32     * @return The PropertyEditor found, null otherwise
33     */

34    public static synchronized PropertyEditor JavaDoc findEditor(Class JavaDoc clazz)
35    {
36       PropertyEditor JavaDoc result = PropertyEditorManager.findEditor(clazz);
37
38       // If the PropertyEditor is not found in mapping, it is the first time it is required
39
// so fetch it by the PropertyEditorManager
40
if (result == null)
41       {
42          // Search among the interfaces if there is one that has a PropertyEditor
43
Class JavaDoc[] interfaces = clazz.getInterfaces();
44          for (int i = 0; i < interfaces.length; i++)
45          {
46             Class JavaDoc current = interfaces[i];
47             result = CustomPropertyEditorManager.findEditor(current);
48             if (result != null)
49             {
50                PropertyEditorManager.registerEditor(clazz, result.getClass());
51                break;
52             }
53          }
54       }
55
56       return result;
57    }
58
59    /** Add a new package for the search */
60    static
61    {
62       String JavaDoc[] paths = PropertyEditorManager.getEditorSearchPath();
63       Vector JavaDoc newPaths = new Vector JavaDoc();
64       newPaths.addAll(Arrays.asList(paths));
65       if (!newPaths.contains(EDITORS_PACKAGE))
66       {
67          newPaths.add(EDITORS_PACKAGE);
68       }
69       PropertyEditorManager.setEditorSearchPath((String JavaDoc[]) newPaths.toArray(new String JavaDoc[0]));
70    }
71 }
72
Popular Tags