classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] Patch: JList multiple selection and keyboard selection


From: Roman Kennke
Subject: Re: [cp-patches] Patch: JList multiple selection and keyboard selection
Date: Fri, 17 Jun 2005 12:11:53 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

Hi Tony,

This is a more comprehensive patch than the last one I sent, it should
be used instead.

Allows for multiple selection in JLists as well as selection/navigation
of the JList using the keyboard.
This patch looks very good to me. The only little thing I changed before committing it is the stunt you made for finding out if ctrl is pressed when a mouseClick is received. This can be done easier, the isControlDown() method is also available in MouseEvent. Besides that, good work, thank you!

Let me give you some general hints for sending in patches (makes life easier for all of us):

- please don't include ChangeLog entries in the patch itself, only in the email body to classpath-patches - please seperate formatting changes from code changes (send two patches in, one for formatting/documentation changes, one for actual code changes)

This is mainly so that code review and merging between branches / repositories is easier.

I committed the slightly changed attached patch.

2005-06-16  Anthony Balkissoon  <address@hidden>

        * javax/swing/DefaultListSelectionModel.java:
        (addSelectionInterval): Added update to leadSelectionIndex
        and anchorSelectionIndex variables.
        (removeSelectionInterval): Same as above.
        (setSelectionInterval): Same as above.
        * javax/swing/JList.java:
        (getSelectedIndices): Increased for loop upper bound by 1.
* javax/swing/plaf/basic/BasicListUI.java: (KeyHandler): New class.
        (MouseInputHandler): Moved code from MousePressed to
        MouseClicked.
        (MouseInputHandler.MouseClicked): Added check for control
        key being down.

/Roman

Index: javax/swing/DefaultListSelectionModel.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/DefaultListSelectionModel.java,v
retrieving revision 1.13
diff -u -r1.13 DefaultListSelectionModel.java
--- javax/swing/DefaultListSelectionModel.java  23 May 2005 11:24:38 -0000      
1.13
+++ javax/swing/DefaultListSelectionModel.java  17 Jun 2005 09:59:14 -0000
@@ -240,7 +240,7 @@
     int R2 = Math.max(anchorSelectionIndex, oldLeadIndex);
     int S1 = Math.min(anchorSelectionIndex, leadIndex);
     int S2 = Math.max(anchorSelectionIndex, leadIndex);
-
+
     int lo = Math.min(R1, S1);
     int hi = Math.max(R2, S2);

@@ -262,9 +262,7 @@

     int beg = sel.nextSetBit(0), end = -1;
     for(int i=beg; i >= 0; i=sel.nextSetBit(i+1))
-      {
-        end = i;
-      }
+      end = i;
     fireValueChanged(beg, end, valueIsAdjusting);
   }

@@ -409,8 +407,24 @@
     int lo = Math.min(index0, index1);
     int hi = Math.max(index0, index1);

-    sel.set(lo, hi+1);
-    fireValueChanged(lo, hi, valueIsAdjusting);
+    /* We have to update the anchorSelectionIndex and leadSelectionIndex
+       variables */
+
+    /* The next if statements breaks down to "if this selection is adjacent
+       to the previous selection and going in the same direction" */
+    if (((index0 - 1 == leadSelectionIndex && (index1 >= index0)
+              && (leadSelectionIndex >= anchorSelectionIndex))
+             || (index0 + 1 == leadSelectionIndex && (index1 <= index0)
+                 && (leadSelectionIndex <= anchorSelectionIndex)))
+        && (anchorSelectionIndex != -1 || leadSelectionIndex != -1))
+      setLeadSelectionIndex(index1);
+    else
+      {
+        leadSelectionIndex = index1;
+        anchorSelectionIndex = index0;
+        sel.set(lo, hi+1);
+        fireValueChanged(lo, hi, valueIsAdjusting);
+      }
   }


@@ -430,6 +444,10 @@
     int lo = Math.min(index0, index1);
     int hi = Math.max(index0, index1);
     sel.clear(lo, hi+1);
+    //update anchorSelectionIndex and leadSelectionIndex variables
+    //TODO: will probably need MouseDragged to test properly and know if this 
works
+    setAnchorSelectionIndex(index0);
+    leadSelectionIndex = index1;
     fireValueChanged(lo, hi, valueIsAdjusting);
   }

@@ -462,6 +480,9 @@
     int hi = Math.max(index0, index1);
     sel.set(lo, hi+1);
     fireValueChanged(lo, hi, valueIsAdjusting);
+    // update the anchorSelectionIndex and leadSelectionIndex variables
+    setAnchorSelectionIndex(index0);
+    leadSelectionIndex=index1;
   }

   /**
Index: javax/swing/JList.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JList.java,v
retrieving revision 1.26
diff -u -r1.26 JList.java
--- javax/swing/JList.java      30 May 2005 12:58:01 -0000      1.26
+++ javax/swing/JList.java      17 Jun 2005 09:59:15 -0000
@@ -633,7 +633,7 @@
     lo = selectionModel.getMinSelectionIndex();
     hi = selectionModel.getMaxSelectionIndex();
     n = 0;
-    for (i = lo; i < hi; ++i)
+    for (i = lo; i <= hi; ++i)
       if (selectionModel.isSelectedIndex(i))
         n++;
     int [] v = new int[n];
Index: javax/swing/plaf/basic/BasicListUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicListUI.java,v
retrieving revision 1.18
diff -u -r1.18 BasicListUI.java
--- javax/swing/plaf/basic/BasicListUI.java     4 Jun 2005 19:16:11 -0000       
1.18
+++ javax/swing/plaf/basic/BasicListUI.java     17 Jun 2005 09:59:15 -0000
@@ -49,6 +49,8 @@
 import java.awt.event.ComponentListener;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
@@ -184,6 +186,47 @@
   }

   /**
+   * A helper class which listens for address@hidden KeyEvents}s
+   * from the address@hidden JList}.
+   */
+  private class KeyHandler extends KeyAdapter
+  {
+    public KeyHandler()
+    {
+    }
+
+    public void keyPressed( KeyEvent evt )
+    {
+      if (evt.getKeyCode() == KeyEvent.VK_DOWN)
+        {
+          int lead = BasicListUI.this.list.getLeadSelectionIndex();
+          if (!evt.isShiftDown())
+            {
+              BasicListUI.this.list.clearSelection();
+              BasicListUI.this.list.setSelectedIndex(lead+1);
+            }
+          else
+            {
+              
BasicListUI.this.list.getSelectionModel().setLeadSelectionIndex(lead+1);
+            }
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_UP)
+        {
+          int lead = BasicListUI.this.list.getLeadSelectionIndex();
+          if (!evt.isShiftDown())
+            {
+              BasicListUI.this.list.clearSelection();
+              BasicListUI.this.list.setSelectedIndex(Math.max(lead-1,0));
+            }
+          else
+            {
+              
BasicListUI.this.list.getSelectionModel().setLeadSelectionIndex(Math.max(lead-1,0));
+            }
+        }
+    }
+  }
+
+  /**
    * A helper class which listens for address@hidden MouseEvent}s
    * from the address@hidden JList}.
    */
@@ -197,6 +240,22 @@
      */
     public void mouseClicked(MouseEvent event)
     {
+      Point click = event.getPoint();
+      int index = BasicListUI.this.locationToIndex(list, click);
+      if (index == -1)
+        return;
+      boolean controlPressed = event.isControlDown();
+      if (controlPressed)
+        {
+          if (BasicListUI.this.list.getSelectionMode() == 
ListSelectionModel.SINGLE_SELECTION)
+            BasicListUI.this.list.setSelectedIndex(index);
+          else if (BasicListUI.this.list.isSelectedIndex(index))
+            BasicListUI.this.list.removeSelectionInterval(index,index);
+          else
+            BasicListUI.this.list.addSelectionInterval(index,index);
+        }
+      else
+        BasicListUI.this.list.setSelectedIndex(index);
     }

     /**
@@ -207,12 +266,6 @@
      */
     public void mousePressed(MouseEvent event)
     {
-      Point click = event.getPoint();
-      int index = BasicListUI.this.locationToIndex(list, click);
-      if (index == -1)
-        return;
-
-      BasicListUI.this.list.setSelectedIndex(index);
     }

     /**
@@ -315,6 +368,9 @@
   /** The mouse listener listening to the list. */
   protected MouseInputListener mouseInputListener;

+  /** The key listener listening to the list */
+  private KeyHandler keyListener;
+
   /** The property change listener listening to the list. */
   protected PropertyChangeListener propertyChangeListener;

@@ -525,6 +581,7 @@
     listDataListener = new ListDataHandler();
     listSelectionListener = new ListSelectionHandler();
     mouseInputListener = new MouseInputHandler();
+    keyListener = new KeyHandler();
     propertyChangeListener = new PropertyChangeHandler();
     componentListener = new ComponentHandler();
     updateLayoutStateNeeded = 1;
@@ -572,6 +629,7 @@
     list.getModel().addListDataListener(listDataListener);
     list.addListSelectionListener(listSelectionListener);
     list.addMouseListener(mouseInputListener);
+    list.addKeyListener(keyListener);
     list.addMouseMotionListener(mouseInputListener);
     list.addPropertyChangeListener(propertyChangeListener);
     list.addComponentListener(componentListener);
@@ -586,6 +644,7 @@
     list.getModel().removeListDataListener(listDataListener);
     list.removeListSelectionListener(listSelectionListener);
     list.removeMouseListener(mouseInputListener);
+    list.removeKeyListener(keyListener);
     list.removeMouseMotionListener(mouseInputListener);
     list.removePropertyChangeListener(propertyChangeListener);
   }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]