1
2 package hoplugins.commons.ui;
3
4 import java.awt.Container;
5 import java.awt.Dialog;
6 import java.awt.Dimension;
7 import java.awt.Toolkit;
8 import java.awt.Window;
9
10 /***
11 * Class of Utility for positioning of Frames and Dialog
12 */
13 public class PositioningUtilities {
14 /***
15 * Method that place Dialog in the center of his parent
16 *
17 * @param dialog
18 */
19 public static void centerDialogInParent(Dialog dialog) {
20 positionDialogRelativeToParent(dialog, 0.5D, 0.5D);
21 }
22
23 /***
24 * Method the center the frame in the center of the screen
25 *
26 * @param window
27 */
28 public static void centerFrameOnScreen(Window window) {
29 positionFrameOnScreen(window, 0.5D, 0.5D);
30 }
31
32 /***
33 * Method that place the dialog relative to the parent
34 *
35 * @param dialog
36 * @param x horizontal placement (0-1)
37 * @param y vertical placement (0-1)
38 */
39 public static void positionDialogRelativeToParent(Dialog dialog, double x,
40 double y) {
41 Dimension dimension = dialog.getSize();
42 Container container = dialog.getParent();
43 Dimension dimension1 = container.getSize();
44 int i = container.getX() - dimension.width;
45 int j = container.getY() - dimension.height;
46 int k = dimension.width + dimension1.width;
47 int l = dimension.height + dimension1.height;
48 int i1 = i + (int) (x * (double) k);
49 int j1 = j + (int) (y * (double) l);
50 Dimension dimension2 = Toolkit.getDefaultToolkit().getScreenSize();
51
52 i1 = Math.min(i1, dimension2.width - dimension.width);
53 i1 = Math.max(i1, 0);
54 j1 = Math.min(j1, dimension2.height - dimension.height);
55 j1 = Math.max(j1, 0);
56 dialog.setBounds(i1, j1, dimension.width, dimension.height);
57 }
58
59 /***
60 * Method that place the window relative to the screen
61 *
62 * @param window
63 * @param x horizontal placement (0-1)
64 * @param y vertical placement (0-1)
65 */
66 public static void positionFrameOnScreen(Window window, double x, double y) {
67 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
68 Dimension dimension1 = window.getSize();
69 int i = Math.max(dimension.width - dimension1.width, 0);
70 int j = Math.max(dimension.height - dimension1.height, 0);
71 int k = (int) (x * (double) i);
72 int l = (int) (y * (double) j);
73
74 window.setBounds(k, l, dimension1.width, dimension1.height);
75 }
76
77 /***
78 * Method that place the window randomly in the screen
79 *
80 * @param window
81 */
82 public static void positionFrameRandomly(Window window) {
83 positionFrameOnScreen(window, Math.random(), Math.random());
84 }
85 }