
/** @(#) ArrayEditor.java 1.0 97/7/24
    * Copyright (c) 1997 Lawrence Rodrigues
    */
package vis.beans.editors;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;

  /** ArrayEditor Class
    * @version 1.0  24 July 1997
    * @author Lawrence Rodrigues
    * An indexed property editor class for Integer arrays.
    **/
  public class ArrayEditor extends PropertyEditorSupport{
     Object value;
     protected Object oldVal = null;
     String txtValue;
     int arrayLength;
     /** This method is initially called by the builder tool. It casts the
       * input object into an integer array. It converts this array
       * into a string an saves it in txtValue. It is essential that this
       * conversion done here. This is because when the builder tool calls the
       * setAsText method, txtValue will not be null.
       * @param val the value of the property.
       */
     public void setValue(Object val){
        if(!(val instanceof int[])) return;
        oldVal = value;
        value = (int[])val;
        arrayLength = getLength(val);
        StringBuffer stb = new StringBuffer();
        String str[] = convertToString(val);
        for(int i=0;i <arrayLength;i++){
           //insert a comma between the elements.
           if(i< arrayLength-1) stb.append(str[i]+ ",");
           else stb.append(str[i]); //not on the last one.
        }
        txtValue = stb.toString();
     }
     /** @return the actual integer array.
       */
     public Object getValue(){
        return  value;
     }
     /** This method is called by the builder tool whenever there is a change
       * in the text in the edit field. It converts the text value to an integer
       * array. It fires the property change event only when the array length equals
       * to the length of the property.
       */
     public void setAsText(String txt){
        txtValue = txt;
        if((value = convertInput(txt))!= null) {
            int len = getLength(value);
            if(len==  arrayLength) {
               setValue(value);
               firePropertyChange();
            }
         }
     }
     /** @return txtValue the text value of the integer array
       */
     public String getAsText(){
        return txtValue;
     }
     /** Converts the string to integer array.
       * @str the text value of the integer array.
       */
     protected Object convertInput(String str){
        StringTokenizer st = new StringTokenizer(str, ",");
        int tok = st.countTokens();
        if(value instanceof int[]){
           int intArr[] = new int[tok];
           for(int i =0; i< tok; i++){
              try{
                 String token = (String) st.nextElement();
                 int val = (Integer.valueOf(token)).intValue();
                 intArr[i] = val;
              } catch(NumberFormatException n){
               System.out.println("NumberFormat Exception");
               return null;
              }
           }
           return intArr;
        }
        if(value instanceof double[]){
           double doubleArr[] = new double[tok];
           for(int i =0; i< tok; i++){
              try{
                 String token = (String) st.nextElement();
                 double val = (Double.valueOf(token)).doubleValue();
                 doubleArr[i] = val;
              } catch(NumberFormatException n){
               System.out.println("NumberFormat Exception");
               return null;
              }
           }
           return doubleArr;
         }

         if(value instanceof float[]){
           float floatArr[] = new float[tok];
           for(int i =0; i< tok; i++){
              try{
                 String token = (String) st.nextElement();
                 float val = (Float.valueOf(token)).floatValue();
                 floatArr[i] = val;
              } catch(NumberFormatException n){
               System.out.println("NumberFormat Exception");
               return null;
              }
           }
           return floatArr;
         }

         if(value instanceof short[]){
           short shortArr[] = new short[tok];
           for(int i =0; i< tok; i++){
              try{
                 String token = (String) st.nextElement();
                 short val = (Short.valueOf(token)).shortValue();
                 shortArr[i] = val;
              } catch(NumberFormatException n){
               System.out.println("NumberFormat Exception");
               return null;
              }
           }
           return shortArr;
         }
         if(value instanceof long[]){
           long longArr[] = new long[tok];
           for(int i =0; i< tok; i++){
              try{
                 String token = (String) st.nextElement();
                 long val = (Long.valueOf(token)).longValue();
                 longArr[i] = val;
              } catch(NumberFormatException n){
               System.out.println("NumberFormat Exception");
               return null;
              }
           }
           return longArr;
         }
         return null;
     }

     public static int getLength(Object obj){
        if(obj == null) return 0;
        if(obj instanceof int[]) return ((int[])obj).length;
        if(obj instanceof double[]) return ((double[])obj).length;
        if(obj instanceof float[]) return ((float[])obj).length;
        if(obj instanceof short[]) return ((short[])obj).length;
        if(obj instanceof long[]) return ((long[])obj).length;
        if(obj instanceof String[]) return ((String[])obj).length;
        return 0 ;
     }

     public static String[] convertToString(Object obj){
      if(obj == null) return null;
      int len;
      String outStr[];
      if(obj instanceof int[]) {
         len = ((int[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Integer.toString(((int[])obj)[i]);
         }
         return outStr;
      }
      if(obj instanceof double[]) {
         len = ((double[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Double.toString(((double[])obj)[i]);
         }
         return outStr;
      }

      if(obj instanceof float[]) {
         len = ((float[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Float.toString(((float[])obj)[i]);
         }
         return outStr;
      }

      if(obj instanceof short[]) {
         len = ((short[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Short.toString(((short[])obj)[i]);
         }
         return outStr;
      }

      if(obj instanceof long[]) {
         len = ((long[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Long.toString(((long[])obj)[i]);
         }
         return outStr;
      }
      if(obj instanceof String[]) return (String[])obj;
      return null;
   }

  }
