/*
 * @(#)PDFTextFieldBorder.java  3.1.1  2008-01-04
 *
 * Copyright (c) 2004-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.pdf.plaf;

import ch.randelshofer.gui.border.BackgroundBorder;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;

/**
 * PDFTextFieldBorder.
 *
 * @author  Werner Randelshofer
 * @version 4.1 2008-01-04 Don't draw focus border when component is disabled. 
 * <br>4.0 2007-07-26 Add support for client property "Quaqua.TextField.style"
 * "search", "plain".
 * <br>3.1 2007-04-12 Honour margin of JTextComponent.
 * <br>3.0.1 2006-01-04 Non-editable text field border must be same as
 * disabled text field border.
 * <br>3.0 2005-12-08 Rewritten to create border images lazily and to not
 * hardcode the URL to the border images.
 * <br>2.0 2005-10-01 Turned into a background border.
 * <br>1.3 2005-06-20 1.2 Changed border insets to achieve baseline alignment with other
 * components.
 * <br>1.2 2005-04-10 1.1.1 Alignemnt changed for small size text fields.
 * <br>1.1 2005-03-27 1.1 Updated to take account of larger focus ring and of
 * margins.
 * <br>1.0  July 4, 2004  Created.
 */
public class PDFTextFieldBorder extends VisualMargin implements BackgroundBorder {
    /** Location of the border images. */
    private String imagesLocation;
    private String searchImagesLocation;
    private String smallSearchImagesLocation;
    private Insets imageInsets;
    private Insets borderInsets;
    /** Array with image bevel plainBorders.
     * This array is created lazily.
     **/
    private Border[] plainBorders;
    
    private Border textFieldBackground = new Border() {
        public Insets getBorderInsets(Component c) {
            return new Insets(0,0,0,0);
        }
        public boolean isBorderOpaque() {
            return false;
        }
        
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            if (c.isOpaque()) {
                g.setColor(UIManager.getColor("Panel.background"));
                g.fillRect(0, 0, width, height);
            }
            
            g.setColor(c.getBackground());
            Insets insets = getVisualMargin(c, new Insets(0,0,0,0));
                g.fillRect(
                        insets.left + 2,
                        insets.top + 2,
                        width - insets.left - insets.right - 4,
                        height - insets.top - insets.bottom - 4
                        );
        }
    };
    
    
    /** Creates a new instance. */
    public PDFTextFieldBorder(String imagesLocation, Insets imageInsets, Insets borderInsets) {
        super(0,0,0,0);
        this.imagesLocation = imagesLocation;
        this.imageInsets = imageInsets;
        this.borderInsets = borderInsets;
    }
    
    private static boolean isSmall(Component c) {
            return PDFUtilities.isSmallSizeVariant((JComponent) c);
    }
    
    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        insets = getVisualMargin(c, insets);
        Insets inner = isSmall(c) ?
            new Insets(6, 7, 5, 7) :
              (Insets) borderInsets.clone();
        InsetsUtil.addTo(inner, insets);
        
        if (c instanceof JTextComponent) {
            Insets margin = ((JTextComponent) c).getMargin();
            if (margin != null) {
                InsetsUtil.addTo(margin, insets);
            }
        }
        
        return insets;
    }
    
    @Override
    public boolean isBorderOpaque() {
        return false;
    }
    
    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Insets margin = getVisualMargin(c, new Insets(0,0,0,0));
        Border border = getBorder(c);
        border.paintBorder(c, g,
                x + margin.left, y + margin.top,
                width - margin.left - margin.right,
                height - margin.top - margin.bottom
                );
        
    }
    
    public Border getBackgroundBorder() {
        return textFieldBackground;
    }
    
    private Border getBorder(Component c) {
        Border[] borders;
            if (plainBorders == null) {
                plainBorders = (Border[]) PDFBorderFactory.create(imagesLocation, imageInsets, 3, true);
            }
            borders = plainBorders;
        
        boolean isEditable;
        if (c instanceof JTextComponent) {
            isEditable = ((JTextComponent) c).isEditable();
        } else {
            isEditable = true;
        }

        if (PDFUtilities.isFocused(c) && c.isEnabled()) {
            return borders[2];
        } else if (c.isEnabled() && isEditable) {
            return borders[0];
        } else {
            return borders[1];
        }
    }
    
    
    public static class UIResource extends PDFTextFieldBorder implements javax.swing.plaf.UIResource {
        public UIResource(String imagesLocation, Insets imageInsets, Insets borderInsets) {
            super(imagesLocation, imageInsets, borderInsets);
        }
    }
}
