ac569261e92956e8b8399f826c739302c68669c1
[jSite2.git] / src / net / pterodactylus / jsite / 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.jsite.i18n.gui;
21
22 import java.awt.Component;
23
24 import javax.swing.JLabel;
25
26 import net.pterodactylus.jsite.i18n.I18n;
27 import net.pterodactylus.jsite.i18n.I18nable;
28
29 /**
30  * Label that can update itself from {@link I18n}.
31  *
32  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33  * @version $Id$
34  */
35 public class I18nLabel extends JLabel implements I18nable {
36
37         /** The I18n basename of the label. */
38         private final String i18nBasename;
39
40         /** Optional arguments for i18n replacement. */
41         private final Object[] arguments;
42
43         /**
44          * Creates a new label with the given I18n basename.
45          *
46          * @param i18nBasename
47          *            The I18n basename of the label
48          */
49         public I18nLabel(String i18nBasename) {
50                 this(i18nBasename, (Component) null);
51         }
52
53         /**
54          * Creates a new label with the given I18n basename that optionally is a
55          * label for the given component.
56          *
57          * @param i18nBasename
58          *            The I18n basename of the label
59          * @param component
60          *            The component that is activated by the label, or
61          *            <code>null</code> if this label should not activate a
62          *            component
63          */
64         public I18nLabel(String i18nBasename, Component component) {
65                 this(i18nBasename, component, (Object[]) null);
66         }
67
68         /**
69          * Creates a new label with the given I18n basename that optionally is a
70          * label for the given component.
71          *
72          * @param i18nBasename
73          *            The I18n basename of the label
74          * @param arguments
75          *            Optional arguments that are handed in to
76          *            {@link I18n#get(String, Object...)}
77          */
78         public I18nLabel(String i18nBasename, Object... arguments) {
79                 this(i18nBasename, null, arguments);
80         }
81
82         /**
83          * Creates a new label with the given I18n basename that optionally is a
84          * label for the given component.
85          *
86          * @param i18nBasename
87          *            The I18n basename of the label
88          * @param component
89          *            The component that is activated by the label, or
90          *            <code>null</code> if this label should not activate a
91          *            component
92          * @param arguments
93          *            Optional arguments that are handed in to
94          *            {@link I18n#get(String, Object...)}
95          */
96         public I18nLabel(String i18nBasename, Component component, Object... arguments) {
97                 super();
98                 this.i18nBasename = i18nBasename;
99                 this.arguments = arguments;
100                 if (component != null) {
101                         setLabelFor(component);
102                 }
103                 updateI18n();
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         public void updateI18n() {
110                 setText(I18n.get(i18nBasename + ".name", arguments));
111                 if (getLabelFor() != null) {
112                         setDisplayedMnemonic(I18n.getKey(i18nBasename + ".mnemonic"));
113                 }
114         }
115
116 }