Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JThequeI18nLabel |
|
| 1.1428571428571428;1.143 |
1 | package org.jtheque.core.managers.view.impl.components; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import org.jtheque.core.managers.Managers; | |
20 | import org.jtheque.core.managers.language.ILanguageManager; | |
21 | import org.jtheque.core.managers.language.Internationalizable; | |
22 | import org.jtheque.core.managers.view.ViewComponent; | |
23 | import org.jtheque.utils.collections.ArrayUtils; | |
24 | ||
25 | import javax.swing.JLabel; | |
26 | import java.awt.Color; | |
27 | import java.awt.Font; | |
28 | import java.awt.Graphics; | |
29 | import java.awt.Graphics2D; | |
30 | import java.awt.RenderingHints; | |
31 | ||
32 | /** | |
33 | * An internationalizable label. | |
34 | * | |
35 | * @author Baptiste Wicht | |
36 | */ | |
37 | public final class JThequeI18nLabel extends JLabel implements Internationalizable, ViewComponent { | |
38 | private String textKey; | |
39 | private Object[] replaces; | |
40 | ||
41 | /** | |
42 | * Construct a new <code>JThequeI18nLabel</code>. | |
43 | * | |
44 | * @param textKey The internationalization key. | |
45 | * @param replaces The replaces for the message. | |
46 | */ | |
47 | public JThequeI18nLabel(String textKey, Object... replaces) { | |
48 | 0 | super(); |
49 | ||
50 | 0 | setTextKey(textKey, replaces); |
51 | ||
52 | 0 | Managers.getManager(ILanguageManager.class).addInternationalizable(this); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Construct a new <code>JThequeI18nLabel</code>. | |
57 | * | |
58 | * @param key The internationalization key. | |
59 | * @param font The font to use. | |
60 | */ | |
61 | public JThequeI18nLabel(String key, Font font) { | |
62 | 0 | this(key); |
63 | ||
64 | 0 | setFont(font); |
65 | 0 | } |
66 | ||
67 | /** | |
68 | * Construct a new <code>JThequeI18nLabel</code>. | |
69 | * | |
70 | * @param key The internationalization key. | |
71 | * @param font The font to use. | |
72 | * @param foreground The foreground color. | |
73 | */ | |
74 | public JThequeI18nLabel(String key, Font font, Color foreground) { | |
75 | 0 | this(key); |
76 | ||
77 | 0 | setFont(font); |
78 | 0 | setForeground(foreground); |
79 | 0 | } |
80 | ||
81 | /** | |
82 | * Set the text key of the label. | |
83 | * | |
84 | * @param textKey The i18n key of the message to be display in this label. | |
85 | * @param replaces The object to use as replacement for the parameters of the message. | |
86 | */ | |
87 | public void setTextKey(String textKey, Object... replaces) { | |
88 | 0 | this.textKey = textKey; |
89 | 0 | this.replaces = ArrayUtils.copyOf(replaces); |
90 | ||
91 | 0 | refreshText(); |
92 | 0 | } |
93 | ||
94 | @Override | |
95 | public void refreshText() { | |
96 | 0 | if (ArrayUtils.isEmpty(replaces)) { |
97 | 0 | setText(Managers.getManager(ILanguageManager.class).getMessage(textKey)); |
98 | } else { | |
99 | 0 | setText(Managers.getManager(ILanguageManager.class).getMessage(textKey, replaces)); |
100 | } | |
101 | 0 | } |
102 | ||
103 | @Override | |
104 | public void paint(Graphics g) { | |
105 | 0 | ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
106 | ||
107 | 0 | super.paint(g); |
108 | 0 | } |
109 | ||
110 | @Override | |
111 | public Object getImpl(){ | |
112 | 0 | return this; |
113 | } | |
114 | } |