/*
 * Fea3DFactory.java
 *
 * Created on November 9, 2000, 11:13 PM
 */

package edu.ou.eml.fea3d;

/**
 *
 * @author  Qiuli Sun
 * @version 1.0
 */

//standard import
import javax.swing.*;
import java.net.*;
import javax.vecmath.*;
import java.util.*;

public final class Fea3DFactory extends Object {

    /** Creates new Fea3DFactory */
    public Fea3DFactory() {
    }

    //******************** create button function
    static public  JButton createButton(URL iconURL, String tipText )
    {
        ImageIcon image = new ImageIcon(iconURL);
        JButton but = new JButton(image);
        //but.setMargin(pInserts);
        //but.addActionListener(this);
        but.setToolTipText(tipText);
        return but;
    }

    //**************************************
    static public URL getURL(Fea3D owner, String filename)
    {
        //Fea3D is an extension of applets
        URL codeBase = owner.getCodeBase();
        URL  url = null;
        try {
            url = new URL(codeBase, filename);
        }
        catch(MalformedURLException e){
            System.out.println("Bad URL...");
            return null;
        }
        return url;
    }

    //**************************************
    static  public URL getURL3DData(Fea3D owner, String filename)
    {
        //Fea3D is an extension of applets
        URL codeBase = owner.getCodeBase();
        String host = codeBase.getHost();
        System.out.println("host="+codeBase);
        URL  url = null;
        try {
            url = new URL(codeBase, filename);
        }
        catch(MalformedURLException e){
            System.out.println("Bad URL in reading 3D Data ...");
            return null;
        }
        return url;
    }


    //******************************
    public static void displayArr(String arrName, int[] arr)
    {
        for(int i = 0 ; i<arr.length; i++)
        {
            System.out.print (arrName+"["+i+"]= "+arr[i] + "  ");
            if ( (i+1) % 4  == 0)
            {
                System.out.println ("");
            }
        }
    }
    
   //******************************
    public static void displayArr(String arrName, float[] arr)
    {
        for(int i = 0 ; i<arr.length; i++)
        {
            System.out.print(arrName+"["+i+"]= "+arr[i]+"  ");
            if ( (i+1) % 3 == 0)
            {
                 System.out.println("");
            }
        }
    }
    
    //******************************
    public static void displayArr(String arrName, Point3f[] arr)
    {
        for(int i = 0 ; i<arr.length; i++)
        {
            System.out.println(arrName+"["+i+"]= "+arr[i].x+"  "+arr[i].y+"  "+arr[i].z);
        }
    }
    
    //Search an array for the index of the key value
    public static int search(int[] arr, int theKey)
    {
        for(int i = 0 ; i < arr.length; i++)
        {
            if ( arr[i] == theKey)
            {
                return i;
            }
        }
        return -1;
    }

    public static float max(float[] unsortedArr)
    {
        float[] sortedArr = new float[unsortedArr.length];
        for( int i = 0; i < unsortedArr.length; i++)
        {
            sortedArr[i] = unsortedArr[i];
        }
        Arrays.sort(sortedArr);
        return sortedArr[unsortedArr.length - 1];
    }
    
    public static float min(float[] unsortedArr)
    {
        float[] sortedArr = new float[unsortedArr.length];
        for( int i = 0; i < unsortedArr.length; i++)
        {
            sortedArr[i] = unsortedArr[i];
        }
        Arrays.sort(sortedArr);
        return sortedArr[0];
    }
    
    public static void displayWarningMessage()
    {
          JOptionPane.showMessageDialog(
            DataContainer.baseFrame,
             "Illegal input value. Please check!",
             "Input Warning Message",
            JOptionPane.WARNING_MESSAGE);
    }
}
