/*
 * $URL$
 * $Revision$
 * $LastChangedBy$
 * $LastChangedDate$
 *
 * Copyright (c) 2011 spherIC, Switzerland
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of spherIC. ("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 spherIC.
 */
package ch.spherIC.recurvebowsight.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.ImageView;

import ch.spherIC.recurvebowsight.R;


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

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

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

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

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


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

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.XImageView);
        this.drawborder = a.getBoolean(R.styleable.XImageView_drawImgBorder, false);
        this.drawborderColor = a.getColor(R.styleable.XImageView_drawImgBorderColor, 0xffff0000);
        this.drawborderWidth = a.getDimensionPixelSize(R.styleable.XImageView_drawImgBorderWidth, 1);

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


    /**
     * @see  android.widget.TextView#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(final 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);
        }
    }


    /**
     * @param  drawborderColor  the drawborderColor to set
     */
    public void setDrawborderColor(final int drawborderColor) {
        this.drawborderColor = drawborderColor;
        invalidate();
    }

}
