org.grinvin.gui

Class SelectionModel<T>

Implemented Interfaces:
Iterable
Known Direct Subclasses:
GraphContext

public class SelectionModel<T>
extends Observable
implements Iterable

Keeps track of a selection of items of similar type. This model allows multiple selections (but no interval selections).

Field Summary

protected Collection
selection

Constructor Summary

SelectionModel()
Creates a new selection model with empty selection.
SelectionModel(Collection c)
Creates a new SelectionModel which uses the given collection to keep track of the selected objects.

Method Summary

void
addSelection(Collection c)
Add the objects in the given set to the selection.
void
addSelection(T c)
Add the given object to the selection.
void
clearSelection()
Clear the selection.
Collection
getSelection(Collection c)
Add the current selection to the given collection.
boolean
isSelected(T item)
Check whether the given element is selected.
boolean
isSelectionEmpty()
Check whether the current selection is empty.
Iterator
iterator()
Return an iterator that iterates over all selected elements.
protected void
notifySelectionChanged()
Tell all observers that the selection has changed.
int
selectionCount()
Return the number of items that is currently selected.
void
setSelection(Collection set)
Set the selection to be the given set of objects.
void
setSelection(T v)
Set the selection to be (only) the given object.
void
toggleSelection(Collection c)
Toggle the selection for the given set of objects.
void
toggleSelection(T item)
Toggles the selection.

Field Details

selection

protected Collection selection

Constructor Details

SelectionModel

public SelectionModel()
Creates a new selection model with empty selection. Uses a HashSet to keep track of the selected objects.

SelectionModel

public SelectionModel(Collection c)
Creates a new SelectionModel which uses the given collection to keep track of the selected objects.

Method Details

addSelection

public void addSelection(Collection c)
Add the objects in the given set to the selection.

addSelection

public void addSelection(T c)
Add the given object to the selection. Does nothing when the object was already selected.

clearSelection

public void clearSelection()
Clear the selection.

getSelection

public Collection getSelection(Collection c)
Add the current selection to the given collection.
Parameters:
c - Collection to which all currently selected elements are to be added.
Returns:
A reference to argument c

isSelected

public boolean isSelected(T item)
Check whether the given element is selected.
Returns:
whether the given item is currently selected.

isSelectionEmpty

public boolean isSelectionEmpty()
Check whether the current selection is empty.
Returns:
true if and only if the selection is empty.

iterator

public Iterator iterator()
Return an iterator that iterates over all selected elements. Clients should not use the remove-method of this iterator as removals will not be signaled to the observers of this model.

notifySelectionChanged

protected void notifySelectionChanged()
Tell all observers that the selection has changed.

selectionCount

public int selectionCount()
Return the number of items that is currently selected.

setSelection

public void setSelection(Collection set)
Set the selection to be the given set of objects.

setSelection

public void setSelection(T v)
Set the selection to be (only) the given object.

toggleSelection

public void toggleSelection(Collection c)
Toggle the selection for the given set of objects. The resulting selection is the symmetric difference of the current selection and the given set of elements.

toggleSelection

public void toggleSelection(T item)
Toggles the selection. Removes the given item from the selection if it already belongs to the selection. Otherwise adds it to the selection.