Package bdv.viewer

Class SynchronizedViewerState

  • All Implemented Interfaces:
    ViewerState

    public class SynchronizedViewerState
    extends Object
    implements ViewerState
    Maintains the BigDataViewer state and implements 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.

    Author:
    Tobias Pietzsch
    • Constructor Detail

      • SynchronizedViewerState

        public SynchronizedViewerState​(ViewerState state)
    • Method Detail

      • changeListeners

        public org.scijava.listeners.Listeners<ViewerStateChangeListener> changeListeners()
        Description copied from interface: ViewerState
        ViewerStateChangeListeners can be added/removed here, and will be notified about changes to this ViewerState.
        Specified by:
        changeListeners in interface ViewerState
      • snapshot

        public ViewerState snapshot()
        Get a snapshot of this ViewerState.
        Specified by:
        snapshot in interface ViewerState
        Returns:
        unmodifiable copy of the current state
      • getDisplayMode

        public DisplayMode getDisplayMode()
        Description copied from interface: ViewerState
        Get the current DisplayMode.
        • In DisplayMode.SINGLE only the current source is visible.
        • In DisplayMode.GROUP the sources in the current group are visible.
        • In DisplayMode.FUSED all active sources are visible.
        • In DisplayMode.FUSEDROUP the sources in all active groups are visible.
        Specified by:
        getDisplayMode in interface ViewerState
        Returns:
        the current display mode
      • setAccumulateProjectorFactory

        public void setAccumulateProjectorFactory​(AccumulateProjectorFactory<net.imglib2.type.numeric.ARGBType> factory)
        Description copied from interface: ViewerState
        Set the AccumulateProjectorFactory.

        This factory provides AccumulateProjectors for blending rendered overlapping sources into the final screen image. This can be used for switching between "sum" and "average" blending mode for example.

        Specified by:
        setAccumulateProjectorFactory in interface ViewerState
        Parameters:
        factory - AccumulateProjector factory to set
      • getNumTimepoints

        public int getNumTimepoints()
        Description copied from interface: ViewerState
        Get the number of timepoints.
        Specified by:
        getNumTimepoints in interface ViewerState
        Returns:
        the number of timepoints
      • getCurrentTimepoint

        public int getCurrentTimepoint()
        Description copied from interface: ViewerState
        Get the current timepoint.
        Specified by:
        getCurrentTimepoint in interface ViewerState
        Returns:
        current timepoint (index)
      • setCurrentTimepoint

        public void setCurrentTimepoint​(int t)
        Description copied from interface: ViewerState
        Set the current timepoint (optional operation).
        Specified by:
        setCurrentTimepoint in interface ViewerState
      • getSources

        public List<SourceAndConverter<?>> getSources()
        Get the list of sources. The returned 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.

        Specified by:
        getSources in interface ViewerState
        Returns:
        the list of sources
      • getCurrentSource

        public SourceAndConverter<?> getCurrentSource()
        Get the current source. (May return null if there is no current source)
        Specified by:
        getCurrentSource in interface ViewerState
        Returns:
        the current source
      • isCurrentSource

        public boolean isCurrentSource​(SourceAndConverter<?> source)
        Returns true if source is the current source. Equivalent to (getCurrentSource() == source).
        Specified by:
        isCurrentSource in interface ViewerState
        Parameters:
        source - the source. Passing null checks whether no source is current.
        Returns:
        true if source is the current source
      • setCurrentSource

        public boolean setCurrentSource​(SourceAndConverter<?> source)
        Make 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).

        Specified by:
        setCurrentSource in interface ViewerState
        Parameters:
        source - the source to make current. Passing null clears the current source.
        Returns:
        true, if current source changed as a result of the call
      • getActiveSources

        public Set<SourceAndConverter<?>> getActiveSources()
        Get the set of active sources. The returned Set reflects changes to the viewer state. It is unmodifiable and not thread-safe.
        Specified by:
        getActiveSources in interface ViewerState
        Returns:
        the set of active sources
      • isSourceActive

        public boolean isSourceActive​(SourceAndConverter<?> source)
        Check whether the given 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).
        Specified by:
        isSourceActive in interface ViewerState
        Returns:
        true, if source is active
        Throws:
        NullPointerException - if source == null
      • setSourceActive

        public boolean setSourceActive​(SourceAndConverter<?> source,
                                       boolean active)
        Set 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).

        Specified by:
        setSourceActive in interface ViewerState
        Returns:
        true, if source activity changed as a result of the call
        Throws:
        NullPointerException - if source == null
      • setSourcesActive

        public boolean setSourcesActive​(Collection<? extends SourceAndConverter<?>> collection,
                                        boolean active)
        Set all sources in 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).

        Specified by:
        setSourcesActive in interface ViewerState
        Returns:
        true, if source activity changed as a result of the call
        Throws:
        NullPointerException - if collection == null or any element of collection is null.
      • isSourceVisible

        public boolean isSourceVisible​(SourceAndConverter<?> source)
        Check whether the given 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:

        • In DisplayMode.SINGLE only the current source is visible.
        • In DisplayMode.GROUP the sources in the current group are visible.
        • In DisplayMode.FUSED all active sources are visible.
        • In DisplayMode.FUSEDROUP the sources in all active groups are visible.
        Specified by:
        isSourceVisible in interface ViewerState
        Returns:
        true, if source is visible
        Throws:
        NullPointerException - if source == null
        IllegalArgumentException - if source is not contained in the state (and not null).
      • isSourceVisibleAndPresent

        public boolean isSourceVisibleAndPresent​(SourceAndConverter<?> source)
        Check whether the given 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:

        • In DisplayMode.SINGLE only the current source is visible.
        • In DisplayMode.GROUP the sources in the current group are visible.
        • In DisplayMode.FUSED all active sources are visible.
        • In DisplayMode.FUSEDROUP the sources in all active groups are visible.
        Additionally, the source must be present, i.e., provide image data for the current timepoint.
        Specified by:
        isSourceVisibleAndPresent in interface ViewerState
        Returns:
        true, if source is both visible and present
        Throws:
        NullPointerException - if source == null
      • getVisibleSources

        public Set<SourceAndConverter<?>> getVisibleSources()
        Get the set of visible sources.

        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:

        • In DisplayMode.SINGLE only the current source is visible.
        • In DisplayMode.GROUP the sources in the current group are visible.
        • In DisplayMode.FUSED all active sources are visible.
        • In DisplayMode.FUSEDROUP the sources in all active groups are visible.
        Specified by:
        getVisibleSources in interface ViewerState
        Returns:
        the set of visible sources
      • getVisibleAndPresentSources

        public Set<SourceAndConverter<?>> getVisibleAndPresentSources()
        Get the set of visible sources that also provide image data for the current timepoint.

        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:

        • In DisplayMode.SINGLE only the current source is visible.
        • In DisplayMode.GROUP the sources in the current group are visible.
        • In DisplayMode.FUSED all active sources are visible.
        • In DisplayMode.FUSEDROUP the sources in all active groups are visible.
        Additionally, the source must be present, i.e., provide image data for the current timepoint.
        Specified by:
        getVisibleAndPresentSources in interface ViewerState
        Returns:
        the set of sources that are both visible and present
      • addSource

        public boolean addSource​(SourceAndConverter<?> source)
        Add 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

        Specified by:
        addSource in interface ViewerState
        Returns:
        true, if list of sources changed as a result of the call.
        Throws:
        NullPointerException - if source == null
      • addSources

        public boolean addSources​(Collection<? extends SourceAndConverter<?>> collection)
        Add all sources in 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.

        Specified by:
        addSources in interface ViewerState
        Returns:
        true, if list of sources changed as a result of the call.
        Throws:
        NullPointerException - if collection == null or any element of collection is null.
      • removeSource

        public boolean removeSource​(SourceAndConverter<?> source)
        Remove 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).

        Specified by:
        removeSource in interface ViewerState
        Returns:
        true, if list of sources changed as a result of the call
        Throws:
        NullPointerException - if source == null
      • removeSources

        public boolean removeSources​(Collection<? extends SourceAndConverter<?>> collection)
        Remove all sources in 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).

        Specified by:
        removeSources in interface ViewerState
        Returns:
        true, if list of sources changed as a result of the call.
        Throws:
        NullPointerException - if collection == null or any element of collection is null.
      • clearSources

        public void clearSources()
        Remove all sources from the state.
        Specified by:
        clearSources in interface ViewerState
      • sourceOrder

        public Comparator<SourceAndConverter<?>> sourceOrder()
        Returns a 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).
        Specified by:
        sourceOrder in interface ViewerState
      • getGroups

        public List<SourceGroup> getGroups()
        Get the list of groups. The returned 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.

        Specified by:
        getGroups in interface ViewerState
        Returns:
        the list of groups
      • getCurrentGroup

        public SourceGroup getCurrentGroup()
        Get the current group. (May return null if there is no current group)
        Specified by:
        getCurrentGroup in interface ViewerState
        Returns:
        the current group
      • isCurrentGroup

        public boolean isCurrentGroup​(SourceGroup group)
        Returns true if group is the current group. Equivalent to (getCurrentGroup() == group).
        Specified by:
        isCurrentGroup in interface ViewerState
        Parameters:
        group - the group. Passing null checks whether no group is current.
        Returns:
        true if group is the current group
      • setCurrentGroup

        public boolean setCurrentGroup​(SourceGroup group)
        Make 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).

        Specified by:
        setCurrentGroup in interface ViewerState
        Parameters:
        group - the group to make current. Passing null clears the current group.
        Returns:
        true, if current group changed as a result of the call.
      • getActiveGroups

        public Set<SourceGroup> getActiveGroups()
        Get the set of active groups. The returned Set reflects changes to the viewer state. It is unmodifiable and not thread-safe.
        Specified by:
        getActiveGroups in interface ViewerState
        Returns:
        the set of active groups
      • isGroupActive

        public boolean isGroupActive​(SourceGroup group)
        Check whether the given 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).
        Specified by:
        isGroupActive in interface ViewerState
        Returns:
        true, if group is active
        Throws:
        NullPointerException - if group == null
        IllegalArgumentException - if group is not contained in the state (and not null).
      • setGroupActive

        public boolean setGroupActive​(SourceGroup group,
                                      boolean active)
        Set 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).

        Specified by:
        setGroupActive in interface ViewerState
        Returns:
        true, if group activity changed as a result of the call
        Throws:
        NullPointerException - if group == null
      • setGroupsActive

        public boolean setGroupsActive​(Collection<? extends SourceGroup> collection,
                                       boolean active)
        Set all groups in 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).

        Specified by:
        setGroupsActive in interface ViewerState
        Returns:
        true, if group activity changed as a result of the call
        Throws:
        NullPointerException - if collection == null or any element of collection is null.
      • getGroupName

        public String getGroupName​(SourceGroup group)
        Get the name of a 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.

        Specified by:
        getGroupName in interface ViewerState
        Returns:
        name of the group, may be null
        Throws:
        NullPointerException - if group == null
      • setGroupName

        public void setGroupName​(SourceGroup group,
                                 String name)
        Set the 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.

        Specified by:
        setGroupName in interface ViewerState
        Throws:
        NullPointerException - if group == null
      • addGroup

        public boolean addGroup​(SourceGroup group)
        Add 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

        Specified by:
        addGroup in interface ViewerState
        Returns:
        true, if list of groups changed as a result of the call.
        Throws:
        NullPointerException - if group == null
      • addGroups

        public boolean addGroups​(Collection<? extends SourceGroup> collection)
        Add all groups in 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.

        Specified by:
        addGroups in interface ViewerState
        Returns:
        true, if list of groups changed as a result of the call.
        Throws:
        NullPointerException - if collection == null or any element of collection is null.
      • removeGroup

        public boolean removeGroup​(SourceGroup group)
        Remove 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).

        Specified by:
        removeGroup in interface ViewerState
        Returns:
        true, if list of groups changed as a result of the call
        Throws:
        NullPointerException - if group == null
      • removeGroups

        public boolean removeGroups​(Collection<? extends SourceGroup> collection)
        Remove all groups in 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).

        Specified by:
        removeGroups in interface ViewerState
        Returns:
        true, if list of groups changed as a result of the call.
        Throws:
        NullPointerException - if collection == null or any element of collection is null.
      • addSourceToGroup

        public boolean addSourceToGroup​(SourceAndConverter<?> source,
                                        SourceGroup group)
        Add 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).

        Specified by:
        addSourceToGroup in interface ViewerState
        Returns:
        true, if set of sources in group changed as a result of the call
        Throws:
        NullPointerException - if source == null or group == null
      • addSourcesToGroup

        public boolean addSourcesToGroup​(Collection<? extends SourceAndConverter<?>> collection,
                                         SourceGroup group)
        Add all sources in 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).

        Specified by:
        addSourcesToGroup in interface ViewerState
        Returns:
        true, if set of sources in group changed as a result of the call
        Throws:
        NullPointerException - if group == null or collection == null or any element of collection is null.
      • removeSourceFromGroup

        public boolean removeSourceFromGroup​(SourceAndConverter<?> source,
                                             SourceGroup group)
        Remove 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).

        Specified by:
        removeSourceFromGroup in interface ViewerState
        Returns:
        true, if set of sources in group changed as a result of the call
        Throws:
        NullPointerException - if source == null or group == null
      • removeSourcesFromGroup

        public boolean removeSourcesFromGroup​(Collection<? extends SourceAndConverter<?>> collection,
                                              SourceGroup group)
        Remove all sources in 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).

        Specified by:
        removeSourcesFromGroup in interface ViewerState
        Returns:
        true, if set of sources in group changed as a result of the call
        Throws:
        NullPointerException - if group == null or collection == null or any element of collection is null.
      • getSourcesInGroup

        public Set<SourceAndConverter<?>> getSourcesInGroup​(SourceGroup group)
        Get the set sources in 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.

        Specified by:
        getSourcesInGroup in interface ViewerState
        Returns:
        the set of sources in group
        Throws:
        NullPointerException - if group == null
      • clearGroups

        public void clearGroups()
        Remove all groups from the state.
        Specified by:
        clearGroups in interface ViewerState
      • groupOrder

        public Comparator<SourceGroup> groupOrder()
        Returns a 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).
        Specified by:
        groupOrder in interface ViewerState
      • getWrappedState

        public ViewerState getWrappedState()
        Returns the wrapped ViewerState.

        When using this, explicit synchronization (on this SynchronizedViewerState) is required. PLEASE BE CAREFUL!