Receiver rcv, rcv2;
Transferable o;
Point2D pt, pt2;
boolean success = false;
boolean retry;
AbstractCompoundEdit edit;
Object[] args;
final Application app = AbstractApplication.getApplication();
try {
t = app.getClipboard().getContents( this );
if( t == null ) return false;
if( t.isDataFlavorSupported( TransferableCollection.collectionFlavor )) {
coll = (java.util.List) t.getTransferData( TransferableCollection.collectionFlavor );
} else if( t.isDataFlavorSupported( Receiver.receiverFlavor )) {
rcv = (Receiver ) t.getTransferData( Receiver.receiverFlavor );
coll = new Vector( 1 );
coll.add( rcv );
} else {
return false;
}
try {
args = new Object[ 3 ];
doc.bird.waitExclusive( Session.DOOR_RCV | Session.DOOR_GRP );
coll2 = doc.getReceivers().getAll();
coll3 = new ArrayList( coll2 );
for( int i = 0; i < coll.size(); i++ ) {
o = (Transferable) coll.get( i );
if( o.isDataFlavorSupported( Receiver.receiverFlavor )) {
rcv = (Receiver) o.getTransferData( Receiver.receiverFlavor );
do {
retry = false;
for( int j = 0; j < coll3.size(); j++ ) {
rcv2 = (Receiver) coll3.get( j );
pt = rcv.getAnchor();
if( pt.distance( rcv2.getAnchor() ) < 0.05 ) {
pt2 = new Point2D.Double( Math.min( 1.0, pt.getX() + 0.05 ),
Math.min( 1.0, pt.getY() + 0.05 ));
rcv.setAnchor( pt2 );
if( pt2.distance( pt ) > 0.05 ) retry = true;
}
}
} while( retry );
coll3.add( rcv );
}
} // for( i = 0; i < coll.size(); i++ )
coll3.removeAll( coll2 ); // now only the new ones remain
// ensure unique names
for( int i = 0; i < coll3.size(); i++ ) {
rcv = (Receiver) coll3.get( i );
if( doc.getReceivers().findByName( rcv.getName() ) != null ) {
Session.makeNamePattern( rcv.getName(), args );
rcv.setName( BasicSessionCollection.createUniqueName( Session.SO_NAME_PTRN, args, coll2 ));
}
coll2.add( rcv );
}
if( !coll3.isEmpty() ) {
edit = new BasicCompoundEdit();
final List selectedGroups = doc.getSelectedGroups().getAll();
if( !selectedGroups.isEmpty() ) {
for( int i = 0; i < coll3.size(); i++ ) {
final GroupableSessionObject so = (GroupableSessionObject) coll3.get( i );
edit.addPerform( new EditAddSessionObjects( this, so.getGroups(), selectedGroups ));
}
}
edit.addPerform( new EditAddSessionObjects( this, doc.getMutableReceivers(), coll3 ));
// for( int i = 0; i < doc.getSelectedGroups().size(); i++ ) {
// group = (SessionGroup) doc.getSelectedGroups().get( i );
// edit.addPerform( new EditAddSessionObjects( this, group.getReceivers(), coll3 ));
// }
edit.addPerform( new EditSetSessionObjects( this, doc.getMutableSelectedReceivers(), coll3 ));
edit.perform();
edit.end();
doc.getUndoManager().addEdit( edit );
}
success = true;
}