OSDN Git Service

リポジトリ内改行コードのLFへの修正
[charactermanaj/CharacterManaJ.git] / src / main / java / charactermanaj / util / UIHelper.java
1 package charactermanaj.util;
2
3 import java.awt.Component;
4 import java.awt.Container;
5 import java.awt.Graphics;
6 import java.awt.image.BufferedImage;
7 import java.io.IOException;
8 import java.net.URL;
9 import java.util.ArrayList;
10 import java.util.Collection;
11
12 import javax.imageio.ImageIO;
13 import javax.swing.AbstractButton;
14 import javax.swing.Icon;
15 import javax.swing.ImageIcon;
16 import javax.swing.JButton;
17 import javax.swing.JMenu;
18
19
20 public final class UIHelper extends ResourceLoader {
21
22         private static final UIHelper singleton = new UIHelper();
23         
24         private UIHelper() {
25                 super();
26         }
27         
28         public static final UIHelper getInstance() {
29                 return singleton;
30         }
31         
32         /**
33          * 指定したコンテナに含まれる指定したコンポーネント型のすべてのコンポーネントを返す.<br>
34          * 一つも該当するものがなければ空を返す
35          * @param <T> 対象のコンポーネントのクラス型
36          * @param clz クラス
37          * @param container 対象のコンテナ
38          * @return コンポーネントのコレクション、もしくは空
39          */
40         @SuppressWarnings("unchecked")
41         public <T> Collection<T> getDescendantOfClass(Class<T> clz, Container container) {
42                 if (container == null || clz == null) {
43                         throw new IllegalArgumentException();
44                 }
45                 Collection<Component> components = new ArrayList<Component>();
46                 getDescendantOfClass(clz, container, components);
47                 return (Collection<T>) components;
48         }
49         
50         private void getDescendantOfClass(Class<?> clz, Container container, Collection<Component> results) {
51                 if (container == null) {
52                         return;
53                 }
54                 Component[] children = (container instanceof JMenu) ?
55                                 ((JMenu) container).getMenuComponents() : container.getComponents();
56                 int mx = children.length;
57                 for (int idx = 0; idx < mx; idx++) {
58                         Component comp = children[idx];
59                         if (clz.isInstance(comp)) {
60                                 results.add(comp);
61                         } else if (comp instanceof Container) {
62                                 getDescendantOfClass(clz, (Container) comp, results);
63                         }
64                 }
65         }
66         
67         /**
68          * 2つのステートをもつアイコンを作成します.<br>
69          * このアイコンは、使用するコンポーネントがAbstractButton派生クラスであれば、isSelectedの結果が
70          * trueである場合は2番目のアイコンイメージを表示します.<br>
71          * isSelectedの結果がfalseであるか、もしくはAbstractButton派生クラスでなければ
72          * 最初のアイコンイメージを表示します.<br>
73          * @param iconName1 アイコン1
74          * @param iconName2 アイコン2
75          * @return アイコン
76          */
77         public Icon createTwoStateIcon(String iconName1, String iconName2) {
78                 if (iconName1 == null || iconName2 == null || iconName1.length() == 0
79                                 || iconName2.length() == 0) {
80                         throw new IllegalArgumentException();
81                 }
82                 final BufferedImage pinIcon1 = getImage(iconName1);
83                 final BufferedImage pinIcon2 = getImage(iconName2);
84                 
85                 Icon icon = new Icon() {
86                         public void paintIcon(Component c, Graphics g, int x, int y) {
87                                 boolean selected = false;
88                                 if (c instanceof AbstractButton) {
89                                         AbstractButton btn = (AbstractButton) c;
90                                         selected = btn.isSelected();
91                                 }
92                                 BufferedImage iconImage;
93                                 if ( !selected) {
94                                         iconImage = pinIcon1;
95                                 } else {
96                                         iconImage = pinIcon2;
97                                 }
98                                 int w = iconImage.getWidth();
99                                 int h = iconImage.getHeight();
100                                 g.drawImage(iconImage, x, y, w, h, 0, 0, w, h, null);
101                         }
102                         public int getIconHeight() {
103                                 return pinIcon1.getHeight();
104                         }
105                         public int getIconWidth() {
106                                 return pinIcon1.getWidth();
107                         }
108                 };
109                 
110                 return icon;
111         }
112
113         /**
114          * アイコンボタン(非透過)を作成して返す.<br>
115          * リソースが取得できない場合は実行時例外が返される.<br>
116          * @param iconName 画像リソース名
117          * @return アイコンボタン
118          */
119         public JButton createIconButton(String iconName) {
120                 if (iconName == null || iconName.length() == 0) {
121                         throw new IllegalArgumentException();
122                 }
123                 JButton btn = new JButton();
124                 btn.setIcon(new ImageIcon(getImage(iconName)));
125                 
126                 return btn;
127         }
128         
129         /**
130          * 通常時の画像のみをもつ透過ボタンを作成して返す.<br>
131          * リソースが取得できない場合は実行時例外が返される.<br>
132          * @param normal 通常時の画像リソース
133          * @return 透過ボタン
134          */
135         public JButton createTransparentButton(String normal) {
136                 return createTransparentButton(normal, null);
137         }
138
139         /**
140          * リソースから通常とホバー時の画像をもつ透過ボタンを作成して返す.<br>
141          * リソースが取得できない場合は実行時例外が返される.<br>
142          * @param normal 通常時の画像リソース
143          * @param rollover ホバー時の画像リソース
144          * @return 透過ボタン
145          */
146         public JButton createTransparentButton(String normal, String rollover) {
147                 if (normal == null || normal.length() == 0) {
148                         throw new IllegalArgumentException();
149                 }
150                 ImageIcon normIcon = new ImageIcon(getImage(normal));
151                 JButton btn = new JButton(normIcon);
152
153                 if (rollover != null && rollover.length() != 0) {
154                         ImageIcon rolloverIcon = new ImageIcon(getImage(rollover));
155                         btn.setRolloverEnabled(true);
156                         btn.setRolloverIcon(rolloverIcon);
157                         btn.setPressedIcon(rolloverIcon);
158                 }
159
160                 btn.setOpaque(false);
161                 btn.setBorderPainted(false);
162                 btn.setContentAreaFilled(false);
163
164                 return btn;
165         }
166         
167         /**
168          * リソースから画像を取得する.<br>
169          * 画像が取得できない場合は実行時例外を返す.<br>
170          * @param name リソース
171          * @return 画像
172          */
173         public BufferedImage getImage(String name) {
174                 URL url = getResource(name);
175                 if (url == null) {
176                         throw new RuntimeException("resource not found. " + name);
177                 }
178                 try {
179                         return ImageIO.read(url);
180
181                 } catch (IOException ex) {
182                         throw new RuntimeException("image load error." + ex.getMessage(), ex);
183                 }
184         }
185                 
186 }