﻿<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Component File -->
<application-components>
	 <application-component id = "hierarchy-decorator" participant-id ="canvas">
			<![CDATA[
				 SetHierarchy : function(o){
						this.getPointers().Hierarchy = o;	 
					},
				 handle_canvas_mousedown : function(oCanvas, e){
						if(!oCanvas.getStatus().MouseTrackChoose) return;
						
						oCanvas.ClearTempCanvas();
						var oShape = oCanvas.getPointers().CurrentShape;
						if(oShape.type == "Rect"){
							var oM = oCanvas.Rect(oShape.x, oShape.y, oShape.width, oShape.height, "#00FF00", "#000000");
							oM.selectable = 0;
							var oCurrentNode = this.getPointers().Hierarchy.getPointers().nodes[oShape.reference_id];
							this.BlotChildrenToTempShape(oCanvas, oCurrentNode, oCurrentNode, oM);
						}

				 },
				 handle_canvas_mouseup : function(oCanvas, e){
						var _s = oCanvas.getStatus(), _p = oCanvas.getPointers();
						oCanvas.ClearTempCanvas();
						if(
									!_s.MouseTrackChoose
									||
									!_p.CurrentShape
									||
									_p.CurrentShape.reference_id < 0
									||
									!_p.MouseDropShape
									||
									_p.MouseDropShape.reference_id < 0
						){
							return;
						}
						var oSourceNode = this.getPointers().Hierarchy.getPointers().nodes[_p.CurrentShape.reference_id];
						var oTargetNode = this.getPointers().Hierarchy.getPointers().nodes[_p.MouseDropShape.reference_id];
						if(
							oSourceNode.type == "org"
							&&
							oTargetNode.type == "org"
						){
							this.getPointers().Hierarchy.ReparentOrganizationNode(oSourceNode,oTargetNode);
						}	
							
				 },
				 handle_canvas_mousemove : function(oCanvas, e){
					//if(!oCanvas.getStatus().MouseTrackDown) return;
					oCanvas.ClearTempCanvas();
					
					var sDropColor = "#FFFF00";
					var oDropShape = oCanvas.getPointers().MouseDropShape;
					var oDropNode, oCurrentNode = 0;
							 
					if(oDropShape && oDropShape.reference_id >= 0){
						oDropNode = this.getPointers().Hierarchy.getPointers().nodes[oDropShape.reference_id];
						//org.cote.js.message.MessageService.sendMessage("Drop Shape at " + oDropShape.x + ", " + oDropShape.y);
						//var oM = oCanvas.Rect(oDropShape.x, oDropShape.y, oDropShape.width, oDropShape.height, "#FFFF00", "#000000");
						//oM.selectable = 0;
					}

					var oCurrent = oCanvas.getPointers().CurrentShape;
					if(oCanvas.getStatus().MouseTrackChoose && oCurrent && oCurrent.reference_id >= 0){
						oCurrentNode = this.getPointers().Hierarchy.getPointers().nodes[oCurrent.reference_id];
						if(oCurrent.type == "Rect"){
							var oM = oCanvas.Rect(oCurrent.x, oCurrent.y, oCurrent.width, oCurrent.height, "#00FF00", "#000000");
							oM.selectable = 0;
							oM = oCanvas.Rect(
								oCanvas.getStatus().MouseTrackLeft - oCanvas.getStatus().MouseOffsetX,
								oCanvas.getStatus().MouseTrackTop - oCanvas.getStatus().MouseOffsetY,
								oCurrent.width,
								oCurrent.height, 
								"#00FF00", "#000000"
							);
							oM.selectable = 0;
							this.BlotChildrenToTempShape(oCanvas, oCurrentNode, oCurrentNode, oM);
						}
						
						if(oDropNode && oCurrentNode &&
									(
									this.getPointers().Hierarchy.IsChild(oDropNode,oCurrentNode)
									||
									oCurrentNode.parent == oDropNode.index
									)
						) sDropColor = "#FF0000";
					} 

					if(oDropNode){
						var oM = oCanvas.Rect(oDropShape.x, oDropShape.y, oDropShape.width, oDropShape.height, sDropColor, "#000000");
						oM.selectable = 0;
					}
				 },
				   
				 BlotChildrenToTempShape : function(oCanvas, oSourceParent, oCurrent, oShape){
					for(var i = 0; i < oCurrent.children.length; i++){
						var oChild = this.getPointers().Hierarchy.getPointers().nodes[oCurrent.children[i]];
						if(!oChild.shape) continue;
						var iX = oChild.shape.x - (oSourceParent.shape.x - oShape.x);
						var iY = oChild.shape.y - (oSourceParent.shape.y - oShape.y);
						var oM = oCanvas.Rect(oChild.shape.x, oChild.shape.y, oChild.shape.width, oChild.shape.height, "#00FF00", "#000000");
						oM.selectable = 0;

						oM = oCanvas.Rect(
							iX,
							iY,
							oShape.width,
							oShape.height, 
							oShape.fillStyle, oShape.strokeColor
						);
						oM.selectable = 0;
						this.BlotChildrenToTempShape(oCanvas, oSourceParent,oChild,oShape);
					    
				    }
				 }
			]]>
		</application-component>				
</application-components>
