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


// @(#)CheckBoxWidget.java, 1.34, 12/09/96





package marimba.gui;





import java.awt.*;





import marimba.persist.*;





/**


 * A checkbox widget. Checkboxes support options that are


 * either on or off; When the choice is set, a check mark


 * appears in the box. When the choice is not set, the


 * check box is empty.


 * Grouping checkboxes does not prevent setting the check


 * boxes on or off in combination; each check box's setting


 * is typically independent of the others.


 *


 * @author	Klaas Waslander


 * @version 	1.34, 12/09/96


 */


public class CheckBoxWidget extends ButtonWidget {


    /**


     * The current alignment.


     * @see #alignOptions


     */


    public int align = LEFT;





    /**


     * The available options for the alignment.


     * @see #align


     * @see #getAlignOptions()


     */


    public static Options alignOptions = new Options();


    static {


	alignOptions.add("left", LEFT);


	alignOptions.add("right", RIGHT);


    }





    /**


     * Default constructor.


     */


    public CheckBoxWidget() {


	label = "CheckBox";


    }


    


    /**


     * Constructor with label


     */


    public CheckBoxWidget(String label, boolean value) {


	this.label = label;


	this.value = value;


    }





    /**


     * Get the properties of the checkbox.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setOption("align", alignOptions, align, LEFT);


    }





    /**


     * Set the properties of the checkbox.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	align = list.getOption("align", alignOptions, LEFT);


    }





    /**


     * Get the possible align options.


     * @see #alignOptions


     */


    public Options getAlignOptions() {


	return alignOptions;


    }





    /**


     * Get the editor.


     */


    public String getEditor() {


	return "marimba.builder.CheckBoxWidgetEditor";


    }





    /**


     * Get the align of this button (LEFT or RIGHT).


     * @see #align


     */


    public int getAlign() {


	return align;


    }





    /**


     * Set the mode for the button.


     * @see #align


     */


    public void setAlign(int align) {


	if (this.align != align) {


	    this.align = align;


	    repaint();


	}


    }





    /**


     * Paint the label and also the input focus rectangle, if there is a label.


     */


    public void paintLabel(Graphics g) {


	if (editor == null && label != null) {


	    FontMetrics fm = g.getFontMetrics();


	    int siz = Math.max(12, Math.min(20, fm.getAscent()));


	    int lx = (align == RIGHT) ? width-(siz+5) : 5;


	    int ly = (height - fm.getHeight())/2 + fm.getAscent() - (siz-2);





	    switch (align) {


	      case RIGHT:


		lx -= fm.stringWidth(label) + 10;


		break;





	      default:


		lx += siz + 10;


		break;


	    }


	    ly += siz-2;





	    if (disabled) {


		g.setColor(Bevel.getBrighter(parent.background, true));


		g.drawString(label, lx+1, ly+1);


		g.setColor(Bevel.getDarker(parent.background, true));


		g.drawString(label, lx, ly);


	    } else {


		g.setColor(foreground);


		g.drawString(label, lx, ly);


		if (hasFocus() && label.length() > 0) {


		    g.setColor(getFocusColor());


		    int  height = fm.getHeight();


		    Dash.drawFocusRect(g, lx-1, (this.height-height)/2 - 1, fm.stringWidth(label)+2, height+2);


		}


	    }


	}


    }





    /**


     * Paint the checkbox and also the focus rectangle if there is no label.


     */


    public void paintButton(Graphics g) {


	FontMetrics fm = g.getFontMetrics();


	int siz = Math.max(13, Math.min(20, fm.getAscent()));


	int lx = (align == RIGHT) ? width-(siz+5) : 5;


	int ly = (height - fm.getHeight())/2 + fm.getAscent() - (siz-2);





	g.setColor(parent.background);


	if (disabled) {


	    Bevel.drawFieldBorder(g, lx, ly, siz, siz);


	} else {


	    Bevel.fillFieldBorder(g, lx, ly, siz, siz, down ? background : hilite);


	}


	


	if (value) {


	    // calculate coordinates of the bottom line


	    int  x1 = lx + 3;


	    int  x3 = lx + siz - 4;


	    int  x2 = x1 + ((x3-x1) / 3);





	    int  y2 = ly + siz - 4;


	    int  y1 = y2 - (x2-x1);


	    int  y3 = y2 - (x3-x2);





	    // draw the check mark using the coordinates above


	    g.setColor(foreground);


	    int nrOfLines = (siz-1)/4;


	    for (int index=0; index < nrOfLines; index++) {


		g.drawLine(x1, y1-index, x2, y2-index);


		g.drawLine(x2, y2-index, x3, y3-index);


	    }


	}


	if (hasFocus() && label.length() == 0) {


	    g.setColor(getFocusColor());


	    Dash.drawFocusRect(g, lx - 1, ly - 1, siz + 2, siz + 2);


	}


    }   





    /**


     * Debugging.


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	buf.append(",align=");


	buf.append(alignOptions.get(align));


    }


}


