/*
 * $URL$
 * $Revision$
 * $LastChangedBy$
 * $LastChangedDate$
 *
 * Copyright (c) 2005 syseca AG, Switzerland
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of syseca AG. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you
 * entered into with syseca AG.
 */
package ch.spherIC.components;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
import ch.spherIC.R;


/**
 * DOCUMENT ME!
 *
 * @author   $author$
 * @version  $Revision$, $Date$
 */
public class XTextView extends TextView {

    private boolean drawborder;
    private int drawborderColor;
    private int drawborderWidth;

    /**
     * Creates a new XTextView object.
     *
     * @param  context  DOCUMENT ME!
     */
    public XTextView(Context context) {
        super(context);
    }

    /**
     * Creates a new $class.name$ object.
     *
     * @param  context   DOCUMENT ME!
     * @param  attrs     DOCUMENT ME!
     * @param  defStyle  DOCUMENT ME!
     */
    public XTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    /**
     * Creates a new $class.name$ object.
     *
     * @param  context  DOCUMENT ME!
     * @param  attrs    DOCUMENT ME!
     */
    public XTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }


    /**
     * DOCUMENT ME!
     *
     * @param  attrs  DOCUMENT ME!
     */
    private void init(AttributeSet attrs) {

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.XTextView);
        this.drawborder = a.getBoolean(R.styleable.XTextView_drawBorder, false);
        this.drawborderColor = a.getColor(R.styleable.XTextView_drawBorderColor, 0xffff0000);
        this.drawborderWidth = a.getDimensionPixelSize(R.styleable.XTextView_drawBorderWidth, 1);

        if (this.drawborder) {
            setPadding(getPaddingLeft() + this.drawborderWidth, getPaddingTop() + this.drawborderWidth,
                       getPaddingRight() + this.drawborderWidth, getPaddingBottom() + this.drawborderWidth);
        }
    }


    /**
     * @see  android.widget.TextView#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (this.drawborder) {
            Rect rect = new Rect();
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(this.drawborderColor);
            paint.setStrokeWidth(this.drawborderWidth);
            getLocalVisibleRect(rect);
            rect.bottom -= 1;
            rect.right -= 1;
            canvas.drawRect(rect, paint);
        }
    }
}
