Class SelectionDescription

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.Integer>

    public final class SelectionDescription
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.Integer>
    Generic description of selection. Each selection can be in one of the following (disjunct) states:

    SelectionDescription is an immutable object - once it is created it cannot be changed. You need to create a new one if you want to change the selection.

    SelectionDescription implements Iterable interface which iterates through selected indices of the instance. Note that this allows SelectionDescription instance to be used in foreach loop:

      SelectionDescription selection = ...;
      for (Integer selectedIndex : selection) {
          // do something with selectedIndex
      }
     

    Selection have a subselection which is represented as another SelectionDescription instance and can be obatined with getSubSelection() method. Subselection contains a subset of indexes of its main selection. E.g.:

       Selection: {100, 101, ..., 110}
       Subselection: {102, 105, 106}
     

    To get a relative subselection, i.e. indexes into main selection, use getRelativeSubSelection() method. There are several other methods to work with subselection. See Javadoc of individual methods for details.

    Author:
    Petr Hamernik
    • Method Detail

      • create

        public static SelectionDescription create​(SelectionDescription oldSel,
                                                  java.util.List<? extends java.lang.Comparable<?>> oldIds,
                                                  java.util.List<? extends java.lang.Comparable<?>> newIds)
        This method can be used when ids are reordered or completely changed and you want to recalculate the selection. SelectionDescription object works with indexes and never with real values. It means when IDs (primary key values) are for example reordered, the new selection indexes must be recalculated. You must provide both (original and a new) lists of IDS and of course the original selection. New selection is computed from these objects.

        New selection contains obviously only those values which are present in newIds list. It means new selection will contain the same or less selected rows. In extreme case new selection can be EMPTY (if no originally selected value is present in newIds).

        Parameters:
        oldSel - The original selection
        oldIds - The original list of ids (usually primary key values)
        newIds - The new list of ids (can contain the same or completely different values)
        Returns:
        The new SelectionDescription
      • create

        public static SelectionDescription create​(java.util.List<? extends java.lang.Integer> indexes)
        Crete a new selection based on the given indexes.
        Parameters:
        indexes - indexes to be selected.
        Returns:
        a new selection description
      • create

        public static SelectionDescription create​(int singleRowIndex)
        Returns:
        New single row selection.
      • create

        public static SelectionDescription create​(javax.swing.ListSelectionModel listSelectionModel,
                                                  SelectionDescription subSelection)
        Returns a new selection having main selection corresponding to a given ListSelectionModel with subselection set to given subSelection parameter.
        Parameters:
        listSelectionModel - main selection
        subSelection - subselection
        Returns:
        new selection
      • create

        public static SelectionDescription create​(SelectionDescription selection,
                                                  javax.swing.ListSelectionModel listSubSelectionModel)
        Returns a new selection having main selection same as a given selection and subselection corresponding to a given ListSelectionModel.
        Parameters:
        selection - main selection
        listSubSelectionModel - subselection
        Returns:
        new selection
      • create

        public static SelectionDescription create​(javax.swing.ListSelectionModel listSelectionModel)
        Returns a new selection from a ListSelectionModel with empty subselection.
        Parameters:
        listSelectionModel - main selection
        Returns:
        new selection
      • create

        public static SelectionDescription create​(int minIndex,
                                                  int maxIndex)
        Returns:
        New selection for a single interval [min,max]. All values inside the interval (including min and max) are selected.
      • move

        public static SelectionDescription move​(SelectionDescription origSelection,
                                                int delta,
                                                int rowCount)
        Parameters:
        origSelection - Original selection to be moved
        delta - The number of rows to move the selection. Can be either positive or negative number.
        rowCount - The total number of rows in the table. This parameter is here because of overflow prevention. New selection will respect this table size and won't go over it.
        Returns:
        A new moved selection based on original one.
      • convertToAbsoluteSubSelection

        public static SelectionDescription convertToAbsoluteSubSelection​(SelectionDescription selection,
                                                                         javax.swing.ListSelectionModel relativeSubselection)
        Takes relative subselection in form of a ListSelectionModel and converts it to absolute subselection within the given selection. I.e. if range of the given selection is <100, 110> and given relativeSubselection contains (3, 4, 5, 8) the result is a new SelectionDescription instance with (103, 104, 105, 108) indices.
        Parameters:
        selection - main selection
        relativeSubselection - relative subselection within the given selection
        Returns:
        absolute subselection within the given selection
      • convertToAbsoluteSubSelection

        public static SelectionDescription convertToAbsoluteSubSelection​(SelectionDescription selection,
                                                                         java.util.Collection<? extends java.lang.Integer> relativeSubselection)
        Takes relative subselection in form of a Collection and converts it to absolute subselection within the given selection. I.e. if range of the given selection is <100, 110> and given relativeSubselection contains (3, 4, 5, 8) the result is a new SelectionDescription instance with (103, 104, 105, 108) indices.
        Parameters:
        selection - main selection
        relativeSubselection - relative subselection within the given selection
        Returns:
        absolute subselection within the given selection
      • getRelativeSubSelection

        public SelectionDescription getRelativeSubSelection()
        Returns subselection of this selection but with indexes relative to this selection. I.e. if the selection represents range <100, 110> and its subselection is (103, 104, 105, 108), the returned selection is (3, 4, 5, 8).
        Returns:
        subselection of this selection with indexes relative to this selection
      • getSubSelection

        public SelectionDescription getSubSelection()
        Returns subselection within this selection.
        Returns:
        subselection; never null
      • iterator

        public java.util.Iterator<java.lang.Integer> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Integer>
      • getMinSelectionIndex

        public int getMinSelectionIndex()
        Returns:
        The first selected index or -1 if the selection is empty.
      • getMaxSelectionIndex

        public int getMaxSelectionIndex()
        Returns:
        The last selected index or -1 if the selection is empty.
      • getSelectedRowsCount

        public int getSelectedRowsCount()
        Returns:
        Count of rows represented by this selection.
      • isSingleRow

        public boolean isSingleRow()
        Returns:
        Does this selection represent a single row?
      • isSingleInterval

        public boolean isSingleInterval()
        Returns:
        Does this selection represent a single interval?
      • isMultiInterval

        public boolean isMultiInterval()
        Returns:
        Does this selection represent more intervals?
      • isEmpty

        public boolean isEmpty()
        Returns:
        Is the selection empty?
      • isSelectedIndex

        public boolean isSelectedIndex​(int index)
        Returns:
        True if the specified index is selected.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toStringSimple

        public java.lang.String toStringSimple​(boolean startFromRowOne)
        Parameters:
        startFromRowOne - If true first row has number 1; otherwise 0.
        Returns:
        Only selected rows indexes. For empty selection returns "", for single row selection returns e.g. "2", for interval returns e.g. "4-6"
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object