001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.util; 017 018 import java.awt.Component; 019 import java.awt.Dimension; 020 import java.awt.GraphicsEnvironment; 021 import java.awt.Point; 022 import java.awt.Rectangle; 023 import java.awt.Toolkit; 024 import java.awt.Window; 025 026 /** 027 * Utility functions for manipulating the display of windows. 028 * 029 * @author Keith Donald 030 */ 031 public class WindowUtils { 032 033 private WindowUtils() { 034 } 035 036 /** 037 * Return the system screen size. 038 * 039 * @return The dimension of the system screen size. 040 */ 041 public static Dimension getScreenSize() { 042 return Toolkit.getDefaultToolkit().getScreenSize(); 043 } 044 045 /** 046 * Return the centering point on the screen for the object with the 047 * specified dimension. 048 * 049 * @param dimension the dimension of an object 050 * @return The centering point on the screen for that object. 051 */ 052 public static Point getCenteringPointOnScreen(Dimension dimension) { 053 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 054 if (dimension.width > screen.width) { 055 dimension.width = screen.width; 056 } 057 if (dimension.height > screen.height) { 058 dimension.height = screen.height; 059 } 060 return new Point((screen.width - dimension.width) / 2, (screen.height - dimension.height) / 2); 061 } 062 063 /** 064 * Pack the window, center it on the screen, and set the window visible. 065 * 066 * @param window the window to center and show. 067 */ 068 public static void centerOnScreenAndSetVisible(Window window) { 069 window.pack(); 070 centerOnScreen(window); 071 window.setVisible(true); 072 } 073 074 /** 075 * Take the window and center it on the screen. 076 * <p> 077 * This works around a bug in setLocationRelativeTo(...): it currently does 078 * not take multiple monitors into accounts on all operating systems. 079 * 080 * @param window the window to center 081 */ 082 public static void centerOnScreen(Window window) { 083 Assert.notNull(window, "window cannot be null"); 084 085 // This works around a bug in setLocationRelativeTo(...): it currently 086 // does not take multiple monitors into accounts on all operating 087 // systems. 088 try { 089 // Note that if this is running on a JVM prior to 1.4, then an 090 // exception will be thrown and we will fall back to 091 // setLocationRelativeTo(...). 092 final Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); 093 094 final Dimension windowSize = window.getSize(); 095 final int x = screenBounds.x + ((screenBounds.width - windowSize.width) / 2); 096 final int y = screenBounds.y + ((screenBounds.height - windowSize.height) / 2); 097 window.setLocation(x, y); 098 } 099 catch (Throwable t) { 100 window.setLocationRelativeTo(window); 101 } 102 } 103 104 /** 105 * Pack the window, center it relative to it's parent, and set the window 106 * visible. 107 * 108 * @param window the window to center and show. 109 */ 110 public static void centerOnParentAndSetVisible(Window window) { 111 window.pack(); 112 centerOnParent(window, window.getParent()); 113 window.setVisible(true); 114 } 115 116 /** 117 * Center the window relative to it's parent. If the parent is null, or not showing, 118 * the window will be centered on the screen 119 * 120 * @param window the window to center 121 * @param parent the parent 122 */ 123 public static void centerOnParent(Window window, Component parent) { 124 if (parent == null || !parent.isShowing()) { 125 // call our own centerOnScreen so we work around bug in 126 // setLocationRelativeTo(null) 127 centerOnScreen(window); 128 } 129 else { 130 window.setLocationRelativeTo(parent); 131 } 132 } 133 134 /** 135 * Return a <code>Dimension</code> whose size is defined not in terms of 136 * pixels, but in terms of a given percent of the screen's width and height. 137 * 138 * <P> 139 * Use to set the preferred size of a component to a certain percentage of 140 * the screen. 141 * 142 * @param percentWidth percentage width of the screen, in range 143 * <code>1..100</code>. 144 * @param percentHeight percentage height of the screen, in range 145 * <code>1..100</code>. 146 */ 147 public static final Dimension getDimensionFromPercent(int percentWidth, int percentHeight) { 148 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 149 return calcDimensionFromPercent(screenSize, percentWidth, percentHeight); 150 } 151 152 private static Dimension calcDimensionFromPercent(Dimension dimension, int percentWidth, int percentHeight) { 153 int width = dimension.width * percentWidth / 100; 154 int height = dimension.height * percentHeight / 100; 155 return new Dimension(width, height); 156 } 157 158 public static int getScreenWidth() { 159 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 160 return screenSize.width; 161 } 162 163 public static int getScreenHeight() { 164 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 165 return screenSize.height; 166 } 167 168 }