/*
 * @(#)PDFSplitPaneUI.java  1.1.1  2009-09-21
 *
 * Copyright (c) 2005-2009 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.pdf.plaf.color.PaintableColor;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;

/**
 * PDFSplitPaneUI.
 *
 * @author  Werner Randelshofer
 * @version 1.1.1 2009-09-21 Set split pane opaque.
 * <br>1.0 November 27, 2005 Created.
 */
public class PDFSplitPaneUI extends BasicSplitPaneUI {

    /**
     * Creates a new instance.
     */
    public PDFSplitPaneUI() {
    }

    /**
     * Creates a new BasicSplitPaneUI instance
     */
    public static ComponentUI createUI(JComponent x) {
        return new PDFSplitPaneUI();
    }

    /**
     * Installs the UI defaults.
     */
    @Override
    protected void installDefaults() {
        super.installDefaults();
        PDFLookAndFeel plaf = PDFLookAndFeel.getInstance();
        PDFLookAndFeel.installBorder(splitPane, "SplitPane.border");
        PDFLookAndFeel.installColors(splitPane, "SplitPane.background",
                                  "SplitPane.foreground");
        PDFLookAndFeel.installProperty(splitPane, "opaque", plaf.get("SplitPane.opaque"));
        splitPane.setFocusable(plaf.getBoolean("SplitPane.focusable"));
	//Border    b = divider.getBorder();

	//if (b == null || !(b instanceof UIResource)) {
	    divider.setBorder(plaf.getBorder("SplitPaneDivider.border"));
	//}
            splitPane.setOpaque(true);
    }

    /**
     * Creates the default divider.
     */
    public BasicSplitPaneDivider createDefaultDivider() {
        return new PDFSplitPaneDivider(this);
    }

    @Override
    public int getMinimumDividerLocation(JSplitPane jc) {
        int mdl= super.getMinimumDividerLocation(jc);
        return mdl;
    }

    public void paint(Graphics gr, JComponent c) {
        Graphics2D g = (Graphics2D) gr;
        Object oldHints = PDFUtilities.beginGraphics(g);
        if (c.isOpaque()) {
            g.setPaint(PaintableColor.getPaint(c.getBackground(), c));
            g.fillRect(0, 0, c.getWidth(), c.getHeight());
        }
        super.paint(gr, c);
        PDFUtilities.endGraphics((Graphics2D) g, oldHints);
    }

    /**
     * Messaged after the JSplitPane the receiver is providing the look
     * and feel for paints its children.
     */
    public void finishedPaintingChildren(JSplitPane jc, Graphics g) {
        if (jc == splitPane && getLastDragLocation() != -1 &&
                !isContinuousLayout() && !draggingHW) {
            Dimension size = splitPane.getSize();

        PDFLookAndFeel plaf = PDFLookAndFeel.getInstance();
            g.setColor(plaf.getColor("SplitPaneDivider.draggingColor"));
            if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
                g.fillRect(getLastDragLocation(), 0, dividerSize,
                        size.height);
            } else {
                g.fillRect(0, getLastDragLocation(), size.width,
                        dividerSize);
            }
        }
    }

    /**
     * Returns the default non continuous layout divider, which is an
     * instanceof Canvas that fills the background in dark gray.
     */
    protected Component createDefaultNonContinuousLayoutDivider() {
        return new Canvas() {

            public void paint(Graphics g) {
                if (!isContinuousLayout() && getLastDragLocation() != -1) {
                    Dimension size = splitPane.getSize();
        PDFLookAndFeel plaf = PDFLookAndFeel.getInstance();
                    g.setColor(plaf.getColor("SplitPaneDivider.draggingColor"));
                    if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
                        g.fillRect(0, 0, dividerSize, size.height);
                    } else {
                        g.fillRect(0, 0, size.width, dividerSize);
                    }
                }
            }
        };
    }
}
