package jas2.hist;
import jas2.util.JASDialog;
import jas2.util.JASState;
import jas2.util.NestedRuntimeException;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;
import java.util.Vector;


/**
 *
 * A class which maintains a list of functions which can be overlayed
 *
 * on a plot.
 *
 */

public class FunctionRegistry
{
    private FunctionRegistry()
    {
    }
    
   /**
    * Get the (unique) instance of FunctionRegistry
    */
    static public FunctionRegistry instance()
    {
        return theFunctionRegistry;   
    }
    
   /**
    * Register a function given its class and name
    * @param c The class of the function
    * @param name The name of the function
    */
    
    public void registerFunction(Class c, String name)
    {
        registerFunction(createFunctionFactory(c,name));   
    }
    
   /**
    * Create a default function factory from a class and a name
    * @param c The class of the function
    * @param name The name of the function
    * @return The function factory
    */

    public FunctionFactory createFunctionFactory(Class c, String name)    
    {
        try   
        { 
            return new DefaultFunctionFactory(c,name);
        }
        catch (FunctionFactoryError e)
        {
            // convert the error to a runtime error   
            throw new NestedRuntimeException(e);
        }
    }
 
   /**
    * Add a function by specifing a FunctionFactory
    */   
    public void registerFunction(FunctionFactory f)
    {
        m_functions.addElement(f);   
    }
    
   /**
    * Remove a function factory from the list
    */
    public void removeFunctionFactory(FunctionFactory f)
    {
        m_functions.removeElement(f);   
    }
    
   /**
    * Clear the list
    */
    public void removeAllFunctions()
    {
        m_functions.removeAllElements();   
    }
 
   /**
    * Get an enumeration of all the FunctionFactories in the registry
    */
    
    public Enumeration elements()   
    {
        return m_functions.elements();   
    }
    
   /**
    * Get the size of the registry
    */
    public int size()
    {
        return m_functions.size();   
    }
    public FunctionFactory find(String name)
    {
        Enumeration e = m_functions.elements();
        while (e.hasMoreElements())
        {
            FunctionFactory ff = (FunctionFactory) e.nextElement();
            if (ff.getFunctionName().equals(name)) return ff;
        }
        return null;
    }
   /**
    * Replace the contents of the list
    * @param e An enumeration of function factories
    */
    
    public void setContents(Vector v)
    {
        m_functions = v;   
    }
    FunctionFactory chooseFunction(Frame f)
    {
        // These lines need to be excised in applet version
        ChooseFunctionDialog dlg = new ChooseFunctionDialog(f);
        if (dlg.doModal())
        {
            return dlg.getSelectedFunction();   
        }
        return null;
    }
    private Vector m_functions = new Vector();
    private static FunctionRegistry theFunctionRegistry = new FunctionRegistry();
    private class ChooseFunctionDialog extends JASDialog implements ItemListener, ActionListener
    
    {
        
        ChooseFunctionDialog(Frame f)
        
        {
            
            super(f,"Choose Function...");
            
            m_list = new javax.swing.JList(m_functions);
            
            m_list.setCellRenderer(new FunctionListCellRenderer());
            
            getContentPane().add("Center",m_list);
            
            pack();
            
        }
        
        public void itemStateChanged(ItemEvent evt)
        
        {
            
            callEnable();
            
        }
        
        public void actionPerformed(ActionEvent evt)
        
        {
            
            onOK();
            
        }
        
        public void enableOK(JASState state)
        
        {
            
            //state.setEnabled(m_list.getSelectedValue()!=null);
            
        }
        
        
        
        public void onOK()
        
        {
            
            m_selection = (FunctionFactory) m_list.getSelectedValue();
            
            super.onOK();
            
        }
        
        public FunctionFactory getSelectedFunction()
        
        {
            
            return m_selection;
            
        }
        
    }
    
    private FunctionFactory m_selection;
    
    private javax.swing.JList m_list;
    
}

 

Ads help maintain this website.