﻿<?xml version="1.0" encoding="utf-8" ?> 
<html-fragment>
	<div>
		<input type = "button" value = "Registry" onclick = "${this}.PrintRegistry()" />
		<input type = "button" value = "Engines" onclick = "${this}.PrintEngines()" />
		<input type = "button" value = "Tasks" onclick = "${this}.PrintTasks()" />
		<input type = "button" value = "Transactions" onclick = "${this}.PrintTrans()" />
		<!--
		<input type = "button" value = "Destroy" onclick = "${this}.SendDestroy()" style = "margin-left:25px;" />
		-->
		
	</div>
	<div ___pointer_marker = "never" style = "width:50%;float:left;" acid = "wideselect" appcomp_path = "Gizmos/component.wideselect.xml" rid = "oRegistry">
	</div>
	<div ___pointer_marker = "never" style = "width:50%;float:left;" acid = "wideselect" appcomp_path = "Gizmos/component.wideselect.xml" rid = "oModel">
	</div>
	<div>
		<!--
		<input type = "text" rid = "oUidPrefix" />
		-->
		Filter Results: <input type = "checkbox" rid = "oFilterPrefix" />
	</div>
<!--
	<div>
		<textarea rid = "data" style = "border:1px solid #000000; width:500px; height:50px;background-color:#efefef;"></textarea>
	</div>
-->
	<embedded-script><![CDATA[
		embedded_init : function (){
			this.GetList().getContainer().style.height = "450px";
			this.GetModel().getContainer().style.height = "450px";
			this.getPointers().model_list = [];
			this.getPointers().model_index = [];
			this.getPointers().pathHash = [];
			this.getPointers().intModels = 0;
			this.createHandler("resultclick",0,0,1);
			this.createHandler("modelclick",0,0,1);
			//this.createHandler("model",0,0,1);
			
			this.GetComponentByRID("oRegistry").GetWideSelect().setResultHandler( this._prehandle_resultclick);
			this.GetComponentByRID("oModel").GetWideSelect().setResultHandler( this._prehandle_modelclick);
			
			//this.GetComponentByRID("oRegistry").GetWideSelect().getPointers().h();
			//var oR = this.GetComponentByRID("oRegistry");
			//alert(oR);
			//alert(this.getTemplateEngine().getObjectByName("oRegistry").object.getApplicationComponent());
			

			this.getPointers().dwacMonitor = {
			   handle_mouse_click:function(e){
				var o = org.cote.js.dom.event._gevt_src(e);
				if(!o) return;
				if(o.nodeType != 3) o = o.parentNode;
				var r = o.getAttribute("rid");
				while(o && typeof o.getAttribute != "undefined" && (typeof r != "string" || r.length == 0)){
					o = o.parentNode;
					if(!o || typeof o.getAttribute != "undefined" ) break;
					r = o.getAttribute("rid");
				}
				document.title = "r='" + r + "'";
				if(typeof r != "string" || r.length == 0) return;
				var oObj = org.cote.js.registry.ObjectRegistry.getObject(r);

				if(oObj) this.ModelObject(oObj);
				
			   },
			   initializeMonitor:function(){
				  // must return true for monitoring to be enabled;
				  return 1;
			   }
			};
			//org.cote.js.prepareObject("DWACMonitor","1.0",true,this.getPointers().dwacMonitor);
			//var x = org.cote.js.monitor.MonitorService.addMonitor(this.getPointers().dwacMonitor);
			org.cote.js.uid_prefix = "dwd-";
			//this.GetElementByRID("oUidPrefix").value = "dwd-";
			this.GetElementByRID("oFilterPrefix").checked = true;
		},
		embedded_destroy : function(){
			this.getPointers().model_list = 0;
			this.getPointers().model_index = 0;
			this._prehandle_resultclick = 0;
			this._prehandle_modelclick = 0;
			this._prehandle_model = 0;
			org.cote.js.uid_prefix = "swc-";
			org.cote.js.monitor.MonitorService.removeMonitor(this.getPointers().dwacMonitor);
			org.cote.js.registry.ObjectRegistry.removeObject(this.getPointers().dwacMonitor);
			this.getPointers().dwacMonitor = 0;
		},
		SendDestroy : function(){
			org.cote.js.message.MessageService.sendMessage("Sending Sig Term","200.1");
			org.cote.js.registry.ObjectRegistry.sendSigterm();
		},
		GetList : function(){
			return this.GetComponentByRID("oRegistry").GetWideSelect();
		},
		GetModel : function(){
			return this.GetComponentByRID("oModel").GetWideSelect();
		},
		_handle_resultclick : function(sEvent,oItem){
			//this.Model();
			this.ModelObject(this.GetModel(),oItem);
		},
		_handle_modelclick : function(sEvent,oItem){
			//this.Model();
			/*
			var oList = this.GetList();
			oList.clearItems();
			var aI = this.GetModel().getItems();
			for(var i = 0; i < aI.length; i++) oList.addItem(aI[i].label,aI[i].value);
			*/
			this.ModelObject(this.GetModel(),oItem);
			
		},

		ModelObject : function(oList, oItem){
			//var oModel = this.GetModel();
			
			var sV = oItem.data;
			if(sV == null || sV.length == 0 || sV == "_avoid") return;
			
			
			if(sV == "__aslink"){
				/// The ActiveSource viewer embeds a global object, ActiveSource, on the page
				///

				if(typeof this.ShowTab == "function" && typeof ActiveSource == "object"){
					this.ShowTab("Active Source");
					
					var oSource = this.GetElementByRID("oSource");
					var oEval = this.GetElementByRID("oEval");
					
					var oObj = org.cote.js.registry.ObjectRegistry.getObject(this.getStatus().model_id);
					if(!oObj) oObj = org.cote.js.engine.EngineService.getEngine(this.getStatus().model_id);
					var sCont = "";
					ActiveObjects.valContainer = 0;
					ActiveObjects.valReference = 0;
					ActiveObjects.valBridge = 0;
					ActiveObjects.valComponent = 0;
					if(typeof oObj.getReferenceId == "function"){
						var oC = org.cote.js.engine.EngineService.getEngine(oObj.getReferenceId());
						if(oC){
							sCont = "\nEngine Reference " + oC.engine_name + "(" + oC.engine_id + ") as unregistered 'EngineObject' with ActiveObjects.valReference";
							ActiveObjects.valReference = oC;
						}
					}
					if(typeof oObj.getContainerId == "function"){
						var oC = org.cote.js.registry.ObjectRegistry.getObject(oObj.getContainerId());
						if(oC){
							sCont = "\nAccess Container Bridge " + oC.getObjectId() + " as " + oC.getObjectType() + " with ActiveObjects.valBridge";
							ActiveObjects.valBridge = oC;
						}
					}
					if(typeof oObj.getComponentId == "function" && oObj.getComponentId()){
						sCont = "\nComponent Id: " + oObj.getComponentId();
					}
					if(typeof oObj.getApplicationComponent == "function"){
						var oC = oObj.getApplicationComponent();
						if(oC){
							sCont = "\nComponent Bridge " + oC.getObjectId() + " as " + oC.getObjectType() + " with ActiveObjects.valComponent";
							ActiveObjects.valComponent = oC;
						}
					}
					if(typeof oObj.getContainer == "function"){
						var oC = oObj.getContainer();
						if(oC && oC.nodeType == 1){
							sCont = "\nAccess Container " + oC.nodeName + " with ActiveObjects.valContainer\n" + this.PrototypeElement(oC);
							ActiveObjects.valContainer = oC;
						}
					}
					oSource.value = 
						"Active Object " + this.getStatus().model_id + (typeof oObj.getObjectType == "function" ? " as " + oObj.getObjectType() + "(v " + oObj.getObjectVersion() + ")" : "")
						+"\nAccess with ActiveObjects.val"
						+ sCont
					;
					oEval.value = "ActiveObjects.val";
					ActiveObjects.val = org.cote.js.registry.ObjectRegistry.getObject(this.getStatus().model_id);
					
					
				}

				return;
			}
			this.getStatus().model_id = 0;
			oList.clearItems();
			oList.addItem("MODEL","_avoid");

			
			var oObj = org.cote.js.registry.ObjectRegistry.getObject(sV);
			if(!oObj) oObj = org.cote.js.engine.EngineService.getEngine(oObj);
			oList.addItem("ID " + sV,"_avoid");
			if(typeof oObj.getReferenceId == "function"){
				var oRef = org.cote.js.engine.EngineService.getEngine(oObj.getReferenceId());
				//var oRef = org.cote.js.registry.ObjectRegistry.getObject(oObj.getReferenceId());
				 //&& oObj.getObjectType() == "application_component"
				//alert(oRef + ":" + oObj.getReferenceId());
				if(oRef){
					oList.addItem("Reference Engine: " + oRef.engine_name + " (" + oRef.engine_id + ")",oRef.engine_id);
				}
			}
			if(typeof oObj.getIsComponentLinked == "function" && oObj.getIsComponentLinked()){
				var oC = oObj.getComponentCollection();
				if(oC){
					oList.addItem("Component Link: " + oC.getObjectId() + " as " + oC.getObjectType(),oC.getObjectId());
				}
			}
			if(typeof oObj.getApplicationComponent == "function"){
				var oC = oObj.getApplicationComponent();
				if(oC){
					oList.addItem("Application Component: " + oC.getObjectId() + " as " + oC.getDefinitionId(),oC.getObjectId());
				}
			}
			if(typeof oObj.getComponentId == "function" && oObj.getComponentId()){
				oList.addItem("Component Id: " + oObj.getComponentId(),"_avoid");
			}
			if(typeof oObj.getContainerId == "function"){
				var oC = org.cote.js.registry.ObjectRegistry.getObject(oObj.getContainerId());
				if(oC){
					oList.addItem("Container Bridge: " + oC.getObjectId() + " as " + oC.getObjectType(),oC.getObjectId());
				}
			}
			oList.addItem("Send To Active Source","__aslink");
			
			this.getStatus().model_id = sV;

		},
		PrintRegistry : function(){
			var oR = org.cote.js.registry.ObjectRegistry;
			var a = oR.getObjects();
			var oList = this.GetList();
			oList.clearItems();
			var s = "";
			var o;
			var b;
			oList.addItem("REGISTRY","_avoid");
			for(var i = 0;i < a.length; i++){
				o = a[i];
				if(o != 0 && this.GetElementByRID("oFilterPrefix").checked && !o.getObjectId().match(/^dwd-/)) continue;
				if(o != 0){
					//s += (i + 1) + " [ null ] \n";
					//oList.addItem("[ null ]");
				//else{
					//s+= (i + 1) + " " + o.t + " (" + o.i + ")\n";
					oList.addItem(o.getObjectType() + " " + (o.getObjectType() == "xhtml_component" ? o.getContainer().nodeName + " " : "") + "(" + o.getObjectId() + ")",o.getObjectId());
					if(o.t == "engine_service"){
						//s+= "    Engines: " + o.getEngines().length + "\n";
						oList.addItem("....Engines: " + o.getEngines().length);
					}
					else if(o.t == "task_service"){
						b = o.getTasks();
						// s+= "....Tasks: " + b.length + "\n";
						oList.addItem("....Tasks: " + b.length,o.getObjectId());
						for(var c = 0;c<b.length;c++){
							//s+= "-        Tasks (" + (c + 1) + ") " + b[c].task_name + "\n";
							oList.addItem("........Tasks (" + (c + 1) + ") " + b[c].task_name,b[c].getObjectId());
						}

						
					}
					else if(o.t == "transaction_service"){
						oList.addItem("....Registered Objects: " + o.getRegisteredObjects().length,o.getObjectId());
						oList.addItem("....Transaction Packets: " + o.getPackets().length,o.getObjectId());
						//s+= "    Registered Objects: " + o.getRegisteredObjects().length + "\n";
						//s+= "    Transaction Packets: " + o.getPackets().length + "\n";
					}
					else if(o.t == "message_service"){
						//oList.addItem("....Subscriptions: " + o.o.p.s.length,o.getObjectId());
						oList.addItem("....Subscriptions","_avoid");
						for(var q in o.o.p.s){
							oList.addItem("......" + q + " = " + o.o.p.s[q].length,o.getObjectId());
						}
						// " + o.o.p.s.length,o.getObjectId());
						oList.addItem("....Delayed Publications: " + o.o.p.dd.length,o.getObjectId());
						oList.addItem("....Message Entries: " + o.o.p.e.length,o.getObjectId());
					}
				}
				
				
			}
			//this.GetElementByRID("data").value = s;
			/*org.cote.js.util.driver.ApplicationDriver.destroy();*/
		},
		PrintTrans : function(){
			var oList = this.GetList();
			oList.clearItems();
			
			// _t = org.cote.js.transaction.TransactionService,
			var i,t,a,_m=org.cote.js.message.MessageService,ts,h,p,o;
			//a = _t.getPackets();
			//var s = "";
			oList.addItem("TRANSACTIONS","_avoid");
			ts = org.cote.js.registry.ObjectRegistry.getObjects();
			for(h = 0; h < ts.length; h++){
				if(!ts[h] || ts[h].getObjectType() != "transaction_service" || (this.GetElementByRID("oFilterPrefix").checked && !ts[h].getObjectId().match(/^dwd-/))) continue;
				oList.addItem("Transaction Service " + ts[h].getObjectId(),ts[h].getObjectId());
				a = ts[h].getPackets();
				for(i=0;i<a.length;i++){
					t = a[i];
					if(ts[h].isPacket(t)){
						//s += "Transaction " + t.packet_name + " / s = " + t.packet_state + " / o = " + t.is_open + " / f = " + t.is_finalized + "\n";
						oList.addItem("....Transaction " + t.packet_name + " (" + t.packet_id + ") / s = " + t.packet_state + " / o = " + t.is_open + " / f = " + t.is_finalized,t.getObjectId());
						for(var p = 0; p < t.participants.length; p++){
							o = org.cote.js.registry.ObjectRegistry.getObject(t.participants[p]);
							if(!o) oList.addItem("........Null Participant " + t.participants[p],"_avoid");
							else oList.addItem("........Participant " + t.participants[p] + " " + o.getObjectType(),o.getObjectId());
						}
					}
				}
			}
			//this.GetElementByRID("data").value = s;			
		},
		PrintTasks : function(){
			var oList = this.GetList();
			oList.clearItems();
			
			var _t = org.cote.js.task.TaskService,i,t,a,_m=org.cote.js.message.MessageService,or,h;
			a = _t.getTasks();
			//var s = "COMMON TASK SERVICE\n";
			/*
			oList.addItem("COMMON TASK SERVICE","_avoid");
			for(i=0;i<a.length;i++){
				t = a[i];
				if(this.GetElementByRID("oFilterPrefix").checked && !t.getObjectId().match(/^dwd-/)) continue;
				//s += "Task " + t.task_name + " / e = " + t.executed + " / h = " + t.handled + " / d = " + t.depends.length + " / s = " + t.task_state + " / " + _t.getTaskDepends(t).join(",") + "\n";
				oList.addItem("Task " + t.task_name + " (" + t.task_id + ") / e = " + t.executed + " / h = " + t.handled + " / d = " + t.depends.length + " / s = " + t.task_state + " / " + _t.getTaskDepends(t).join(","),t.task_id);

			}
			*/
			//s += "\nENGINE TASK SERVICE\n";
			oList.addItem("TASK SERVICES","_avoid");
			or = org.cote.js.registry.ObjectRegistry.getObjects();
			for(h = 0; h < or.length; h++){
				if(!or[h] || or[h].getObjectType() != "task_service" || (this.GetElementByRID("oFilterPrefix").checked && !or[h].getObjectId().match(/^dwd-/))) continue;

				a = or[h].getTasks();
				//org.cote.js.engine.EngineService.getTaskService().getTasks();
				//oList.addItem("Engine " + or[h].getObjectId(),or[h].getObjectId());
				oList.addItem("....Task Service " + or[h].getObjectId(),or[h].getObjectId());
				for(i=0;i<a.length;i++){
					t = a[i];
					//s += "Task " + t.task_name + " / e = " + t.executed + " / h = " + t.handled + " / d = " + t.depends.length + " / s = " + t.task_state + " / " + _t.getTaskDepends(t).join(",") + "\n";
					oList.addItem("........Task " + t.task_name + " (" + t.task_id + ") / e = " + t.executed + " / h = " + t.handled + " / d = " + t.depends.length + " / s = " + t.task_state + " / " + _t.getTaskDepends(t).join(","),t.task_id);

				}	
			}		
			//this.GetElementByRID("data").value = s;
		},
		PrintEngines : function(){
			var oList = this.GetList();
			oList.clearItems();
			
			var i,t,a,or,h,eo,j;

			//s += "\nENGINE TASK SERVICE\n";
			oList.addItem("ENGINE SERVICES","_avoid");
			or = org.cote.js.registry.ObjectRegistry.getObjects();
			for(h = 0; h < or.length; h++){
				if(!or[h] || or[h].getObjectType() != "engine_service") continue;
				oList.addItem("Engine Service " + or[h].getObjectId(),or[h].getObjectId());
				a = or[h].getEngines();
				//oList.addItem("....Task Service " + or[h].getObjectId(),or[h].getObjectId());
				for(i=0;i<a.length;i++){
					t = a[i];
					if(this.GetElementByRID("oFilterPrefix").checked && !t.getObjectId().match(/^dwd-/)) continue;
					oList.addItem("....Engine " + t.engine_name + " (" + t.engine_id + ")" + (t.is_primary ? " (primary)" : ""),t.engine_id);
					eo = t.getObjects();
					for(j = 0; j < eo.length; j++){
						var label = 0;
						var etype = 0;
						var ntype = "";
						var eid = 0;
						if(!eo[j].object) label = "null";
						else{
							eid = eo[j].object.getObjectId();
							etype = eo[j].object.getObjectType();
							if(etype == "xhtml_component") ntype = " " + eo[j].object.getContainer().nodeName + " ";
							if(eo[j].rid) label = etype + ntype + " " + eo[j].rid;
							else if(!label) label = etype + etype;
							label += " (" + eid + ")";
						}
						oList.addItem("........" + label,eid);
					}
				}	
			}		
			//this.GetElementByRID("data").value = s;
		},
		Model : function(){
			var oList = this.GetList();
			this.getStatus().debug_mode = 1;
			this.getStatus().iWin = 0;
			this.getPointers().model_list = [];
			this.getPointers().model_index = [];
			this.getPointers().pathHash = [];
			this.getPointers().intModels = 0;
			var o = oList.getActiveItem(),ob;
			if(!o || !o.data || !(ob = org.cote.js.registry.ObjectRegistry.getObject(o.data))) return;
			oList.clearItems();
			this.getStatus().pointer_marker = "___pointer_marker_" + (new Date().getTime());

			var s = this.ModelDrill(window,0,this._prehandle_model,(ob.getContainer ? ob.getContainer() : ob));
			alert(s);
		},
		_handle_model : function(oWin, frame_index, parent_path, object_name, object_pointer, depth){
			//this.GetList().addItem("model");
			if(!this.getStatus().sProtoParent) this.getStatus().sProtoParent=null;
			
			var wName = "window";
			if(oWin.name) wName = oWin.name;
			var sPName = "__Model-" + this.getStatus().iWin + "-";
			var sFName = sPName + wName + "::" + object_name;
			if(!this.getPointers().model_list[sFName])
				this.getPointers().model_index[this.getPointers().model_index] = sFName;

			try{
				this.getPointers().model_list[sFName]={"window":oWin,findex:this.getStatus().iWin,"pointer":object_pointer,"srcFile":wName,"name":object_name,"line":0,"proto":this.getStatus().sProtoParent};
				if(!this.getPointers().pathHash[sPName]){
					this.GetList().addItem("Window '" + wName + "' (" + this.getStatus().iWin + ")",sPName);
					this.getPointers().pathHash[sPName]=1;
					this.getStatus().intModels++;
				}
			}
			catch(e){
				this.GetList().addItem("Error " + e.message);
			}
		}
		

	]]></embedded-script>
</html-fragment>