Add window listener to main frame.
[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 i18n
46          *            The I18n container
47          * @param i18nBasename
48          *            The I18n basename of the label
49          */
50         public I18nLabel(I18n i18n, String i18nBasename) {
51                 this(i18n, i18nBasename, (Component) null);
52         }
53
54         /**
55          * Creates a new label with the given I18n basename that optionally is a
56          * label for the given component.
57          *
58          * @param i18n
59          *            The I18n container
60          * @param i18nBasename
61          *            The I18n basename of the label
62          * @param component
63          *            The component that is activated by the label, or
64          *            <code>null</code> if this label should not activate a
65          *            component
66          */
67         public I18nLabel(I18n i18n, String i18nBasename, Component component) {
68                 this(i18n, i18nBasename, component, (Object[]) null);
69         }
70
71         /**
72          * Creates a new label with the given I18n basename that optionally is a
73          * label for the given component.
74          *
75          * @param i18n
76          *            The I18n container
77          * @param i18nBasename
78          *            The I18n basename of the label
79          * @param arguments
80          *            Optional arguments that are handed in to
81          *            {@link I18n#get(String, Object...)}
82          */
83         public I18nLabel(I18n i18n, String i18nBasename, Object... arguments) {
84                 this(i18n, i18nBasename, null, arguments);
85         }
86
87         /**
88          * Creates a new label with the given I18n basename that optionally is a
89          * label for the given component.
90          *
91          * @param i18n
92          *            The I18n container
93          * @param i18nBasename
94          *            The I18n basename of the label
95          * @param component
96          *            The component that is activated by the label, or
97          *            <code>null</code> if this label should not activate a
98          *            component
99          * @param arguments
100          *            Optional arguments that are handed in to
101          *            {@link I18n#get(String, Object...)}
102          */
103         public I18nLabel(I18n i18n, String i18nBasename, Component component, Object... arguments) {
104                 super();
105                 this.i18nBasename = i18nBasename;
106                 this.arguments = arguments;
107                 if (component != null) {
108                         setLabelFor(component);
109                 }
110                 updateI18n(i18n);
111         }
112
113         /**
114          * {@inheritDoc}
115          */
116         public void updateI18n(I18n i18n) {
117                 setText(i18n.get(i18nBasename + ".name", arguments));
118                 if (getLabelFor() != null) {
119                         setDisplayedMnemonic(i18n.getKey(i18nBasename + ".mnemonic"));
120                 }
121         }
122
123 }