001/* 002 * $Id: WindowUtils.java 4084 2011-11-15 18:53:49Z kschaefe $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 022package org.jdesktop.swingx.util; 023 024import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment; 025 026import java.awt.Component; 027import java.awt.Container; 028import java.awt.GraphicsConfiguration; 029import java.awt.Insets; 030import java.awt.Point; 031import java.awt.Rectangle; 032import java.awt.Toolkit; 033import java.awt.Window; 034import java.util.ArrayList; 035import java.util.List; 036 037import javax.swing.JDialog; 038import javax.swing.JInternalFrame; 039import javax.swing.JOptionPane; 040import javax.swing.SwingUtilities; 041 042/** 043 * Encapsulates various utilities for windows (ie: <code>Frame</code> and 044 * <code>Dialog</code> objects and descendants, in particular). 045 * 046 * @author Richard Bair 047 */ 048public final class WindowUtils { 049 /** 050 * Hide the constructor - don't wan't anybody creating an instance of this 051 */ 052 private WindowUtils() { 053 } 054 055 private static GraphicsConfiguration getDefaultGraphicsConfiguration() { 056 return getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 057 } 058 059 private static boolean isUnowned(Window window) { 060 return window.getOwner() == null || (window instanceof JDialog && JOptionPane.getRootFrame().equals(window.getOwner())); 061 } 062 063 private static Rectangle getUsableDeviceBounds(GraphicsConfiguration gc) { 064 Rectangle bounds = gc.getBounds(); 065 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 066 067 bounds.x += insets.left; 068 bounds.y += insets.top; 069 bounds.width -= (insets.left + insets.right); 070 bounds.height -= (insets.top + insets.bottom); 071 072 return bounds; 073 } 074 075 /** 076 * <p> 077 * Returns the <code>Point</code> at which a window should be placed to 078 * center that window on the screen. 079 * </p> 080 * <p> 081 * Some thought was taken as to whether to implement a method such as this, 082 * or to simply make a method that, given a window, will center it. It was 083 * decided that it is better to not alter an object within a method. 084 * </p> 085 * 086 * @param window The window to calculate the center point for. This object 087 * can not be null. 088 * 089 * @return the <code>Point</code> at which the window should be placed to 090 * center that window on the screen. 091 */ 092 public static Point getPointForCentering(Window window) { 093 Window w = window.isShowing() || isUnowned(window) ? window : window.getOwner(); 094 GraphicsConfiguration gc = w.getGraphicsConfiguration(); 095 096 Rectangle usableBounds = getUsableDeviceBounds(gc); 097 int screenWidth = usableBounds.width; 098 int screenHeight = usableBounds.height; 099 int width = window.getWidth(); 100 int height = window.getHeight(); 101 102 return new Point(((screenWidth - width) / 2) + usableBounds.x, 103 ((screenHeight - height) / 2) + usableBounds.y); 104 } 105 106 /** 107 * <p/> 108 * Returns the <code>Point</code> at which a window should be placed to 109 * center that window on the given desktop. 110 * </p> 111 * <p/> 112 * Some thought was taken as to whether to implement a method such as this, 113 * or to simply make a method that, given a window, will center it. It was 114 * decided that it is better to not alter an object within a method. 115 * </p> 116 * 117 * @param window The window (JInternalFrame) to calculate the center point 118 * for. This object can not be null. 119 * 120 * @return the <code>Point</code> at which the window should be placed to 121 * center that window on the given desktop 122 */ 123 public static Point getPointForCentering(JInternalFrame window) { 124 Window w = SwingUtilities.getWindowAncestor(window); 125 GraphicsConfiguration gc = w == null ? getDefaultGraphicsConfiguration() 126 : w.getGraphicsConfiguration(); 127 128 Rectangle usableBounds = getUsableDeviceBounds(gc); 129 int screenWidth = usableBounds.width; 130 int screenHeight = usableBounds.height; 131 int width = window.getWidth(); 132 int height = window.getHeight(); 133 134 return new Point(((screenWidth - width) / 2) + usableBounds.x, 135 ((screenHeight - height) / 2) + usableBounds.y); 136 } 137 138 /** 139 * <p/> 140 * Returns the <code>Point</code> at which a window should be placed in 141 * order to be staggered slightly from another "origin" window to 142 * ensure that the title areas of both windows remain visible to the user. 143 * </p> 144 * 145 * @param originWindow Window from which the staggered location will be calculated 146 * 147 * @return location staggered from the upper left location of the origin 148 * window 149 */ 150 public static Point getPointForStaggering(Window originWindow) { 151 Point origin = originWindow.getLocation(); 152 Insets insets = originWindow.getInsets(); 153 origin.x += insets.top; 154 origin.y += insets.top; 155 return origin; 156 } 157 158 public static Window findWindow(Component c) { 159 if (c == null) { 160 return JOptionPane.getRootFrame(); 161 } else if (c instanceof Window) { 162 return (Window) c; 163 } else { 164 return findWindow(c.getParent()); 165 } 166 } 167 168 public static List<Component> getAllComponents(final Container c) { 169 Component[] comps = c.getComponents(); 170 List<Component> compList = new ArrayList<Component>(); 171 for (Component comp : comps) { 172 compList.add(comp); 173 if (comp instanceof Container) { 174 compList.addAll(getAllComponents((Container) comp)); 175 } 176 } 177 return compList; 178 } 179}