// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)ValueWidget.java, 1.12, 11/17/96





package marimba.gui;





import java.awt.*;


import marimba.persist.*;





/**


 * The ValueWidget class, a general class for widgets


 * that represent a range of values.


 *


 * @author	Klaas Waslander


 * @version 	1.12, 11/17/96


 */


public abstract class ValueWidget extends Widget {


    /**


     * The current value of the valueWidget.


     * @see #getValue


     * @see #getIntegerValue


     * @see #setValue


     * @see #setParam


     */


    public int  value;





    /**


     * The lowest possible value.


     * @see #getMinValue


     * @see #setMinValue


     * @see #getRange


     * @see #setRange


     * @see #setParam


     */


    public int  minValue;





    /**


     * The highest possible value.


     * @see #getMaxValue


     * @see #setMaxValue


     * @see #getRange


     * @see #setRange


     * @see #setParam


     */


    public int  maxValue = 100;








    /**


     * Constructor.


     */


    public ValueWidget() {


    }





    /**


     * Get the properties.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setInteger("value", value, 0);


	list.setInteger("min", minValue, 0);


	list.setInteger("max", maxValue, 100);


    }





    /**


     * Set the properties.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	setRange(list.getInteger("min", 0), list.getInteger("max", 100));


	setValue(list.getInteger("value", 0));


    }





    /**


     * Return the current value in string representation


     * as the text of value widgets.


     */


    public String getText() {


	return String.valueOf(value);


    }





    /**


     * Get the integer value of the ValueWidget.


     * @see #value


     */


    public int getIntegerValue() {


	return value;


    }





    /**


     * Get the value of the ValueWidget.


     * @see #value


     */


    public Object getValue() {


	return new Integer(getIntegerValue());


    }





    /**


     * Get the minimum value of the ValueWidget.


     * @see #minValue


     */


    public int getMinValue() {


	return minValue;


    }





    /**


     * Set the minimum value.


     * @see #minValue


     */


    public void setMinValue(int minValue) {


	setRange(minValue, this.maxValue);


    }





    /**


     * Get the maximum value of the ValueWidget.


     * @see #maxValue


     */


    public int getMaxValue() {


	return maxValue;


    }





    /**


     * Set the maximum value.


     * @see #maxValue


     */


    public void setMaxValue(int maxValue) {


	setRange(this.minValue, maxValue);


    }





    /**


     * Set the value of the ValueWidget. The value is restricted


     * by the min and max value.


     * @see #value


     */


    public void setValue(Object value) {


	if (value instanceof Number) {


	    setValue(((Number)value).intValue());


	} else if (value instanceof String) {


	    try {


		setValue(Integer.parseInt((String)value));


	    } catch (NumberFormatException e) {


	    }


	}


    }





    /**


     * Set the value of the ValueWidget. The value is restricted


     * by the min and max value.


     * @see #value


     */


    public synchronized void setValue(int value) {


	setParam(value, this.minValue, this.maxValue);


    }





    /**


     * Get the range of the ValueWidget.


     * @see #minValue


     * @see #maxValue


     */


    public int getRange() {


	return maxValue - minValue;


    }





    /**


     * Set the range of the ValueWidget.


     * The value is adjusted if necessary.


     * @see #minValue


     * @see #maxValue


     */


    public synchronized void setRange(int minValue, int maxValue) {


	setParam(this.value, minValue, maxValue);


    }





    /**


     * Set all parameters of the ValueWidget at once.


     * @see #value


     * @see #minValue


     * @see #maxValue


     */


    public synchronized void setParam(int value, int minValue, int maxValue) {


	if (maxValue < minValue) {


	    maxValue = minValue;


	}


	if (value < minValue) {


	    value = minValue;


	} else if (value > maxValue) {


	    value = maxValue;


	}


	boolean  newLook = repaintFor(value, minValue, maxValue);


	this.value = value;


	this.minValue = minValue;


	this.maxValue = maxValue;


	if (newLook) {


	    repaint();


	}


    }





    /**


     * Checks whether the given value and range would require a repaint


     * when they would become the new value and range.


     * This method adjusts the given values to make sure they are valid.


     * It does not assume the given values are valid.


     */


    public boolean repaint(int newValue, int newMin, int newMax) {


	boolean  result = false;





	if (newMax < newMin) {


	    newMax = newMin;


	}


	if (newValue < newMin) {


	    newValue = newMin;


	} else if (newValue > newMax) {


	    newValue = newMax;


	}





	if (repaintFor(newValue, newMin, newMax)) {


	    result = true;


	}


	return result;


    }





    /**


     * Assumes the given values are valid. Checks whether these values


     * would result in a new look that requires a repaint.


     * The number of repaints can easily be optimized in sub classes


     * by overriding this method.


     */


    protected boolean repaintFor(int newValue, int newMin, int newMax) {


	boolean  result = false;


	if (newValue != this.value || newMin != minValue || newMax != maxValue) {


	    result = true;


	}


	return result;


    }





    /**


     * Debugging.


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	buf.append(",val=");


	buf.append(value);


	buf.append(",min=");


	buf.append(minValue);


	buf.append(",max=");


	buf.append(maxValue);


    }


}


