WindowUtils.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:11k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: WindowUtils.java,v 1.5 2005/10/10 18:03:00 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.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.util;
  22. import java.awt.Component;
  23. import java.awt.GraphicsDevice;
  24. import java.awt.GraphicsEnvironment;
  25. import java.awt.GridBagConstraints;
  26. import java.awt.Insets;
  27. import java.awt.MouseInfo;
  28. import java.awt.Point;
  29. import java.awt.Rectangle;
  30. import java.awt.Window;
  31. import java.awt.event.ComponentAdapter;
  32. import java.awt.event.ComponentListener;
  33. import javax.swing.JComponent;
  34. import javax.swing.JDesktopPane;
  35. import javax.swing.JDialog;
  36. import javax.swing.JFrame;
  37. import javax.swing.JInternalFrame;
  38. import javax.swing.RootPaneContainer;
  39. import javax.swing.SwingUtilities;
  40. /**
  41.  * Encapsulates various utilities for windows (ie: <code>Frame</code> and
  42.  * <code>Dialog</code> objects and descendants, in particular).
  43.  * @author Richard Bair
  44.  */
  45. public final class WindowUtils {
  46. /**
  47.  * Hide the constructor - don't wan't anybody creating an instance of this
  48.  */
  49. private WindowUtils() {
  50. }
  51. /**
  52.  * <p>
  53.  * Returns the <code>Point</code> at which a window should be placed to
  54.  * center that window on the screen.
  55.  * </p>
  56.  * <p>
  57.  * Some thought was taken as to whether to implement a method such as this,
  58.  * or to simply make a method that, given a window, will center it.  It was
  59.  * decided that it is better to not alter an object within a method.
  60.  * </p>
  61.  * @param window The window to calculate the center point for.  This object
  62.  * can not be null.
  63.  * @return the <code>Point</code> at which the window should be placed to
  64.  * center that window on the screen.
  65.  */
  66. public static Point getPointForCentering(Window window) {
  67. //assert window != null;
  68.         try {
  69.             Point mousePoint = MouseInfo.getPointerInfo().getLocation();
  70.             GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  71.             for (GraphicsDevice device : devices) {
  72.                 Rectangle bounds = device.getDefaultConfiguration().getBounds();
  73.                 //check to see if the mouse cursor is within these bounds
  74.                 if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y
  75.                         && mousePoint.x <= (bounds.x + bounds.width)
  76.                         && mousePoint.y <= (bounds.y + bounds.height)) {
  77.                     //this is it
  78.                     int screenWidth = bounds.width;
  79.                     int screenHeight = bounds.height;
  80.                     int width = window.getWidth();
  81.                     int height = window.getHeight();
  82.                     Point p = new Point(((screenWidth - width) / 2) + bounds.x, ((screenHeight - height) / 2) + bounds.y);
  83.                     return p;
  84.                 }
  85.             }
  86.         } catch (Exception e) {
  87.             //this can occur do to a Security exception in sandboxed apps
  88.             e.printStackTrace();
  89.         }
  90. return new Point(0,0);
  91. }
  92. /**
  93.  * <p>
  94.  * Returns the <code>Point</code> at which a window should be placed to
  95.  * center that window on the given desktop.
  96.  * </p>
  97.  * <p>
  98.  * Some thought was taken as to whether to implement a method such as this,
  99.  * or to simply make a method that, given a window, will center it.  It was
  100.  * decided that it is better to not alter an object within a method.
  101.  * </p>
  102.  * @param window The window (JInternalFrame) to calculate the center point
  103.  * for.  This object can not be null.
  104.  * @param desktop The JDesktopPane that houses this window.
  105.  * @return the <code>Point</code> at which the window should be placed to
  106.  * center that window on the given desktop
  107.  */
  108. public static Point getPointForCentering(JInternalFrame window, JDesktopPane desktop) {
  109.         try {
  110.             //assert window != null;
  111.             Point mousePoint = MouseInfo.getPointerInfo().getLocation();
  112.             GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  113.             for (GraphicsDevice device : devices) {
  114.                 Rectangle bounds = device.getDefaultConfiguration().getBounds();
  115.                 //check to see if the mouse cursor is within these bounds
  116.                 if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y
  117.                         && mousePoint.x <= (bounds.x + bounds.width)
  118.                         && mousePoint.y <= (bounds.y + bounds.height)) {
  119.                     //this is it
  120.                     int screenWidth = bounds.width;
  121.                     int screenHeight = bounds.height;
  122.                     int width = window.getWidth();
  123.                     int height = window.getHeight();
  124.                     Point p = new Point(((screenWidth - width) / 2) + bounds.x, ((screenHeight - height) / 2) + bounds.y);
  125.                     return p;
  126.                 }
  127.             }
  128.         } catch (Exception e) {
  129.             //this can occur do to a Security exception in sandboxed apps
  130.             e.printStackTrace();
  131.         }
  132. return new Point(0,0);
  133. }
  134.  
  135.         /**
  136.          *<p>
  137.  * Returns the <code>Point</code> at which a window should be placed in
  138.          * order to be staggered slightly from another &quot;origin&quot; window to 
  139.          * ensure that the title areas of both windows remain visible to the user.
  140.  * </p> 
  141.          * @param originWindow Window from which the staggered location will be calculated
  142.          * @return location staggered from the upper left location of the origin
  143.          *         window
  144.          */ 
  145.         public static Point getPointForStaggering(Window originWindow) {
  146.             Point origin = originWindow.getLocation();
  147.             Insets insets = originWindow.getInsets();
  148.             origin.x += insets.top;
  149.             origin.y += insets.top;
  150.             return origin;
  151.         }
  152. /**
  153.  * Utility method used to load a GridBagConstraints object (param gbc) with the
  154.  * data in the other parameters.  This method saves code space over doing the
  155.  * assignments by hand, and also allows you to reuse the same GridBagConstraints
  156.  * object reducing temporary object creating (at the expense of a method call.
  157.  * Go figure).
  158.  */
  159. public static void setConstraints(GridBagConstraints gbc, int gridx, int gridy, int gridwidth, int gridheight,
  160.  double weightx, double weighty, int anchor, int fill, int top, int left, int bottom, int right) {
  161. gbc.gridx = gridx;
  162. gbc.gridy = gridy;
  163. gbc.gridwidth = gridwidth;
  164. gbc.gridheight = gridheight;
  165. gbc.weightx = weightx;
  166. gbc.weighty = weighty;
  167. gbc.anchor = anchor;
  168. gbc.fill = fill;
  169. gbc.insets = new Insets(top, left, bottom, right);
  170. }
  171. /**
  172.  * Get a <code>Spatial</code> object representing the given window's position and
  173.  * magnitude in space.
  174.  * @param win The window to get a Spatial object for
  175.  * @return a Spatial object.  @see com.jgui.Spatial
  176.  */
  177. public static Spatial getSpatial(Window win) {
  178. Spatial spatial = new Spatial(win.getY(), win.getX(), win.getWidth(), win.getHeight());
  179. return spatial;
  180. }
  181. /**
  182.  * Get a <code>Spatial</code> object representing the given JComponent's position and
  183.  * magnitude in space.
  184.  * @param comp The JComponent to get a Spatial object for
  185.  * @return a Spatial object.  @see com.jgui.Spatial
  186.  */
  187. public static Spatial getSpatial(JComponent comp) {
  188. Spatial spatial = new Spatial(comp.getY(), comp.getX(), comp.getWidth(), comp.getHeight());
  189. return spatial;
  190. }
  191. /**
  192.  * Locates the RootPaneContainer for the given component
  193.  * @param c
  194.  * @return
  195.  */
  196. public static RootPaneContainer findRootPaneContainer(Component c) {
  197. if (c == null) {
  198. return null;
  199. } else if (c instanceof RootPaneContainer) {
  200. return (RootPaneContainer)c;
  201. } else {
  202. return findRootPaneContainer(c.getParent());
  203. }
  204. }
  205. /**
  206.  * Locates the JFrame for the given component
  207.  * @param c
  208.  * @return
  209.  */
  210. public static JFrame findJFrame(Component c) {
  211. if (c == null) {
  212. return null;
  213. } else if (c instanceof RootPaneContainer) {
  214. return (JFrame)c;
  215. } else {
  216. return findJFrame(c.getParent());
  217. }
  218. }
  219. /**
  220.  * Locates the JDialog for the given component
  221.  * @param c
  222.  * @return
  223.  */
  224. public static JDialog findJDialog(Component c) {
  225. if (c == null) {
  226. return null;
  227. } else if (c instanceof JDialog) {
  228. return (JDialog)c;
  229. } else {
  230. return findJDialog(c.getParent());
  231. }
  232. }
  233.     /**
  234.      * 
  235.      * Installs/resets a ComponentListener to resize the
  236.      * given window to minWidth/Height if needed.
  237.      * 
  238.      * @param window
  239.      * @param minWidth
  240.      * @param minHeight
  241.      */
  242.     public static void setMinimumSizeManager(Window window, int minWidth,
  243.             int minHeight) {
  244.         ComponentListener[] listeners = window.getComponentListeners();
  245.         ComponentListener listener = null;
  246.         boolean found = false;
  247.         for (ComponentListener l : listeners) {
  248.             if (l instanceof MinSizeComponentListener) {
  249.                 listener = l;
  250.                 break;
  251.             }
  252.         }
  253.         if (listener == null) {
  254.             window.addComponentListener(new MinSizeComponentListener(
  255.                             window, minWidth, minHeight));
  256.         } else {
  257.             ((MinSizeComponentListener) listener).resetSizes(minWidth,
  258.                     minHeight);
  259.         }
  260.     }
  261.     
  262.     /**
  263.      * Resets window size to minSize if needed.
  264.      * 
  265.      * @author Patrick Wright
  266.      */
  267.     public static class MinSizeComponentListener extends ComponentAdapter {
  268.         private Window window;
  269.         private int minHeight;
  270.         private int minWidth;
  271.         MinSizeComponentListener(Window frame, int minWidth, int minHeight) {
  272.             this.window = frame;
  273.             resetSizes(minWidth, minHeight);
  274.         }
  275.         public void resetSizes(int minWidth, int minHeight) {
  276.             this.minWidth = minWidth;
  277.             this.minHeight = minHeight;
  278.             adjustIfNeeded(window);
  279.         }
  280.         public void componentResized(java.awt.event.ComponentEvent evt) {
  281.             adjustIfNeeded((Window) evt.getComponent());
  282.         }
  283.         private void adjustIfNeeded(final Window window) {
  284.             boolean doSize = false;
  285.             int newWidth = window.getWidth();
  286.             int newHeight = window.getHeight();
  287.             if (newWidth < minWidth) {
  288.                 newWidth = minWidth;
  289.                 doSize = true;
  290.             }
  291.             if (newHeight < minHeight) {
  292.                 newHeight = minHeight;
  293.                 doSize = true;
  294.             }
  295.             if (doSize) {
  296.                 final int w = newWidth;
  297.                 final int h = newHeight;
  298.                 SwingUtilities.invokeLater(new Runnable() {
  299.                     public void run() {
  300.                         window.setSize(w, h);
  301.                     }
  302.                 });
  303.             }
  304.         }
  305.     }
  306. }