// Sample code file: SplashScreen.java

// Warning: This code has been marked up for HTML
/***************************************************************************
$name: SplashScreen.java 
$version: 1.0 
$date_modified: 210104
$description:  
$owner: Beans for Novell Services Team 
Copyright (c) 1998 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/

//package com.novell.application.console.shell;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

/**
* Simple splash screen.  It is created in a static initializer inside the
* console class and stays up for the given amount of seconds passed in
* on construction.
*/
class SplashScreen extends Window
{
   private static final int   m_iconsHeight = 30;
   private static final int   m_iconsWidth = 30;

   private int                m_splashWidth;
   private int                m_splashHeight;
   private ImageIcon          m_icon;                                   // splash image
   private ImageIcon[]        m_icons = new ImageIcon[100];             // product icons
   private JLabel             m_splashLabel;
   private JPanel             m_iconsPanel;
   private boolean            m_bKilled = false;
   private int                m_iconsIdx = 0;                           // number of product icons
   private boolean            m_bLayout = false;
   private boolean            m_bWaiting = false;
   
SplashScreen( Frame frame, Image image )
   {
      super( (null==frame) ? new Frame() : frame );
      try
      {
         setLayout( new BorderLayout() );
         changeSplashImage( new ImageIcon( image ) );
      }
      catch (Exception e)
      {
         System.out.println("Exception in SplashScreen constructor - " + e);
         // don't try to do anything else if there was a problem.
         return;
      }
      catch(Error e)
      {
         System.out.println("Error in SplashScreen constructor - " + e);
         // don't try to do anything else if there was a problem.
         return;
      }

      // interrupt the thread if the mouse is clicked.
      MouseAdapter ma = new MouseAdapter()
      {
         public void mousePressed( MouseEvent me )
         {
            kill();
         }
      };
      
      //addMouseListener( ma );
      
      addKeyListener(new KeyAdapter()
         {
            public void keyPressed(KeyEvent e)
            {
               if(e.getKeyCode() == e.VK_SPACE)
               {
                  kill();
               }
            }
         });
      
      // change the cursor to the wait cursor
      startWaitCursor();
   }

   /**
   * Only constructor for splash screen.
   *
   * @param frame the frame for this window, if available
   * @param image the image to display
   */
   SplashScreen( Frame frame, ImageIcon image )
   {
      super( (null==frame) ? new Frame() : frame );
      try
      {
         setLayout( new BorderLayout() );
         changeSplashImage(  image  );
      }
      catch (Exception e)
      {
         System.out.println("Exception in SplashScreen constructor - " + e);
         // don't try to do anything else if there was a problem.
         return;
      }
      catch(Error e)
      {
         System.out.println("Error in SplashScreen constructor - " + e);
         // don't try to do anything else if there was a problem.
         return;
      }

      // interrupt the thread if the mouse is clicked.
      MouseAdapter ma = new MouseAdapter()
      {
         public void mousePressed( MouseEvent me )
         {
            kill();
         }
      };
      
      //addMouseListener( ma );
      
      addKeyListener(new KeyAdapter()
         {
            public void keyPressed(KeyEvent e)
            {
               if(e.getKeyCode() == e.VK_SPACE)
               {
                  kill();
               }
            }
         });
      
      // change the cursor to the wait cursor
      startWaitCursor();
   }
   
   public void setVisible(final boolean visible)
   {
      Runnable runner = new Runnable()
         {
            public void run()
            {
               superSetVisible(visible);
            }
         };
      
      Utilities.runOnEventThreadLater(runner);
   }
   
   private void superSetVisible(boolean visible)
   {
      super.setVisible(visible);
   }
   
   // Sets the dialog cursor to a wait cursor.
   private void startWaitCursor()
   {
      if(!m_bWaiting)
      {
         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
         m_bWaiting = true;
      }
   }

   // Sets the cursor back to normal from a wait cursor.
   private void endWaitCursor()
   {
      if(m_bWaiting)
      {
         m_bWaiting = false;
         setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      }
   }

   /**
   * Set the splash screen background, icons panel and redisplay window.
   */
   synchronized public void changeSplashImage(final ImageIcon icon)
   {
      Runnable runner = new Runnable()
         {
            public void run()
            {
               m_icon = icon;
               m_splashHeight = m_icon.getIconHeight();
               m_splashWidth = m_icon.getIconWidth();

               // set the window size based on the size of splash image
               setSize( m_splashWidth, m_splashHeight+ m_iconsHeight+12);
               //D.out("Splash screen w,h: "+m_icon.getIconWidth()+","+m_icon.getIconHeight());

               // define splashlabel layout
               m_splashLabel = new JLabel(m_icon)
                  {
                     public void paintComponent(Graphics g)
                     {
                        super.paintComponent(g);
                        int tleft = 76;
                        int ttop = 133;
                        Font f = g.getFont();
                        Font newFont = new Font(f.getName(),f.getStyle(),f.getSize()-2);
                        g.setFont(newFont);
//                        g.drawString(Version.getDisplayVersion(), this.getInsets().left+tleft, this.getInsets().top+ttop); 
                     }
                  };

               displaySplash();
            }
         };
      
      Utilities.runOnEventThreadLater(runner);
   }
   
   /**
   * Add text to the bottom of the splash screen.
   */
   synchronized public void addText(final String msg )
   {
      Runnable runner = new Runnable()
         {
            public void run()
            {
               m_iconsPanel.removeAll();
               
               //******************Cygsoft modification starts*********************
            // Company       : Cygsoft Inc.
            // Author      : <Vinod Venugopal>
            // Peer Reviewer   : <Bhavesh Shah>
            // Code change date   : <20 May,2001>
            // Comments      :adding accessibility
            //******************************************************************      

               // clear and setup the icon panel
               JLabel txtLabel = new JLabel(msg);//COMMENTED BY VINOD V
//               JLabel txtLabel = NConeFactory.novellJLabel(new JLabel(msg),msg,"");//ADDED BY VINOD V
               
               //******************Cygsoft modification ends*********************
               
               Dimension d = txtLabel.getPreferredSize();
               d.height = m_iconsHeight;
               txtLabel.setPreferredSize(d);
               m_iconsPanel.add(txtLabel);

               // redisplay icon panel on splash window
               m_iconsPanel.doLayout();
               m_iconsPanel.repaint();
            }
         };
      
//      Utilities.runOnEventThreadLater(runner);   
   }
   
   /**
   * Add an icon to the bottom of the splash screen.
   */
   synchronized public void addIcon(final ImageIcon icon )
   {
      Runnable runner = new Runnable()
         {
            public void run()
            {
               int numIcons = m_splashWidth / m_iconsWidth;

               if (m_iconsIdx < numIcons)
               {
                  //D.out("Adding icon " + icon);
                  m_icons[m_iconsIdx] = icon;
                  m_iconsIdx++;
               }
               else
               {
                  // move everything down one and then insert it
                  for (int i=0; i<numIcons-1; i++)
                  {
                     m_icons[i] = m_icons[i+1];
                  }
                  m_icons[numIcons-1] = icon;
                  m_bLayout = true;
               }


            }
         };
      
      Utilities.runOnEventThreadLater(runner);   
   }



   /**
   * Setup the window layout and display it.
   */
   synchronized private void displaySplash()
   {
      removeAll();      // remove all containers from window

      // add mainLabel to window
      add(m_splashLabel, BorderLayout.CENTER);
      pack();

      
      // Center the dialog over the window
      Utilities.centerWindow( this );
   }


  /**
   * Kill the splash screen.
   */
   synchronized public void kill()
   {
      Runnable runner = new Runnable()
         {
            public void run()
            {
               if (!m_bKilled)
               {  // If the splashScreen has not been killed before
                  dispose();
                  m_bKilled = true;
                  
                  // set the cursor back to the default cursor
                  endWaitCursor();
               }
            }
         };
      
      Utilities.runOnEventThreadLater(runner);
   }

};

/*
$Log: SplashScreen.java,v $
Revision 1.3  2001/05/24 22:37:40  vinod
Added accessibility to some components left,
placed constanst in ResourceBundle

Revision 1.2  2001/05/20 12:37:08  vinod
added accessibility

Revision 1.1.1.1  2001/05/09 12:43:15  rommel
Sandbox initial version uploaded from novell cd 1/3

Revision 1.27  2000/03/06 20:42:22  DChamberlain
Fixed compiler warnings.
Revision 1.26  2000/03/03 20:02:31Z  DChamberlain
Changed deprecated calls.
Revision 1.25  2000/01/04 17:19:30Z  DChamberlain
Fix for Bug# 217866.  Wait for refresh, insert, delete, etc. to complete before returning.
Revision 1.24  1999/10/28 16:25:54  DChamberlain
Make sure all interactions are on event thread.
Revision 1.23  1999/08/18 18:58:46  DChamberlain
No longer close when mouse is clicked.  Defect# 69952.
Revision 1.22  1999/04/08 00:12:33  mharris
Revision 1.21  1999/04/07 20:25:02  mharris
Display verion on splash screen.
Revision 1.20  1999/03/18 17:30:39  mharris
Changed cursor to wait cursor per SPD 201141.
Revision 1.19  1999/01/07 19:27:41  DChamberlain
Updated copyright to include 1999.
Revision 1.18  1998/11/05 00:39:33  mharris
Changed splash area to white w/ border.  Compute max number of icons on bottom.
Revision 1.17  1998/10/06 16:08:00  DChamberlain
Renamed com.sun.java.swing to javax.swing since we moved to Swing 11b3.
Revision 1.16  1998/09/29 16:57:35  DChamberlain
Moving constants from headers into code for simplicity.
Revision 1.15  1998/09/29 15:44:39  DChamberlain
Moved items that will not be translated from resources to constants.
Revision 1.14  1998/08/13 21:22:51  mharris
Revision 1.13  1998/08/06 21:36:01  mharris
Revision 1.12  1998/07/28 22:08:17  mharris
Revision 1.11  1998/07/02 18:09:38  mharris
Revision 1.10  1998/06/26 23:20:34  mharris
Revision 1.9  1998/04/29 23:13:54  SJENSEN
Allow the SplashScreen to stay up intil the kill() routine is called. the caller must pass SPLASHSCREEN_DURATION_INFINITE as the number of seconds to display splash. (SPD 190881)
Revision 1.8  1998/04/08 23:07:15  DChamberlain
Changed visibility in preparation for obfuscation.
Revision 1.7  1998/04/02 00:32:26  DChamberlain
Catch errors on startup and ignore.  Seems to kind of fix SPD# 183395.
Revision 1.6  1998/04/01 03:27:30  DChamberlain
Cleaned up error reporting.
Revision 1.5  1998/02/13 17:06:59  DChamberlain
New copyright.  Make sure file had log.  Wrap calls to snapins in try blocks.
Revision 1.4  1998/01/23 17:50:32  DChamberlain
Close splash screen on startup.
Revision 1.3  1998/01/12 18:32:43  DChamberlain
Moved from ncc package to console and from ncc.console package to console.shell.
Revision 1.2  1998/01/07 17:13:16  DChamberlain
Brought up to coding standards.
*/