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