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

Add getSelectedIndices() to ListSelectionModel

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P4
    • 11
    • client-libs
    • None
    • source
    • minimal
    • Hide
      This is only code structure change and the client code using these classes JList, JTable and DefaultTableColumnModel will not see any change.
      New functions have been added to the ListSelectionModel which are default functions in Interface. So no compatibility risk there as well.
      Show
      This is only code structure change and the client code using these classes JList, JTable and DefaultTableColumnModel will not see any change. New functions have been added to the ListSelectionModel which are default functions in Interface. So no compatibility risk there as well.
    • Java API

    Description

      Summary

      Add getSelectedIndices function to ListSelectionModel Interface as same function with duplicate implementation is being used in JList, JTable and DefaultTableColumnModel.

      Problem

      Currently, JList, JTable and DefaultTableColumnModel have there own implementation of getSelectedIndices function.

      • JList has a method getSelectedIndices(), which gets the indices from its ListSelectionModel.
      • JTable has a method getSelectedRows(), which gets the indices from its ListSelectionModel.
      • DefaultTableColumnModel has a method getSelectedColumns(), which gets the indices from its ListSelectionModel.

      These functions are same, which results in lot of duplicate code. It makes sense to add the getSelectedIndices function to ListSelectionModel interface.

      Solution

      Solution is to add the getSelectedIndices function to the ListSelectionModel and then call this function from JList, JTable and DefaultTableColumnModel . This can be done by using default functions in interfaces to maintain backwards compatibility.

      The solution also changes getSelectedValuesList function in JList to use this function instead of duplicating the same code.

      In addition to that, the getSelectedItemsCount function is also added to ListSelectionModel as similar function is implemented at different places. The getSelectedRowCount and getSelectedColumnCount functions in JTable and DefaultTableColumnModel respectively have also been changed to call getSelectedItemsCount. Webrev: http://cr.openjdk.java.net/~pbansal/8074286/webrev.03/

      Specification

      Providing the list of methods whose specification is being changed

      1) src/java.desktop/share/classes/javax/swing/ListSelectionModel.java

      /**
       * Returns an array of all of the selected indices in the selection model,
       * in increasing order.
       *
       * @return all of the selected indices, in increasing order,
       *         or an empty array if nothing is selected
       * @see #removeSelectionInterval
       * @see #addListSelectionListener
       * @since 11
       * @implSpec The default implementation iterates from minimum selected
       * index {@link #getMinSelectionIndex()} to maximum selected index {@link
       * #getMaxSelectionIndex()} and returns the selected indices {@link
       * #isSelectedIndex(int)} in a newly allocated int array.
       */
      default int[] getSelectedIndices() {}
      
      
      /**
       * Returns the number of selected items.
       *
       * @return the number of selected items, 0 if no items are selected
       * @since 11
       * @implSpec The default implementation iterates from minimum selected
       * index {@link #getMinSelectionIndex()} to maximum selected index {@link
       * #getMaxSelectionIndex()} and returns the number of selected indices
       * {@link #isSelectedIndex(int)}
       */
      default int getSelectedItemsCount() {}

      Providing the public methods that are being affected here, but the specification is not being changed

      1) src/java.desktop/share/classes/javax/swing/JList.java
      
      public int[] getSelectedIndices() {}    
      public List<E> getSelectedValuesList() {} 2) src/java.desktop/share/classes/javax/swing/JTable.java public int[] getSelectedRows() {} public int getSelectedRowCount() {} 3) src/java.desktop/share/classes/javax/swing/table/DefaultTableColumnModel.java public int[] getSelectedColumns() {} public int getSelectedColumnCount() {}

      Attachments

        1. webrev.00.zip
          1.02 MB
        2. webrev.01.zip
          1.12 MB
        3. webrev.02.zip
          1.34 MB
        4. webrev.03.zip
          1.34 MB

        Issue Links

          Activity

            People

              pbansal Pankaj Bansal (Inactive)
              webbuggrp Webbug Group
              Prasanta Sadhukhan, Sergey Bylokhov
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: