1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui;
20
21 import java.awt.event.ActionListener;
22
23 import javax.swing.ImageIcon;
24 import javax.swing.JButton;
25 import javax.swing.JMenu;
26 import javax.swing.JMenuItem;
27
28 import net.sf.magicproject.tools.MToolKit;
29 import net.sf.magicproject.ui.i18n.LanguageManager;
30
31 /***
32 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
33 * @since 0.92
34 */
35 public final class UIHelper {
36
37 /***
38 * Create a new instance of this class.
39 */
40 private UIHelper() {
41 super();
42 }
43
44 /***
45 * Returns the associated icon.
46 *
47 * @param icon
48 * the icon path relative to the images dir
49 * @return the icon used for the UI
50 */
51 public static ImageIcon getIcon(String icon) {
52 String iconFile = MToolKit.getIconPath(icon);
53 if (iconFile != null)
54 return new ImageIcon(iconFile);
55 return null;
56 }
57
58 /***
59 * Returns the associated TBS icon.
60 *
61 * @param icon
62 * the TBS icon path relative to the images dir
63 * @return the icon used for the UI
64 */
65 public static ImageIcon getTbsIcon(String icon) {
66 String iconFile = MToolKit.getTbsPicture(icon);
67 if (iconFile != null)
68 return new ImageIcon(iconFile);
69 return null;
70 }
71
72 /***
73 * Build and return a JMenu instance with an i18n text, and <param>menuName</param>
74 * as action command.
75 *
76 * @param menuName
77 * the menu key name.
78 * @return the new JMenu instance.
79 */
80 public static JMenu buildMenu(String menuName) {
81 JMenu menu = new JMenu(LanguageManager.getString(menuName));
82 menu.setActionCommand(menuName);
83 return menu;
84 }
85
86 /***
87 * Build and return a JButton instance with a gif icon with <param>actionName</param>
88 * as action command.
89 *
90 * @param actionName
91 * the action name.
92 * @return the new JMenu instance.
93 */
94 public static JButton buildButton(String actionName) {
95 JButton menu = new JButton(getIcon(actionName + ".gif"));
96 menu.setActionCommand(actionName);
97 return menu;
98 }
99
100 /***
101 * Build and return a JButton instance with a gif icon with <param>actionName</param>
102 * as action command.
103 *
104 * @param actionName
105 * the action name.
106 * @param actionListener
107 * the listern to register to the new component.
108 * @return the new JMenu instance.
109 */
110 public static JButton buildButton(String actionName, ActionListener actionListener) {
111 JButton menu = buildButton(actionName);
112 menu.addActionListener(actionListener);
113 return menu;
114 }
115
116 /***
117 * Build and return a JMenu instance with an i18n text, and <param>menuName</param>
118 * as action command.
119 *
120 * @param menuName
121 * the menu key name.
122 * @param mnemonic
123 * the mnemonic char for this menu.
124 * @return the new JMenu instance.
125 */
126 public static JMenu buildMenu(String menuName, char mnemonic) {
127 JMenu menu = buildMenu(menuName);
128 String icon = MToolKit.getIconPath(menuName + ".gif");
129 if (icon == null)
130 icon = MToolKit.getIconPath(menuName + ".png");
131 if (icon != null)
132 menu.setIcon(new ImageIcon(icon));
133 return menu;
134 }
135
136 /***
137 * Build and return a JMenuItem instance with an i18n text, and
138 * <param>menuName</param> as action command. The tooltip is also set with
139 * the i18n + '.tooltip' key.
140 *
141 * @param menuName
142 * the menu key name.
143 * @param mnemonic
144 * the mnemonic char for this menu.
145 * @param icon
146 * the icon name to set to this menu.
147 * @param actionListener
148 * the listern to register to the new component.
149 * @return the new JMenuItem instance.
150 */
151 public static JMenuItem buildMenu(String menuName, char mnemonic,
152 String icon, ActionListener actionListener) {
153 JMenuItem result = buildMenu(menuName, mnemonic, actionListener);
154 result.setMnemonic(mnemonic);
155 if (icon != null)
156 result.setIcon(getIcon(MToolKit.getIconPath(icon)));
157 return result;
158 }
159
160 /***
161 * Build and return a JMenuItem instance with an i18n text, and
162 * <param>menuName</param> as action command. The tooltip is also set with
163 * the i18n + '.tooltip' key.
164 *
165 * @param menuName
166 * the menu key name.
167 * @param mnemonic
168 * the mnemonic char for this menu.
169 * @param actionListener
170 * the listern to register to the new component.
171 * @return the new JMenuItem instance.
172 */
173 public static JMenuItem buildMenu(String menuName, char mnemonic,
174 ActionListener actionListener) {
175 JMenuItem result = buildMenu(menuName, actionListener);
176 result.setMnemonic(mnemonic);
177 return result;
178 }
179
180 /***
181 * Build and return a JMenuItem instance with an i18n text, and
182 * <param>menuName</param> as action command. The tooltip is also set with
183 * the i18n + '.tooltip' key. The used icon is first '.gif', then '.png'
184 * extension using <param>menuName</param> as basename.
185 *
186 * @param menuName
187 * the menu key name.
188 * @param actionListener
189 * the listern to register to the new component.
190 * @return the new JMenuItem instance.
191 */
192 public static JMenuItem buildMenu(String menuName,
193 ActionListener actionListener) {
194 JMenuItem result = new JMenuItem(LanguageManager.getString(menuName));
195 result.addActionListener(actionListener);
196 result.setActionCommand(menuName);
197 String tooltip = LanguageManager.getNullString(menuName + ".tooltip");
198 if (tooltip != null)
199 result.setToolTipText("<html>" + tooltip);
200 String icon = MToolKit.getIconPath(menuName + ".gif");
201 if (icon == null)
202 icon = MToolKit.getIconPath(menuName + ".png");
203 if (icon != null)
204 result.setIcon(new ImageIcon(icon));
205 return result;
206 }
207
208 }