JComboBox2.java

/*
 * $RCSfile: JComboBox2.java,v $
 * $Id: JComboBox2.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;

import javax.swing.*;
import java.util.*;

/**
  A JComboBox that can handle Point-based parameters 

  @author Lee Wilson
  @version 1.0
 */

public class JComboBox2 extends JComboBox  {
  //-------------------------------------------------------------------
  // Global Constants
  //
  public static final int NO_ASSIGNED_VALUE = Integer.MAX_VALUE;


  //-------------------------------------------------------------------
  // Data
  //
  Vector pValues;
  Vector pPointValues;
  int iPreviousIndex = -1;
  


  //-------------------------------------------------------------------
  // Constructors
  //
  JComboBox2 ()  {
    super ();
    init();
  }

  private void init ()  {
    pValues = new Vector();
    pPointValues = new Vector();
  }


  //-------------------------------------------------------------------
  // Accessors & Mutators
  //
  public void addItem(Object anObject, int iValue)  {
    super.addItem(anObject);
    pValues.add(new Integer(iValue));
    pPointValues.add(new Integer(NO_ASSIGNED_VALUE));
  }

  public void addItem(Object anObject, int iValue, int iPointValue)  {
    super.addItem(anObject);
    pValues.add(new Integer(iValue));
    pPointValues.add(new Integer(iPointValue));
  }

  public int getValueAt (int index)  {
    return ((Integer)pValues.elementAt(index)).intValue();
  }

  public int getPointValueAt (int index)  {
    return ((Integer)pPointValues.elementAt(index)).intValue();
  }

  public int getPrevSelectedIndex ()  {
    return iPreviousIndex;
  }

  public void setPrevSelectedIndex (int iIndex)  {
    iPreviousIndex = iIndex;
  }

  public int getSelectedValue ()  {
    int iIndex = getSelectedIndex();
    return ((Integer)pValues.elementAt(iIndex)).intValue();
  }

  public int getSelectedPointValue ()  {
    int iIndex = getSelectedIndex();
    return ((Integer)pPointValues.elementAt(iIndex)).intValue();
  }

  public void insertItemAt (Object anObject, int iValue, int index)  {
    super.insertItemAt (anObject, index);
    pValues.insertElementAt (new Integer(iValue), index);
    pPointValues.insertElementAt (new Integer(NO_ASSIGNED_VALUE), index);
  }

  public void insertItemAt (Object anObject,
			    int iValue,
			    int iPointValue,
			    int index)  {
    super.insertItemAt (anObject, index);
    pValues.insertElementAt (new Integer(iValue), index);
    pPointValues.insertElementAt (new Integer(iPointValue), index);
  }

  public void removeAllItems ()  {
    super.removeAllItems();
    pValues.removeAllElements();
    pPointValues.removeAllElements();
  }   

  public void removeItem (Object anObject)  {
    Object pObj = null;
    int iItemCount = getItemCount();
    int i, iIndex = -1;
    for (i = 0; i < iItemCount && iIndex < 0; i++)  {
      pObj = getItemAt(i);
      if (pObj == anObject)  {
	iIndex = i;
      }
    }
    super.removeItem(anObject);
    pValues.removeElementAt(iIndex);
    pPointValues.removeElementAt(iIndex);
  }

  public void removeItemAt (int anIndex)  {
    super.removeItemAt(anIndex);
    pValues.removeElementAt(anIndex);
    pPointValues.removeElementAt(anIndex);
  }


  //-------------------------------------------------------------------
  // Methods
  //

}