001 /** 002 * CreateProfile.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-García. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 02/11/2007 008 */ 009 package jcolibri.method.gui.editors; 010 011 import java.io.File; 012 import java.util.Collection; 013 import java.util.Date; 014 import java.util.HashMap; 015 016 import jcolibri.datatypes.Instance; 017 import jcolibri.datatypes.Text; 018 019 /** 020 * Factory to obtain the ParameterEditor of a data type. 021 * 022 * @author Juan A. Recio-Garcia 023 * @version 1.0 024 * 025 */ 026 public class ParameterEditorFactory { 027 028 private static HashMap<Class, Class> table = new HashMap<Class, Class>(); 029 030 //Code to register editors. 031 //TODO search in classpath. 032 static{ 033 ParameterEditorFactory.registerEditor(Boolean.class, BooleanEditor.class); 034 ParameterEditorFactory.registerEditor(Date.class, DateEditor.class); 035 ParameterEditorFactory.registerEditor(Double.class, DoubleEditor.class); 036 ParameterEditorFactory.registerEditor(Enum.class, EnumEditor.class); 037 ParameterEditorFactory.registerEditor(File.class, FileEditor.class); 038 ParameterEditorFactory.registerEditor(Instance.class, InstanceEditor.class); 039 ParameterEditorFactory.registerEditor(String.class, StringEditor.class); 040 ParameterEditorFactory.registerEditor(Text.class, TextEditor.class); 041 ParameterEditorFactory.registerEditor(Integer.class, IntegerEditor.class); 042 } 043 044 /** 045 * Creates the editor and configures it with its data-type name 046 */ 047 public static ParameterEditor getEditor(Class<?> type) { 048 try 049 { 050 051 Class editor = table.get(type); 052 if(editor != null) 053 return (ParameterEditor)table.get(type).newInstance(); 054 055 for(Class<?> c : table.keySet()) 056 if(c.isAssignableFrom(type)) 057 editor = table.get(c); 058 059 if(editor.equals(EnumEditor.class)) 060 return new EnumEditor(type); 061 if(editor != null) 062 return (ParameterEditor)editor.newInstance(); 063 064 throw new Exception("No editor found for type: "+type.getName()); 065 066 } catch (Exception e) 067 { 068 org.apache.commons.logging.LogFactory.getLog(ParameterEditorFactory.class).error(e); 069 } 070 return null; 071 } 072 073 /** 074 * Creates the editor and configures it with its data-type name 075 */ 076 public static ParameterEditor getEditor(Class<?> type, Collection<Object> allowedValues) 077 { 078 ParameterEditor pe = getEditor(type); 079 if(pe==null) 080 return null; 081 pe.setAllowedValues(allowedValues); 082 return pe; 083 } 084 085 /** 086 * Registers and editor 087 * @param type that the editor manages 088 * @param editor of the type 089 */ 090 public static void registerEditor(Class type, Class editor) { 091 table.put(type, editor); 092 } 093 094 /** 095 * Unregisters the editor of a type. 096 * @param type of the editor to remove 097 */ 098 public static void unregisterEditor(Class type) { 099 table.remove(type); 100 } 101 102 /** 103 * Clears all the registered editors 104 */ 105 public static void clear() { 106 table.clear(); 107 } 108 }