package cnp.ew.slider;

import java.awt.*;
import cnp.ew.util.*;
import cnp.ew.displayer.*;
import cnp.ew.image.*;
import cnp.ew.lightweight.*;
import cnp.ew.scrolling.*;

public class CpWin95SliderLc extends CpSliderLc
{
    static int verticalThumbImageId;
    static int horizontalThumbImageId;

    static Cp3DBorderDisplayer borderDisplayer = new Cp3DBorderDisplayer(false, true);

    static {
        verticalThumbImageId = CpToolkit.registerImageName("vslider.gif");
        horizontalThumbImageId = CpToolkit.registerImageName("hslider.gif");
    }

    Image cachedBackground;

    public CpWin95SliderLc(int orientation)
    {
        super(orientation);
        if (orientation == CpScrollbarLc.VERTICAL) {
            thumbImage = CpToolkit.getImage(verticalThumbImageId);
            pageSize = thumbImage.getHeight(CpToolkit.defaultComponent());
        } else {
            thumbImage = CpToolkit.getImage(horizontalThumbImageId);
            pageSize = thumbImage.getWidth(CpToolkit.defaultComponent());
        }
    }

    public void paintBackground(Graphics g)
    {

        int imageSize, left, top, right, bottom;
        int scaleDimension;
        Rectangle clientRect = getClientRect();
        Graphics tempG = null;

        if (cachedBackground == null) {

            cachedBackground = getComponent().createImage(clientRect.width, clientRect.height);
            try {
                tempG = cachedBackground.getGraphics();
                tempG.setColor(getBackground());
                tempG.fillRect(0, 0, clientRect.width, clientRect.height);
                scaleDimension = getDimension();

                switch (orientation) {
                case CpScrollbarLc.HORIZONTAL:
                    borderDisplayer.paintIn(this, tempG, 0, 10, scaleDimension, 4);
                    tempG.setColor(Color.black);
                    tempG.drawLine(1, 11, scaleDimension - 3, 11);
                    imageSize = thumbImage.getWidth(CpToolkit.defaultComponent());
                    left =  imageSize/2;
                    right =  scaleDimension - imageSize;
                    top = thumbImage.getHeight(CpToolkit.defaultComponent()) + 1;
                    tempG.drawLine(left, top, left, top + 4);
                    left += imageSize;
                    while (left < right) {
                        tempG.drawLine(left, top, left, top + 3);
                        left += imageSize;
                    }
                    tempG.drawLine(left, top, left, top + 4);
                    break;
                case CpScrollbarLc.VERTICAL:
                    break;
                }
            } finally {
                if (tempG != null) {
                    tempG.dispose();
                }
            }
        }


        g.drawImage(cachedBackground, clientRect.x, clientRect.y, CpToolkit.defaultComponent());
    }

    public void resize(int width, int height)
    {
        super.resize(width, height);
        cachedBackground = null;
    }

    int getDimension()
    {
        Rectangle clientRect = getClientRect();
        if (orientation == CpScrollbarLc.VERTICAL) {
            return clientRect.height - (clientRect.height % thumbImage.getHeight(CpToolkit.defaultComponent()));
        } else {
            return clientRect.width - (clientRect.width % thumbImage.getWidth(CpToolkit.defaultComponent()));
        }
    }

    public Dimension preferredSize()
    {
        if (orientation == CpScrollbarLc.HORIZONTAL) {
            return new Dimension(100, thumbImage.getHeight(CpToolkit.defaultComponent()) + 6);
        } else {
            return new Dimension(thumbImage.getWidth(CpToolkit.defaultComponent()), 100);
        }
    }
}

