/*
 * Copyright (C) 2005-2006 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 */ 

package org.jdesktop.layout;

import junit.framework.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalTheme;

/**
 *
 * @author sky
 */
public class MetalLayoutStyleTest extends TestCase {
    private LayoutStyle p;
    
    /** Creates a new instance of MetalPaddingTest */
    public MetalLayoutStyleTest(String name) {
        super(name);
    }

    public void testOceanButtons() {
        if (setTheme(true)) {
            JButton button1 = new JButton();
            JButton button2 = new JButton();
            testSides(button1, button2, LayoutStyle.RELATED, 6);
            testSides(button1, button2, LayoutStyle.UNRELATED, 12);
        }
    }

    public void testOceanCheckBoxs() {
        if (setTheme(true)) {
            JComponent button1 = new JCheckBox("HI");
            JComponent button2 = new JCheckBox();

            testSides(button1, button2, LayoutStyle.RELATED, 0);
            testSides(button1, button2, LayoutStyle.UNRELATED, 4, 4);

            button1.setBorder(new EmptyBorder(0, 0, 0, 0));
            button2.setBorder(new EmptyBorder(0, 0, 0, 0));
            testSides(button1, button2, LayoutStyle.RELATED, 6, 6);
            testSides(button1, button2, LayoutStyle.UNRELATED, 12, 12);
        
            button2 = new JRadioButton();
            testSides(button1, button2, LayoutStyle.RELATED, 2, 2, 1, 2);
            testSides(button1, button2, LayoutStyle.UNRELATED, 8, 8, 7, 8);
        }
    }

    public void testOceanLabels() {
        if (setTheme(true)) {
            JComponent c1 = new JLabel("HI");
            JComponent c2 = new JButton("HI");

            testSides(c1, c2, LayoutStyle.RELATED, 12, 12, 6, 6);
            testSides(c1, c2, LayoutStyle.UNRELATED, 18, 18, 12, 12);
        }
    }
    
    public void testOceanOther() {
        if (setTheme(true)) {
            JComponent c1 = new JComboBox();
            JComponent c2 = new JTree();

            testSides(c1, c2, LayoutStyle.RELATED, 6);
            testSides(c1, c2, LayoutStyle.UNRELATED, 12);
        }
    }

    public void testSteelToggleButtons() {
        setTheme(false);
        JToggleButton tb1 = new JToggleButton("tb1");
        JToggleButton tb2 = new JToggleButton("tb2");
        testSides(tb1, tb2, LayoutStyle.RELATED, 5);
        testSides(tb1, tb2, LayoutStyle.UNRELATED, 11);
        ButtonGroup bg = new ButtonGroup();
        bg.add(tb1);
        bg.add(tb2);
        testSides(tb1, tb2, LayoutStyle.RELATED, 2);
        testSides(tb1, tb2, LayoutStyle.UNRELATED, 11);
    }
    
    public void testOceanToggleButtons() {
        if (setTheme(true)) {
            JToggleButton tb1 = new JToggleButton("tb1");
            JToggleButton tb2 = new JToggleButton("tb2");
            testSides(tb1, tb2, LayoutStyle.RELATED, 6);
            testSides(tb1, tb2, LayoutStyle.UNRELATED, 12);
            ButtonGroup bg = new ButtonGroup();
            bg.add(tb1);
            bg.add(tb2);
            testSides(tb1, tb2, LayoutStyle.RELATED, 2);
            testSides(tb1, tb2, LayoutStyle.UNRELATED, 12);
        }
    }
    
    public void testSteelOther() {
        if (setTheme(false)) {
            JComponent c1 = new JComboBox();
            JComponent c2 = new JTree();

            testSides(c1, c2, LayoutStyle.RELATED, 6);
            testSides(c1, c2, LayoutStyle.UNRELATED, 12);
        }
    }

    public void testSteelButtons() {
        setTheme(false);
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        testSides(button1, button2, LayoutStyle.RELATED, 5);
        testSides(button1, button2, LayoutStyle.UNRELATED, 11);
    }

    public void testSteelCheckBoxs() {
        setTheme(false);
        JComponent button1 = new JCheckBox("HI");
        JComponent button2 = new JCheckBox();

        testSides(button1, button2, LayoutStyle.RELATED, 0);
        testSides(button1, button2, LayoutStyle.UNRELATED, 4, 3);

        button1.setBorder(new EmptyBorder(0, 0, 0, 0));
        button2.setBorder(new EmptyBorder(0, 0, 0, 0));
        testSides(button1, button2, LayoutStyle.RELATED, 6, 5);
        testSides(button1, button2, LayoutStyle.UNRELATED, 12, 11);
        
        button2 = new JRadioButton();
        testSides(button1, button2, LayoutStyle.RELATED, 2, 1);
        testSides(button1, button2, LayoutStyle.UNRELATED, 8, 7);
    }

    public void testSteelLabels() {
        setTheme(false);
        JComponent c1 = new JLabel("HI");
        JComponent c2 = new JButton("HI");

        testSides(c1, c2, LayoutStyle.RELATED, 12, 11, 5, 6);
        testSides(c1, c2, LayoutStyle.UNRELATED, 18, 17, 11, 12);
    }
    
    private void testSides(JComponent c1, JComponent c2, int type, int size) {
        testSides(c1, c2, type, size, size);
    }

    private void testSides(JComponent c1, JComponent c2, int type, int hSize,
                           int vSize) {
        testSides(c1, c2, type, hSize, hSize, vSize, vSize);
    }
    
    private JPanel panel = new JPanel();

    private void testSides(JComponent c1, JComponent c2, int type, int eSize,
                           int wSize, int nSize, int sSize) {
        assertEquals(eSize, p.getPreferredGap(c1, c2, type, SwingConstants.EAST, panel));
        assertEquals(wSize, p.getPreferredGap(c1, c2, type, SwingConstants.WEST, panel));
        assertEquals(nSize, p.getPreferredGap(c1, c2, type, SwingConstants.NORTH, panel));
        assertEquals(sSize, p.getPreferredGap(c1, c2, type, SwingConstants.SOUTH, panel));
    }

    private boolean setTheme(boolean useOcean) {
        boolean success = true;
        if (useOcean) {
            try {
                MetalLookAndFeel.class.getMethod("getCurrentTheme", (Class[])null);
                MetalLookAndFeel.setCurrentTheme((MetalTheme)Class.forName(
                        "javax.swing.plaf.metal.OceanTheme").newInstance());
            } catch (Exception e) {
                success = false;
                MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
            }
        }
        else {
            MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
        }
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (Exception e) {
        }
        p = LayoutStyle.getSharedInstance();
        return success;
    }
}
