Make I18n class non-static.
[jkeytool.git] / src / net / pterodactylus / util / i18n / gui / I18nAction.java
1 package net.pterodactylus.util.i18n.gui;
2
3 import javax.swing.AbstractAction;
4 import javax.swing.Action;
5 import javax.swing.Icon;
6
7 import net.pterodactylus.util.i18n.I18n;
8 import net.pterodactylus.util.i18n.I18nable;
9
10 /**
11  * Helper class that initializes actions with values from {@link I18n}.
12  *
13  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
14  */
15 public abstract class I18nAction extends AbstractAction implements I18nable {
16
17         /** The I18n basename. */
18         private final String i18nName;
19
20         /**
21          * Creates a new action that uses the given name as base name to get values
22          * from {@link I18n}.
23          *
24          * @param i18n
25          *            The I18n container
26          * @param i18nName
27          *            The base name of the action
28          */
29         public I18nAction(I18n i18n, String i18nName) {
30                 this(i18n, i18nName, null);
31         }
32
33         /**
34          * Creates a new action that uses the given name as base name to get values
35          * from {@link I18n} and the given icon.
36          *
37          * @param i18n
38          *            The I18n container
39          * @param i18nName
40          *            The base name of the action
41          * @param icon
42          *            The icon for the action
43          */
44         public I18nAction(I18n i18n, String i18nName, Icon icon) {
45                 this(i18n, i18nName, true, icon);
46         }
47
48         /**
49          * Creates a new action that uses the given name as base name to get values
50          * from {@link I18n}.
51          *
52          * @param i18n
53          *            The I18n container
54          * @param i18nName
55          *            The base name of the action
56          * @param enabled
57          *            Whether the action should be enabled
58          */
59         public I18nAction(I18n i18n, String i18nName, boolean enabled) {
60                 this(i18n, i18nName, enabled, null);
61         }
62
63         /**
64          * Creates a new action that uses the given name as base name to get values
65          * from {@link I18n} and the given icon.
66          *
67          * @param i18n
68          *            The I18n container
69          * @param i18nName
70          *            The base name of the action
71          * @param enabled
72          *            Whether the action should be enabled
73          * @param icon
74          *            The icon for the action
75          */
76         public I18nAction(I18n i18n, String i18nName, boolean enabled, Icon icon) {
77                 this.i18nName = i18nName;
78                 if (icon != null) {
79                         putValue(Action.SMALL_ICON, icon);
80                 }
81                 setEnabled(enabled);
82                 updateI18n(i18n);
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         public void updateI18n(I18n i18n) {
89                 putValue(Action.NAME, i18n.get(i18nName + ".name"));
90                 putValue(Action.MNEMONIC_KEY, i18n.getKey(i18nName + ".mnemonic"));
91                 putValue(Action.ACCELERATOR_KEY, i18n.getKeyStroke(i18nName + ".accelerator"));
92                 putValue(Action.SHORT_DESCRIPTION, i18n.get(i18nName + ".shortDescription"));
93                 putValue(Action.LONG_DESCRIPTION, i18n.get(i18nName + ".longDescription"));
94         }
95
96 }