| 1 |
/** |
| 2 |
* ======================================== |
| 3 |
* JFreeReport : a free Java report library |
| 4 |
* ======================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or modify it under the terms |
| 11 |
* of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 12 |
* either version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* See the GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with this |
| 19 |
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 23 |
* in the United States and other countries.] |
| 24 |
* |
| 25 |
* ------------ |
| 26 |
* $Id: SwingUtil.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2000-2005, by Object Refinery Limited. |
| 29 |
* (C) Copyright 2005-2007, by Pentaho Corporation. |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.report.modules.gui.swing.common; |
| 33 |
|
| 34 |
import java.awt.Component; |
| 35 |
import java.awt.Window; |
| 36 |
import java.awt.Point; |
| 37 |
import java.awt.GraphicsEnvironment; |
| 38 |
import java.awt.Dimension; |
| 39 |
import java.awt.Toolkit; |
| 40 |
import java.awt.Rectangle; |
| 41 |
import java.awt.Dialog; |
| 42 |
import java.awt.Container; |
| 43 |
import java.lang.reflect.Method; |
| 44 |
|
| 45 |
/** |
| 46 |
* Creation-Date: 20.11.2006, 22:42:21 |
| 47 |
* |
| 48 |
* @author Thomas Morgner |
| 49 |
*/ |
| 50 |
public class SwingUtil |
| 51 |
{ |
| 52 |
private SwingUtil() |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Computes the center point of the current screen device. If this method is called on JDK 1.4, Xinerama-aware results |
| 59 |
* are returned. (See Sun-Bug-ID 4463949 for details). |
| 60 |
* |
| 61 |
* @return the center point of the current screen. |
| 62 |
*/ |
| 63 |
public static Point getCenterPoint() |
| 64 |
{ |
| 65 |
final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 66 |
try |
| 67 |
{ |
| 68 |
final Method method = GraphicsEnvironment.class.getMethod("getCenterPoint", (Class[]) null); |
| 69 |
return (Point) method.invoke(localGraphicsEnvironment, (Object[]) null); |
| 70 |
} |
| 71 |
catch (Exception e) |
| 72 |
{ |
| 73 |
// ignore ... will fail if this is not a JDK 1.4 .. |
| 74 |
} |
| 75 |
|
| 76 |
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); |
| 77 |
return new Point(s.width / 2, s.height / 2); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Computes the maximum bounds of the current screen device. If this method is called on JDK 1.4, Xinerama-aware |
| 82 |
* results are returned. (See Sun-Bug-ID 4463949 for details). |
| 83 |
* |
| 84 |
* @return the maximum bounds of the current screen. |
| 85 |
*/ |
| 86 |
public static Rectangle getMaximumWindowBounds() |
| 87 |
{ |
| 88 |
final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 89 |
try |
| 90 |
{ |
| 91 |
final Method method = GraphicsEnvironment.class.getMethod("getMaximumWindowBounds", (Class[]) null); |
| 92 |
return (Rectangle) method.invoke(localGraphicsEnvironment, (Object[]) null); |
| 93 |
} |
| 94 |
catch (Exception e) |
| 95 |
{ |
| 96 |
// ignore ... will fail if this is not a JDK 1.4 .. |
| 97 |
} |
| 98 |
|
| 99 |
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); |
| 100 |
return new Rectangle(0, 0, s.width, s.height); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Positions the specified frame in the middle of the screen. |
| 105 |
* |
| 106 |
* @param frame the frame to be centered on the screen. |
| 107 |
*/ |
| 108 |
public static void centerFrameOnScreen(final Window frame) |
| 109 |
{ |
| 110 |
positionFrameOnScreen(frame, 0.5, 0.5); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Positions the specified frame at a relative position in the screen, where 50% is considered to be the center of the |
| 115 |
* screen. |
| 116 |
* |
| 117 |
* @param frame the frame. |
| 118 |
* @param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0, where 0.5 is the center of the |
| 119 |
* screen). |
| 120 |
* @param verticalPercent the relative vertical position of the frame (0.0 to 1.0, where 0.5 is the center of the |
| 121 |
* screen). |
| 122 |
*/ |
| 123 |
public static void positionFrameOnScreen(final Window frame, |
| 124 |
final double horizontalPercent, |
| 125 |
final double verticalPercent) |
| 126 |
{ |
| 127 |
|
| 128 |
final Rectangle s = getMaximumWindowBounds(); |
| 129 |
final Dimension f = frame.getSize(); |
| 130 |
final int w = Math.max(s.width - f.width, 0); |
| 131 |
final int h = Math.max(s.height - f.height, 0); |
| 132 |
final int x = (int) (horizontalPercent * w) + s.x; |
| 133 |
final int y = (int) (verticalPercent * h) + s.y; |
| 134 |
frame.setBounds(x, y, f.width, f.height); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Positions the specified frame at a random location on the screen while ensuring that the entire frame is visible |
| 140 |
* (provided that the frame is smaller than the screen). |
| 141 |
* |
| 142 |
* @param frame the frame. |
| 143 |
*/ |
| 144 |
public static void positionFrameRandomly(final Window frame) |
| 145 |
{ |
| 146 |
positionFrameOnScreen(frame, Math.random(), Math.random()); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Positions the specified dialog within its parent. |
| 151 |
* |
| 152 |
* @param dialog the dialog to be positioned on the screen. |
| 153 |
*/ |
| 154 |
public static void centerDialogInParent(final Dialog dialog) |
| 155 |
{ |
| 156 |
positionDialogRelativeToParent(dialog, 0.5, 0.5); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Positions the specified dialog at a position relative to its parent. |
| 161 |
* |
| 162 |
* @param dialog the dialog to be positioned. |
| 163 |
* @param horizontalPercent the relative location. |
| 164 |
* @param verticalPercent the relative location. |
| 165 |
*/ |
| 166 |
public static void positionDialogRelativeToParent(final Dialog dialog, |
| 167 |
final double horizontalPercent, |
| 168 |
final double verticalPercent) |
| 169 |
{ |
| 170 |
final Dimension d = dialog.getSize(); |
| 171 |
final Container parent = dialog.getParent(); |
| 172 |
final Dimension p = parent.getSize(); |
| 173 |
|
| 174 |
final int baseX = parent.getX() - d.width; |
| 175 |
final int baseY = parent.getY() - d.height; |
| 176 |
final int w = d.width + p.width; |
| 177 |
final int h = d.height + p.height; |
| 178 |
int x = baseX + (int) (horizontalPercent * w); |
| 179 |
int y = baseY + (int) (verticalPercent * h); |
| 180 |
|
| 181 |
// make sure the dialog fits completely on the screen... |
| 182 |
final Rectangle s = getMaximumWindowBounds(); |
| 183 |
x = Math.min(x, (s.width - d.width)); |
| 184 |
x = Math.max(x, 0); |
| 185 |
y = Math.min(y, (s.height - d.height)); |
| 186 |
y = Math.max(y, 0); |
| 187 |
|
| 188 |
dialog.setBounds(x + s.x, y + s.y, d.width, d.height); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
public static Window getWindowAncestor(Component component) |
| 193 |
{ |
| 194 |
while (component instanceof Window == false) |
| 195 |
{ |
| 196 |
if (component == null) |
| 197 |
{ |
| 198 |
return null; |
| 199 |
} |
| 200 |
component = component.getParent(); |
| 201 |
} |
| 202 |
return (Window) component; |
| 203 |
} |
| 204 |
} |