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


// @(#)GroupBoxWidget.java, 1.12, 09/29/96





package marimba.gui;





import java.awt.*;





import marimba.persist.*;





/**


 * This group has an outline and potentially


 * a label.


 *


 * @author	Arthur van Hoff


 * @version 	1.12, 09/29/96


 */


public class GroupBoxWidget extends ContainerWidget {


    /**


     * The possible options for the alignment.


     * @see #getAlignOptions


     * @see #align


     */


    public static Options  alignOptions = new Options();


    static {


	alignOptions.add("left", LEFT);


	alignOptions.add("right", RIGHT);


	alignOptions.add("center", CENTER);


    }





    /**


     * The label of this group box, that appears at the top.


     * @see #getText


     * @see #setText


     */


    public String label;





    /**


     * The current alignment of the label.


     * It can be left, right or center.


     * @see #getAlign


     * @see #setAlign


     * @see #alignOptions


     */


    public int align;





    /**


     * The font of the label.


     * @see #getLabelFont


     * @see #setLabelFont


     * @see #label


     */


    public Font labelFont;





    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


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


	list.setString("label", label, null);


	list.setFont("labelfont", labelFont, null);


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


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


	label = list.getString("label", null);


	labelFont = list.getFont("labelfont", null);


    }





    /**


     * Get the possible options for the alignment.


     * @see #alignOptions


     */


    public Options getAlignOptions() {


	return alignOptions;


    }





    /**


     * Get the current alignment of the label.


     * @see #align


     */


    public int getAlign() {


	return align;


    }





    /**


     * Set the alignment of the label.


     * @see #align


     */


    public void setAlign(int align) {


	if (align != this.align) {


	    this.align = align;


	    repaint();


	}


    }





    /**


     * Allocate the content.


     */


    public Widget newContent() {


	GroupWidget w = new GroupWidget();


	w.lineMode = NONE;


	return w;


    }





    /**


     * Get the label.


     * @see #label


     * @see #getLabel


     */


    public String getText() {


	return label;


    }





    /**


     * Set the label.


     * @see #label


     * @see #setLabel


     */


    public void setText(String label) {


	if ((label != null) && !label.equals(this.label)) {


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


		this.label = null;


	    } else {


		this.label = label;


	    }


	    invalidate();


	    repaint();


	}


    }





    /**


     * Get the label.


     * @see #getText


     */


    public String getLabel() {


	return getText();


    }





    /**


     * Set the label.


     * @see #setText


     */


    public void setLabel(String label) {


	setText(label);


    }





    /**


     * Get the font of the label.


     * @see #labelFont


     */


    public Font getLabelFont() {


	return labelFont;


    }





    /**


     * Set the font of the label.


     * @see #labelFont


     */


    public void setLabelFont(Font labelFont) {


	if (labelFont!=null && !labelFont.equals(this.labelFont)) {


	    this.labelFont = labelFont;


	    repaint();


	}


    }





    /**


     * Layout the widget itself.


     */


    public void layout() {


	if (label == null) {


	    content.reshape(0, 0, width, height);


	} else {


	    FontMetrics fm = getFontMetrics(font);


	    int h = ((fm.getHeight()/2 + 4)/5)*5;


	    content.reshape(0, h, width, height-h);


	}


    }





    /**


     * Paint the outline.


     */


    public void paint(Graphics g, int x, int y, int wd, int ht) {


	if (label == null) {


	    if ((x < 2) || (y < 2) || (x + wd > width-2) || (y + ht > height-2)) {


		g.setColor(background);


		Bevel.drawRect(g, 0, 0, width, height, true, 1);


		Bevel.drawRect(g, 1, 1, width-2, height-2, false, 1);


	    }


	    g.clipRect(2, 2, width-4, height-4);


	    return;


	}





	if (labelFont != null) {


	    g.setFont(labelFont);


	}


	


	FontMetrics fm = g.getFontMetrics();


	int h = ((fm.getHeight()/2 + 4)/5)*5;





	if ((x < 2) || (y < h+2) || (x + wd > width-2) || (y + ht > height-2)) {


	    int x1 = 0, x2 = fm.stringWidth(label) + 8;





	    switch (align) {


	      case LEFT:


		x1 = 5;


		x2 += 5;


		break;


	      case RIGHT:


		x1 = ((width - 5) - x2);


		x2 = width - 5;


		break;


	      case CENTER:


		x1 = (width - x2)/2;


		x2 = (width + x2)/2;


		break;


	    }





	    int lx = x1;


	    x1 = Math.max(5, x1);


	    x2 = Math.min(width-5, x2);





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


	    g.drawLine(0, h, 0, height-1);


	    g.drawLine(0, height-2, width-2, height-2);


	    g.drawLine(width-2, h, width-2, height-2);


	    g.drawLine(0, h, x1, h);


	    g.drawLine(x2, h, width-1, h);





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


	    g.drawLine(1, h+1, 1, height-3);


	    g.drawLine(0, height-1, width-1, height-1);


	    g.drawLine(width-1, h, width-1, height-1);


	    g.drawLine(1, h+1, x1, h+1);


	    g.drawLine(x2, h+1, width-3, h+1);





	    g.setColor(foreground);


	    Graphics gc = g.create();


	    try {


		gc.clipRect(x1+4, 0, (x2-x1)-8, height);


		gc.drawString(label, lx+4, (h - fm.getHeight()/2) + fm.getAscent());


	    } finally {


		gc.dispose();


	    }


	}


	g.clipRect(2, h + 2, width-4, height-(h+4));


    }





    /**


     * Debugging.


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	buf.append(",label=");


	buf.append(label);


	buf.append(",align=");


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


    }


}


