/*
** 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.config;

import java.awt.Font;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import luxor.core.*;

public class XulDefaults
{

   private static XulDefaults _instance;
   private ImageIcon _errorIcon;

   private Properties _props;

   private XulDefaults()
   {
      Properties defaultProps = new Properties();

      defaultProps.setProperty( Key.TOOLBAR_BUTTON_FONT, "sansserif-plain-11" );
      defaultProps.setProperty( Key.TOOLBAR_BUTTON_HEIGHT_PREF, "44" );
      defaultProps.setProperty( Key.TOOLBAR_BUTTON_HEIGHT_MIN, "44" );
      defaultProps.setProperty( Key.TOOLBAR_BUTTON_WIDTH_PREF, "76" );
      defaultProps.setProperty( Key.TOOLBAR_BUTTON_WIDTH_MIN, "76" );

      _props = new Properties( defaultProps );

      ClassLoader cl = XulDefaults.class.getClassLoader();

      try
      {
         InputStream in = cl.getResourceAsStream( Constants.LUXOR_PROPERTIES );
         if( in == null )
            Xul.error( "failed to find properties " + Constants.LUXOR_PROPERTIES );
         else
            _props.load( in );
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to load properties " + Constants.LUXOR_PROPERTIES + ": " + ioex.toString() );
      }

      URL errorIconUrl = cl.getResource( Constants.ERROR_ICON );
      if( errorIconUrl == null )
         Xul.error( "failed to find icon " + Constants.ERROR_ICON );
      else
         _errorIcon = new ImageIcon( errorIconUrl );
   }

   public static XulDefaults getXulDefaults()
   {
      if( _instance == null )
      {
         _instance = new XulDefaults();
      }
      return _instance;
   }

   public ImageIcon getErrorIcon()
   {
      return _errorIcon;
   }

   public Font getFont( String key )
   {
      String value = _props.getProperty( key );

      if( value == null )
         Xul.warning( "property " + key + " not found in " + Constants.LUXOR_PROPERTIES );

      // note: passing in a null string to Font.decode
      //    results in Dialog, 12, PLAIN

      return Font.decode( value );
   }

   public int getInt( String key, int defaultInt )
   {
      String value = _props.getProperty( key );
      if( value == null )
      {
         Xul.warning( "property " + key + " + not found in " + Constants.LUXOR_PROPERTIES );
         return defaultInt;
      }

      try
      {
         return Integer.parseInt( value );
      }
      catch( NumberFormatException nex )
      {
         Xul.warning( "property " + key + " value " + value + " is not a number: " + nex.toString() );
         return defaultInt;
      }
   }

   public static class Key
   {
      public final static String TOOLBAR_BUTTON_FONT = "toolbar.button.font";
      public final static String TOOLBAR_BUTTON_HEIGHT_MIN = "toolbar.button.height.min";
      public final static String TOOLBAR_BUTTON_HEIGHT_PREF = "toolbar.button.height.pref";
      public final static String TOOLBAR_BUTTON_WIDTH_MIN = "toolbar.button.width.min";
      public final static String TOOLBAR_BUTTON_WIDTH_PREF = "toolbar.button.width.pref";
   }
}