Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6862171

DefaultListSelectionModel: inconsistent lead on removeIndexInterval

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 6u14
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b05, mixed mode)
      6 Update 14

      ADDITIONAL OS VERSION INFORMATION :
      not relevant

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      not relevant

      A DESCRIPTION OF THE PROBLEM :
      When removing the indexInterval from 0 to maxSelection, the lead/anchor is
      -1 for maxSelection > 0
      0 for maxSelection == 0

      This is inconsistent: in both cases we remove all possibly selected indices, so the lead/anchor should be the same in both. The correct (expected by me ;-) behaviour is to reset the lead/anchor to -1 (nothing selected, no lead, no anchor).

      This is relevant in contexts where clients must keep the lead valid (f.i. in-bounds of the size of a listModel).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the unit test, the passing fixture is expected, the failing is not <g>

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      see above
      ACTUAL -
      see above

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * $Id: ListSelectionIssues.java,v 1.4 2008/02/12 12:18:36 kleopatra Exp $
       *
       * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
       * Santa Clara, California 95054, U.S.A. All rights reserved.
       *
       * This library is free software; you can redistribute it and/or
       * modify it under the terms of the GNU Lesser General Public
       * License as published by the Free Software Foundation; either
       * version 2.1 of the License, or (at your option) any later version.
       *
       * This library 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
       * Lesser General Public License for more details.
       *
       * You should have received a copy of the GNU Lesser General Public
       * License along with this library; if not, write to the Free Software
       * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
       *
       */
      package org.jdesktop.swingx.decorator;

      import javax.swing.DefaultListSelectionModel;
      import javax.swing.ListSelectionModel;

      import junit.framework.TestCase;

      /**
       * Test to expose inconsistent lead/anchor state after removeIndexInterval in
       * <code>DefaultListSelectionModel</code>.
       *
       *
       * @author Jeanette Winzenburg
       */
      public class ListSelectionBug extends TestCase {


          /**
           * sanity: understand DefaultListSelectionModel behaviour.
           *
           *
           * Here: select index 5, remove 0..5 -> lead == -1
           * This is what I expect - explicitly removing the complete interval from
           * 0 to the maxSelected should reset the lead/anchor to "no lead/anchor"
           */
          public void testLeadAnchorAfterRemoveAll() {
              ListSelectionModel viewSelectionModel = new DefaultListSelectionModel();
              assertLeadAnchorAfterRemoveAll(viewSelectionModel, 5, 0);
          }

          /**
           * sanity: understand DefaultListSelectionModel behaviour.
           *
           * Here: select 0, remove 0..0 --> lead == 0
           *
           * This is unexpected - again we remove the complete interval from
           * 0 to maxSelected, so the lead/anchor should be reset to "no lead/anchor"
           * just in the same way as it is for any maxSelected > 0.
           */
          public void testLeadAnchorAfterRemoveAll0() {
              ListSelectionModel viewSelectionModel = new DefaultListSelectionModel();
              assertLeadAnchorAfterRemoveAll(viewSelectionModel, 0, 0);
          }

          /**
           * Asserts lead/anchor after removeIndexInterval.
           * The setup is to select selectedIndex and removes the
           * index interval [first, selected].
           *
           * @param viewSelectionModel
           * @param selected
           * @param firstOfRemoveInterval, <= selected
           */
          private void assertLeadAnchorAfterRemoveAll(
                  ListSelectionModel viewSelectionModel, int selected, int firstOfRemoveInterval) {
              viewSelectionModel.setSelectionInterval(selected, selected);
              assertEquals(selected, viewSelectionModel.getAnchorSelectionIndex());
              assertEquals(selected, viewSelectionModel.getLeadSelectionIndex());
              int length = selected - firstOfRemoveInterval + 1;
              viewSelectionModel.removeIndexInterval(firstOfRemoveInterval, selected);
              int leadAnchor = selected - length;
              assertTrue(viewSelectionModel.isSelectionEmpty());
              assertEquals(leadAnchor, viewSelectionModel.getLeadSelectionIndex());
              assertEquals(leadAnchor, viewSelectionModel.getAnchorSelectionIndex());
          }
          
      }

      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: