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