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