package jdraw.ui;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 *
 * A class for miscellaneous helper functions.
 *
 */
public final class Misc
{
  /************************************* private constructor *************************************/

  /**
   * This class is not intended to be instancied.
   */
  private Misc()
  {
  }


  /************************************ public static methods ************************************/

  /**
   * Buils a cursor from the specified file name.
   *
   * @param   file_name           the image file name
   * @param   hot_spot            the hot spot position
   * @param   accessible_name     the accessible string
   */
  public static Cursor buildCursor( final String  file_name,
                                    final Point   hot_spot,
                                    final String  accessible_name )
  {
    try
    {
      final Toolkit          toolkit= Toolkit.getDefaultToolkit();
      final java.net.URL     url= Misc.class.getResource( file_name );

      if (url == null)
      {
        /***** image loading failed *****/
        javax.swing.JOptionPane.showMessageDialog( null,
                                                   "Could not load cursor image (" + file_name + ").",
                                                   "JDraw Fatal error",
                                                   javax.swing.JOptionPane.ERROR_MESSAGE );
        System.exit( 69 );
      }

      final java.awt.Image   image= toolkit.getImage( url );

      return toolkit.createCustomCursor( image, hot_spot, accessible_name );
    }
    catch (java.security.AccessControlException ex)
    {
      /***** access to cursor file denied *****/
      javax.swing.JOptionPane.showMessageDialog( null,
                                                 "Fatal error",
                                                 "Access to cursor file " + file_name + " denied.",
                                                 javax.swing.JOptionPane.ERROR_MESSAGE );
      System.exit( 69 );
      return null;      // in order to avoid compiler error
    }
  }

  /**
   * Buils an image icon from the specified file name.
   *
   * @param   file_name           the image file name
   */
  public static ImageIcon buildImageIcon( final String  file_name )
  {
    try
    {
      final Toolkit          toolkit= Toolkit.getDefaultToolkit();
      final java.net.URL     url= Misc.class.getResource( file_name );

      if (url == null)
      {
        /***** image loading failed *****/
        javax.swing.JOptionPane.showMessageDialog( null,
                                                   "Could not load image (" + file_name + ").",
                                                   "JDraw Fatal error",
                                                   javax.swing.JOptionPane.ERROR_MESSAGE );
        System.exit( 69 );
      }

      return new ImageIcon( url );
    }
    catch (java.security.AccessControlException ex)
    {
      /***** access to cursor file denied *****/
      javax.swing.JOptionPane.showMessageDialog( null,
                                                 "Fatal error",
                                                 "Access to cursor file " + file_name + " denied.",
                                                 javax.swing.JOptionPane.ERROR_MESSAGE );
      System.exit( 69 );
      return null;      // in order to avoid compiler error
    }
  }

  /**
   * Centers a window on the screen.
   */
  public static final void centerWindow( final Window  window )
  {
    final Dimension     window_dimension= window.getSize();
    final Dimension     screen_dimension= java.awt.Toolkit.getDefaultToolkit().getScreenSize();

    window.setLocation( (screen_dimension.width - window_dimension.width) / 2,
                        (screen_dimension.height - window_dimension.height) / 2 );
  }

  /**
   * Sets the specified look and feel for the specified component tree.
   */
  public static void setLookAndFeel( final Component  component,
                                     final String     lf_class_name )
  {
    try
    {
      UIManager.setLookAndFeel( lf_class_name );
      SwingUtilities.updateComponentTreeUI( component );
    }
    catch ( final Exception  e1 )
    {
      System.err.println( "Unsupported look and feel: " + lf_class_name );
      try
      {
        UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
      }
      catch ( final Exception  e2 )
      {
        System.err.println( "Failed to set cross-platform look and feel !" );
      }
    }
  }
}

