This fixes BasicListUI.installKeyboardActions to fit with the
InputMap/ActionMap architecture, and implements the missing
uninstallKeyboardActions.

2006-06-26  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicListUI.java
        (installKeyboardActions): Rewritten to fit with the
        ActionMap/InputMap architecture.
        (uninstallKeyboardActions): Implemented.
        (ListAction): Made private. Added TODO for splitting
        up this bulk Action.
        (ListAction.ListAction): New constructor. This one
        takes a cmd parameter to be installed as actionCommand.

/Roman

-- 
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: javax/swing/plaf/basic/BasicListUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicListUI.java,v
retrieving revision 1.58
diff -u -2 -0 -r1.58 BasicListUI.java
--- javax/swing/plaf/basic/BasicListUI.java	1 Jun 2006 05:17:02 -0000	1.58
+++ javax/swing/plaf/basic/BasicListUI.java	26 Jun 2006 13:59:34 -0000
@@ -21,80 +21,78 @@
 Linking this library statically or dynamically with other modules is
 making a combined work based on this library.  Thus, the terms and
 conditions of the GNU General Public License cover the whole
 combination.
 
 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 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.Component;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Insets;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.awt.event.MouseEvent;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 
 import javax.swing.AbstractAction;
 import javax.swing.ActionMap;
 import javax.swing.CellRendererPane;
 import javax.swing.DefaultListSelectionModel;
 import javax.swing.InputMap;
 import javax.swing.JComponent;
 import javax.swing.JList;
-import javax.swing.KeyStroke;
 import javax.swing.ListCellRenderer;
 import javax.swing.ListModel;
 import javax.swing.ListSelectionModel;
 import javax.swing.LookAndFeel;
 import javax.swing.SwingUtilities;
+import javax.swing.TransferHandler;
 import javax.swing.UIDefaults;
 import javax.swing.UIManager;
 import javax.swing.event.ListDataEvent;
 import javax.swing.event.ListDataListener;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 import javax.swing.event.MouseInputListener;
 import javax.swing.plaf.ActionMapUIResource;
 import javax.swing.plaf.ComponentUI;
-import javax.swing.plaf.InputMapUIResource;
 import javax.swing.plaf.ListUI;
+import javax.swing.plaf.UIResource;
 
 /**
  * The Basic Look and Feel UI delegate for the 
  * JList.
  */
 public class BasicListUI extends ListUI
 {
 
   /**
    * A helper class which listens for [EMAIL PROTECTED] FocusEvent}s
    * from the JList.
    */
   public class FocusHandler implements FocusListener
   {
     /**
      * Called when the JList acquires focus.
      *
      * @param e The FocusEvent representing focus acquisition
      */
     public void focusGained(FocusEvent e)
@@ -198,43 +196,60 @@
   {
     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);
     }
   }
-  
-  class ListAction extends AbstractAction
+
+  /**
+   * Implements the action for the JList's keyboard commands.
+   */
+  private class ListAction
+    extends AbstractAction
   {
+    // TODO: Maybe make a couple of classes out of this bulk Action.
+    // Form logical groups of Actions when doing this.
+
+    /**
+     * Creates a new ListAction for the specified command.
+     *
+     * @param cmd the actionCommand to set
+     */
+    ListAction(String cmd)
+    {
+      putValue(ACTION_COMMAND_KEY, cmd);
+    }
+
     public void actionPerformed(ActionEvent e)
     {
       int lead = list.getLeadSelectionIndex();
       int max = list.getModel().getSize() - 1;
       DefaultListSelectionModel selModel 
           = (DefaultListSelectionModel) list.getSelectionModel();
       String command = e.getActionCommand();
       // Do nothing if list is empty
       if (max == -1)
         return;
       
       if (command.equals("selectNextRow"))
         {
           selectNextIndex();
         }
       else if (command.equals("selectPreviousRow"))
         {
           selectPreviousIndex();
         }
       else if (command.equals("clearSelection"))
@@ -381,41 +396,41 @@
         }
       else if (command.equals("toggleAndAnchor"))
         {
           if (!list.isSelectedIndex(lead))
             list.addSelectionInterval(lead, lead);
           else
             list.removeSelectionInterval(lead, lead);
           selModel.setAnchorSelectionIndex(lead);
         }
       else 
         {
           // DEBUG: uncomment the following line to print out 
           // key bindings that aren't implemented yet
           
           // System.out.println ("not implemented: "+e.getActionCommand());
         }
       
       list.ensureIndexIsVisible(list.getLeadSelectionIndex());
     }
   }
-     
+
   /**
    * A helper class which listens for [EMAIL PROTECTED] MouseEvent}s 
    * from the [EMAIL PROTECTED] JList}.
    */
   public class MouseInputHandler implements MouseInputListener
   {
     /**
      * Called when a mouse button press/release cycle completes
      * on the [EMAIL PROTECTED] JList}
      *
      * @param event The event representing the mouse click
      */
     public void mouseClicked(MouseEvent event)
     {
       Point click = event.getPoint();
       int index = locationToIndex(list, click);
       if (index == -1)
         return;
       if (event.isShiftDown())
         {
@@ -975,73 +990,117 @@
   }
 
   /**
    * Detaches all the listeners we attached in [EMAIL PROTECTED] #installListeners}.
    */
   protected void uninstallListeners()
   {
     list.removeFocusListener(focusListener);
     list.getModel().removeListDataListener(listDataListener);
     list.removeListSelectionListener(listSelectionListener);
     list.removeMouseListener(mouseInputListener);
     list.removeMouseMotionListener(mouseInputListener);
     list.removePropertyChangeListener(propertyChangeListener);
   }
   
   /**
    * Installs keyboard actions for this UI in the [EMAIL PROTECTED] JList}.
    */
   protected void installKeyboardActions()
   {
+    // Install UI InputMap.
     InputMap focusInputMap = (InputMap) UIManager.get("List.focusInputMap");
-    InputMapUIResource parentInputMap = new InputMapUIResource();
-    // FIXME: The JDK uses a LazyActionMap for parentActionMap
-    ActionMap parentActionMap = new ActionMapUIResource();
-    action = new ListAction();
-    Object keys[] = focusInputMap.allKeys();
-    // Register key bindings in the UI InputMap-ActionMap pair
-    for (int i = 0; i < keys.length; i++)
+    SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED,
+                                     focusInputMap);
+
+    // Install UI ActionMap.
+    ActionMap am = (ActionMap) UIManager.get("List.actionMap");
+    if (am == null)
       {
-        KeyStroke stroke = (KeyStroke) keys[i];
-        String actionString = (String) focusInputMap.get(stroke);
-        parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),
-                                                  stroke.getModifiers()),
-                           actionString);
+        // Create the actionMap once and store it in the current UIDefaults
+        // for use in other components.
+        am = new ActionMapUIResource();
+        ListAction action;
+        action = new ListAction("selectPreviousRow");
+        am.put("selectPreviousRow", action);
+        action = new ListAction("selectNextRow");
+        am.put("selectNextRow", action);
+        action = new ListAction("selectPreviousRowExtendSelection");
+        am.put("selectPreviousRowExtendSelection", action);
+        action = new ListAction("selectNextRowExtendSelection");
+        am.put("selectNextRowExtendSelection", action);
+
+        action = new ListAction("selectPreviousColumn");
+        am.put("selectPreviousColumn", action);
+        action = new ListAction("selectNextColumn");
+        am.put("selectNextColumn", action);
+        action = new ListAction("selectPreviousColumnExtendSelection");
+        am.put("selectPreviousColumnExtendSelection", action);
+        action = new ListAction("selectNextColumnExtendSelection");
+        am.put("selectNextColumnExtendSelection", action);
+
+        action = new ListAction("selectFirstRow");
+        am.put("selectFirstRow", action);
+        action = new ListAction("selectLastRow");
+        am.put("selectLastRow", action);
+        action = new ListAction("selectFirstRowExtendSelection");
+        am.put("selectFirstRowExtendSelection", action);
+        action = new ListAction("selectLastRowExtendSelection");
+        am.put("selectLastRowExtendSelection", action);
+
+        action = new ListAction("scrollUp");
+        am.put("scrollUp", action);
+        action = new ListAction("scrollUpExtendSelection");
+        am.put("scrollUpExtendSelection", action);
+        action = new ListAction("scrollDown");
+        am.put("scrollDown", action);
+        action = new ListAction("scrollDownExtendSelection");
+        am.put("scrollDownExtendSelection", action);
+
+        action = new ListAction("selectAll");
+        am.put("selectAll", action);
+        action = new ListAction("clearSelection");
+        am.put("clearSelection", action);
+
+        am.put("copy", TransferHandler.getCopyAction());
+        am.put("cut", TransferHandler.getCutAction());
+        am.put("paste", TransferHandler.getPasteAction());
 
-        parentActionMap.put(actionString, 
-                            new ActionListenerProxy(action, actionString));
+        UIManager.put("List.actionMap", am);
       }
-    // Register the new InputMap-ActionMap as the parents of the list's
-    // InputMap and ActionMap
-    parentInputMap.setParent(list.getInputMap().getParent());
-    parentActionMap.setParent(list.getActionMap().getParent());
-    list.getInputMap().setParent(parentInputMap);
-    list.getActionMap().setParent(parentActionMap);
+
+    SwingUtilities.replaceUIActionMap(list, am);
   }
 
   /**
    * Uninstalls keyboard actions for this UI in the [EMAIL PROTECTED] JList}.
    */
   protected void uninstallKeyboardActions()
-    throws NotImplementedException
   {
-    // TODO: Implement this properly.
+    // Uninstall the InputMap.
+    InputMap im = SwingUtilities.getUIInputMap(list, JComponent.WHEN_FOCUSED);
+    if (im instanceof UIResource)
+      SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED, null);
+
+    // Uninstall the ActionMap.
+    if (SwingUtilities.getUIActionMap(list) instanceof UIResource)
+      SwingUtilities.replaceUIActionMap(list, null);
   }
 
   /**
    * Installs the various aspects of the UI in the [EMAIL PROTECTED] JList}. In
    * particular, calls [EMAIL PROTECTED] #installDefaults}, [EMAIL PROTECTED] #installListeners}
    * and [EMAIL PROTECTED] #installKeyboardActions}. Also saves a reference to the
    * provided component, cast to a [EMAIL PROTECTED] JList}.
    *
    * @param c The [EMAIL PROTECTED] JList} to install the UI into
    */
   public void installUI(final JComponent c)
   {
     super.installUI(c);
     list = (JList) c;
     installDefaults();
     installListeners();
     installKeyboardActions();
     maybeUpdateLayoutState();
   }
 

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

Reply via email to