package jas2.hist;

import jas2.util.JASIcon;
import jas2.util.ObjectFactory;
import jas2.util.ObjectFactoryException;

import javax.swing.Icon;

public class DefaultFunctionFactory extends ObjectFactory implements FunctionFactory
{
    
    public DefaultFunctionFactory(Class c, String name) throws FunctionFactoryError
    
    {
        
        super(c);
        
        this.name = name;
        
        
        
        // Class must be a subclass of Basic1DFunction
        
        
        
        if (!inheritsFrom(Basic1DFunction.class))
            
            throw new FunctionFactoryError("Function "+name+" does not inherit from Basic1DFunction");
        
        
        
        // Class must be declared public
        
        
        
        if (!checkAccess())
            
            throw new FunctionFactoryError("Function "+name+" is not declared public");
        
        
        
        // The function needs to have a suitable constructor
        
        
        
        if (!canBeCreatedFrom(Double.TYPE,Double.TYPE,Double.TYPE,Double.TYPE))
            
            throw new FunctionFactoryError("Function "+name+" does not have a suitable constructor");
        
    }
    
    public Basic1DFunction createFunction(JASHist h) throws FunctionFactoryError
    
    {
        
        try
        
        {
            
            JASHistAxis xAxis = h.getXAxis();
            
            JASHistAxis yAxis = h.getYAxis(); // which Y axis?
            
            return (Basic1DFunction) create(new Double(xAxis.getMin()),
            
            new Double(xAxis.getMax()),
            
            new Double(yAxis.getMin()),
            
            new Double(yAxis.getMax()));
            
        }
        
        catch (ObjectFactoryException e)
        
        {
            
            throw new FunctionFactoryError("Unexpected failure to create function");
            
        }
        
    }
    
    public String getFunctionName()
    
    {
        
        return name;
        
    }
    
    public Icon getFunctionIcon()
    
    {
        
        return icon;
        
    }
    
    private String name;
    
    private Icon icon = JASIcon.create(this,"function.gif");
    
}
 

Ads help maintain this website.