/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package luxor.css.xul.data;

import java.util.*;

public class StyleRegister
{
   private static StyleRegister _instance;

   private StyleSheet EMPTY_STYLE_SHEET = new StyleSheet( Collections.EMPTY_MAP );
   private StyleTable _clazzTable = new StyleTable();
   private StyleTable _elementTable = new StyleTable();

   private StyleTable _idTable = new StyleTable();
   private Map _props[] = new Map[3];

   private int _propsCount = 0;

   private StyleRegister() { }

   public static StyleRegister getStyleRegister()
   {
      if( _instance == null )
         _instance = new StyleRegister();

      return _instance;
   }

   public StyleSheet getStyleSheet( String element )
   {
      _propsCount = 0;

      Map elementProps = _elementTable.lookup( element );
      if( elementProps != null )
         _props[_propsCount++] = elementProps;

      return createStyleSheet();
   }

   public StyleSheet getStyleSheet( String id, String element )
   {
      _propsCount = 0;

      Map idProps = _idTable.lookup( id );
      if( idProps != null )
         _props[_propsCount++] = idProps;

      Map elementProps = _elementTable.lookup( element );
      if( elementProps != null )
         _props[_propsCount++] = elementProps;

      return createStyleSheet();
   }

   public StyleSheet getStyleSheet( String id, String clazz, String element )
   {
      _propsCount = 0;

      Map idProps = _idTable.lookup( id );
      if( idProps != null )
         _props[_propsCount++] = idProps;

      Map clazzProps = _clazzTable.lookup( clazz );
      if( clazzProps != null )
         _props[_propsCount++] = clazzProps;

      Map elementProps = _elementTable.lookup( element );
      if( elementProps != null )
         _props[_propsCount++] = elementProps;

      return createStyleSheet();
   }

   public Map createStylePropsForClass( String clazz )
   {
      return _clazzTable.create( clazz );
   }

   public Map createStylePropsForElement( String element )
   {
      return _elementTable.create( element );
   }

   public Map createStylePropsForId( String id )
   {
      return _idTable.create( id );
   }

   private StyleSheet createStyleSheet()
   {
      if( _propsCount == 0 )
         return EMPTY_STYLE_SHEET;

      StyleSheet next = null;

      // reverse loop
      for( int i = _propsCount - 1; i >= 0; i-- )
      {
         if( next == null )
            // last entry?
            next = new StyleSheet( _props[i] );
         else
            next = new StyleSheet( _props[i], next );
      }

      return next;
   }
}