// Item.java

package lab09;

import java.awt.*;

/** This class represents the type of each data element stored
 * in an ordered list or a binary search tree in these labs.
 * Each Item object contains a String value, together with
 * an occurrence count for that value.
 */
public class Item {
    private static final Color CELL_COLOR = new Color (0xF0, 0xF0, 0xF0);

    private String value;
    private int occurrences;
  
    /** Creates a new Item instance containing the specified
     * String value, and initializes its occurrence count to 1.
     */
    public Item (String value) {
        this.value = value;
        occurrences = 1;
    }
  
    /** Sets the String value of the object to the specified
     *  parameter value, and automatically resets the associated
     *  occurrence count to 1.
     * @param value The new value to be stored in this Item object.
     */
    public void setValue (String value) {
        this.value = value;
        this.occurrences = 1;
    }
  
    /** Returns the String value currently stored by this Item object.
     * @return The value currently stored in this object.
     */
    public String getValue () {
        return value;
    }
  
    /** Increments the occurrence counter for this <B>Item</B> object.
     */
    public void incOccurrences () {
        occurrences++;
    }
  	 
    /** Decrements the occurrence counter for this <B>Item</B> object.
     */
    public void decOccurrences () {
       occurrences--;
    }
  	 
    /** Returns the occurrence count for this <B>Item</B> object.
     * This values represents the number of times the <B>String</B>
     * value stored in this object has been inserted into an
     * ordered list or a binary search tree.
     */
    public int getOccurrences () {
        return occurrences;
    }
  	  
    /**
     * Renders the Item object using the specified graphics context.
     * The base parameter represents the midpoint of the left-hand
     * side of the Item rectangle, whose dimensions are specified
     * by the size parameter.
     * @param g Graphics context.
     * @param base (x,y) coordinate of midpoint of left-hand side
     * of Item rectangle.
     * @param size (width,height) of Item rectangle.
     */
    public void render (Graphics g, Point base, Dimension size) {
        int width = size.width;
        int height = size.height;

        int xloc = base.x;
        int yloc = base.y - height / 2;
        
        int x = xloc;
    
        Color oldColor = g.getColor ();
        g.setColor (CELL_COLOR);
        g.fillRect(x, yloc, 3 * width / 4, height);
        g.setColor (Color.black);
        g.drawRect(x, yloc, 3 * width / 4, height);
        g.drawString (value, x + 5, base.y + 5);

        x += 3 * width / 4;
    
        g.setColor (CELL_COLOR);
        g.fillRect (x, yloc, width / 4, height);
        g.setColor (Color.black);
        g.drawRect (x, yloc, width / 4, height);
        g.drawString ("" + occurrences, x + 5, base.y + 5);
        g.setColor (oldColor);
    }

    public String toString () {
        return value + " (" + occurrences + ")";
    }
}
