KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > utils > WindowUtils


1 /*
2  * $Id: WindowUtils.java,v 1.1 2005/02/24 20:35:25 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.swing.utils;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.GraphicsDevice JavaDoc;
11 import java.awt.GraphicsEnvironment JavaDoc;
12 import java.awt.GridBagConstraints JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.MouseInfo JavaDoc;
15 import java.awt.Point JavaDoc;
16 import java.awt.Rectangle JavaDoc;
17 import java.awt.Window JavaDoc;
18
19 import javax.swing.JComponent JavaDoc;
20 import javax.swing.JDesktopPane JavaDoc;
21 import javax.swing.JDialog JavaDoc;
22 import javax.swing.JFrame JavaDoc;
23 import javax.swing.JInternalFrame JavaDoc;
24 import javax.swing.RootPaneContainer JavaDoc;
25 import org.jdesktop.swing.utils.Spatial;
26
27 /**
28  * Encapsulates various utilities for windows (ie: <code>Frame</code> and
29  * <code>Dialog</code> objects and descendants, in particular).
30  * @author Richard Bair
31  */

32 public final class WindowUtils {
33     
34     /**
35      * Hide the constructor - don't wan't anybody creating an instance of this
36      */

37     private WindowUtils() {
38     }
39     
40     /**
41      * <p>
42      * Returns the <code>Point</code> at which a window should be placed to
43      * center that window on the screen.
44      * </p>
45      * <p>
46      * Some thought was taken as to whether to implement a method such as this,
47      * or to simply make a method that, given a window, will center it. It was
48      * decided that it is better to not alter an object within a method.
49      * </p>
50      * @param window The window to calculate the center point for. This object
51      * can not be null.
52      * @return the <code>Point</code> at which the window should be placed to
53      * center that window on the screen.
54      */

55     public static Point JavaDoc getPointForCentering(Window JavaDoc window) {
56         //assert window != null;
57
Point JavaDoc mousePoint = MouseInfo.getPointerInfo().getLocation();
58         GraphicsDevice JavaDoc[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
59         for (GraphicsDevice JavaDoc device : devices) {
60             Rectangle JavaDoc bounds = device.getDefaultConfiguration().getBounds();
61             //check to see if the mouse cursor is within these bounds
62
if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y
63                     && mousePoint.x <= (bounds.x + bounds.width)
64                     && mousePoint.y <= (bounds.y + bounds.height)) {
65                 //this is it
66
int screenWidth = bounds.width;
67                 int screenHeight = bounds.height;
68                 int width = window.getWidth();
69                 int height = window.getHeight();
70                 Point JavaDoc p = new Point JavaDoc(((screenWidth - width) / 2) + bounds.x, ((screenHeight - height) / 2) + bounds.y);
71                 return p;
72             }
73         }
74         return new Point JavaDoc(0,0);
75     }
76     
77     /**
78      * <p>
79      * Returns the <code>Point</code> at which a window should be placed to
80      * center that window on the given desktop.
81      * </p>
82      * <p>
83      * Some thought was taken as to whether to implement a method such as this,
84      * or to simply make a method that, given a window, will center it. It was
85      * decided that it is better to not alter an object within a method.
86      * </p>
87      * @param window The window (JInternalFrame) to calculate the center point
88      * for. This object can not be null.
89      * @param desktop The JDesktopPane that houses this window.
90      * @return the <code>Point</code> at which the window should be placed to
91      * center that window on the given desktop
92      */

93     public static Point JavaDoc getPointForCentering(JInternalFrame JavaDoc window, JDesktopPane JavaDoc desktop) {
94         //assert window != null;
95
Point JavaDoc mousePoint = MouseInfo.getPointerInfo().getLocation();
96         GraphicsDevice JavaDoc[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
97         for (GraphicsDevice JavaDoc device : devices) {
98             Rectangle JavaDoc bounds = device.getDefaultConfiguration().getBounds();
99             //check to see if the mouse cursor is within these bounds
100
if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y
101                     && mousePoint.x <= (bounds.x + bounds.width)
102                     && mousePoint.y <= (bounds.y + bounds.height)) {
103                 //this is it
104
int screenWidth = bounds.width;
105                 int screenHeight = bounds.height;
106                 int width = window.getWidth();
107                 int height = window.getHeight();
108                 Point JavaDoc p = new Point JavaDoc(((screenWidth - width) / 2) + bounds.x, ((screenHeight - height) / 2) + bounds.y);
109                 return p;
110             }
111         }
112         return new Point JavaDoc(0,0);
113     }
114
115     /**
116      * Utility method used to load a GridBagConstraints object (param gbc) with the
117      * data in the other parameters. This method saves code space over doing the
118      * assignments by hand, and also allows you to reuse the same GridBagConstraints
119      * object reducing temporary object creating (at the expense of a method call.
120      * Go figure).
121      */

122     public static void setConstraints(GridBagConstraints JavaDoc gbc, int gridx, int gridy, int gridwidth, int gridheight,
123      double weightx, double weighty, int anchor, int fill, int top, int left, int bottom, int right) {
124         gbc.gridx = gridx;
125         gbc.gridy = gridy;
126         gbc.gridwidth = gridwidth;
127         gbc.gridheight = gridheight;
128         gbc.weightx = weightx;
129         gbc.weighty = weighty;
130         gbc.anchor = anchor;
131         gbc.fill = fill;
132         gbc.insets = new Insets JavaDoc(top, left, bottom, right);
133     }
134     
135     /**
136      * Get a <code>Spatial</code> object representing the given window's position and
137      * magnitude in space.
138      * @param win The window to get a Spatial object for
139      * @return a Spatial object. @see com.jgui.Spatial
140      */

141     public static Spatial getSpatial(Window JavaDoc win) {
142         Spatial spatial = new Spatial(win.getY(), win.getX(), win.getWidth(), win.getHeight());
143         return spatial;
144     }
145
146     /**
147      * Get a <code>Spatial</code> object representing the given JComponent's position and
148      * magnitude in space.
149      * @param comp The JComponent to get a Spatial object for
150      * @return a Spatial object. @see com.jgui.Spatial
151      */

152     public static Spatial getSpatial(JComponent JavaDoc comp) {
153         Spatial spatial = new Spatial(comp.getY(), comp.getX(), comp.getWidth(), comp.getHeight());
154         return spatial;
155     }
156     
157     /**
158      * Locates the RootPaneContainer for the given component
159      * @param c
160      * @return
161      */

162     public static RootPaneContainer JavaDoc findRootPaneContainer(Component JavaDoc c) {
163         if (c == null) {
164             return null;
165         } else if (c instanceof RootPaneContainer JavaDoc) {
166             return (RootPaneContainer JavaDoc)c;
167         } else {
168             return findRootPaneContainer(c.getParent());
169         }
170     }
171
172     /**
173      * Locates the JFrame for the given component
174      * @param c
175      * @return
176      */

177     public static JFrame JavaDoc findJFrame(Component JavaDoc c) {
178         if (c == null) {
179             return null;
180         } else if (c instanceof RootPaneContainer JavaDoc) {
181             return (JFrame JavaDoc)c;
182         } else {
183             return findJFrame(c.getParent());
184         }
185     }
186
187     /**
188      * Locates the JDialog for the given component
189      * @param c
190      * @return
191      */

192     public static JDialog JavaDoc findJDialog(Component JavaDoc c) {
193         if (c == null) {
194             return null;
195         } else if (c instanceof JDialog JavaDoc) {
196             return (JDialog JavaDoc)c;
197         } else {
198             return findJDialog(c.getParent());
199         }
200     }
201 }
202
Popular Tags