Class SelectionDescription
- java.lang.Object
-
- com.im.df.api.support.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:- Empty selection:
isEmpty()
=> trueisSingleRow()
=> falseisSingleInterval()
=> falseisMultiInterval()
=> false
EMPTY
. You can compare it using == operator. - Single row selection:
isEmpty()
=> falseisSingleRow()
=> trueisSingleInterval()
=> falseisMultiInterval()
=> false.
- Single interval selection (e.g. rows 1-10):
isEmpty()
=> falseisSingleRow()
=> falseisSingleInterval()
=> trueisMultiInterval()
=> false.
- Multi interval selection (e.g. rows 1-10 and 20-30):
isEmpty()
=> falseisSingleRow()
=> falseisSingleInterval()
=> falseisMultiInterval()
=> true.
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 inforeach
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
-
-
Field Summary
Fields Modifier and Type Field Description static SelectionDescription
EMPTY
Empty selection.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SelectionDescription
complement(SelectionDescription sd1, SelectionDescription sd2)
static SelectionDescription
convertToAbsoluteSubSelection(SelectionDescription selection, java.util.Collection<? extends java.lang.Integer> relativeSubselection)
Takes relative subselection in form of aCollection
and converts it to absolute subselection within the givenselection
.static SelectionDescription
convertToAbsoluteSubSelection(SelectionDescription selection, javax.swing.ListSelectionModel relativeSubselection)
Takes relative subselection in form of aListSelectionModel
and converts it to absolute subselection within the givenselection
.static SelectionDescription
create(int singleRowIndex)
static SelectionDescription
create(int minIndex, int maxIndex)
static SelectionDescription
create(SelectionDescription selection, SelectionDescription subSelection)
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.static SelectionDescription
create(SelectionDescription selection, javax.swing.ListSelectionModel listSubSelectionModel)
Returns a new selection having main selection same as a givenselection
and subselection corresponding to a givenListSelectionModel
.static SelectionDescription
create(java.util.List<? extends java.lang.Integer> indexes)
Crete a new selection based on the given indexes.static SelectionDescription
create(javax.swing.ListSelectionModel listSelectionModel)
Returns a new selection from aListSelectionModel
with empty subselection.static SelectionDescription
create(javax.swing.ListSelectionModel listSelectionModel, SelectionDescription subSelection)
Returns a new selection having main selection corresponding to a givenListSelectionModel
with subselection set to givensubSelection
parameter.boolean
equals(java.lang.Object obj)
int
getMaxSelectionIndex()
int
getMinSelectionIndex()
SelectionDescription
getRelativeSubSelection()
Returns subselection of this selection but with indexes relative to this selection.int
getSelectedRowsCount()
SelectionDescription
getSubSelection()
Returns subselection within this selection.int
hashCode()
static SelectionDescription
intersection(SelectionDescription sd1, SelectionDescription sd2)
boolean
isEmpty()
boolean
isMultiInterval()
boolean
isSelectedIndex(int index)
boolean
isSingleInterval()
boolean
isSingleRow()
java.util.Iterator<java.lang.Integer>
iterator()
static SelectionDescription
move(SelectionDescription origSelection, int delta, int rowCount)
java.lang.String
toString()
java.lang.String
toStringSimple(boolean startFromRowOne)
static SelectionDescription
union(SelectionDescription sd1, SelectionDescription sd2)
-
-
-
Field Detail
-
EMPTY
public static final SelectionDescription EMPTY
Empty selection.
-
-
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 selectionoldIds
- 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
-
union
public static SelectionDescription union(SelectionDescription sd1, SelectionDescription sd2)
- Returns:
- Union of the given selection descriptions. I.e. all indices selected either in
sd1
or insd2
.
-
intersection
public static SelectionDescription intersection(SelectionDescription sd1, SelectionDescription sd2)
- Returns:
- Intersection of the given selection descriptions. I.e. all indices selected both -
sd1
andsd2
.
-
complement
public static SelectionDescription complement(SelectionDescription sd1, SelectionDescription sd2)
- Returns:
- Selection description containing relative complement of
sd2
insd1
. I.e. indices which are selected insd1
but not insd2
.
-
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 givenListSelectionModel
with subselection set to givensubSelection
parameter.- Parameters:
listSelectionModel
- main selectionsubSelection
- 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 givenselection
and subselection corresponding to a givenListSelectionModel
.- Parameters:
selection
- main selectionlistSubSelectionModel
- subselection- Returns:
- new selection
-
create
public static SelectionDescription create(javax.swing.ListSelectionModel listSelectionModel)
Returns a new selection from aListSelectionModel
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.
-
create
public static SelectionDescription create(SelectionDescription selection, SelectionDescription subSelection)
- 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 moveddelta
- 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 aListSelectionModel
and converts it to absolute subselection within the givenselection
. I.e. if range of the given selection is <100, 110> and givenrelativeSubselection
contains (3, 4, 5, 8) the result is a new SelectionDescription instance with (103, 104, 105, 108) indices.- Parameters:
selection
- main selectionrelativeSubselection
- relative subselection within the givenselection
- 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 aCollection
and converts it to absolute subselection within the givenselection
. I.e. if range of the given selection is <100, 110> and givenrelativeSubselection
contains (3, 4, 5, 8) the result is a new SelectionDescription instance with (103, 104, 105, 108) indices.- Parameters:
selection
- main selectionrelativeSubselection
- relative subselection within the givenselection
- 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 interfacejava.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 classjava.lang.Object
-
toStringSimple
public java.lang.String toStringSimple(boolean startFromRowOne)
- Parameters:
startFromRowOne
- Iftrue
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 classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-