/*
** Salsa - Swing Add-On Suite
** Copyright (c) 2001, 2002, 2003 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 salsa.util;

import java.awt.*;
import java.util.prefs.*;
import houston.*;

public class FrameUtils
{
   static Logger T = Logger.getLogger( FrameUtils.class );

   public static void restoreBounds( Frame f, int defaultX, int defaultY, int defaultWidth, int defaultHeight, int defaultState )
   {
      Preferences prefs = Preferences.userNodeForPackage( f.getClass() );

      int x = prefs.getInt( "x", defaultX );
      int y = prefs.getInt( "y", defaultY );
      int width = prefs.getInt( "width", defaultWidth );
      int height = prefs.getInt( "height", defaultHeight );
      int state = prefs.getInt( "state", defaultState );

      f.setLocation( x, y );
      f.setSize( width, height );
      f.setExtendedState( state );
   }

   /**
    *  convenience method; use two thirds of screen size (centered) for frame
    *  defaults
    */
   public static void restoreBounds( Frame f )
   {
      Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

      // use two thirds of screen size (that is, 0.66)
      int defaultWidth = ( int ) ( screen.width * 0.66 );
      int defaultHeight = ( int ) ( screen.height * 0.66 );

      // center frame on screen
      int defaultX = ( screen.width - defaultWidth ) / 2;
      int defaultY = ( screen.height - defaultHeight ) / 2;

      restoreBounds( f, defaultX, defaultY, defaultWidth, defaultHeight, Frame.NORMAL );
   }

   public static void saveBounds( Frame f )
   {
      // fix: use your own extends JFrame class
      //  that keeps track of the pre-maximized bounds
      //  - using maximized bounds when maximized is unintuitive
      //    as restore no longer works

      T.debug( "frame=" + f.getClass().getName() );

      int state = f.getExtendedState();
      if( ( state & Frame.ICONIFIED ) == Frame.ICONIFIED )
      {
         T.debug( "frame is iconified" );
      }
      else if( ( state & Frame.MAXIMIZED_BOTH ) == Frame.MAXIMIZED_BOTH )
      {
         T.debug( "frame is maximized" );
      }
      else
      {
         // assume NORMAL state

         T.debug( "frame is normal" );
      }

      Rectangle rect = f.getBounds();
      T.debug( "x,y,width,height=" + rect.x + "," + rect.y + "," + rect.width + "," + rect.height );

      Preferences prefs = Preferences.userNodeForPackage( f.getClass() );

      // todo: should i add the class name or an id before the attribute name?
      //  or is the package name sufficent?
      prefs.putInt( "x", rect.x );
      prefs.putInt( "y", rect.y );
      prefs.putInt( "width", rect.width );
      prefs.putInt( "height", rect.height );
      prefs.putInt( "state", state );
   }
}