/*
 * @(#)BorderFactory.java  2.0  2007-10-30
 *
 * Copyright (c) 2005-2007 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.pdf.plaf;

import ch.randelshofer.gui.border.ImageBevelBorder;
import ch.randelshofer.gui.border.CachedImageBevelBorder;
import ch.randelshofer.gui.border.BackgroundBorderUIResource;
import org.monte.media.image.Images;
import java.awt.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.net.URL;
/**
 * Creates an ImageBevelBorder instance optimized for this JVM.
 *
 * @author  Werner Randelshofer
 * @version 2.0 2007-10-30 Dropped support for Java 1.3.
 * <br>1.3 2005-01-04 Method createSmallSquareBorder added.
 * <br>1.2 2005-11-30 Fixed broken creation of CachedImageBevelBorder.
 * <br>1.1 2005-09-07 Support for caching image bevel borders added.
 * <br>1.0  25 April 2005  Created.
 */
public class PDFBorderFactory {
    
    /**
     * Prevent instance creation of the factory.
     */
    private PDFBorderFactory() {
        //1.4.2_05
    }
    
    /** Creates a new instance of an ImageBevelBorder optimized for this JVM. */
    public static Border create(Image img, Insets borderInsets) {
        return create(img, borderInsets, borderInsets);
    }
    
    /**
     * Creates a new instance of an ImageBevelBorder with the given image and insets.
     * The image has different insets than the border.
     */
    public static Border create(Image img, Insets imageInsets, Insets borderInsets) {
        return create(img, imageInsets, borderInsets, false, true);
    }
    /**
     * Creates a new instance of an ImageBevelBorder with the given image and insets.
     * The image has different insets than the border.
     */
    public static Border create(Image img, Insets borderInsets, boolean fillContentArea, boolean isCaching) {
        return create(img, borderInsets, borderInsets, fillContentArea, isCaching);
    }
    /**
     * Creates a new instance of an ImageBevelBorder with the given image and insets.
     * The image has different insets than the border.
     */
    public static Border create(Image img, Insets imageInsets, Insets borderInsets, boolean fillContentArea) {
        return create(img, imageInsets, borderInsets, fillContentArea, true);
    }
    /**
     * Creates a new instance of an ImageBevelBorder with the given image and insets.
     * The image has different insets than the border.
     */
    public static Border create(Image img, Insets imageInsets, Insets borderInsets, boolean fillContentArea, boolean isCaching) {
        if (isCaching) {
            return new CachedImageBevelBorder(img, imageInsets, borderInsets, fillContentArea);
        } else {
            return new ImageBevelBorder(img, imageInsets, borderInsets, fillContentArea);
        }
    }
    public static Border create(String location, Insets borderInsets, boolean fill) {
        URL resource = PDFBorderFactory.class.getResource(location);
        if (resource == null) {
            System.err.println("Warning: PDFBorderFactory couldn't find resource for "+location);
            return null;
        }
        return create(Images.createImage(resource), borderInsets, borderInsets, fill, false);
    }
    public static Border create(String location, Insets imageInsets, Insets borderInsets, boolean fill) {
        URL resource = PDFBorderFactory.class.getResource(location);
        if (resource == null) {
            System.err.println("Warning: PDFBorderFactory couldn't find resource for "+location);
            return null;
        }
        return create(Images.createImage(resource), imageInsets, borderInsets, fill, false);
    }
    public static Border createBackgroundBorder(String location, Insets imageInsets, Insets borderInsets, boolean fill) {
        URL resource = PDFBorderFactory.class.getResource(location);
        if (resource == null) {
            System.err.println("Warning: PDFBorderFactory couldn't find resource for "+location);
            return null;
        }
        return new BackgroundBorderUIResource(create(Images.createImage(resource), imageInsets, borderInsets, fill, false));
    }
    
    /**
     * Creates an array of ImageBevelBorders.
     *
     * @param location URL of the image that contains the border images.
     * @param insets Insets of the borders.
     * @param count Number of borders to generate.
     * @param horizontal True, if the image is to be split horizontally to get
     * the individual image of each border. If set to false, the image is split
     * vertically.
     */
    public static Object create(String location, Insets insets, int count, boolean horizontal) {
        return create(location, insets, count, horizontal, true, true);
    }
    public static Object create(String location, Insets insets, int count, boolean horizontal, boolean fill, boolean isCaching) {
        URL resource = PDFBorderFactory.class.getResource(location);
        if (resource == null) {
            System.err.println("Warning: PDFBorderFactory couldn't find resource for "+location);
            return null;
        }
        BufferedImage[] images = Images.split(
                Images.createImage(resource),
                count, horizontal
                );
        Border[] borders = new Border[count];
        for (int i=0; i < count; i++) {
            borders[i] = create(images[i], insets, insets, fill, isCaching);
        }
        return borders;
    }
    
    public static Border createButtonBorder(String type) {
        return new BackgroundBorderUIResource(new PDFButtonBorder("push"));
    }
}
