/*
 * @(#)ScrollBarThumbBorder.java  1.0  2008-10-04
 * 
 * Copyright (c) 2008 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */

package ch.randelshofer.gui.border;

import org.monte.media.image.Images;
import ch.randelshofer.pdf.plaf.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
 * ScrollBarThumbBorder.
 *
 * @author Werner Randelshofer, Hausmatt 10, CH-6405 Immensee
 * @version 1.0 2008-10-04 Created.
 */
public class ScrollBarThumbBorder implements Border {
    public final static int E = 0;
    public final static int EP = 1;
    public final static int D = 2;
    public final static int I = 3;
    public final static int DI = 4;
    public final static int DEFAULT = DI;
    /**
     * Borders
     */
    private Border[] borders;
    
    /** Holds the icon pictures in a single image. This variable is used only
     *until we create the icons array. Then it is set to null.
     */
    private Image tiledImage;
    /**
     * The number of icons in the tiledImage.
     */
    private int tileCount;
    /**
     * Whether the tiledImage needs to be tiled horizontally or vertically
     * to get the icons out of it.
     */
    private boolean isTiledHorizontaly;
    
    private Insets borderInsets;
    /** Only used for tiled image. */
    private boolean fill;
    /** Only used for tiled image. */
    private Insets imageInsets;
    
    
    /**
     * Creates a new instance.
     * All borders must have the same insets.
     * If a border is null, nothing is drawn for this state.
     */
    public ScrollBarThumbBorder(Border e, Border ep, Border d, Border i,
Border di) {
        borders = new Border[DEFAULT+1];
        borders[E] = e;
        borders[EP] = ep;
        borders[D] = d;
        borders[I] = i;
        borders[DI] = di;
    }
    /**
     * Creates a new instance.
     * All borders must have the same insets.
     */
    public ScrollBarThumbBorder(Border[] borders) {
        this.borders = new Border[DEFAULT+1];
        System.arraycopy(borders, 0, this.borders, 0, Math.min(borders.length, this.borders.length));
    }
    /**
     * Creates a new instance.
     * All borders must have the same dimensions.
     */
    public ScrollBarThumbBorder(Image[] images, Insets imageInsets, Insets borderInsets, boolean fill) {
        this.borders = new Border[DEFAULT+1];
        for (int i=0, n = Math.min(images.length, borders.length); i < n; i++) {
            if (images[i] != null) {
                borders[i] = PDFBorderFactory.create(images[i], imageInsets, borderInsets, fill);
            }
        }
    }
    
    /**
     * Creates a new instance.
     * All borders must have the same dimensions.
     */
    public ScrollBarThumbBorder(Image tiledImage, int tileCount, boolean isTiledHorizontaly,
    Insets imageInsets, Insets borderInsets, boolean fill) {
        this.tiledImage = tiledImage;
        this.tileCount = tileCount;
        this.isTiledHorizontaly = isTiledHorizontaly;
        this.imageInsets = imageInsets;
        this.borderInsets = borderInsets;
        this.fill = fill;
    }
    
    public void setBorder(int key, Border b) {
        borders[key] = b;
    }
    
    
    public Insets getBorderInsets(Component c) {
        if (borderInsets != null) {
            return (Insets) borderInsets.clone();
        } else {
            generateBordersFromTiledImage();
            return (Insets) borders[0].getBorderInsets(c).clone();
        }
    }
    
    public boolean isBorderOpaque() {
        generateBordersFromTiledImage();
        return borders[0].isBorderOpaque();
    }
    
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        generateBordersFromTiledImage();
        Border border = getBorder(c);
        if (border != null) {
            border.paintBorder(c, g, x, y, width, height);
        }
    }
    
    protected Border getBorder(Component c) {
        Border border;
        boolean isActive = PDFUtilities.isOnActiveWindow(c);
        
        if (c instanceof JScrollBar) {
            BoundedRangeModel model = ((JScrollBar) c).getModel();
            if (isActive) {
                if (c.isEnabled()) {
                    if (model.getValueIsAdjusting()) {
                            border = borders[EP];
                    } else {
                            border = borders[E];
                    }
                } else {
                        border = borders[D];
                }
            } else {
                if (c.isEnabled()) {
                        border = borders[I];
                } else {
                        border = borders[DI];
                }
            }
        } else {
            if (isActive) {
                if (c.isEnabled()) {
                    border = borders[E];
                } else {
                    border = borders[D];
                }
            } else {
                if (c.isEnabled()) {
                    border = borders[I];
                } else {
                    border = borders[DI];
                }
            }
        }
        return border;
    }
    private void generateBordersFromTiledImage() {
        if (borders == null) {
            borders = new Border[DEFAULT+1];
            Image[] images = Images.split(tiledImage, tileCount, isTiledHorizontaly);
            for (int i=0, n = Math.min(images.length, borders.length); i < n; i++) {
                borders[i] = PDFBorderFactory.create(images[i], imageInsets, borderInsets, fill);
            }
            generateMissingBorders();
            tiledImage = null;
        }
    }
    private void generateMissingBorders() {
        
    }
}