ConfigComponent.java

/*
 * $RCSfile: ConfigComponent.java,v $
 * $Id: Main.java,v 1.6 1998/11/30 02:21:07 devnull Exp $
 * by Lee Wilson, http://www.ad1440.net/~devnull
 * Development started on 1998 10 01
 * (c) Devnull Software, LLC. (http://www.devnullsoftware.com)
 */

package com.devnullsoftware.config;

/**
 * Comfigurable component information and utility routines.
 *
 * @version 1.0 $Date$
 * @author Lee Wilson (devnull@ad1440.net)
 */

import java.awt.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.swing.*;


public class ConfigComponent implements Serializable
{
//-------------------------------------------------------------------
// Global Constants
//
  public static final int CHECKBOX = 1;
  public static final int CHECKBOX2 = 2;
  public static final int CHOICE = 3;
  public static final int CHOICE2 = 4;
  public static final int TEXTFIELD = 5;
  public static final int FILEFIELD = 6;
  public static final int TEXTAREA = 7;
  public static final int NUM_ITEMS = 7;
  public static final String [] TypeLabels = {
    "None",
    "Checkbox",
    "Checkbox2",
    "Choice",
    "Choice2",
    "TextField",
    "File TextField",
    "TextArea"
  };
  public static final int BOOLEAN = 1;
  public static final int STRING = 2;
  public static final int COLOR = 3;
  public static final int INTEGER = 4;
  public static final int FLOAT = 5;
  public static final int TIME = 6;
  public static final int LABELVALUE = 7;
  public static final int NUM_DATATYPES = 7;
  public static final String [] DataTypeLabels = {
    "none",
    "boolean",
    "String",
    "Color",
    "int",
    "float",
    "Date",
    "LabelValue"
  };

  //-------------------------------------------------------------------
  // Instance Variables
  //
  private String sName;
  private int iComponentType;
  private Object pAWTItem;
  private int iDataType;
  private Class pCl;
  private String sVarName;
  private Vector pArrayNum;


  //-------------------------------------------------------------------
  // Constructors
  //
  public ConfigComponent ()
    throws IllegalArgumentException
  {
    init ( false, null, 0, null, 0, null, null, null);
  }

  public ConfigComponent
    (String sName,
     int iComponentType,
     Object pAWTItem,
     int iDataType,
     Class pCl,
     String sVarName,
     Vector pArrayNum)
    throws IllegalArgumentException
  {
    init ( true, sName, iComponentType, pAWTItem, iDataType, pCl, sVarName, pArrayNum);
  }

  private void init (boolean bCheckValues,
       String sName,
       int iComponentType,
       Object pAWTItem,
       int iDataType,
       Class pCl,
       String sVarName,
       Vector pArrayNum)
    throws IllegalArgumentException
  {
    if (bCheckValues) 
    {
      if (sName == null)
      {
        throw (new IllegalArgumentException ("name cannot be null"));
      }
      if (iDataType < 1 || iDataType > NUM_DATATYPES)
      {
        throw (new IllegalArgumentException
               ("DataType must be 1<=n<="+NUM_DATATYPES));
      }
      if (pAWTItem == null || pCl == null || sVarName == null)
      {
        throw (new IllegalArgumentException
               ("awtItem, class, and varName must be non-null"));
      }
    }
    this.sName = sName;
    this.iComponentType = iComponentType;
    this.pAWTItem = pAWTItem;
    this.iDataType = iDataType;
    this.pCl = pCl;
    this.sVarName = sVarName;
    this.pArrayNum = pArrayNum;
  }



  //-------------------------------------------------------------------
  // Accessors & Mutators
  //
  public String toString () 
  {
    return (sName);
  }

  public String getName () {
    return sName;
  }

  public int getComponentType () {
    return iComponentType;
  }

  public Object getAWTItem () {
    return pAWTItem;
  }

  public int getDataType () {
    return iDataType;
  }

  public Class getCl () {
    return pCl;
  }

  public String getVarName () {
    return sVarName;
  }

  public Vector getArrayNum () {
    return pArrayNum;
  }


  //-------------------------------------------------------------------
  // Security
  //


  //-------------------------------------------------------------------
  // Methods
  //
  public static Color stringToColor (String sColor)
  {
    if ("black".equals(sColor))
    {
      return Color.black;
    }
    else if ("blue".equals(sColor))
    {
      return Color.blue;
    }
    else if ("cyan".equals(sColor))
    {
      return Color.cyan;
    }
    else if ("darkGray".equals(sColor)) 
    {
      return Color.darkGray;
    }
    else if ("gray".equals(sColor)) 
    {
      return Color.gray;
    }
    else if ("green".equals(sColor))
    {
      return Color.green;
    }
    else if ("lightGray".equals(sColor)) 
    {
      return Color.lightGray;
    }
    else if ("magenta".equals(sColor)) 
    {
      return Color.magenta;
    }
    else if ("orange".equals(sColor))
    {
      return Color.orange;
    }
    else if ("pink".equals(sColor)) 
    {
      return Color.pink;
    }
    else if ("red".equals(sColor)) 
    {
      return Color.red;
    }
    else if ("white".equals(sColor))
    {
      return Color.white;
    }
    else if ("yellow".equals(sColor))
    {
      return Color.yellow;
    }
    else 
    {
      return Color.black;
    }
  }


  public static String colorToString (Color cColor)
  {
    int iColor = cColor.getRGB();

    if (iColor == Color.black.getRGB())
    {
      return ("black");
    }
    else if (iColor == Color.blue.getRGB())
    {
      return ("blue");
    }
    else if (iColor == Color.cyan.getRGB())
    {
      return ("cyan");
    }
    else if (iColor == Color.darkGray.getRGB()) 
    {
      return ("darkGray");
    }
    else if (iColor == Color.gray.getRGB())
    {
      return ("gray");
    }
    else if (iColor == Color.green.getRGB())
    {
      return ("green");
    }
    else if (iColor == Color.lightGray.getRGB())
    {
      return ("lightGray");
    }
    else if (iColor == Color.magenta.getRGB())
    {
      return ("magenta");
    }
    else if (iColor == Color.orange.getRGB())
    {
      return ("orange");
    }
    else if (iColor == Color.pink.getRGB())
    {
      return ("pink");
    }
    else if (iColor == Color.red.getRGB())
    {
      return ("red");
    }
    else if (iColor == Color.white.getRGB())
    {
      return ("white");
    }
    else if (iColor == Color.yellow.getRGB())
    {
      return ("yellow");
    }
    else 
    {
      return ("unknown");
    }
  }

  public static String timeToString  (long iRealTime)
  {
    String sTime;
    long iDivider = 1L;
    String sSuffix = "";

    if (iRealTime >= 2592000000L) 
    {
      iDivider = 2592000000L;
      sSuffix = "M";
    }
    else if (iRealTime >= 86400000L) 
    {
      iDivider = 86400000L;
      sSuffix = "d";
    }
    else if (iRealTime >= 3600000L)
    {
      iDivider = 3600000L;
      sSuffix = "h";
    }
    else if (iRealTime >= 60000L) 
    {
      iDivider = 60000L;
      sSuffix = "m";
    }
    else if (iRealTime >= 1000L) 
    {
      iDivider = 1000L;
      sSuffix = "s";
    }
    sTime = String.valueOf(iRealTime / iDivider) + sSuffix;

    return sTime;
  }


  public static long stringToTime (String sTime)
    throws Exception
  {
    long iRealTime = 0L;

    sTime.trim();
    if (sTime.endsWith("M")) 
    {        // Months
      iRealTime = Long.parseLong(sTime.substring(0,sTime.indexOf('M')))
        * 1000 * 60 * 60 * 24 * 30;
    }
    else if (sTime.endsWith("d")) 
    {        // days
      iRealTime = Long.parseLong(sTime.substring(0,sTime.indexOf('d')))
        * 1000 * 60 * 60 * 24;
    }
    else if (sTime.endsWith("h")) 
    {   // hours
      iRealTime = Long.parseLong(sTime.substring(0,sTime.indexOf('h')))
        * 1000 * 60 * 60;
    }
    else if (sTime.endsWith("m")) 
    {   // minutes
      iRealTime = Long.parseLong(sTime.substring(0,sTime.indexOf('m')))
        * 1000 * 60;
    }
    else if (sTime.endsWith("s")) 
    {   // seconds
      iRealTime = Long.parseLong(sTime.substring(0,sTime.indexOf('s')))
        * 1000;
    }
    else 
    {
      iRealTime = Long.parseLong(sTime);
    }
    
    return iRealTime;
  }

}