/*
 * @(#) Enum.java 1.0 97/7/9
 * Copyright (c) 1996 Lawrence Rodrigues
 */
package vis.util;

import java.io.*;
import java.util.*;

import java.awt.*;
import java.lang.reflect.*;
import java.awt.event.*;
import java.beans.*;

 /** The class implements the enumerated type.
   * @version 1.0  7 July 1997
   * @author Lawrence Rodrigues
   **/
  public class Enum implements Serializable{
     private String[] members;
     private int[] intValues;
     private String currentMember;
     private int currentValue;

     public Enum(){
  
     }

     /**
       * Contructor when the elements are strings.
       * @param elements the elements of the enumeration.
       */
     public Enum(String[] elements){
        if(elements != null){
           members = elements;
           intValues = new int[members.length];
           for(int i=0;i<members.length;i++){
               intValues[i] =i;
           }
        currentMember = members[0];
        currentValue = 0;
        }
     }

     /**
       * Constructor when the enumerated elements are defined as constants
       * in a class of interface.
       * @param cl the meta class for the class in
       *  which the enumeration members are defined.
       */
     public Enum(Class cl){
        try{
        Field fl[] = cl.getFields();
        int len = fl.length;
        for(int i=0;i<len;i++){
            members[i] = fl[i].getName();
            intValues[i] =fl[i].getInt(null);
        }
        currentMember = members[0];
        currentValue= fl[0].getInt(null);
        }catch(Exception e){ }
     }

     /**
       * If the input is a valid value, sets the current value and member name.
       * @param value the current value.
       */
     public void setCurrentValue(int value){
        String member = getMember(value);
        if(member !=null){
           currentValue = value;
           currentMember = member;
        }
     }

     /**
       * @return the currently selected value of the enumeration.
       */
     public int getCurrentValue(){
        return currentValue;
     }

     /**
       * If the input is a valid member name, sets the current value & member name.
       * @param member the member name.
       */
     public void setCurrentMember(String member){
        int val = getValue(member); 
        if(val>=0){
           currentMember = member;
           currentValue = val;
         }
     }

     /**
       * @return the current member of the enumeration.
       */
     public String getCurrentMember(){
        return currentMember;
     }


     /**
       *  @param  member the name of the member.
       *  @return the actual value of that member.
       */
     public int getValue(String member){
        if(member == null) return -1;
         for(int i=0;i<members.length;i++){
             if(members[i].equals(member))
             return intValues[i];
          }
          return -1;
     }

     /**
       *  @param  value the actual value of the member.
       *  @returns the member name.
       */
     public String getMember(int value){
        if(value <0) return null;
         for(int i=0;i<intValues.length;i++){
             if(intValues[i] == value)
             return members[i];
          }
          return null;
     }


     /**
       *  @param  member the member name.
       *  @return true if the value of the member matches the current value.
       */
     public boolean equals(String member){
        if(currentValue == getValue(member)) return true;
        return false;
     }

     /**
       *  @return the members of the enumerated type.
       */
     public String[] getMembers(){
        return members;
     }
  }

