KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > util > FrameUtils


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package salsa.util;
24
25 import java.awt.*;
26 import java.util.prefs.*;
27 import houston.*;
28
29 public class FrameUtils
30 {
31    static Logger T = Logger.getLogger( FrameUtils.class );
32
33    public static void restoreBounds( Frame f, int defaultX, int defaultY, int defaultWidth, int defaultHeight, int defaultState )
34    {
35       Preferences prefs = Preferences.userNodeForPackage( f.getClass() );
36
37       int x = prefs.getInt( "x", defaultX );
38       int y = prefs.getInt( "y", defaultY );
39       int width = prefs.getInt( "width", defaultWidth );
40       int height = prefs.getInt( "height", defaultHeight );
41       int state = prefs.getInt( "state", defaultState );
42
43       f.setLocation( x, y );
44       f.setSize( width, height );
45       f.setExtendedState( state );
46    }
47
48    /**
49     * convenience method; use two thirds of screen size (centered) for frame
50     * defaults
51     */

52    public static void restoreBounds( Frame f )
53    {
54       Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
55
56       // use two thirds of screen size (that is, 0.66)
57
int defaultWidth = ( int ) ( screen.width * 0.66 );
58       int defaultHeight = ( int ) ( screen.height * 0.66 );
59
60       // center frame on screen
61
int defaultX = ( screen.width - defaultWidth ) / 2;
62       int defaultY = ( screen.height - defaultHeight ) / 2;
63
64       restoreBounds( f, defaultX, defaultY, defaultWidth, defaultHeight, Frame.NORMAL );
65    }
66
67    public static void saveBounds( Frame f )
68    {
69       // fix: use your own extends JFrame class
70
// that keeps track of the pre-maximized bounds
71
// - using maximized bounds when maximized is unintuitive
72
// as restore no longer works
73

74       T.debug( "frame=" + f.getClass().getName() );
75
76       int state = f.getExtendedState();
77       if( ( state & Frame.ICONIFIED ) == Frame.ICONIFIED )
78       {
79          T.debug( "frame is iconified" );
80       }
81       else if( ( state & Frame.MAXIMIZED_BOTH ) == Frame.MAXIMIZED_BOTH )
82       {
83          T.debug( "frame is maximized" );
84       }
85       else
86       {
87          // assume NORMAL state
88

89          T.debug( "frame is normal" );
90       }
91
92       Rectangle rect = f.getBounds();
93       T.debug( "x,y,width,height=" + rect.x + "," + rect.y + "," + rect.width + "," + rect.height );
94
95       Preferences prefs = Preferences.userNodeForPackage( f.getClass() );
96
97       // todo: should i add the class name or an id before the attribute name?
98
// or is the package name sufficent?
99
prefs.putInt( "x", rect.x );
100       prefs.putInt( "y", rect.y );
101       prefs.putInt( "width", rect.width );
102       prefs.putInt( "height", rect.height );
103       prefs.putInt( "state", state );
104    }
105 }
106
Popular Tags