<RULES urn="#[FILE_URN]" language="GmlScript">

<define rule="doCopy" protected="true" autodoc="true">
	<summary>Copies a collection of elements to the clipboard</summary>

   <parameters>
	   	<param name="unit"     type="gml2:Unit">The unit classifier</param>
	   	<param name="board"    type="dev:IBoard">The board classifier</param>
	   	<param name="parent"   type="gml2:Element">The parent classifier. This is the most immediate ancestor that contains all the elements to be copied</param>
	   	<param name="elements" type="GmlObject[id]">The collection of element instances to copy</param>
   </parameters>

   <classifiers>
	   	<classifier name="unit"/>
	   	<classifier name="board"/>
	   	<classifier name="parent"/>
   </classifiers>

   <initialize>
   		<![CDATA[
			if( ISEMPTY(elements) ) return; // Do not clear the clipboard in case of empty selection
			
			var clipboard = $ENV.getClipboard();
			clipboard.clear();
			clipboard.sourceUnit = unit;

			// Set the sourceWinSize if has UI elements in selection
			if(ISA(unit, '#NS[Scenario]')) {
				for (var k in elements) {
					if(ISA(elements[k], '#NS[Interactor]', '#NS[Container]'))	{
						clipboard.sourceWinSize = unit.root.size;
						break;
					}
				}
			}
		]]>
	</initialize>

   <return type="Boolean"/>

	<remarks>
	<title>Behavior</title>
	The default behavior is to copy the given elements collection to the clipboard.
	The behavior can be extended to perform custom actions when elements are copied to the clipboard.
	</remarks>
   <copyright>(c) SAP AG 2003-2006. All rights reserved.</copyright>
</define>

<extend rule="doCopy">
   <constraint>board isa svg:ZDrawing</constraint>
   <comments>Copy to Clipboard from the Drawing box</comments>
   	<behavior><![CDATA[
	
	  	for (var k in elements) {
	  		var e = elements[k];

	  		if(e.isa('#NS[Window]') && !e.isa('#NS[Popup]') )  { delete elements[k]; continue;}
	  		
	  		// Remove selected link whose source/target was not selected
	  		if(e.isa('#NS[Link]') ){
	  			for(var p = e.source.parent; p && !(p.id in elements); p = p.parent);
	  			if(!p) { delete elements[k]; continue;}
	  			for(var p = e.target.parent; p && !(p.id in elements); p = p.parent);
	  			if(!p) { delete elements[k]; continue;}
	  		}
	  	}

	  	// Calculating bounding box
	  	clipboard.boundingBox = rule.getBoundingBox(elements);

	  	// Clone elements
		var res = clipboard.cloneElements(unit, elements);
		clipboard.elements = res.clonedElements;
		clipboard.globalInfoshapes = res.clonedGlobalInfoshapes;
	  	
	]]></behavior>
</extend>


<helper rule="doCopy" function="getBoundingBox">
	<parameters>
		<param name="elements" type="@Element![id]">The collection of elements to copy</param>
	</parameters>
	<behavior><![CDATA[
		var BB = {x:99999, y:999999, x2:0, y2:0};
		for (var k in elements) {
	  		var e = elements[k];
			var x, y, w=undefined, h, size = e['z$size']; 
	  		if(size) { 
	  			size = SPLIT(size); 
	  			w = INT(size[0]); 
	  			h = INT(size[1]);
	  		}
			else {
				var bb = e['z$boundingBox'];
				if(bb) {
					w = bb.defWidth;
					h = bb.defHeight;
				}
	  		}

			if(w === undefined) 
				continue;
			
			var pos = SPLIT(e['z$pos']);
			x = INT(pos[0]);
			y = INT(pos[1]);

			if(x<BB.x) BB.x = x;
	  		if(y<BB.y) BB.y = y;
	  		if((x+w)>BB.x2) BB.x2 = x+w;
	  		if((y+h)>BB.y2) BB.y2 = y+h;
		}

		return BB;
		
	]]></behavior>
</helper>

</RULES>
