/*
 * #%L
 * BigDataViewer core classes with minimal dependencies
 * %%
 * Copyright (C) 2012 - 2016 Tobias Pietzsch, Stephan Saalfeld, Stephan Preibisch,
 * Jean-Yves Tinevez, HongKee Moon, Johannes Schindelin, Curtis Rueden, John Bogovic
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */
package bdv.tools.transformation;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;

import bdv.viewer.Source;
import bdv.viewer.SourceAndConverter;
import bdv.viewer.ViewerPanel;
import net.imglib2.realtransform.AffineTransform3D;

public class ManualTransformation
{
	private final XmlIoTransformedSources io;

	private final ViewerPanel viewer;

	private final List< SourceAndConverter< ? > > sources;

	public ManualTransformation( final List< SourceAndConverter< ? > > sources )
	{
		this.sources = sources;
		this.viewer = null;
		io = new XmlIoTransformedSources();
	}

	public ManualTransformation( final ViewerPanel viewer )
	{
		this.viewer = viewer;
		this.sources = null;
		io = new XmlIoTransformedSources();
	}

	public Element toXml()
	{
		final List< TransformedSource< ? > > sources = getTransformedSources();
		final ArrayList< AffineTransform3D > transforms = new ArrayList<>( sources.size() );
		for ( final TransformedSource< ? > s : sources )
		{
			final AffineTransform3D t = new AffineTransform3D();
			s.getFixedTransform( t );
			transforms.add( t );
		}
		return io.toXml( new ManualSourceTransforms( transforms ) );
	}

	public void restoreFromXml( final Element parent )
	{
		final Element elem = parent.getChild( io.getTagName() );
		final List< TransformedSource< ? > > sources = getTransformedSources();
		final List< AffineTransform3D > transforms = io.fromXml( elem ).getTransforms();
		if ( sources.size() != transforms.size() )
			System.err.println( "failed to load <" + io.getTagName() + "> source and transform count mismatch" );
		else
			for ( int i = 0; i < sources.size(); ++i )
				sources.get( i ).setFixedTransform( transforms.get( i ) );
	}

	private ArrayList< TransformedSource< ? > > getTransformedSources()
	{
		final ArrayList< TransformedSource< ? > > list = new ArrayList<>();
		final List< ? extends SourceAndConverter< ? > > sourceList = ( sources != null )
				? sources
				: viewer.getState().getSources();
		for ( final SourceAndConverter< ? > soc : sourceList )
		{
			final Source< ? > source = soc.getSpimSource();
			if ( TransformedSource.class.isInstance( source ) )
				list.add( (TransformedSource< ? > ) source );
		}
		return list;
	}
}

@@ -390,17 +390,16 @@
  met:%0A *
- 
 %0A * 1. R
@@ -751,17 +751,16 @@
 tion.%0A *
- 
 %0A * THIS
@@ -1588,16 +1588,90 @@
 ation;%0A%0A
+import bdv.viewer.SynchronizedViewerState;%0Aimport bdv.viewer.ViewerState;%0A
 import j
@@ -3422,78 +3422,8 @@
 %0A%09%7B%0A
-%09%09final ArrayList%3C TransformedSource%3C ? %3E %3E list = new ArrayList%3C%3E();%0A
 %09%09fi
@@ -3478,18 +3478,22 @@
 urceList
- =
+;%0A%09%09if
  ( sourc
@@ -3512,18 +3512,28 @@
 %0A%09%09%09
-%09?
+sourceList =
  sources
 %0A%09%09%09
@@ -3532,43 +3532,229 @@
 rces
+;
 %0A%09%09
-%09%09: viewer.getState().getSources
+else%0A%09%09%7B%0A%09%09%09final ViewerState state = viewer.state();%0A%09%09%09synchronized ( state )%0A%09%09%09%7B%0A%09%09%09%09sourceList = new ArrayList%3C%3E( state.getSources() );%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09final ArrayList%3C TransformedSource%3C ? %3E %3E list = new ArrayList%3C%3E
 ();%0A
@@ -3877,54 +3877,45 @@
 f ( 
-TransformedSource.class.isInstance( s
+source instanceof TransformedS
 ource )
- )
 %0A%09%09%09

Use new ViewerState in ManualTransformation