32ad57dcb104b6cd21a39c8e2aafe9616ad234bd
[jkeytool.git] / src / net / pterodactylus / util / i18n / gui / I18nLabel.java
1 /*
2  * jSite2 - I18nLabel.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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 this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.util.i18n.gui;
21
22 import java.awt.Component;
23
24 import javax.swing.JLabel;
25
26 import net.pterodactylus.util.i18n.I18n;
27 import net.pterodactylus.util.i18n.I18nable;
28
29 /**
30  * Label that can update itself from {@link I18n}.
31  * 
32  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33  */
34 public class I18nLabel extends JLabel implements I18nable {
35
36         /** The I18n basename of the label. */
37         private final String i18nBasename;
38
39         /** Optional arguments for i18n replacement. */
40         private final Object[] arguments;
41
42         /**
43          * Creates a new label with the given I18n basename.
44          * 
45          * @param i18nBasename
46          *            The I18n basename of the label
47          */
48         public I18nLabel(String i18nBasename) {
49                 this(i18nBasename, (Component) null);
50         }
51
52         /**
53          * Creates a new label with the given I18n basename that optionally is a
54          * label for the given component.
55          * 
56          * @param i18nBasename
57          *            The I18n basename of the label
58          * @param component
59          *            The component that is activated by the label, or
60          *            <code>null</code> if this label should not activate a
61          *            component
62          */
63         public I18nLabel(String i18nBasename, Component component) {
64                 this(i18nBasename, component, (Object[]) null);
65         }
66
67         /**
68          * Creates a new label with the given I18n basename that optionally is a
69          * label for the given component.
70          * 
71          * @param i18nBasename
72          *            The I18n basename of the label
73          * @param arguments
74          *            Optional arguments that are handed in to
75          *            {@link I18n#get(String, Object...)}
76          */
77         public I18nLabel(String i18nBasename, Object... arguments) {
78                 this(i18nBasename, null, arguments);
79         }
80
81         /**
82          * Creates a new label with the given I18n basename that optionally is a
83          * label for the given component.
84          * 
85          * @param i18nBasename
86          *            The I18n basename of the label
87          * @param component
88          *            The component that is activated by the label, or
89          *            <code>null</code> if this label should not activate a
90          *            component
91          * @param arguments
92          *            Optional arguments that are handed in to
93          *            {@link I18n#get(String, Object...)}
94          */
95         public I18nLabel(String i18nBasename, Component component, Object... arguments) {
96                 super();
97                 this.i18nBasename = i18nBasename;
98                 this.arguments = arguments;
99                 if (component != null) {
100                         setLabelFor(component);
101                 }
102                 updateI18n();
103         }
104
105         /**
106          * {@inheritDoc}
107          */
108         public void updateI18n() {
109                 setText(I18n.get(i18nBasename + ".name", arguments));
110                 if (getLabelFor() != null) {
111                         setDisplayedMnemonic(I18n.getKey(i18nBasename + ".mnemonic"));
112                 }
113         }
114
115 }