public class SynchronizedViewerState extends Object implements ViewerState
ViewerState to
expose query and modification methods. ViewerStateChangeListeners can
be registered and will be notified about various state changes.
All methods of this class are synchronized, so that every individual
change to the viewer state is atomic. IllegalArgumentExceptions
thrown by the wrapped BasicViewerState are silently swallowed, under
the assumption that they result from concurrent changes (for example another
thread might have removed the source that you are trying to make current).
To perform sequences of operations atomically explicit synchronization is
required. In particular, this is true when using the collections returned by
getSources(), getActiveSources(), getGroups(),
getActiveGroups(), and getSourcesInGroup(SourceGroup).
These collections are backed by the ViewerState, they reflect changes, and
they are not thread-safe. It is possible to run into
ConcurrentModificationException when iterating them, etc.
Example where explicit synchronization is required:
List<SourceGroup> groupsContainingCurrentSource;
synchronized (state) {
SourceAndConverter<?> currentSource = state.getCurrentSource();
groupsContainingCurrentSource =
state.getGroups().stream()
.filter(g -> state.getSourcesInGroup(g).contains(currentSource))
.collect(Collectors.toList());
}
Alternatively, for read-only access, it is possible to (atomically) take an
unmodifiable snapshot() of the current state.
| Constructor and Description |
|---|
SynchronizedViewerState(ViewerState state) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
addGroup(SourceGroup group)
Add
group to the state. |
boolean |
addGroups(Collection<? extends SourceGroup> collection)
Add all groups in
collection to the state. |
boolean |
addSource(SourceAndConverter<?> source)
Add
source to the state. |
boolean |
addSources(Collection<? extends SourceAndConverter<?>> collection)
Add all sources in
collection to the state. |
boolean |
addSourcesToGroup(Collection<? extends SourceAndConverter<?>> collection,
SourceGroup group)
Add all sources in
collection to group. |
boolean |
addSourceToGroup(SourceAndConverter<?> source,
SourceGroup group)
Add
source to group. |
org.scijava.listeners.Listeners<ViewerStateChangeListener> |
changeListeners()
ViewerStateChangeListeners can be added/removed here,
and will be notified about changes to this ViewerState. |
void |
clearGroups()
Remove all groups from the state.
|
void |
clearSources()
Remove all sources from the state.
|
boolean |
containsGroup(SourceGroup group)
Check whether the state contains the
group. |
boolean |
containsSource(SourceAndConverter<?> source)
Check whether the state contains the
source. |
Set<SourceGroup> |
getActiveGroups()
Get the set of active groups.
|
Set<SourceAndConverter<?>> |
getActiveSources()
Get the set of active sources.
|
SourceGroup |
getCurrentGroup()
Get the current group.
|
SourceAndConverter<?> |
getCurrentSource()
Get the current source.
|
int |
getCurrentTimepoint()
Get the current timepoint.
|
DisplayMode |
getDisplayMode()
Get the current
DisplayMode. |
String |
getGroupName(SourceGroup group)
Get the name of a
group. |
List<SourceGroup> |
getGroups()
Get the list of groups.
|
Interpolation |
getInterpolation()
Get the interpolation method.
|
int |
getNumTimepoints()
Get the number of timepoints.
|
List<SourceAndConverter<?>> |
getSources()
Get the list of sources.
|
Set<SourceAndConverter<?>> |
getSourcesInGroup(SourceGroup group)
Get the set sources in
group. |
void |
getViewerTransform(AffineTransform3D transform)
Get the viewer transform.
|
Set<SourceAndConverter<?>> |
getVisibleAndPresentSources()
Get the set of visible sources that also provide image data for the
current timepoint.
|
Set<SourceAndConverter<?>> |
getVisibleSources()
Get the set of visible sources.
|
ViewerState |
getWrappedState()
Returns the wrapped
ViewerState. |
Comparator<SourceGroup> |
groupOrder()
Returns a
Comparator that compares groups according to the order
in which they occur in the groups list. |
boolean |
isCurrentGroup(SourceGroup group)
Returns
true if group is the current group. |
boolean |
isCurrentSource(SourceAndConverter<?> source)
Returns
true if source is the current source. |
boolean |
isGroupActive(SourceGroup group)
Check whether the given
group is active. |
boolean |
isSourceActive(SourceAndConverter<?> source)
Check whether the given
source is active. |
boolean |
isSourceVisible(SourceAndConverter<?> source)
Check whether the given
source is visible. |
boolean |
isSourceVisibleAndPresent(SourceAndConverter<?> source)
Check whether the given
source is both visible and provides image
data for the current timepoint. |
boolean |
removeGroup(SourceGroup group)
Remove
group from the state. |
boolean |
removeGroups(Collection<? extends SourceGroup> collection)
Remove all groups in
collection from the state. |
boolean |
removeSource(SourceAndConverter<?> source)
Remove
source from the state. |
boolean |
removeSourceFromGroup(SourceAndConverter<?> source,
SourceGroup group)
Remove
source from group. |
boolean |
removeSources(Collection<? extends SourceAndConverter<?>> collection)
Remove all sources in
collection from the state. |
boolean |
removeSourcesFromGroup(Collection<? extends SourceAndConverter<?>> collection,
SourceGroup group)
Remove all sources in
collection from group. |
boolean |
setCurrentGroup(SourceGroup group)
Make
group the current group. |
boolean |
setCurrentSource(SourceAndConverter<?> source)
Make
source the current source. |
void |
setCurrentTimepoint(int t)
Set the current timepoint (optional operation).
|
void |
setDisplayMode(DisplayMode mode)
Set the
DisplayMode (optional operation). |
boolean |
setGroupActive(SourceGroup group,
boolean active)
Set
group active or inactive. |
void |
setGroupName(SourceGroup group,
String name)
Set the
name of a group. |
boolean |
setGroupsActive(Collection<? extends SourceGroup> collection,
boolean active)
Set all groups in
collection active or inactive. |
void |
setInterpolation(Interpolation interpolation)
Set the interpolation method (optional operation).
|
void |
setNumTimepoints(int n)
Set the number of timepoints (optional operation).
|
boolean |
setSourceActive(SourceAndConverter<?> source,
boolean active)
Set
source active or inactive. |
boolean |
setSourcesActive(Collection<? extends SourceAndConverter<?>> collection,
boolean active)
Set all sources in
collection active or inactive. |
void |
setViewerTransform(AffineTransform3D transform)
Set the viewer transform (optional operation).
|
ViewerState |
snapshot()
Get a snapshot of this ViewerState.
|
Comparator<SourceAndConverter<?>> |
sourceOrder()
Returns a
Comparator that compares sources according to the order
in which they occur in the sources list. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetViewerTransformpublic SynchronizedViewerState(ViewerState state)
public org.scijava.listeners.Listeners<ViewerStateChangeListener> changeListeners()
ViewerStateViewerStateChangeListeners can be added/removed here,
and will be notified about changes to this ViewerState.changeListeners in interface ViewerStatepublic ViewerState snapshot()
snapshot in interface ViewerStatepublic Interpolation getInterpolation()
ViewerStategetInterpolation in interface ViewerStatepublic void setInterpolation(Interpolation interpolation)
ViewerStatesetInterpolation in interface ViewerStatepublic DisplayMode getDisplayMode()
ViewerStateDisplayMode.
DisplayMode.SINGLE only the current source is visible.DisplayMode.GROUP the sources in the current group are visible.DisplayMode.FUSED all active sources are visible.DisplayMode.FUSEDROUP the sources in all active groups are visible.getDisplayMode in interface ViewerStatepublic void setDisplayMode(DisplayMode mode)
ViewerStateDisplayMode (optional operation).setDisplayMode in interface ViewerStatepublic int getNumTimepoints()
ViewerStategetNumTimepoints in interface ViewerStatepublic void setNumTimepoints(int n)
ViewerState
If ViewerState.getCurrentTimepoint() current timepoint} is
>= n, it will be adjusted to n-1.
setNumTimepoints in interface ViewerStatepublic int getCurrentTimepoint()
ViewerStategetCurrentTimepoint in interface ViewerStatepublic void setCurrentTimepoint(int t)
ViewerStatesetCurrentTimepoint in interface ViewerStatepublic void getViewerTransform(AffineTransform3D transform)
ViewerStategetViewerTransform in interface ViewerStatetransform - is set to the viewer transformpublic void setViewerTransform(AffineTransform3D transform)
ViewerStatesetViewerTransform in interface ViewerStatepublic List<SourceAndConverter<?>> getSources()
List reflects changes to
the viewer state. It is unmodifiable and not thread-safe.
The returned list is also a set, there are no duplicate entries.
getSources in interface ViewerStatepublic SourceAndConverter<?> getCurrentSource()
null if there is no current
source)getCurrentSource in interface ViewerStatepublic boolean isCurrentSource(SourceAndConverter<?> source)
true if source is the current source. Equivalent
to (getCurrentSource() == source).isCurrentSource in interface ViewerStatesource - the source. Passing null checks whether no source is current.true if source is the current sourcepublic boolean setCurrentSource(SourceAndConverter<?> source)
source the current source. Returns true, if current
source changes as a result of the call. Returns false, if
source is already the current source.
Also returns false, if source is not valid (for example
because it has been removed from the state by a different thread).
setCurrentSource in interface ViewerStatesource - the source to make current. Passing null clears the current
source.true, if current source changed as a result of the callpublic Set<SourceAndConverter<?>> getActiveSources()
Set reflects changes
to the viewer state. It is unmodifiable and not thread-safe.getActiveSources in interface ViewerStatepublic boolean isSourceActive(SourceAndConverter<?> source)
source is active.
Returns true, if source is active. Returns false,
if source is inactive or not valid (for example because it has
been removed from the state by a different thread).isSourceActive in interface ViewerStatetrue, if source is activeNullPointerException - if source == nullpublic boolean setSourceActive(SourceAndConverter<?> source, boolean active)
source active or inactive.
Returns true, if source activity changes as a result of the call.
Returns false, if source is already in the desired
active state.
Also returns false, if source is not valid (for example
because it has been removed from the state by a different thread).
setSourceActive in interface ViewerStatetrue, if source activity changed as a result of the callNullPointerException - if source == nullpublic boolean setSourcesActive(Collection<? extends SourceAndConverter<?>> collection, boolean active)
collection active or inactive.
Returns true, if source activity changes as a result of the call.
Returns false, if all sources were already in the desired
active state.
Does nothing and returns false, if any source in
collection is not valid (for example because it was removed from
the state by a different thread).
setSourcesActive in interface ViewerStatetrue, if source activity changed as a result of the callNullPointerException - if collection == null or any element of collection is
null.public boolean isSourceVisible(SourceAndConverter<?> source)
source is visible.
Returns false, if source is not valid (for example
because it has been removed from the state by a different thread).
Whether a source is visible depends on the display mode:
DisplayMode.SINGLE only the current source is visible.DisplayMode.GROUP the sources in the current group are visible.DisplayMode.FUSED all active sources are visible.DisplayMode.FUSEDROUP the sources in all active groups are visible.isSourceVisible in interface ViewerStatetrue, if source is visibleNullPointerException - if source == nullIllegalArgumentException - if source is not contained in the state (and not
null).public boolean isSourceVisibleAndPresent(SourceAndConverter<?> source)
source is both visible and provides image
data for the current timepoint.
Returns false, if source is not valid (for example
because it has been removed from the state by a different thread).
Whether a source is visible depends on the display mode:
DisplayMode.SINGLE only the current source is visible.DisplayMode.GROUP the sources in the current group are visible.DisplayMode.FUSED all active sources are visible.DisplayMode.FUSEDROUP the sources in all active groups are visible.present, i.e., provide image data for the current timepoint.isSourceVisibleAndPresent in interface ViewerStatetrue, if source is both visible and presentNullPointerException - if source == nullpublic Set<SourceAndConverter<?>> getVisibleSources()
The returned Set is a copy. Changes to the set will not be
reflected in the viewer state, and vice versa.
Whether a source is visible depends on the display mode:
DisplayMode.SINGLE only the current source is visible.DisplayMode.GROUP the sources in the current group are visible.DisplayMode.FUSED all active sources are visible.DisplayMode.FUSEDROUP the sources in all active groups are visible.getVisibleSources in interface ViewerStatepublic Set<SourceAndConverter<?>> getVisibleAndPresentSources()
The returned Set is a copy. Changes to the set will not be
reflected in the viewer state, and vice versa.
Whether a source is visible depends on the display mode:
DisplayMode.SINGLE only the current source is visible.DisplayMode.GROUP the sources in the current group are visible.DisplayMode.FUSED all active sources are visible.DisplayMode.FUSEDROUP the sources in all active groups are visible.present, i.e., provide image data for the current timepoint.getVisibleAndPresentSources in interface ViewerStatepublic boolean containsSource(SourceAndConverter<?> source)
source.containsSource in interface ViewerStatetrue, if source is in the list of sources.NullPointerException - if source == nullpublic boolean addSource(SourceAndConverter<?> source)
source to the state. Returns true, if the source is
added. Returns false, if the source is already present.
If source is added and no other source was current, then
source is made current
addSource in interface ViewerStatetrue, if list of sources changed as a result of the call.NullPointerException - if source == nullpublic boolean addSources(Collection<? extends SourceAndConverter<?>> collection)
collection to the state. Returns true,
if at least one source was added. Returns false, if all sources
were already present.
If any sources are added and no other source was current, then the first added sources will be made current.
addSources in interface ViewerStatetrue, if list of sources changed as a result of the call.NullPointerException - if collection == null or any element of collection is
null.public boolean removeSource(SourceAndConverter<?> source)
source from the state.
Returns true, if source was removed from the state.
Returns false, if source was not contained in state.
The source is also removed from any groups that contained it. If
source was current, then the first source in the list of sources
is made current (if it exists).
removeSource in interface ViewerStatetrue, if list of sources changed as a result of the callNullPointerException - if source == nullpublic boolean removeSources(Collection<? extends SourceAndConverter<?>> collection)
collection from the state. Returns
true, if at least one source was removed. Returns false,
if none of the sources was present.
Removed sources are also removed from any groups containing them. If the current source was removed, then the first source in the remaining list of sources is made current (if it exists).
removeSources in interface ViewerStatetrue, if list of sources changed as a result of the call.NullPointerException - if collection == null or any element of collection is
null.public void clearSources()
clearSources in interface ViewerStatepublic Comparator<SourceAndConverter<?>> sourceOrder()
Comparator that compares sources according to the order
in which they occur in the sources list. (Sources that do not occur in
the list are ordered before any source in the list).sourceOrder in interface ViewerStatepublic List<SourceGroup> getGroups()
List reflects changes to the
viewer state. It is unmodifiable and not thread-safe.
The returned list is also a set, there are no duplicate entries.
getGroups in interface ViewerStatepublic SourceGroup getCurrentGroup()
null if there is no current
group)getCurrentGroup in interface ViewerStatepublic boolean isCurrentGroup(SourceGroup group)
true if group is the current group. Equivalent to
(getCurrentGroup() == group).isCurrentGroup in interface ViewerStategroup - the group. Passing null checks whether no group is current.true if group is the current grouppublic boolean setCurrentGroup(SourceGroup group)
group the current group. Returns true, if current
group changes as a result of the call. Returns false, if
group is already the current group.
Also returns false, if group is not valid (for example
because it has been removed from the state by a different thread).
setCurrentGroup in interface ViewerStategroup - the group to make current. Passing null clears the current
group.true, if current group changed as a result of the call.public Set<SourceGroup> getActiveGroups()
Set reflects changes
to the viewer state. It is unmodifiable and not thread-safe.getActiveGroups in interface ViewerStatepublic boolean isGroupActive(SourceGroup group)
group is active.
Returns true, if group is active. Returns false,
if group is inactive or not valid (for example because it has
been removed from the state by a different thread).isGroupActive in interface ViewerStatetrue, if group is activeNullPointerException - if group == nullIllegalArgumentException - if group is not contained in the state (and not
null).public boolean setGroupActive(SourceGroup group, boolean active)
group active or inactive.
Returns true, if group activity changes as a result of the call.
Returns false, if group is already in the desired
active state.
Also returns false, if group is not valid (for example
because it has been removed from the state by a different thread).
setGroupActive in interface ViewerStatetrue, if group activity changed as a result of the callNullPointerException - if group == nullpublic boolean setGroupsActive(Collection<? extends SourceGroup> collection, boolean active)
collection active or inactive.
Returns true, if group activity changes as a result of the call.
Returns false, if all groups were already in the desired
active state.
Does nothing and returns false, if any group in
collection is not valid (for example because it was removed from
the state by a different thread).
setGroupsActive in interface ViewerStatetrue, if group activity changed as a result of the callNullPointerException - if collection == null or any element of collection is
null.public String getGroupName(SourceGroup group)
group.
Returns null, if group is not valid (for example because
it has been removed from the state by a different thread), an empty set
is returned.
getGroupName in interface ViewerStatenullNullPointerException - if group == nullpublic void setGroupName(SourceGroup group, String name)
name of a group.
Does nothing, if group is not valid (for example because it has
been removed from the state by a different thread), an empty set is
returned.
setGroupName in interface ViewerStateNullPointerException - if group == nullpublic boolean containsGroup(SourceGroup group)
group.containsGroup in interface ViewerStatetrue, if group is in the list of groups.NullPointerException - if group == nullpublic boolean addGroup(SourceGroup group)
group to the state. Returns true, if the group is
added. Returns false, if the group is already present.
If group is added and no other group was current, then
group is made current
addGroup in interface ViewerStatetrue, if list of groups changed as a result of the call.NullPointerException - if group == nullpublic boolean addGroups(Collection<? extends SourceGroup> collection)
collection to the state. Returns true,
if at least one group was added. Returns false, if all groups
were already present.
If any groups are added and no other group was current, then the first added groups will be made current.
addGroups in interface ViewerStatetrue, if list of groups changed as a result of the call.NullPointerException - if collection == null or any element of collection is
null.public boolean removeGroup(SourceGroup group)
group from the state.
Returns true, if group was removed from the state.
Returns false, if group was not contained in state.
If group was current, then the first group in the list of groups
is made current (if it exists).
removeGroup in interface ViewerStatetrue, if list of groups changed as a result of the callNullPointerException - if group == nullpublic boolean removeGroups(Collection<? extends SourceGroup> collection)
collection from the state. Returns
true, if at least one group was removed. Returns false,
if none of the groups was present.
If the current group was removed, then the first group in the remaining list of groups is made current (if it exists).
removeGroups in interface ViewerStatetrue, if list of groups changed as a result of the call.NullPointerException - if collection == null or any element of collection is
null.public boolean addSourceToGroup(SourceAndConverter<?> source, SourceGroup group)
source to group.
Returns true, if source was added to group.
Returns false, if source was already contained in
group. or either of source and group is not valid
(not in the BDV sources/groups list).
Does nothing and returns false, if source or
group are not valid (for example because they were removed from
the state by a different thread).
addSourceToGroup in interface ViewerStatetrue, if set of sources in group changed as a
result of the callNullPointerException - if source == null or group == nullpublic boolean addSourcesToGroup(Collection<? extends SourceAndConverter<?>> collection, SourceGroup group)
collection to group.
Returns true, if at least one source was added to group.
Returns false, if all sources were already contained in
group.
Does nothing and returns false, if group or any source in
collection are not valid (for example because they were removed
from the state by a different thread).
addSourcesToGroup in interface ViewerStatetrue, if set of sources in group changed as a
result of the callNullPointerException - if group == null or collection == null or any element
of collection is null.public boolean removeSourceFromGroup(SourceAndConverter<?> source, SourceGroup group)
source from group.
Returns true, if source was removed from group.
Returns false, if source was not contained in
group,
Does nothing and returns false, if source or
group are not valid (for example because they were removed from
the state by a different thread).
removeSourceFromGroup in interface ViewerStatetrue, if set of sources in group changed as a
result of the callNullPointerException - if source == null or group == nullpublic boolean removeSourcesFromGroup(Collection<? extends SourceAndConverter<?>> collection, SourceGroup group)
collection from group.
Returns true, if at least one source was removed from
group. Returns false, if none of the sources were
contained in group.
Does nothing and returns false, if group or any source in
collection are not valid (for example because they were removed
from the state by a different thread).
removeSourcesFromGroup in interface ViewerStatetrue, if set of sources in group changed as a
result of the callNullPointerException - if group == null or collection == null or any element
of collection is null.public Set<SourceAndConverter<?>> getSourcesInGroup(SourceGroup group)
group. The returned Set reflects
changes to the viewer state. It is unmodifiable and not thread-safe.
If group is not valid (for example because it has been removed
from the state by a different thread), an empty set is returned.
getSourcesInGroup in interface ViewerStategroupNullPointerException - if group == nullpublic void clearGroups()
clearGroups in interface ViewerStatepublic Comparator<SourceGroup> groupOrder()
Comparator that compares groups according to the order
in which they occur in the groups list. (Groups that do not occur in the
list are ordered before any group in the list).groupOrder in interface ViewerStatepublic ViewerState getWrappedState()
ViewerState.
When using this, explicit synchronization (on this
SynchronizedViewerState) is required. PLEASE BE CAREFUL!
Copyright © 2015–2021 Fiji. All rights reserved.