package prolog.ui;

import java.awt.*;
import java.awt.geom.*;

/**
 * some util methods used by all renderers from
 * over the program.
 */
public class UiUtil
{
	/**
	 * private constructor -> util class
	 */
	private UiUtil()
	{
	}

	/**
	 * paints a cell with centered content
	 * @param g - the graphical context to paint into
	 * @param r - the rectangle to paint into
	 * @param text - the text to paint
	 * @param bkColor - the background color
	 * @param borderColor - the border color
	 */
	public static void paintCenteredCell(
		Graphics2D g,
		Rectangle r,
		String text,
		Color bkColor,
		Color borderColor )
	{
		// calculate string size
		FontMetrics fm = g.getFontMetrics();
		Rectangle2D textRect = fm.getStringBounds( text, g );

		// paing background
		g.setColor( bkColor );
		g.fillRect( r.x, r.y, r.width, r.height );

		// paint border
		g.setColor( borderColor );
		g.drawRect( r.x, r.y, r.width, r.height );

		// set clipping region
		g.setClip( new Rectangle( r.x + 2, r.y + 2, r.width - 1, r.height - 4 ) );

		// paint content
		g.setColor( Color.black );
		g.drawString(
			text,
			r.x + (r.width - (int)textRect.getWidth()) / 2,
			r.y + 15 );

		// unset clipping region
		g.setClip( null );
	}

	/**
	 * paints a cell
	 * @param g - the graphical context to paint into
	 * @param r - the rectangle to paint into
	 * @param text - the text to paint
	 * @param bkColor - the background color
	 * @param borderColor - the border color
	 */
	public static void paintCell(
		Graphics2D g,
		Rectangle r,
		String text,
		Color bkColor,
		Color borderColor )
	{
		// paing background
		g.setColor( bkColor );
		g.fillRect( r.x, r.y, r.width, r.height );

		// paint border
		g.setColor( borderColor );
		g.drawRect( r.x, r.y, r.width, r.height );

		// set clipping region
		g.setClip( new Rectangle( r.x + 2, r.y + 2, r.width - 1, r.height - 4 ) );

		// paint content
		g.setColor( Color.black );
		g.drawString(
			text,
			r.x + 2,
			r.y + 15 );

		// unset clipping region
		g.setClip( null );
	}
}
