SUIManager.java :  » Swing-Library » Swinglets » com » javelin » swinglets » Java Open Source

Java Open Source » Swing Library » Swinglets 
Swinglets » com » javelin » swinglets » SUIManager.java
/*
 * Copyright Javelin Software, All rights reserved.
 */
 
package com.javelin.swinglets;

/**
 * SUIManager is the manager for Look and Feels.
 * 
 * @author Robin Sharp
 */

import java.awt.*;
import java.util.*;
import java.io.*;

import javax.servlet.http.*;

import com.javelin.swinglets.plaf.*;

public class SUIManager
{
   /**
    * Get the currently installed LAF's class names.
    */
    public synchronized static String[] getInstalledLookAndFeels()
    {
        return installedLAFNames;
    }
    
   /**
    * Replaces the current array of installed LookAndFeelInfos class names.
    */
    public synchronized static void setInstalledLookAndFeels( String[] classNames )
    {
        String[] newClassNames = new String[ classNames.length ];
        System.arraycopy(classNames, 0, newClassNames, 0, classNames.length);
        installedLAFNames = newClassNames;
    }
    
   /**
    * If the look and feel has not yet been set the cross platform look and
    * feel is installed.
    * <p>
    * @return The current default look and feel, or null.
    */
    public synchronized static SLookAndFeel getLookAndFeel() 
    {
        if( lookAndFeel == null )
        {
           //new Exception().printStackTrace();
           setLookAndFeel( getCrossPlatformLookAndFeelClassName() ); 
        }
        
        return lookAndFeel;
    }
    
   /**
    * Get a look and feel by name. This will try to create a look and feel by name.
    * This does not install the look and feel, as the default look and feel.
    * <p>
    * If the the class name is one of the installed look and feels then load 
    * the look and feel into a cache.
    */
    public synchronized static SLookAndFeel getLookAndFeel( String className )
    {
        try
        {
            int index = 0;
            for( ; index < installedLAFNames.length; index++ )
            {
                if( installedLAFNames[index].equals( className ) )
                {
                    //Find the index of the class name
                    break;
                }
            }
            
            if( index >= installedLAFNames.length ) 
            {
                //Return the look and feel but dont install it
                return (SLookAndFeel)( Class.forName( className ) ).newInstance();
            }
            
            //If the laf has not yet been installed, then install it.
            if( installedLAFs[index] == null )
            {
                installedLAFs[index] = (SLookAndFeel)( Class.forName( className ) ).newInstance(); 
            }
            
            return installedLAFs[index];
        }
        catch( Exception e )
        {
            e.printStackTrace();
            return null;
        }
    }

   /**
    * Adds the specified look and feel to the current array.
    */
    public synchronized static void installLookAndFeel( String className ) 
    {
        String[] classNames = installedLAFNames;
        String[] newClassNames = new String[ classNames.length + 1 ];
        System.arraycopy( classNames, 0, newClassNames, 0, classNames.length );
        newClassNames[ classNames.length ] = className;
        
        SLookAndFeel[] lAFs = installedLAFs;
        SLookAndFeel[] newLAFS = new SLookAndFeel[ lAFs.length + 1 ];
        System.arraycopy( lAFs, 0, newLAFS, 0, lAFs.length );
        
        setInstalledLookAndFeels( newClassNames );
    }
    
   /**
    * Set the current default look and feel using a LookAndFeel object.  
    */
    public synchronized static void setLookAndFeel( SLookAndFeel newLookAndFeel ) 
    {
        lookAndFeel = newLookAndFeel;
    }
    
   /**
    * Set the current default look and feel using a class name. If the
    * look an feel is one of the installed look and feels, put it in 
    * the cache.
    */
    public synchronized static void setLookAndFeel( String className )
    {
        try
        {
            SLookAndFeel laf = getLookAndFeel( className );
            if( laf != null )
            {
                setLookAndFeel( laf );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }

   /**
    * Returns the name of the LookAndFeel class that implements
    * the default cross platform look and feel.
    */
    public synchronized static String getCrossPlatformLookAndFeelClassName() 
    {
        return "com.javelin.swinglets.plaf.html.HTMLLookAndFeel";
    }
    
    // PRIVATE //////////////////////////////////////////////////////////////////////////////
    
    protected static SLookAndFeel lookAndFeel;
    protected static String[] installedLAFNames = new String[] 
    { 
        "com.javelin.swinglets.plaf.html.HTMLLookAndFeel",
        "com.javelin.swinglets.plaf.javascript.JSLookAndFeel",
        "com.javelin.swinglets.plaf.wml.WMLLookAndFeel",
        "com.javelin.swinglets.plaf.jfc.JFCLookAndFeel"
    };
    protected static SLookAndFeel[] installedLAFs = new SLookAndFeel[installedLAFNames.length];
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.