/*
 * @(#)ExtendedJideTabbedPaneUI.java 5/9/2012
 *
 * Copyright 2002 - 2012 JIDE Software Inc. All rights reserved.
 */

import com.jidesoft.plaf.UIDefaultsLookup;
import com.jidesoft.plaf.office2007.Office2007JideTabbedPaneUI;
import com.jidesoft.swing.JideTabbedPane;
import com.jidesoft.swing.TabColorProvider;
import com.jidesoft.utils.ColorUtils;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import java.awt.*;

public class ExtendedJideTabbedPaneUI extends Office2007JideTabbedPaneUI {
    public static ComponentUI createUI(JComponent c) {
        return new ExtendedJideTabbedPaneUI();
    }

    @Override
    protected Color[] getGradientColors(int tabIndex, boolean isSelected) {
        Color backgroundEnd = null;
        Color backgroundStart = null;

        Boolean highContrast = UIManager.getBoolean("Theme.highContrast");
        if (highContrast) {
            backgroundStart = isSelected ? UIDefaultsLookup.getColor("JideButton.selectedBackground") : UIDefaultsLookup.getColor("JideButton.background");
            backgroundEnd = backgroundStart;
        }
        else {
            Color color = null;
            Component comp = null;
            if (tabIndex >= 0 && tabIndex < _tabPane.getTabCount()) {
                comp = _tabPane.getComponentAt(tabIndex);
            }
            if (comp instanceof TabColorProvider) {
                color = ((TabColorProvider) comp).getTabBackground();
            }
            JideTabbedPane.ColorProvider colorProvider = _tabPane.getTabColorProvider();
            if (color == null && colorProvider != null) {
                backgroundEnd = colorProvider.getBackgroundAt(tabIndex);
                if (colorProvider instanceof JideTabbedPane.GradientColorProvider) {
                    backgroundStart = ((JideTabbedPane.GradientColorProvider) colorProvider).getTopBackgroundAt(tabIndex);
                }
                else {
                    backgroundStart = backgroundEnd != null ? ColorUtils.getDerivedColor(backgroundEnd, colorProvider.getGradientRatio(tabIndex)) : null;
                }
            }
            else {
                if (color == null) {
                    color = _tabPane.getBackground();
                    if (tabIndex != -1)
                        color = _tabPane.getBackgroundAt(tabIndex);
                }
                if (!(color instanceof UIResource) && color != _tabPane.getBackground()) {
                    backgroundEnd = color;
                    if (getColorTheme() == JideTabbedPane.COLOR_THEME_OFFICE2003) {
                        backgroundStart = ColorUtils.getDerivedColor(color, 0.8f);
                    }
                    else {
                        backgroundStart = color;
                    }
                }
            }

            if (isSelected) {
                if (showFocusIndicator()) {
                    if (backgroundEnd == null) {
                        backgroundEnd = Color.GREEN; // note: change the color here
                    }
                    if (backgroundStart == null) {
                        backgroundStart = _backgroundSelectedColorStart;
                    }
                }
                else {
                    if (getColorTheme() == JideTabbedPane.COLOR_THEME_VSNET) {
                        if (backgroundEnd == null) {
                            backgroundEnd = _backgroundSelectedColorEnd;
                        }
                        if (backgroundStart == null) {
                            backgroundStart = _backgroundSelectedColorStart;
                        }
                    }
                    else {
                        if (backgroundEnd == null) {
                            backgroundEnd = ColorUtils.getDerivedColor(_backgroundUnselectedColorEnd, 0.7f);
                        }
                        if (backgroundStart == null) {
                            backgroundStart = ColorUtils.getDerivedColor(_backgroundUnselectedColorStart, 0.8f);
                        }
                    }
                }
            }
            else {
                if (getTabShape() != JideTabbedPane.SHAPE_WINDOWS_SELECTED) {
                    if (backgroundEnd == null) {
                        backgroundEnd = _backgroundUnselectedColorEnd;
                    }
                    if (backgroundStart == null) {
                        backgroundStart = _backgroundUnselectedColorStart;
                    }
                }
            }
        }
        return new Color[]{backgroundStart, backgroundEnd};
    }
}
