/*
 * @(#)Common13.java
 *
 * Summary: methods common to all Swing-using programs.
 *
 * Copyright: (c) 2008-2010 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.3+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.3 2008-11-12 - add JDK version to About box
 */
package com.mindprod.common13;

import com.mindprod.common11.VersionCheck;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * methods common to all Swing-using programs.
 * <p/>
 * @noinspection EmptyCatchBlock
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.3 2008-11-12 - add JDK version to About box
 * @since 2008
 */
public final class Common13
    {
    // ------------------------------ CONSTANTS ------------------------------

    private static final boolean DEBUGGING = false;
    // -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * set the look and feel for a Swing App, based on OS and JDK version..
     */
    public static void setLaf()
        {
        if ( setLaf( "Mac OS X", 1, 4, 0, "ch.randelshofer.quaqua.QuaquaLookAndFeel" ) )
            {
            // will only work if user has installed the 1.7 MB quaqua.jar himself.
            return;
            }
        if ( setLaf( "Mac OS X", 1, 3, 0, "com.sun.java.swing.plaf.mac.MacLookAndFeel" ) )
            {
            return;
            }
        if ( setLaf( "Mac OS", 1, 3, 0, UIManager.getSystemLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( null, 1, 6, 0, "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ) )
            {
            // won't take for anything less than 1.6.0_10
            return;
            }
        if ( setLaf( "Linux", 1, 5, 0, "com.sun.java.swing.plaf.gtk.GTKLooktAndFeel" ) )
            {
            return;
            }
        if ( setLaf( "Windows Vista", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( "Windows XP", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( "Windows 2000", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        setLaf( null, 1, 3, 0, UIManager.getSystemLookAndFeelClassName() );
        // if even that failed, give up. leave original L&F in place.
        }

    // -------------------------- STATIC METHODS --------------------------

    /**
     * Attempt to set the LAF, depending on OS and JDK being suitable.
     *
     * @param os           name of OS, null means it does not matter.
     * @param wantedMajor  java major version e.g. 1
     * @param wantedMinor  Java minor version e.g. 6
     * @param wantedBugFix Java bugfix version e.g. 14
     * @param lafClassName class name of laf to set if os matches and JDK version is sufficiently advanced.
     *
     * @return true the setting took, false if failed.
     */
    private static boolean setLaf( String os, int wantedMajor,
                                   int wantedMinor,
                                   int wantedBugFix,
                                   String lafClassName )
        {
        if ( os != null && !System.getProperty( "os.name", "unknown" ).equals( os ) )
            {
            return false;
            }
        if ( !VersionCheck.isJavaVersionOK( wantedMajor, wantedMinor, wantedBugFix ) )
            {
            return false;
            }
        try
            {
            UIManager.setLookAndFeel( lafClassName );
            }
        catch ( UnsupportedLookAndFeelException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: UnsupportedLookAndFeelException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( IllegalAccessException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: IllegalAccessException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( InstantiationException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: InstantiationException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( ClassNotFoundException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: ClassNotFoundException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( Exception e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: Exception" + lafClassName + e.getMessage() );
                }
            return false;
            }
        if ( DEBUGGING )
            {
            System.err.println( "Choosing L&F " + lafClassName );
            }
        return true;
        }

    // --------------------------- main() method ---------------------------

    /**
     * test harness
     *
     * @param args not used
     *
     * @noinspection EmptyMethod
     */
    public static void main( String[] args )
        {
        // dummy
        }
    }
