I implemented the missing keyboard actions for BasicMenuBarUI. Now it should be possible to bring up the menu using F10. Unfortunately keyboard input seems broken for me so I can't test.

2006-07-25  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicMenuBarUI.java
        (FocusAction): New inner class. Used to grab focus.
        (installKeyboardActions): Implemented.
        (uninstallKeyboardActions): Implemented.
        (getActionMap): New helper method.
        (createDefaultActions): New helper method.

/Roman
Index: javax/swing/plaf/basic/BasicMenuBarUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicMenuBarUI.java,v
retrieving revision 1.16
diff -u -1 -2 -r1.16 BasicMenuBarUI.java
--- javax/swing/plaf/basic/BasicMenuBarUI.java	17 Apr 2006 07:41:05 -0000	1.16
+++ javax/swing/plaf/basic/BasicMenuBarUI.java	25 Jul 2006 11:07:53 -0000
@@ -29,50 +29,98 @@
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package javax.swing.plaf.basic;
 
-import gnu.classpath.NotImplementedException;
-
 import java.awt.Dimension;
+import java.awt.event.ActionEvent;
 import java.awt.event.ContainerEvent;
 import java.awt.event.ContainerListener;
 import java.awt.event.MouseEvent;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.ActionMap;
 import javax.swing.BoxLayout;
+import javax.swing.InputMap;
 import javax.swing.JComponent;
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.LookAndFeel;
 import javax.swing.MenuElement;
+import javax.swing.MenuSelectionManager;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import javax.swing.event.MouseInputListener;
+import javax.swing.plaf.ActionMapUIResource;
 import javax.swing.plaf.ComponentUI;
 import javax.swing.plaf.MenuBarUI;
 
 /**
  * UI Delegate for JMenuBar.
  */
 public class BasicMenuBarUI extends MenuBarUI
 {
+
+  /**
+   * This action is performed for the action command 'takeFocus'.
+   */
+  private class FocusAction
+    extends AbstractAction
+  {
+
+    /**
+     * Creates a new FocusAction.
+     */
+    FocusAction()
+    {
+      super("takeFocus");
+    }
+
+    /**
+     * Performs the action.
+     */
+    public void actionPerformed(ActionEvent event)
+    {
+      // In the JDK this action seems to pop up the first menu of the
+      // menu bar.
+      JMenuBar menuBar = (JMenuBar) event.getSource();
+      MenuSelectionManager defaultManager =
+        MenuSelectionManager.defaultManager();
+      MenuElement me[];
+      MenuElement subElements[];
+      JMenu menu = menuBar.getMenu(0);
+      if (menu != null)
+        {
+          me = new MenuElement[3];
+          me[0] = (MenuElement) menuBar;
+          me[1] = (MenuElement) menu;
+          me[2] = (MenuElement) menu.getPopupMenu();
+          defaultManager.setSelectedPath(me);
+        }
+    }
+    
+  }
+
   protected ChangeListener changeListener;
 
   /*ContainerListener that listens to the ContainerEvents fired from menu bar*/
   protected ContainerListener containerListener;
   
   /*Property change listeners that listener to PropertyChangeEvent from menu bar*/
   private PropertyChangeListener propertyChangeListener;
 
   /* menu bar for which this UI delegate is for*/
   protected JMenuBar menuBar;
   
   /* MouseListener that listens to the mouseEvents fired from menu bar*/
@@ -169,27 +217,64 @@
   protected void installDefaults()
   {
     LookAndFeel.installBorder(menuBar, "MenuBar.border");
     LookAndFeel.installColorsAndFont(menuBar, "MenuBar.background",
                                      "MenuBar.foreground", "MenuBar.font");
     menuBar.setOpaque(true);
   }
 
   /**
    * This method installs the keyboard actions for the JMenuBar.
    */
   protected void installKeyboardActions()
-    throws NotImplementedException
   {
-    // FIXME: implement
+    // Install InputMap.
+    Object[] bindings =
+      (Object[]) SharedUIDefaults.get("MenuBar.windowBindings");
+    InputMap inputMap = LookAndFeel.makeComponentInputMap(menuBar, bindings);
+    SwingUtilities.replaceUIInputMap(menuBar,
+                                     JComponent.WHEN_IN_FOCUSED_WINDOW,
+                                     inputMap);
+
+    // Install ActionMap.
+    SwingUtilities.replaceUIActionMap(menuBar, getActionMap());
+  }
+
+  /**
+   * Creates and returns the shared action map for JTrees.
+   *
+   * @return the shared action map for JTrees
+   */
+  private ActionMap getActionMap()
+  {
+    ActionMap am = (ActionMap) UIManager.get("Tree.actionMap");
+    if (am == null)
+      {
+        am = createDefaultActions();
+        UIManager.getLookAndFeelDefaults().put("Tree.actionMap", am);
+      }
+    return am;
+  }
+
+  /**
+   * Creates the default actions when there are none specified by the L&F.
+   *
+   * @return the default actions
+   */
+  private ActionMap createDefaultActions()
+  {
+    ActionMapUIResource am = new ActionMapUIResource();
+    Action action = new FocusAction();
+    am.put(action.getValue(Action.NAME), action);
+    return am;
   }
 
   /**
    * This method installs the listeners needed for this UI to function.
    */
   protected void installListeners()
   {
     menuBar.addContainerListener(containerListener);
     menuBar.addPropertyChangeListener(propertyChangeListener);
     menuBar.addMouseListener(mouseListener);
   }
 
@@ -217,27 +302,28 @@
   protected void uninstallDefaults()
   {
     menuBar.setBackground(null);
     menuBar.setBorder(null);
     menuBar.setFont(null);
     menuBar.setForeground(null);
   }
 
   /**
    * This method reverses the work done in installKeyboardActions.
    */
   protected void uninstallKeyboardActions()
-    throws NotImplementedException
   {
-    // FIXME: implement. 
+    SwingUtilities.replaceUIInputMap(menuBar,
+                                     JComponent.WHEN_IN_FOCUSED_WINDOW, null);
+    SwingUtilities.replaceUIActionMap(menuBar, null);
   }
 
   /**
    * Unregisters all the listeners that this UI delegate was using.
    */
   protected void uninstallListeners()
   {
     menuBar.removeContainerListener(containerListener);
     menuBar.removePropertyChangeListener(propertyChangeListener);
     menuBar.removeMouseListener(mouseListener);
   }
 

Reply via email to