Source for java.beans.PropertyEditorManager

   1: /* java.beans.PropertyEditorManager
   2:    Copyright (C) 1998 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10:  
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.beans;
  40: 
  41: import gnu.java.beans.editors.ColorEditor;
  42: import gnu.java.beans.editors.FontEditor;
  43: import gnu.java.beans.editors.NativeBooleanEditor;
  44: import gnu.java.beans.editors.NativeByteEditor;
  45: import gnu.java.beans.editors.NativeDoubleEditor;
  46: import gnu.java.beans.editors.NativeFloatEditor;
  47: import gnu.java.beans.editors.NativeIntEditor;
  48: import gnu.java.beans.editors.NativeLongEditor;
  49: import gnu.java.beans.editors.NativeShortEditor;
  50: import gnu.java.beans.editors.StringEditor;
  51: import gnu.java.lang.ClassHelper;
  52: 
  53: import java.awt.Color;
  54: import java.awt.Font;
  55: 
  56: /**
  57:  * PropertyEditorManager is used to find property editors
  58:  * for various types (not necessarily Beans).<P>
  59:  *
  60:  * It first checks to see if the property editor is
  61:  * already registered; if it is, that property editor is
  62:  * used.  Next it takes the type's classname and appends
  63:  * "Editor" to it, and searches first in the class's
  64:  * package and then in the property editor search path.
  65:  *
  66:  * <p>Default property editors are provided for:</p>
  67:  * 
  68:  * <ol>
  69:  * <li>boolean, byte, short, int, long, float, and double</li>
  70:  * <li>java.lang.String</li>
  71:  * <li>java.awt.Color</li>
  72:  * <li>java.awt.Font</li>
  73:  * </ol>
  74:  *
  75:  * <p><strong>Spec Suggestion:</strong> Perhaps an editor for
  76:  * Filename or something like it should be provided.  As well
  77:  * as char.</p>
  78:  *
  79:  * @author John Keiser
  80:  * @since 1.1
  81:  * @version 1.1.0, 29 Jul 1998
  82:  */
  83: 
  84: public class PropertyEditorManager
  85: {
  86:   static java.util.Hashtable editors = new java.util.Hashtable();
  87:   static String[] editorSearchPath = { "gnu.java.beans.editors",
  88:                                        "sun.beans.editors" };
  89: 
  90:   static
  91:     {
  92:       registerEditor(Boolean.TYPE, NativeBooleanEditor.class);
  93:       registerEditor(Byte.TYPE,    NativeByteEditor.class);
  94:       registerEditor(Short.TYPE,   NativeShortEditor.class);
  95:       registerEditor(Integer.TYPE, NativeIntEditor.class);
  96:       registerEditor(Long.TYPE,    NativeLongEditor.class);
  97:       registerEditor(Float.TYPE,   NativeFloatEditor.class);
  98:       registerEditor(Double.TYPE,  NativeDoubleEditor.class);
  99:       registerEditor(String.class, StringEditor.class);
 100:       registerEditor(Color.class,  ColorEditor.class);
 101:       registerEditor(Font.class,   FontEditor.class);
 102:     }
 103: 
 104:   /**
 105:    * Beats me why this class can be instantiated, but there
 106:    * you have it.
 107:    */
 108:   public PropertyEditorManager()
 109:   {
 110:     // Do nothing here
 111:   }
 112: 
 113:   /**
 114:    * Register an editor for a class.  Replaces old editor
 115:    * if there was one registered before.
 116:    *
 117:    * @param editedClass the class that the property editor
 118:    *        will edit.
 119:    * @param editorClass the PropertyEditor class.
 120:    */
 121:   public static void registerEditor(Class editedClass, Class editorClass)
 122:   {
 123:     editors.put(editedClass, editorClass);
 124:   }
 125: 
 126:   /**
 127:    * Returns a new instance of the property editor for the
 128:    * specified class.
 129:    *
 130:    * @param editedClass the class that the property editor
 131:    *        will edit.
 132:    * @return a PropertyEditor instance that can edit the
 133:    *         specified class.
 134:    */
 135:   public static PropertyEditor findEditor(Class editedClass)
 136:   {
 137:     try
 138:       {
 139:         Class found = (Class)editors.get(editedClass);
 140:         if(found != null)
 141:           {
 142:             return (PropertyEditor)found.newInstance();
 143:           }
 144: 
 145:     ClassLoader contextClassLoader
 146:         = Thread.currentThread().getContextClassLoader();
 147: 
 148:         try
 149:           {
 150:             found = Class.forName(editedClass.getName()+"Editor", true,
 151:                   contextClassLoader);
 152:             registerEditor(editedClass,found);
 153:             return (PropertyEditor)found.newInstance();
 154:           }
 155:         catch(ClassNotFoundException E)
 156:           {
 157:           }
 158: 
 159:         String appendName
 160:         = "."
 161:         + ClassHelper.getTruncatedClassName(editedClass)
 162:         + "Editor";
 163:         synchronized(editorSearchPath)
 164:           {
 165:             for(int i=0;i<editorSearchPath.length;i++)
 166:               {
 167:                 try
 168:                   {
 169:                     found = Class.forName(editorSearchPath[i] + appendName,
 170:                       true, contextClassLoader);
 171:                     registerEditor(editedClass,found);
 172:                     return (PropertyEditor)found.newInstance();
 173:                   }
 174:                 catch(ClassNotFoundException E)
 175:                   {
 176:                   }
 177:               }
 178:           }
 179:       }
 180:     catch(InstantiationException E)
 181:       {
 182:       }
 183:     catch(IllegalAccessException E)
 184:       {
 185:       }
 186:     
 187:     return null;
 188:   }
 189: 
 190:   /**
 191:    * Get the editor search path.
 192:    * As a minor departure from the spec, the default value
 193:    * for the editor search path is "gnu.java.beans.editors",
 194:    * "sun.beans.editors".
 195:    *
 196:    * @return the editor search path.
 197:    */
 198:   public static String[] getEditorSearchPath()
 199:   {
 200:     return editorSearchPath;
 201:   }
 202: 
 203:   /**
 204:    * Set the editor search path.
 205:    *
 206:    * @param editorSearchPath the new value for the editor search path.
 207:    */
 208:   public static void setEditorSearchPath(String[] editorSearchPath)
 209:   {
 210:     synchronized(editorSearchPath)
 211:       {
 212:         PropertyEditorManager.editorSearchPath = editorSearchPath;
 213:       }
 214:   }
 215: }