f38097b08b88402ecdb07bb38f2d49b8dd308b52
[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.JMenuItem;
27 import javax.swing.KeyStroke;
28
29 /**
30  * MenuItem which fixes the bug that when the {@link Action#ACCELERATOR_KEY} and
31  * {@link Action#SHORT_DESCRIPTION} values of the action the JMenuItem is based
32  * upen is changed this change is not picked up (see <a
33  * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4304129">bug
34  * #4304129</a>).
35  * 
36  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
37  * @version $Id$
38  */
39 public class FixedJMenuItem extends JMenuItem {
40
41         /**
42          * Creates a new fixed menu item.
43          * 
44          * @param action
45          *            The action of the menu item
46          */
47         public FixedJMenuItem(Action action) {
48                 super(action);
49         }
50
51         /**
52          * {@inheritDoc}
53          */
54         @Override
55         protected PropertyChangeListener createActionPropertyChangeListener(Action action) {
56                 final PropertyChangeListener originalPropertyChangeListener = super.createActionPropertyChangeListener(action);
57                 return new PropertyChangeListener() {
58
59                         /**
60                          * {@inheritDoc}
61                          */
62                         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
63                                 originalPropertyChangeListener.propertyChange(propertyChangeEvent);
64                                 String propertyName = propertyChangeEvent.getPropertyName();
65                                 JMenuItem menuItem = FixedJMenuItem.this;
66                                 if (Action.SHORT_DESCRIPTION.equals(propertyName)) {
67                                         String shortDescription = (String) propertyChangeEvent.getNewValue();
68                                         menuItem.setToolTipText(shortDescription);
69                                         menuItem.repaint();
70                                 } else if (Action.ACCELERATOR_KEY.equals(propertyName)) {
71                                         KeyStroke keyStroke = (KeyStroke) propertyChangeEvent.getNewValue();
72                                         menuItem.setAccelerator(keyStroke);
73                                         menuItem.invalidate();
74                                         menuItem.repaint();
75                                 }
76                         }
77                 };
78         }
79
80 }