fix tooltip not being updated
[jSite2.git] / src / net / pterodactylus / jsite / gui / FixedJMenuItem.java
1 /*
2  * jSite2 - I18nMenuItem.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.gui;
21
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24
25 import javax.swing.Action;
26 import javax.swing.Icon;
27 import javax.swing.JMenuItem;
28 import javax.swing.KeyStroke;
29
30 /**
31  * MenuItem which fixes the bug that when the {@link Action#ACCELERATOR_KEY}
32  * value of the action the JMenuItem is based upen is changed this change is not
33  * picked up (see <a
34  * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4304129">bug
35  * #4304129</a>).
36  * 
37  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
38  * @version $Id$
39  */
40 public class FixedJMenuItem extends JMenuItem {
41
42         /**
43          * Creates a new fixed menu item.
44          * 
45          * @param action
46          *            The action of the menu item
47          */
48         public FixedJMenuItem(Action action) {
49                 super(action);
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         @Override
56         protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
57                 return new PropertyChangeListener() {
58
59                         /**
60                          * {@inheritDoc}
61                          */
62                         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
63                                 String propertyName = propertyChangeEvent.getPropertyName();
64                                 JMenuItem menuItem = FixedJMenuItem.this;
65                                 if ("enabled".equals(propertyName)) {
66                                         Boolean enabledState = (Boolean) propertyChangeEvent.getNewValue();
67                                         menuItem.setEnabled(enabledState.booleanValue());
68                                         menuItem.repaint();
69                                 } else if (Action.NAME.equals(propertyName)) {
70                                         String text = (String) propertyChangeEvent.getNewValue();
71                                         menuItem.setText(text);
72                                         menuItem.repaint();
73                                 } else if (Action.SHORT_DESCRIPTION.equals(propertyName)) {
74                                         String shortDescription = (String) propertyChangeEvent.getNewValue();
75                                         menuItem.setToolTipText(shortDescription);
76                                         menuItem.repaint();
77                                 } else if (Action.MNEMONIC_KEY.equals(propertyName)) {
78                                         Integer mn = (Integer) propertyChangeEvent.getNewValue();
79                                         menuItem.setMnemonic(mn.intValue());
80                                         menuItem.invalidate();
81                                         menuItem.repaint();
82                                 } else if (Action.ACCELERATOR_KEY.equals(propertyName)) {
83                                         KeyStroke keyStroke = (KeyStroke) propertyChangeEvent.getNewValue();
84                                         menuItem.setAccelerator(keyStroke);
85                                         menuItem.invalidate();
86                                         menuItem.repaint();
87                                 } else if (Action.SMALL_ICON.equals(propertyName)) {
88                                         Icon icon = (Icon) propertyChangeEvent.getNewValue();
89                                         menuItem.setIcon(icon);
90                                         menuItem.invalidate();
91                                         menuItem.repaint();
92                                 }
93                         }
94                 };
95         }
96
97 }