/* TDataSourceConnector.m
 * Written By:  Thomas Burkholder
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 */

#import "TDataSourceConnector.h"
#import "TBinderList.h"

@implementation TDataSourceConnector

- init
{
	[super init];
	return self;
}

- free
{
	// Commented out because isTestingInterface doesn't work properly...
	// Note that this introduces the following behaviour:
	// 1.  set dataSource, delete it, save or test interface,
	//     you have ref to freed object in TBinderList.
	// Workaround:  in the write: method of TConnector, look for a
	// datasourceconnector - not finding one, set source of TBinderList to
	// nil.  Or use a documentcontroller to make the change before the save
	// is done.
	if (([NXApp respondsTo:@selector(isTestingInterface)]) &&
		([NXApp isTestingInterface])) {
//		[source setDataSource:nil];
	}
	
	return [super free];
}

- setSource:anObject
{
	source = anObject;
	return self;
}

- setDestination:anObject
{
	destination = anObject;
	return self;
}

- source
{
	return source;
}

- destination
{
	return destination;
}

- establishConnection
{
	return self;
}

- nibInstantiate
{
	// we cheat a bit; the object is archived with this value as dataSource
	[source setDataSource:destination];

	return self;
}

- renewObject:old to:new
{
	if (old == source)
		source = new;
	else if (old == destination)
		destination = new;
	return self;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	source = NXReadObject(stream);
	destination = NXReadObject(stream);
	return self;
}

- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteObject(stream,source);
	NXWriteObject(stream,destination);
	return self;
}

@end