OSDN Git Service

8f7cc7d7bea09649612e63eaab1c07ab248f5436
[charactermanaj/CharacterManaJ.git] / src / charactermanaj / ui / util / WindowAdjustLocationSupport.java
1 package charactermanaj.ui.util;\r
2 \r
3 import java.awt.Dimension;\r
4 import java.awt.GraphicsEnvironment;\r
5 import java.awt.Insets;\r
6 import java.awt.Point;\r
7 import java.awt.Rectangle;\r
8 import java.awt.Window;\r
9 \r
10 import javax.swing.JFrame;\r
11 \r
12 /**\r
13  * ウィンドウの位置を調整するサポートクラス.<br>\r
14  * \r
15  * @author seraphy\r
16  */\r
17 public final class WindowAdjustLocationSupport {\r
18 \r
19         /**\r
20          * プライベートコンストラクタ\r
21          */\r
22         private WindowAdjustLocationSupport() {\r
23                 super();\r
24         }\r
25 \r
26         /**\r
27          * ウィンドウの表示位置をメインウィンドウの右側に調整する.<br>\r
28          * 横位置Xはメインフレームの右側とし、縦位置Yはメインフレームの上位置からのoffset_yを加えた位置とする.<br>\r
29          * \r
30          * @param mainWindow\r
31          *            基準位置となるメインウィンドウ\r
32          * @param window\r
33          *            位置を調整するウィンドウ\r
34          * @param offset_y\r
35          *            表示のYオフセット\r
36          * @param sameHeight\r
37          *            高さをメインウィンドウにそろえるか?\r
38          */\r
39         public static void alignRight(JFrame mainWindow, Window window,\r
40                         int offset_y, boolean sameHeight) {\r
41                 // メインウィンドウよりも左側に位置づけする.\r
42                 // 縦位置はメインウィンドウの上端からオフセットを加えたものとする.\r
43                 Point pt = mainWindow.getLocation();\r
44                 Insets insets = mainWindow.getInsets();\r
45                 pt.x += mainWindow.getWidth();\r
46                 pt.y += (offset_y * insets.top);\r
47 \r
48                 // メインスクリーンサイズを取得する.\r
49                 GraphicsEnvironment genv = GraphicsEnvironment\r
50                                 .getLocalGraphicsEnvironment();\r
51                 Rectangle desktopSize = genv.getMaximumWindowBounds(); // メインスクリーンのサイズ(デスクトップ領域のみ)\r
52 \r
53                 // メインスクリーンサイズを超えた場合は、はみ出た分を移動する.\r
54                 if ((pt.x + window.getWidth()) > desktopSize.width) {\r
55                         pt.x -= ((pt.x + window.getWidth()) - desktopSize.width);\r
56                 }\r
57                 if ((pt.y + window.getHeight()) > desktopSize.height) {\r
58                         pt.y -= ((pt.y + window.getHeight()) - desktopSize.height);\r
59                 }\r
60 \r
61                 window.setLocation(pt);\r
62 \r
63                 // 高さはメインフレームと同じにする.\r
64                 if (sameHeight) {\r
65                         Dimension siz = window.getSize();\r
66                         siz.height = mainWindow.getHeight() - offset_y;\r
67                         window.setSize(siz);\r
68                 }\r
69         }\r
70 }\r