This fixes a regression in BasicTreeUI and reenables keyboard handling for JTree.

- The BasicTreeUI must requestFocus() on the JTree when a mouse click occurs. Lightweight components are no longer requestFocus()ed automatically. - The BasicMenuBarUI must store its ActionMap in MenuBar.actionMap, rather than Tree.actionMap. This is a copy+paste mistake.
- Also, this removes some cruft from BasicTreeUI.

2006-08-05  Roman Kennke  <[EMAIL PROTECTED]>

        PR 28650
        * javax/swing/plaf/basic/BasicMenuBarUI.java
        (getActionMap): Store ActionMap in MenuBar.actionMap, rather
        than Tree.actionMap.
        * javax/swing/plaf/basic/BasicTreeUI.java
        (action): Removed obsolete field.
        (uninstallKeyboardActions): Removed action field handling.
        (installKeyboardActions): Removed action field handling.
        (createDefaultActions): Don't install removed TreeAction.
        (TreeAction): Removed obsolete inner class.
        (ActionListenerProxy): Removed obsolete inner class.
        (MouseHandler.mousePressed): Request focus on JTree component
        on mouse press.

/Roman
Index: javax/swing/plaf/basic/BasicMenuBarUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicMenuBarUI.java,v
retrieving revision 1.18
diff -u -1 -2 -r1.18 BasicMenuBarUI.java
--- javax/swing/plaf/basic/BasicMenuBarUI.java	26 Jul 2006 07:51:04 -0000	1.18
+++ javax/swing/plaf/basic/BasicMenuBarUI.java	8 Aug 2006 12:08:16 -0000
@@ -237,29 +237,29 @@
 
     // 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");
+    ActionMap am = (ActionMap) UIManager.get("MenuBar.actionMap");
     if (am == null)
       {
         am = createDefaultActions();
-        UIManager.getLookAndFeelDefaults().put("Tree.actionMap", am);
+        UIManager.getLookAndFeelDefaults().put("MenuBar.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();
Index: javax/swing/plaf/basic/BasicTreeUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTreeUI.java,v
retrieving revision 1.151
diff -u -1 -2 -r1.151 BasicTreeUI.java
--- javax/swing/plaf/basic/BasicTreeUI.java	1 Aug 2006 14:45:46 -0000	1.151
+++ javax/swing/plaf/basic/BasicTreeUI.java	8 Aug 2006 12:08:17 -0000
@@ -229,27 +229,24 @@
   /** Path that is being edited. */
   protected TreePath editingPath;
 
   /**
    * Row that is being edited. Should only be referenced if editingComponent is
    * null.
    */
   protected int editingRow;
 
   /** Set to true if the editor has a different size than the renderer. */
   protected boolean editorHasDifferentSize;
 
-  /** The action bound to KeyStrokes. */
-  TreeAction action;
-
   /** Boolean to keep track of editing. */
   boolean isEditing;
 
   /** The current path of the visible nodes in the tree. */
   TreePath currentVisiblePath;
 
   /** The gap between the icon and text. */
   int gap = 4;
 
   /** The max height of the nodes in the tree. */
   int maxHeight;
   
@@ -1072,25 +1069,24 @@
     TreeCellEditor tce = tree.getCellEditor();
     if (tce != null)
       tce.removeCellEditorListener(cellEditorListener);
     if (treeModel != null)
       treeModel.removeTreeModelListener(treeModelListener);
   }
 
   /**
    * Uninstall all keyboard actions.
    */
   protected void uninstallKeyboardActions()
   {
-    action = null;
     tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).setParent(
                                                                               null);
     tree.getActionMap().setParent(null);
   }
 
   /**
    * Uninstall the rendererPane.
    */
   protected void uninstallComponents()
   {
     currentCellRenderer = null;
     rendererPane = null;
@@ -1289,26 +1285,24 @@
   protected void installKeyboardActions()
   {
     InputMap focusInputMap =
       (InputMap) SharedUIDefaults.get("Tree.focusInputMap");
     SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED,
                                      focusInputMap);
     InputMap ancestorInputMap =
       (InputMap) SharedUIDefaults.get("Tree.ancestorInputMap");
     SwingUtilities.replaceUIInputMap(tree,
                                  JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
                                  ancestorInputMap);
 
-    action = new TreeAction();
-
     SwingUtilities.replaceUIActionMap(tree, 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)
@@ -1320,27 +1314,24 @@
   }
 
   /**
    * 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;
 
-    action = new TreeAction();
-    am.put(action.getValue(Action.NAME), action);
-
     // TreeHomeAction.
     action = new TreeHomeAction(-1, "selectFirst");
     am.put(action.getValue(Action.NAME), action);
     action = new TreeHomeAction(-1, "selectFirstChangeLead");
     am.put(action.getValue(Action.NAME), action);
     action = new TreeHomeAction(-1, "selectFirstExtendSelection");
     am.put(action.getValue(Action.NAME), action);
     action = new TreeHomeAction(1, "selectLast");
     am.put(action.getValue(Action.NAME), action);
     action = new TreeHomeAction(1, "selectLastChangeLead");
     am.put(action.getValue(Action.NAME), action);
     action = new TreeHomeAction(1, "selectLastExtendSelection");
@@ -2051,112 +2042,24 @@
      * 
      * @param e the ActionEvent that caused this action.
      */
     public void actionPerformed(ActionEvent e)
     {
       TreePath lead = tree.getLeadSelectionPath();
       if (!tree.isEditing()) 
         tree.startEditingAtPath(lead);
     }
   }  
 
   /**
-   * This class implements the actions that we want to happen when specific keys
-   * are pressed for the JTree. The actionPerformed method is called when a key
-   * that has been registered for the JTree is received.
-   */
-  class TreeAction
-      extends AbstractAction
-  {
-
-    /**
-     * What to do when this action is called.
-     * 
-     * @param e the ActionEvent that caused this action.
-     */
-    public void actionPerformed(ActionEvent e)
-    {
-      String command = e.getActionCommand();
-      TreePath lead = tree.getLeadSelectionPath();
-
-      if (command.equals("selectPreviousChangeLead")
-          || command.equals("selectPreviousExtendSelection")
-          || command.equals("selectPrevious") || command.equals("selectNext")
-          || command.equals("selectNextExtendSelection")
-          || command.equals("selectNextChangeLead"))
-        (new TreeIncrementAction(0, "")).actionPerformed(e);
-      else if (command.equals("selectParent") || command.equals("selectChild"))
-        (new TreeTraverseAction(0, "")).actionPerformed(e);
-      else if (command.equals("selectAll"))
-        {
-          TreePath[] paths = new TreePath[treeState.getRowCount()];
-          for (int i = 0; i < paths.length; i++)
-            paths[i] = treeState.getPathForRow(i);
-          tree.addSelectionPaths(paths);
-        }
-      else if (command.equals("startEditing"))
-        tree.startEditingAtPath(lead);
-      else if (command.equals("toggle"))
-        {
-          if (tree.isEditing())
-            tree.stopEditing();
-          else
-            {
-              Object last = lead.getLastPathComponent();
-              TreePath path = new TreePath(getPathToRoot(last, 0));
-              if (! treeModel.isLeaf(last))
-                toggleExpandState(path);
-            }
-        }
-      else if (command.equals("clearSelection"))
-        tree.clearSelection();
-
-      if (tree.isEditing() && ! command.equals("startEditing"))
-        tree.stopEditing();
-
-      tree.scrollPathToVisible(tree.getLeadSelectionPath());
-    }
-  }
-
-  /**
-   * This class is used to mimic the behaviour of the JDK when registering
-   * keyboard actions. It is the same as the private class used in JComponent
-   * for the same reason. This class receives an action event and dispatches it
-   * to the true receiver after altering the actionCommand property of the
-   * event.
-   */
-  private static class ActionListenerProxy
-      extends AbstractAction
-  {
-    ActionListener target;
-
-    String bindingCommandName;
-
-    public ActionListenerProxy(ActionListener li, String cmd)
-    {
-      target = li;
-      bindingCommandName = cmd;
-    }
-
-    public void actionPerformed(ActionEvent e)
-    {
-      ActionEvent derivedEvent = new ActionEvent(e.getSource(), e.getID(),
-                                                 bindingCommandName,
-                                                 e.getModifiers());
-
-      target.actionPerformed(derivedEvent);
-    }
-  }
-
-  /**
    * Updates the preferred size when scrolling, if necessary.
    */
   public class ComponentHandler
       extends ComponentAdapter
       implements ActionListener
   {
     /**
      * Timer used when inside a scrollpane and the scrollbar is adjusting
      */
     protected Timer timer;
 
     /** ScrollBar that is being adjusted */
@@ -2520,24 +2423,27 @@
                       startEditTimer.start();
                     }
                   else
                     {
                       if (e.getClickCount() == 2)
                         toggleExpandState(path);
                       else
                         selectPathForEvent(path, e);
                     }
                 }
             }
         }
+
+      // We need to request the focus.
+      tree.requestFocusInWindow();
     }
 
     /**
      * Invoked when a mouse button is pressed on a component and then dragged.
      * MOUSE_DRAGGED events will continue to be delivered to the component where
      * the drag originated until the mouse button is released (regardless of
      * whether the mouse position is within the bounds of the component).
      * 
      * @param e is the mouse event that occured
      */
     public void mouseDragged(MouseEvent e)
     throws NotImplementedException

Reply via email to