fix broken JMenuItem
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 6 Apr 2008 12:38:07 +0000 (12:38 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 6 Apr 2008 12:38:07 +0000 (12:38 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@617 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/gui/FixedJMenuItem.java [new file with mode: 0644]
src/net/pterodactylus/jsite/gui/MainWindow.java

diff --git a/src/net/pterodactylus/jsite/gui/FixedJMenuItem.java b/src/net/pterodactylus/jsite/gui/FixedJMenuItem.java
new file mode 100644 (file)
index 0000000..5f0940a
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * jSite2 - I18nMenuItem.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jsite.gui;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+
+import javax.swing.Action;
+import javax.swing.Icon;
+import javax.swing.JMenuItem;
+import javax.swing.KeyStroke;
+
+/**
+ * MenuItem which fixes the bug that when the {@link Action#ACCELERATOR_KEY}
+ * value of the action the JMenuItem is based upen is changed this change is not
+ * picked up (see <a
+ * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4304129">bug
+ * #4304129</a>).
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class FixedJMenuItem extends JMenuItem {
+
+       /**
+        * Creates a new fixed menu item.
+        * 
+        * @param action
+        *            The action of the menu item
+        */
+       public FixedJMenuItem(Action action) {
+               super(action);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
+               return new PropertyChangeListener() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+                               String propertyName = propertyChangeEvent.getPropertyName();
+                               JMenuItem menuItem = FixedJMenuItem.this;
+                               if (propertyChangeEvent.getPropertyName().equals(Action.NAME)) {
+                                       String text = (String) propertyChangeEvent.getNewValue();
+                                       menuItem.setText(text);
+                                       menuItem.repaint();
+                               } else if (propertyName.equals("enabled")) {
+                                       Boolean enabledState = (Boolean) propertyChangeEvent.getNewValue();
+                                       menuItem.setEnabled(enabledState.booleanValue());
+                                       menuItem.repaint();
+                               } else if (propertyChangeEvent.getPropertyName().equals(Action.SMALL_ICON)) {
+                                       Icon icon = (Icon) propertyChangeEvent.getNewValue();
+                                       menuItem.setIcon(icon);
+                                       menuItem.invalidate();
+                                       menuItem.repaint();
+                               } else if (propertyChangeEvent.getPropertyName().equals(Action.MNEMONIC_KEY)) {
+                                       Integer mn = (Integer) propertyChangeEvent.getNewValue();
+                                       menuItem.setMnemonic(mn.intValue());
+                                       menuItem.invalidate();
+                                       menuItem.repaint();
+                               } else if (propertyChangeEvent.getPropertyName().equals(Action.ACCELERATOR_KEY)) {
+                                       KeyStroke keyStroke = (KeyStroke) propertyChangeEvent.getNewValue();
+                                       menuItem.setAccelerator(keyStroke);
+                                       menuItem.invalidate();
+                                       menuItem.repaint();
+                               }
+                       }
+               };
+       }
+
+}
index 97b9fe5..694cc98 100644 (file)
@@ -102,10 +102,10 @@ public class MainWindow extends JFrame implements I18nable {
                nodeMenu = new I18nMenu("mainWindow.menu.node");
                menuBar.add(nodeMenu);
 
-               nodeMenu.add(swingInterface.getManageNodesAction());
+               nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
                nodeMenu.addSeparator();
-               nodeMenu.add(swingInterface.getNodeConnectAction());
-               nodeMenu.add(swingInterface.getNodeDisconnectAction());
+               nodeMenu.add(new FixedJMenuItem(swingInterface.getNodeConnectAction()));
+               nodeMenu.add(new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
 
                languageMenu = new I18nMenu("mainWindow.menu.language");
                menuBar.add(languageMenu);