module(..., package.seeall)
local configurator = require("configurator.configurator")

function aaa()

return [[( {:Name: (( {:Name: [a-zA-Z0-9-_" ']* :} ) {('')}? ('{' {:Namespace: [a-zA-Z0-9-_://#&.]* :} '}')? ) -> {} :} )
{('')}? (':' {:Type: (( {:Type: (DT_A) :} ) {('')}? ('{' {:Namespace: [a-zA-Z0-9-_:#\\]* :} '}')? )  -> {} :} )?
{('')}? ('[' {:Multiplicity: [a-zA-Z0-9-_ ..*()]* :} ']')?
{('')}? (' {' {:IsFunctional: [func]* :} '}')?
{('')}? ('{' {:EquivalentProperties: (( {:ASFictitiousEquivalentProperties: (('=' {:EquivalentProperties: (( {:Expression: [a-zA-Z0-9-_]* :} )) -> {} :} )? -> {} (',' ('=' {:EquivalentProperties: (( {:Expression: [a-zA-Z0-9-_]* :} )) -> {} :} )? -> {})*  ) -> {} :})?)  -> {} :} '}')?
{('')}? ('{' {:SuperProperties: (( {:ASFictitiousSuperProperties: (('<' {:SuperProperties: (( {:Expression: [a-zA-Z0-9-_ ]* :} )) -> {} :} )?-> {} (',' ('<' {:SuperProperties: (( {:Expression: [a-zA-Z0-9-_ ]* :} )) -> {} :} )? -> {})*  ) -> {} :})?)  -> {} :} '}')?
{('')}? ('{' {:DisjointProperties: (( {:ASFictitiousDisjointProperties: (('<>' {:DisjointProperties: (( {:Expression: [a-zA-Z0-9-_]* :} )) -> {} :} )? -> {} (',' ('<>' {:DisjointProperties: (( {:Expression: [a-zA-Z0-9-_]* :} )) -> {} :} )? -> {})*  ) -> {} :})?)  -> {} :} '}')?
{('')}? ('\n' {:ASFictitiousAnnotation: (( {:Annotation: (( {:AnnotationType: [a-zA-Z0-9-_ ]* :} )
{('')}? ('{' {:Namespace: [^}]* :} '}')?
{('')}? ('(' {:ValueLanguage: (('"' {:Value: [^"]* :} '"')
{('')}? ('@' {:Language: [a-zA-Z0-9-_ ]* :} )?)  -> {} :} ')')?) -> {} :} )-> {} ('\n' ( {:Annotation: (( {:AnnotationType: [a-zA-Z0-9-_ ]* :} )
{('')}? ('{' {:Namespace: [^}]* :} '}')?
{('')}? ('(' {:ValueLanguage: (('"' {:Value: [^"]* :} '"')
{('')}? ('@' {:Language: [a-zA-Z0-9-_ ]* :} )?)  -> {} :} ')')?) -> {} :} ) -> {})*  ) -> {} :} )?
	DT_A <- ({DT_B ('(' DT_A ')')* })
	DT_B <- ({([^"()"])*})
]]
end

function parametersTable()
	return {
     [" - ontology export"] = true,
     ["ontology - ontology annotations"] = true,
     ["classes - classes"] = true,
     ["ontology - annotation property definitions"] = true,
     ["classes - class annotations"] = true,
     ["classes - keys"] = true,
     ["individuals - individuals"] = true,
     ["individuals - individual annotations"] = true,
     ["object properties - merge inverse object properties"] = true,
     ["object properties - object properties"] = true,
     ["object properties - sub object properties"] = true,
     ["object properties - equivalent object properties"] = true,
     ["object properties - disjoint object properties"] = true,
     ["object properties - property chains"] = true,
     ["object properties - is functional"] = true,
     ["object properties - is inverSefunctional"] = true,
     ["object properties - is symmetric"] = true,
     ["object properties - is asymmetric"] = true,
     ["object properties - is reflexive"] = true,
     ["object properties - is irreflexive"] = true,
     ["object properties - is transitive"] = true,
     ["object properties - object property annotations"] = true,
     ["data properties - data properties"] = true,
     ["data properties - sub data properties"] = true,
     ["data properties - equivalent data properties"] = true,
     ["data properties - disjoint data properties"] = true,
     ["data properties - is functional"] = true,
     ["data properties - data property annotations"] = true,
     ["classes - merge disjoint classes links"] = true,
     ["classes - mutualy disjoint classes larger then 2 as boxes"] = true,
     ["classes - pairs of disjoint classes as lines"] = true,
     ["classes - other disjoint classes as compartments"] = false,
     ["classes - merge equivalent classes links"] = true,
     ["classes - mutualy equivalent classes larger then 2 as boxes"] = true,
     ["classes - pairs of equivalent classes as lines"] = true,
     ["classes - other equivalent classes as compartments"] = false,
     ["individuals - merge different individuals links"] = true,
     ["individuals - mutualy different individuals larger then 2 as boxes"] = true,
     ["individuals - pairs of different individuals as lines"] = true,
     ["individuals - other different individuals as compartments"] = false,
     ["individuals - merge same individuals links"] = true,
     ["individuals - mutualy same individuals larger then 2 as boxes"] = true,
     ["individuals - pairs of same individuals as lines"] = true,
     ["individuals - other same individuals as compartments"] = false,
     ["individuals - class assertions"] = true,
     ["individuals - object property assertions"] = true,
     ["individuals - data property assertions"] = true,
     ["individuals - negative object property assertions"] = true,
     ["individuals - negative data property assertions"] = true,
     [" - subclass axioms"] = true,
     ["object properties - show subclass of cardinality restrictions as multiplicity"] = true,
     ["data properties - show subclass of cardinality restrictions as multiplicity"] = true,
     ["object properties - show subclass of some as line"] = true,
     ["object properties - show subclass of only as line"] = true,
     ["object properties - show subclass of min as line"] = true,
     ["object properties - show subclass of max as line"] = true,
     ["object properties - show subclass of exact as line"] = true,
     ["object properties - object property restrictions as lines"] = true,
     ["classes - anonymous subclasses as boxes"] = true,
     ["classes - make top classes subclass of thing"] = true,
     ["classes - make named classes subclass of corresponding anonymous unions"] = true,
     ["classes - merge subclass lines with forks"] = true,
     [" - subclass forks"] = true,
     ["classes - subclass lines"] = true,
     ["classes - other super class as text"] = false,
     ["classes - equivalent to and named class as subclass link"] = true,
     [" - add compartments to elements"] = true,
     ["data-type - data types"] = true,
     ["ontology - unexported axioms"] = true
}
end

function extraParametersTable()
	return {
		["disjoint classes as text"] = false,
		["equivalent classes as text"] = false,
		["same individuals as text"] = false,
		["different individuals as text"] = false,
		["disjoint classes show others as text"] = true,
		["equivalent classes show others as text"] = true,
		["same individuals show others as text"] = true,
		["different individuals show others as text"] = true,
		["subclasses as text"] = false,
		["subclasses graphicaly"] = true,
		["subclasses graphicaly as lines"] = true,
		["subclasses show others as text"] = true,
	}
end

function completeMetamodel()
	lQuery.model.add_class("OWL_PP#Parameter")
		lQuery.model.add_property("OWL_PP#Parameter", "name")
		lQuery.model.add_property("OWL_PP#Parameter", "defaultValue")
	lQuery.model.add_class("OWL_PP#ParamValue")
		lQuery.model.add_property("OWL_PP#ParamValue", "value")
	lQuery.model.add_class("OWL_PP#ParamProfile")
		lQuery.model.add_property("OWL_PP#ParamProfile", "name")
		lQuery.model.add_property("OWL_PP#ParamProfile", "isSystemProfile")
		
	lQuery.model.add_link("OWL_PP#Parameter", "parameter", "value", "OWL_PP#ParamValue")
	lQuery.model.add_link("OWL_PP#ParamProfile", "profile", "value", "OWL_PP#ParamValue")
	lQuery.model.add_link("OWL_PP#ParamProfile", "current", "toolType", "ToolType")
	
	lQuery.create("Tag", {key='advancedParametersShow', value = false}):link("type", lQuery("ToolType"))
	
	local parametersTable = parametersTable()
	for param, defaultValue in pairs(parametersTable) do
		lQuery.create("OWL_PP#Parameter", {name = param, defaultValue = defaultValue})
	end
	
	local extraParametersTable = extraParametersTable()
	for param, defaultValue in pairs(extraParametersTable) do
		lQuery.create("OWL_PP#Parameter", {name = param, defaultValue = defaultValue})
	end
	
	lQuery.create("OWL_PP#ParamProfile", {name='current', isSystemProfile=true}):link("toolType", lQuery("ToolType"))
	saveDefaultParametersToMetamodel()
	
	local project_dgr_type = lQuery("GraphDiagramType[id=projectDiagram]")

	-- get or create toolbar type
	local toolbarType = project_dgr_type:find("/toolbarType")
	if toolbarType:is_empty() then
	  toolbarType = lQuery.create("ToolbarType", {graphDiagramType = project_dgr_type})
	end
	
	local pl_manager_toolbar_el = lQuery.create("ToolbarElementType", {
	  toolbarType = toolbarType,
	  id = "OntologyImportParametersSets",
	  caption = "Ontology loading preferences",
	  picture = "parameters.bmp",
	  procedureName = "parameters.showForm()"
	})
	
	configurator.make_toolbar(project_dgr_type)
end

function selectAllHH()
	lQuery("D#Event/source/container/container/component/component[id='VerticalBox']/component[type='CheckBox']"):attr("checked", true)
	lQuery("D#Event/source/container/container/component/component[id='VerticalBox']"):each(function(vb)
		vb:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	end)
end

function clearAllHH()
	lQuery("D#Event/source/container/container/component/component[id='VerticalBox']/component[type='CheckBox']"):attr("checked", false)
	lQuery("D#Event/source/container/container/component/component[id='VerticalBox']"):each(function(vb)
		vb:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	end)
end

function selectAllHV()
	lQuery("D#Event/source/container/container/component[id='VerticalBox']/component[type='CheckBox']"):attr("checked", true)
	lQuery("D#Event/source/container/container/component[id='VerticalBox']"):link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
end

function clearAllHV()
	lQuery("D#Event/source/container/container/component[id='VerticalBox']/component[type='CheckBox']"):attr("checked", false)
	lQuery("D#Event/source/container/container/component[id='VerticalBox']"):link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
end

function saveDefaultParametersToMetamodel()
	local profile = lQuery("ToolType"):find("/current")
	local parameterTableFromCheckBox = parameterTableFromCheckBox()
	for parameterName, parentParameterName in pairs (parameterTableFromCheckBox) do
		local value = ""
		createParameterValue(value, parameterName, profile)
	end
	local parameterTableFromRadioButton = parameterTableFromRadioButton()
	for parameterName, parentParameterName in pairs (parameterTableFromRadioButton) do
		local value = ""
		createParameterValue(value, parameterName, profile)
	end
end

function saveCopiedParametersToMetamodel(copyProfile, profile)
	local parameterTableFromCheckBox = parameterTableFromCheckBox()
	for parameterName, parentParameterName in pairs (parameterTableFromCheckBox) do
		local value = profile:find("/value:has(/parameter[name='" .. parameterName .. "'])"):attr("value")
		createParameterValue(value, parameterName, copyProfile)
	end
	local parameterTableFromRadioButton = parameterTableFromRadioButton()
	for parameterName, parentParameterName in pairs (parameterTableFromRadioButton) do
		local value = profile:find("/value:has(/parameter[name='" .. parameterName .. "'])"):attr("value")
		createParameterValue(value, parameterName, copyProfile)
	end
end

function saveParametersToMetamodel()
	local profile = lQuery("ToolType"):find("/current")
	createParameterValueFromCheckBox(profile)
	createParameterValueFromRadioButton(profile)
end

function createParameterValueFromRadioButton(profile)
	local parameterTableFromRadioButton = parameterTableFromRadioButton()
	for parameterName, parentParameterName in pairs (parameterTableFromRadioButton) do
		local value = lQuery("D#RadioButton[id='" .. parameterName .. "']"):attr("selected")
		createParameterValue(value, parameterName, profile)
	end
end

function parameterTableFromCheckBox()
	return {
		["ontology - ontology annotations"] = 0,
		["ontology - unexported axioms"] = 0,
		["classes - classes"] = 0,
		["object properties - object properties"] = 0,
		["data properties - data properties"] = 0,
		["individuals - individuals"] = 0,
		["data-type - data types"] = 0,
		["ontology - annotation property definitions"] = 0,
		
		[" - subclass axioms"] = "classes - classes",
		["classes - merge disjoint classes links"] = "classes - classes",
		["classes - merge equivalent classes links"] = "classes - classes",
		["classes - keys"] = "classes - classes",
		["classes - class annotations"] = "classes - classes",
		["data properties - sub data properties"] = "data properties - data properties",
		["data properties - disjoint data properties"] = "data properties - data properties",
		["data properties - equivalent data properties"] = "data properties - data properties",
		["data properties - data property annotations"] = "data properties - data properties",
		["data properties - is functional"] = "data properties - data properties",
		["data properties - show subclass of cardinality restrictions as multiplicity"] = "data properties - data properties",
		["object properties - merge inverse object properties"] = "object properties - object properties",
		["object properties - sub object properties"] = "object properties - object properties",
		["object properties - disjoint object properties"] = "object properties - object properties",
		["object properties - equivalent object properties"] = "object properties - object properties",
		["object properties - property chains"] = "object properties - object properties",
		["object properties - object property annotations"] = "object properties - object properties",
		["object properties - is functional"] = "object properties - object properties",
		["object properties - is inverSefunctional"] = "object properties - object properties",
		["object properties - is symmetric"] = "object properties - object properties",
		["object properties - is asymmetric"] = "object properties - object properties",
		["object properties - is transitive"] = "object properties - object properties",
		["object properties - is reflexive"] = "object properties - object properties",
		["object properties - is irreflexive"] = "object properties - object properties",
		["object properties - show subclass of some as line"] = "object properties - object properties",
		["object properties - show subclass of only as line"] = "object properties - object properties",
		["object properties - show subclass of min as line"] = "object properties - object properties",
		["object properties - show subclass of max as line"] = "object properties - object properties",
		["object properties - show subclass of exact as line"] = "object properties - object properties",
		["object properties - show subclass of cardinality restrictions as multiplicity"] = "object properties - object properties",
		["individuals - merge same individuals links"] = "individuals - individuals",
		["individuals - merge different individuals links"] = "individuals - individuals",
		["individuals - individual annotations"] = "individuals - individuals",
		["individuals - class assertions"] = "individuals - individuals",
		["individuals - object property assertions"] = "individuals - individuals",
		["individuals - data property assertions"] = "individuals - individuals",
		["individuals - negative object property assertions"] = "individuals - individuals",
		["individuals - negative data property assertions"] = "individuals - individuals",
		
		["classes - make top classes subclass of thing"] = {" - subclass axioms", "classes - classes"},
		["classes - equivalent to and named class as subclass link"] = {" - subclass axioms", "classes - classes"},
		["classes - make named classes subclass of corresponding anonymous unions"] = {" - subclass axioms", "classes - classes"},
		["classes - anonymous subclasses as boxes"] = {" - subclass axioms", "classes - classes", "classes - classes"},
		["classes - mutualy disjoint classes larger then 2 as boxes"] = {"classes - merge disjoint classes links","classes - classes", "classes - pairs of disjoint classes as lines"},
		["classes - mutualy equivalent classes larger then 2 as boxes"] = {"classes - merge equivalent classes links","classes - classes", "classes - pairs of equivalent classes as lines"},
		["individuals - mutualy same individuals larger then 2 as boxes"] = {"individuals - merge same individuals links","individuals - individuals", "individuals - pairs of same individuals as lines"},
		["individuals - mutualy different individuals larger then 2 as boxes"] = {"individuals - merge different individuals links","individuals - individuals", "individuals - pairs of different individuals as lines"},
		
		["subclasses show others as text"] = true,
		["disjoint classes show others as text"] = true,
		["equivalent classes show others as text"] = true,
		["same individuals show others as text"] = true,
		["different individuals show others as text"] = true,
	}
end

function parameterTableFromRadioButton()
	return {
		["classes - pairs of disjoint classes as lines"] = {"classes - merge disjoint classes links", "classes - classes"},
		["classes - pairs of equivalent classes as lines"] = {"classes - merge equivalent classes links", "classes - classes"},
		["individuals - pairs of same individuals as lines"] = {"individuals - merge same individuals links", "individuals - individuals"},
		["individuals - pairs of different individuals as lines"] = {"individuals - merge different individuals links", "individuals - individuals"},
		["disjoint classes as text"] = {"classes - merge disjoint classes links", "classes - classes"},
		["equivalent classes as text"] = {"classes - merge equivalent classes links", "classes - classes"},
		["same individuals as text"] = {"individuals - merge same individuals links", "individuals - individuals"},
		["different individuals as text"] = {"individuals - merge different individuals links", "individuals - individuals"},
		["classes - merge subclass lines with forks"] = {" - subclass axioms", "classes - classes", "subclasses graphicaly"},
		["subclasses graphicaly"] = {" - subclass axioms", "classes - classes"},
		["subclasses graphicaly as lines"] = true,
		["subclasses as text"] = false,
	}
end

function parameterTableOrAnd()
	return {
		["classes - other super class as text"] = {["or"]={"subclasses as text", "subclasses show others as text"}, ["and"]={" - subclass axioms", "classes - classes"}},
		["classes - subclass lines"] = {["or"]={"subclasses graphicaly as lines", "classes - merge subclass lines with forks"}, ["and"]={" - subclass axioms", "classes - classes", "subclasses graphicaly"}},
		["classes - other disjoint classes as compartments"] = {["or"]={"disjoint classes as text", "disjoint classes show others as text"}, ["and"]={"classes - merge disjoint classes links", "classes - classes"}},
		["classes - other equivalent classes as compartments"] = {["or"]={"equivalent classes as text", "equivalent classes show others as text"}, ["and"]={"classes - merge equivalent classes links", "classes - classes"}},
		["individuals - other same individuals as compartments"] = {["or"]={"same individuals as text", "same individuals show others as text"}, ["and"]={"individuals - merge same individuals links", "individuals - individuals"}},
		["individuals - other different individuals as compartments"] = {["or"]={"different individuals as text", "different individuals show others as text"}, ["and"]={"individuals - merge different individuals links", "individuals - individuals"}}
	}
end

function createParameterValueFromCheckBox(profile)
	local parameterTableFromCheckBox = parameterTableFromCheckBox()
	for parameterName, parentParameterName in pairs (parameterTableFromCheckBox) do
		local value = lQuery("D#CheckBox[id='" .. parameterName .. "']"):attr("checked")
		createParameterValue(value, parameterName, profile)
	end
end

function createParameterValue(value, parameterName, profile)
	local parameter = lQuery("OWL_PP#Parameter[name='" .. parameterName .. "']")
	if value == "" then value = parameter:attr("defaultValue") end
	if profile:find("/value/parameter[name='" .. parameterName .. "']"):is_empty() then 
		lQuery.create("OWL_PP#ParamValue", {value = value})
			:link("profile", profile)
			:link("parameter", parameter)
	else
		profile:find("/value:has(/parameter[name='" .. parameterName .. "'])"):attr("value", value)
	end
end

function createParametersTable(profileName)
	if profileName==nil then profileName = lQuery("OWL_PP#ParamProfile[isSystemProfile='true']"):attr("name") end
	local profile = lQuery("OWL_PP#ParamProfile[name='" .. profileName .. "']")
	local parametersTable = parametersTable()
	local parameterTableFromCheckBox = parameterTableFromCheckBox()
	local parameterTableFromRadioButton = parameterTableFromRadioButton()
	local parametersTableForOntologyImport = {}
	for param, defaultValue in pairs(parametersTable) do
		local parentParameterName = nil
		local value = defaultValue
		if parameterTableFromCheckBox[param]~=nil then
			parentParameterName = parameterTableFromCheckBox[param]
		elseif parameterTableFromRadioButton[param]~=nil then
			parentParameterName = parameterTableFromRadioButton[param]
		end
		if  parentParameterName ~= nil then
			value = profile:find("/value:has(/parameter[name='" .. param .. "'])"):attr("value")
			if type(parentParameterName) == "string" then
				local parentParameterValue = profile:find("/value:has(/parameter[name='" .. parentParameterName .. "'])"):attr("value")
				if parentParameterValue=="false" then 
					value = false
				end
			elseif type(parentParameterName) == "table" then
				local label = 0
				for i, parentParameterNames in pairs (parentParameterName) do
					local parentParameterValue = profile:find("/value:has(/parameter[name='" .. parentParameterNames .. "'])"):attr("value")
					if parentParameterValue=="false" then 
						label = 1
						break
					end
				end
				if label == 1 then
					value = false
				end
			end
		end
		local parameterTableOrAnd = parameterTableOrAnd()
		if parameterTableOrAnd[param]~=nil then
			local OrAndTable = parameterTableOrAnd[param]
			local OrTable = OrAndTable["or"]
			local AndTable = OrAndTable["and"]
			-- print(dumptable(OrTable), "OrTable")
			-- print(dumptable(AndTable), "AndTable")
			for i, Or in pairs (OrTable) do
				local OrValue = profile:find("/value:has(/parameter[name='" .. Or .. "'])"):attr("value")
				if OrValue == "true" then value = true end
			end
			for i, And in pairs (AndTable) do
				local AndValue = profile:find("/value:has(/parameter[name='" .. And .. "'])"):attr("value")
				if AndValue == "false" then value = false end
			end
		end
		if param == "classes - equivalent to and named class as subclass link" then param = "classes - equivalent to 'and' named class as subclass link" end
		if value == "true" or value==true then value=true
		else value = false end
		--print(value, "333")
		if string.sub(param, 1, 2) ~= " -" then
			parametersTableForOntologyImport[param] = value
		end
		
	end
	--print(dumptable(parametersTableForOntologyImport))
	return parametersTableForOntologyImport
end

function loadParameterProfile()
	local selectedItem = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']/selected")
	if selectedItem:is_not_empty() then
		local parameterProfileName = selectedItem:attr("value")
		local profile = lQuery("OWL_PP#ParamProfile[name='" .. parameterProfileName .. "']")
		lQuery("ToolType"):remove_link("current", lQuery("ToolType"):find("/current"))
		profile:link("toolType", lQuery("ToolType"))
		lQuery("OWL_PP#ParamProfile[isSystemProfile='true']"):attr("isSystemProfile", false)
		profile:attr("isSystemProfile", true)
		
		local verBox = lQuery("D#VerticalBox[id='VerticalBoxPropertyTabs']")
		verBox:find("/component"):delete()
		verBox:link("component", parametersTabs(profile))
		verBox:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
		closeLoadParameterProfileForm()	
		
		refreshListBoxParametersProfile()
	end
end

function updateParameterProfile()
	saveParametersToMetamodel()
	closeUpdateParameterProfileForm()
end

function saveAsNewParameterProfile()
	local parameterProfileName = lQuery("D#InputField[id='NewParameterProfileName']"):attr("text")
	local profile = lQuery.create("OWL_PP#ParamProfile", {name = parameterProfileName, isSystemProfile=false})
	createParameterValueFromCheckBox(profile)
	createParameterValueFromRadioButton(profile)
	refreshListBoxParametersProfile()
	closeSaveAsNewParameterProfile()
end

function copyParameterProfile()
	local selectedItem = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']/selected")
	if selectedItem:is_not_empty() then
		local newParameterProfileName = lQuery("D#InputField[id='NewParameterProfileName']"):attr("text")
		local profile = lQuery("OWL_PP#ParamProfile[name='" .. newParameterProfileName .. "']")
		local copyProfile = lQuery.create("OWL_PP#ParamProfile", {name=newParameterProfileName, isSystemProfile=false})
		saveCopiedParametersToMetamodel(copyProfile, profile)
		refreshListBoxParametersProfile()
		closeSaveAsNewParameterProfile()
	end
end

function deleteParameterProfile()
	local selectedItem = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']/selected")
	if selectedItem:is_not_empty() then
		local parameterProfileName = selectedItem:attr("value")
		local profile = lQuery("OWL_PP#ParamProfile[name='" .. parameterProfileName .. "']")
		profile:find("/value"):delete()
		profile:delete()
		refreshListBoxParametersProfile()
		closeDeleteParameterProfileForm()
	end
end

function refreshListBoxParametersProfile()
	local listBox = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']")
	listBox:find("/item"):delete()
	local items = collectParameterProfiles()
	listBox:link("item", items)
	listBox:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))	
end

function parameterTableExtraCheckEnabled()
	return {
	["subclasses show others as text"] = {" - subclass axioms", "classes - classes"},
	["disjoint classes show others as text"] = {"classes - merge disjoint classes links", "classes - pairs of disjoint classes as lines", "classes - classes"},
	["equivalent classes show others as text"] = {"classes - classes", "classes - pairs of equivalent classes as lines", "classes - merge equivalent classes links"},
	["same individuals show others as text"] = {"individuals - individuals", "individuals - pairs of same individuals as lines", "individuals - merge same individuals links"},
	["different individuals show others as text"] = {"individuals - individuals","individuals - pairs of different individuals as lines", "individuals - merge different individuals links"},
	["subclasses as text"] = {"classes - classes", " - subclass axioms"},
	["subclasses graphicaly as lines"] = {"classes - classes", " - subclass axioms", "subclasses graphicaly"}
  }
end

function checkEnabled(checkBoxId)
	local enabled = true
	
	local parameterTableFromCheckBox = parameterTableFromCheckBox()
	local parameterTableFromRadioButton = parameterTableFromRadioButton()
	local parameterTableExtraCheckEnabled = parameterTableExtraCheckEnabled()
	
	local parentParameterName = nil
	if parameterTableExtraCheckEnabled[checkBoxId]~=nil then
		parentParameterName = parameterTableExtraCheckEnabled[checkBoxId]
	elseif parameterTableFromCheckBox[checkBoxId]~=nil then
		parentParameterName = parameterTableFromCheckBox[checkBoxId]
	elseif parameterTableFromRadioButton[checkBoxId]~=nil then
		parentParameterName = parameterTableFromRadioButton[checkBoxId]
	end
	if  parentParameterName ~= nil then
		if type(parentParameterName) == "string" then
			local parentParameterValue = lQuery("D#CheckBox[id='" .. parentParameterName .. "']"):attr("checked")
			if parentParameterValue=="false" then 
				enabled = false
			end
		elseif type(parentParameterName) == "table" then
			local label = 0
			for i, parentParameterNames in pairs (parentParameterName) do
				local parentParameterValue = lQuery("D#CheckBox[id='" .. parentParameterNames .. "']"):attr("checked")
				if parentParameterValue=="false" then 
					label = 1
					break
				elseif lQuery("D#RadioButton[id='" .. parentParameterNames .. "']"):attr("selected") == "false" then label = 1
				end
			end
			if label == 1 then
				enabled = false
			end
		end
	end
	return enabled
end

function enabledParameterTable()
	return {
		["data properties - data properties"] = {"data properties - sub data properties", "data properties - equivalent data properties", "data properties - disjoint data properties", "data properties - is functional", "data properties - data property annotations", "data properties - show subclass of cardinality restrictions as multiplicity"}
		,["individuals - merge different individuals links"] = {"different individuals as text", "individuals - pairs of different individuals as lines", "individuals - mutualy different individuals larger then 2 as boxes", "different individuals show others as text"}
		,["individuals - merge same individuals links"] = {"same individuals as text", "individuals - pairs of same individuals as lines", "individuals - mutualy same individuals larger then 2 as boxes", "same individuals show others as text"}
		,["individuals - individuals"] = {"individuals - merge same individuals links", "individuals - merge different individuals links", "different individuals as text", "individuals - pairs of different individuals as lines", "individuals - mutualy different individuals larger then 2 as boxes", "different individuals show others as text", "same individuals as text", "individuals - pairs of same individuals as lines", "individuals - mutualy same individuals larger then 2 as boxes", "same individuals show others as text", "individuals - individual annotations", "individuals - class assertions", "individuals - object property assertions", "individuals - data property assertions", "individuals - negative object property assertions", "individuals - negative data property assertions"}
		,["object properties - object properties"] = {"object properties - merge inverse object properties", "object properties - sub object properties", "object properties - disjoint object properties", "object properties - equivalent object properties", "object properties - property chains", "object properties - object property annotations", "object properties - is functional", "object properties - is inverSefunctional", "object properties - is symmetric", "object properties - is asymmetric", "object properties - is transitive", "object properties - is reflexive", "object properties - is irreflexive", "object properties - show subclass of some as line", "object properties - show subclass of only as line", "object properties - show subclass of min as line", "object properties - show subclass of max as line", "object properties - show subclass of exact as line", "object properties - show subclass of cardinality restrictions as multiplicity"}
		,[" - subclass axioms"] = {"subclasses as text", "subclasses graphicaly", "subclasses graphicaly as lines", "classes - merge subclass lines with forks", "subclasses show others as text", "classes - make top classes subclass of thing", "classes - equivalent to and named class as subclass link", "classes - make named classes subclass of corresponding anonymous unions", "classes - anonymous subclasses as boxes"}
		,["classes - merge disjoint classes links"] = {"disjoint classes as text", "classes - pairs of disjoint classes as lines", "classes - mutualy disjoint classes larger then 2 as boxes", "disjoint classes show others as text"}
		,["classes - merge equivalent classes links"] = {"equivalent classes as text", "classes - pairs of equivalent classes as lines", "classes - mutualy equivalent classes larger then 2 as boxes", "equivalent classes show others as text"}
		,["classes - classes"] = {" - subclass axioms", "classes - merge disjoint classes links", "classes - merge equivalent classes links", "subclasses as text", "subclasses graphicaly", "subclasses graphicaly as lines", "classes - merge subclass lines with forks", "subclasses show others as text", "classes - make top classes subclass of thing", "classes - equivalent to and named class as subclass link", "classes - make named classes subclass of corresponding anonymous unions", "classes - anonymous subclasses as boxes", "disjoint classes as text", "classes - pairs of disjoint classes as lines", "classes - mutualy disjoint classes larger then 2 as boxes", "disjoint classes show others as text", "equivalent classes as text", "classes - pairs of equivalent classes as lines", "classes - mutualy equivalent classes larger then 2 as boxes", "equivalent classes show others as text", "classes - keys", "classes - class annotations"}
		,["subclasses as text"] = {"subclasses graphicaly as lines", "classes - merge subclass lines with forks"}
		,["subclasses graphicaly"] = {"subclasses graphicaly as lines", "classes - merge subclass lines with forks"}
		,["disjoint classes as text"] = {"classes - mutualy disjoint classes larger then 2 as boxes", "disjoint classes show others as text"}
		,["classes - pairs of disjoint classes as lines"] = {"classes - mutualy disjoint classes larger then 2 as boxes", "disjoint classes show others as text"}
		,["equivalent classes as text"] = {"classes - mutualy equivalent classes larger then 2 as boxes", "equivalent classes show others as text"}
		,["classes - pairs of equivalent classes as lines"] = {"classes - mutualy equivalent classes larger then 2 as boxes", "equivalent classes show others as text"}
		,["same individuals as text"] = {"individuals - mutualy same individuals larger then 2 as boxes", "same individuals show others as text"}
		,["individuals - pairs of same individuals as lines"] = {"individuals - mutualy same individuals larger then 2 as boxes", "same individuals show others as text"}
		,["different individuals as text"] = {"individuals - mutualy different individuals larger then 2 as boxes", "different individuals show others as text"}
		,["individuals - pairs of different individuals as lines"] = {"individuals - mutualy different individuals larger then 2 as boxes", "different individuals show others as text"}
	}
end

function enableFields()
	local checkedId = lQuery("D#Event/source"):attr("id")
	local enabledParameterTable = enabledParameterTable()
	local enabledParameterFields = enabledParameterTable[checkedId]
	local checked = lQuery("D#Event/source"):attr("checked")
	local fields
	for i, form in pairs(enabledParameterFields) do
		local field = lQuery("D#Component[id='" .. form .. "']")
		field:attr("enabled", checkEnabled(form))
		field:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	end
end

function enableFieldsAsText()
	local checkedId = lQuery("D#Event/source"):attr("id")
	local enabledParameterTable = enabledParameterTable()
	local enabledParameterFields = enabledParameterTable[checkedId]
	for i, form in pairs(enabledParameterFields) do
		local field = lQuery("D#Component[id='" .. form .. "']")
		field:attr("enabled", false)
		field:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	end
end

function enableFieldsGraphically()
	local checkedId = lQuery("D#Event/source"):attr("id")
	local enabledParameterTable = enabledParameterTable()
	local enabledParameterFields = enabledParameterTable[checkedId]
	for i, form in pairs(enabledParameterFields) do
		local field = lQuery("D#Component[id='" .. form .. "']")
		field:attr("enabled", true)
		field:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	end
end

function showForm()
	local currentParameterProfile = lQuery("ToolType"):find("/current")
	show_window(currentParameterProfile)
end

function show_window(profile)

  local close_button = lQuery.create("D#Button", {
    caption = "Close"
    ,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.close()")
  })
  local plus_button = lQuery.create("D#Button", {
    caption = "+"
    ,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.showAdvancedForm()")
  })
  
  local form = lQuery.create("D#Form", {
    id = "visualization_parameters"
    ,caption = "Ontology loading preferences"
    ,buttonClickOnClose = false
    ,cancelButton = close_button
    ,defaultButton = close_button
    ,eventHandler = utilities.d_handler("Close", "lua_engine", "lua.parameters.close()")
    ,minimumWidth = 410
	,minimumHeight=600
	,component = {
		lQuery.create("D#HorizontalBox",{
			id = "HorizontalAllForms"
			,component = {
				lQuery.create("D#VerticalBox",{
					id = "VerticalBoxPropertyTabs"
					,component = {
						parametersTabs(profile)
					}
				})
				,advancedButtonsAutoShow()
				,advancedProfilesAutoShow()
			}})
		,lQuery.create("D#HorizontalBox", {
			horizontalAlignment = 1
			,component = {close_button, plus_button}
		})
    }
  })
  dialog_utilities.show_form(form)
end

function advancedButtonsAutoShow()
	if lQuery("ToolType/tag[key='advancedParametersShow']"):attr("value") == "true" then
		return advancedButtons()
	end
end

function advancedProfilesAutoShow()
	if lQuery("ToolType/tag[key='advancedParametersShow']"):attr("value") == "true" then
		return advancedProfiles()
	end
end

function parametersTabs(profile)

	local tabs = lQuery.create("D#TabContainer", {
			component = {
				lQuery.create("D#Tab", {
					caption = "Ontology"
					,horizontalAlignment = -1
					,verticalAlignment = -1
					,component = {
						lQuery.create("D#VerticalBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,component = {
								lQuery.create("D#CheckBox", {
									caption = "Ontology annotations"
									,id = "ontology - ontology annotations"
									,checked = profile:find("/value:has(/parameter[name='ontology - ontology annotations'])"):attr("value")
								})
								,lQuery.create("D#CheckBox", {
									caption = "Unexported axioms"
									,id = "ontology - unexported axioms"
									,checked = profile:find("/value:has(/parameter[name='ontology - unexported axioms'])"):attr("value")
								})
								,lQuery.create("D#CheckBox", {
									caption = "Annotation property definitions"
									,id = "ontology - annotation property definitions"
									,checked = profile:find("/value:has(/parameter[name='ontology - annotation property definitions'])"):attr("value")
								})
							}
						})
					}
				})
				,lQuery.create("D#Tab", {
					caption = "Classes"
					,horizontalAlignment = -1
					,verticalAlignment = -1
					,component = {
						lQuery.create("D#VerticalBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,component = {
								lQuery.create("D#CheckBox", {
									caption = "Show classes"
									,id = "classes - classes"
									,checked = profile:find("/value:has(/parameter[name='classes - classes'])"):attr("value")
									,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
								})
								,lQuery.create("D#GroupBox", {
									minimumWidth = 385
									,component = {
										lQuery.create("D#HorizontalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Subclasses"
													,id = " - subclass axioms"
													,checked = profile:find("/value:has(/parameter[name=' - subclass axioms'])"):attr("value")
													,enabled = checkEnabled(" - subclass axioms")
													,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
												})
												,lQuery.create("D#VerticalBox", {
													horizontalAlignment = -1
													,verticalAlignment = -1
													,component = {
														lQuery.create("D#RadioButton", {
															caption = "As text"
															,id = "subclasses as text"
															,selected = profile:find("/value:has(/parameter[name='subclasses as text'])"):attr("value")
															,enabled = checkEnabled("subclasses as text")
															,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsAsText()")
														})
														,lQuery.create("D#RadioButton", {
															caption = "Graphically"
															,id = "subclasses graphicaly"
															,selected = profile:find("/value:has(/parameter[name='subclasses graphicaly'])"):attr("value")
															,enabled = checkEnabled("subclasses graphicaly")
															,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsGraphically()")
														})
														,lQuery.create("D#GroupBox", {
															horizontalAlignment = -1
															,verticalAlignment = -1
															,leftMargin = 30
															,component = {
																lQuery.create("D#RadioButton", {
																	caption = "As lines"
																	--,leftMargin = 30
																	,id = "subclasses graphicaly as lines"
																	,selected = profile:find("/value:has(/parameter[name='subclasses graphicaly as lines'])"):attr("value")
																	,enabled = checkEnabled("subclasses graphicaly as lines")
																})
																,lQuery.create("D#RadioButton", {
																	caption = "As forks (if > 1)"
																	--,leftMargin = 30
																	,id = "classes - merge subclass lines with forks"
																	,selected = profile:find("/value:has(/parameter[name='classes - merge subclass lines with forks'])"):attr("value")
																	,enabled = checkEnabled("classes - merge subclass lines with forks")
																})
														}})
														,lQuery.create("D#CheckBox", {
															caption = "Show other as text"
															,id = "subclasses show others as text"
															,checked = profile:find("/value:has(/parameter[name='subclasses show others as text'])"):attr("value")
															,enabled = checkEnabled("subclasses show others as text")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Make top classes subclass of 'Thing'"
															,id = "classes - make top classes subclass of thing"
															,checked = profile:find("/value:has(/parameter[name='classes - make top classes subclass of thing'])"):attr("value")
															,enabled = checkEnabled("classes - make top classes subclass of thing")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Show subclass link for equivalent to 'and' named class"
															,id = "classes - equivalent to and named class as subclass link"
															,checked = profile:find("/value:has(/parameter[name='classes - equivalent to and named class as subclass link'])"):attr("value")
															,enabled = checkEnabled("classes - equivalent to and named class as subclass link")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Make named classes subclass of anonymous unions"
															,id = "classes - make named classes subclass of corresponding anonymous unions"
															,checked = profile:find("/value:has(/parameter[name='classes - make named classes subclass of corresponding anonymous unions'])"):attr("value")
															,enabled = checkEnabled("classes - make named classes subclass of corresponding anonymous unions")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Anonymous subclasses as boxes"
															,id = "classes - anonymous subclasses as boxes"
															,checked = profile:find("/value:has(/parameter[name='classes - anonymous subclasses as boxes'])"):attr("value")
															,enabled = checkEnabled("classes - anonymous subclasses as boxes")
														})
													}
												})
											}
										})
									}
								})
								,lQuery.create("D#GroupBox", {
									minimumWidth = 385
									,component = {
										lQuery.create("D#HorizontalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Disjoint classes"
													,id = "classes - merge disjoint classes links"
													,enabled = checkEnabled("classes - merge disjoint classes links")
													,checked = profile:find("/value:has(/parameter[name='classes - merge disjoint classes links'])"):attr("value")
													,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
												})
												,lQuery.create("D#VerticalBox", {
													horizontalAlignment = -1
													,verticalAlignment = -1
													,component = {
														lQuery.create("D#RadioButton", {
															caption = "As text"
															,id = "disjoint classes as text"
															,enabled = checkEnabled("disjoint classes as text")
															,selected = profile:find("/value:has(/parameter[name='disjoint classes as text'])"):attr("value")
															,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsAsText()")
														})
														,lQuery.create("D#RadioButton", {
															caption = "Graphically"
															,id = "classes - pairs of disjoint classes as lines"
															,enabled = checkEnabled("classes - pairs of disjoint classes as lines")
															,selected = profile:find("/value:has(/parameter[name='classes - pairs of disjoint classes as lines'])"):attr("value")
															,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsGraphically()")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Group as boxes (if > 2)"
															,leftMargin = 20
															,id = "classes - mutualy disjoint classes larger then 2 as boxes"
															,enabled = checkEnabled("classes - mutualy disjoint classes larger then 2 as boxes")
															,checked = profile:find("/value:has(/parameter[name='classes - mutualy disjoint classes larger then 2 as boxes'])"):attr("value")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Show others as text"
															,leftMargin = 20
															,id = "disjoint classes show others as text"
															,enabled = checkEnabled("disjoint classes show others as text")
															,checked = profile:find("/value:has(/parameter[name='disjoint classes show others as text'])"):attr("value")
														})
													}
												})
											}
										})
									}
								})
								,lQuery.create("D#GroupBox", {
									minimumWidth = 385
									,component = {
										lQuery.create("D#HorizontalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Equivalent classes"
													,id = "classes - merge equivalent classes links"
													,enabled = checkEnabled("classes - merge equivalent classes links")
													,checked = profile:find("/value:has(/parameter[name='classes - merge equivalent classes links'])"):attr("value")
													,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
												})
												,lQuery.create("D#VerticalBox", {
													horizontalAlignment = -1
													,verticalAlignment = -1
													,component = {
														lQuery.create("D#RadioButton", {
															caption = "As text"
															,id = "equivalent classes as text"
															,enabled = checkEnabled("equivalent classes as text")
															,selected = profile:find("/value:has(/parameter[name='equivalent classes as text'])"):attr("value")
															,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsAsText()")
														})
														,lQuery.create("D#RadioButton", {
															caption = "Graphically"
															,id = "classes - pairs of equivalent classes as lines"
															,enabled = checkEnabled("classes - pairs of equivalent classes as lines")
															,selected = profile:find("/value:has(/parameter[name='classes - pairs of equivalent classes as lines'])"):attr("value")
															,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsGraphically()")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Group as boxes (if > 2)"
															,leftMargin = 20
															,id = "classes - mutualy equivalent classes larger then 2 as boxes"
															,enabled = checkEnabled("classes - mutualy equivalent classes larger then 2 as boxes")
															,checked = profile:find("/value:has(/parameter[name='classes - mutualy equivalent classes larger then 2 as boxes'])"):attr("value")
														})
														,lQuery.create("D#CheckBox", {
															caption = "Show others as text"
															,leftMargin = 20
															,id = "equivalent classes show others as text"
															,enabled = checkEnabled("equivalent classes show others as text")
															,checked = profile:find("/value:has(/parameter[name='equivalent classes show others as text'])"):attr("value")
														})
													}
												})
											}
										})
									}
								})
								,lQuery.create("D#CheckBox", {
									caption = "Keys"
									,id = "classes - keys"
									,checked = profile:find("/value:has(/parameter[name='classes - keys'])"):attr("value")
									,enabled = checkEnabled("classes - keys")
									,leftMargin = 10
								})
								,lQuery.create("D#CheckBox", {
									caption = "Annotations"
									,id = "classes - class annotations"
									,checked = profile:find("/value:has(/parameter[name='classes - class annotations'])"):attr("value")
									,enabled = checkEnabled("classes - class annotations")
									,leftMargin = 10
								})
							}
						})
					}
				})
				,lQuery.create("D#Tab", {
					caption = "Object properties"
					,horizontalAlignment = -1
					,verticalAlignment = -1
					,component = {
						lQuery.create("D#CheckBox", {
							caption = "Show object properties"
							,id = "object properties - object properties"
							,enabled = checkEnabled("object properties - object properties")
							,checked = profile:find("/value:has(/parameter[name='object properties - object properties'])"):attr("value")
							,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,minimumWidth = 385
							,component = {
								lQuery.create("D#HorizontalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#VerticalBox", {
											horizontalAlignment = -1
											,id = 'VerticalBox'
											,verticalAlignment = -1
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Merge inverse properties"
													,id = "object properties - merge inverse object properties"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - merge inverse object properties")
													,checked = profile:find("/value:has(/parameter[name='object properties - merge inverse object properties'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Superproperties"
													,id = "object properties - sub object properties"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - sub object properties")
													,checked = profile:find("/value:has(/parameter[name='object properties - sub object properties'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Disjoint properties"
													,id = "object properties - disjoint object properties"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - disjoint object properties")
													,checked = profile:find("/value:has(/parameter[name='object properties - disjoint object properties'])"):attr("value")
												})
											}
										})
										,lQuery.create("D#VerticalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,id = 'VerticalBox'
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Equivalent properties"
													,id = "object properties - equivalent object properties"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - equivalent object properties")
													,checked = profile:find("/value:has(/parameter[name='object properties - equivalent object properties'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Property chains"
													,id = "object properties - property chains"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - property chains")
													,checked = profile:find("/value:has(/parameter[name='object properties - property chains'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Annotations"
													,id = "object properties - object property annotations"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - object property annotations")
													,checked = profile:find("/value:has(/parameter[name='object properties - object property annotations'])"):attr("value")
												})
											}
										})
									}
								})
								,lQuery.create("D#HorizontalBox", {
									horizontalAlignment = 1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#Button", {
											caption = "Select all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.selectAllHH()")
										})
										,lQuery.create("D#Button", {
											caption = "Clear all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.clearAllHH()")
										})
									}
								})
							}
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,minimumWidth = 385
							,verticalAlignment = -1
							,component = {
								lQuery.create("D#HorizontalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#VerticalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,id = 'VerticalBox'
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Is functional"
													,id = "object properties - is functional"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is functional")
													,checked = profile:find("/value:has(/parameter[name='object properties - is functional'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Is inverse functional"
													,id = "object properties - is inverSefunctional"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is inverSefunctional")
													,checked = profile:find("/value:has(/parameter[name='object properties - is inverSefunctional'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Is symmetric"
													,id = "object properties - is symmetric"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is symmetric")
													,checked = profile:find("/value:has(/parameter[name='object properties - is symmetric'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Is asymmetric"
													,id = "object properties - is asymmetric"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is asymmetric")
													,checked = profile:find("/value:has(/parameter[name='object properties - is asymmetric'])"):attr("value")
												})
											}
										})
										,lQuery.create("D#VerticalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,id = 'VerticalBox'
											,leftMargin = 25
											,rightMargin = 42
											,component = {
												lQuery.create("D#CheckBox", {
													caption = "Is transitive"
													,id = "object properties - is transitive"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is transitive")
													,checked = profile:find("/value:has(/parameter[name='object properties - is transitive'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Is reflexive"
													,id = "object properties - is reflexive"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is reflexive")
													,checked = profile:find("/value:has(/parameter[name='object properties - is reflexive'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Is irreflexive"
													,id = "object properties - is irreflexive"
													,type = "CheckBox"
													,enabled = checkEnabled("object properties - is irreflexive")
													,checked = profile:find("/value:has(/parameter[name='object properties - is irreflexive'])"):attr("value")
												})
											}
										})
									}
								})
								,lQuery.create("D#HorizontalBox", {
									horizontalAlignment = 1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#Button", {
											caption = "Select all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.selectAllHH()")
										})
										,lQuery.create("D#Button", {
											caption = "Clear all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.clearAllHH()")
										})
									}
								})
							}
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,bottomMargin = 50
							,component = {
								lQuery.create("D#VerticalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,id = "VerticalBox"
									,component = {
										lQuery.create("D#Label", {
											caption = "Show graphically object property restrictions:"
										})
										,lQuery.create("D#Label", {
											caption = "To show unchecked restriction types as text check the"
										})
										,lQuery.create("D#Label", {
											caption = "'Show others as text' box under 'Subclasses' in the 'Classes' tab"
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of 'some' as line"
											,id = "object properties - show subclass of some as line"
											,checked = profile:find("/value:has(/parameter[name='object properties - show subclass of some as line'])"):attr("value")
											,enabled = checkEnabled("object properties - show subclass of some as line")
											,type="CheckBox"
											--,leftMargin = 20
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of 'only' as line"
											,id = "object properties - show subclass of only as line"
											,checked = profile:find("/value:has(/parameter[name='object properties - show subclass of only as line'])"):attr("value")
											,enabled = checkEnabled("object properties - show subclass of only as line")
											,type="CheckBox"
											-- ,leftMargin = 20
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of 'min' as line"
											,id = "object properties - show subclass of min as line"
											,checked = profile:find("/value:has(/parameter[name='object properties - show subclass of min as line'])"):attr("value")
											,enabled = checkEnabled("object properties - show subclass of min as line")
											,type="CheckBox"
											-- ,leftMargin = 20
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of 'max' as line"
											,id = "object properties - show subclass of max as line"
											,checked = profile:find("/value:has(/parameter[name='object properties - show subclass of max as line'])"):attr("value")
											,enabled = checkEnabled("object properties - show subclass of max as line")
											,type="CheckBox"
											-- ,leftMargin = 20
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of 'exactly' as line"
											,id = "object properties - show subclass of exact as line"
											,checked = profile:find("/value:has(/parameter[name='object properties - show subclass of exact as line'])"):attr("value")
											,enabled = checkEnabled("object properties - show subclass of exact as line")
											,type="CheckBox"
											-- ,leftMargin = 20
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of cardinality restrictions as multiplicity"
											,id = "object properties - show subclass of cardinality restrictions as multiplicity"
											,checked = profile:find("/value:has(/parameter[name='object properties - show subclass of cardinality restrictions as multiplicity'])"):attr("value")
											,enabled = checkEnabled("object properties - show subclass of cardinality restrictions as multiplicity")
											,type="CheckBox"
											-- ,leftMargin = 20
										})
									}
								})
								,lQuery.create("D#HorizontalBox", {
									horizontalAlignment = 1
									,minimumWidth = 370
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#Button", {
											caption = "Select all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.selectAllHV()")
										})
										,lQuery.create("D#Button", {
											caption = "Clear all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.clearAllHV()")
										})
									}
								})
							}
						})
					}
				})
				,lQuery.create("D#Tab", {
					caption = "Data properties"
					,horizontalAlignment = -1
					,verticalAlignment = -1
					,component = {
						lQuery.create("D#CheckBox", {
							caption = "Show data properties"
							,id = "data properties - data properties"
							,enabled = checkEnabled("data properties - data properties")
							,checked = profile:find("/value:has(/parameter[name='data properties - data properties'])"):attr("value")
							,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,bottomMargin = 350
							,component = {
								lQuery.create("D#VerticalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,id= 'VerticalBox'
									,component = {
										lQuery.create("D#CheckBox", {
											caption = "Superproperties" 
											,id = "data properties - sub data properties"
										    ,type = "CheckBox"
											,enabled = checkEnabled("data properties - sub data properties")
											,checked = profile:find("/value:has(/parameter[name='data properties - sub data properties'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Disjoint properties" 
											,id = "data properties - disjoint data properties"
										    ,type = "CheckBox"
											,enabled = checkEnabled("data properties - disjoint data properties")
											,checked = profile:find("/value:has(/parameter[name='data properties - disjoint data properties'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Equivalent properties" 
											,id = "data properties - equivalent data properties"
										    ,type = "CheckBox"
											,enabled = checkEnabled("data properties - equivalent data properties")
											,checked = profile:find("/value:has(/parameter[name='data properties - equivalent data properties'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Annotations"
											,id = "data properties - data property annotations"
										    ,type = "CheckBox"
											,enabled = checkEnabled("data properties - data property annotations")
											,checked = profile:find("/value:has(/parameter[name='data properties - data property annotations'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Is functional"
											,id = "data properties - is functional"
										    ,type = "CheckBox"
											,enabled = checkEnabled("data properties - is functional")
											,checked = profile:find("/value:has(/parameter[name='data properties - is functional'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Subclass of cardinality restrictions as multiplicity"
											,id = "data properties - show subclass of cardinality restrictions as multiplicity"
										    ,type = "CheckBox"
											,enabled = checkEnabled("data properties - show subclass of cardinality restrictions as multiplicity")
											,checked = profile:find("/value:has(/parameter[name='data properties - show subclass of cardinality restrictions as multiplicity'])"):attr("value")
										})
									}
								})
								,lQuery.create("D#HorizontalBox", {
									horizontalAlignment = 1
									,minimumWidth = 370
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#Button", {
											caption = "Select all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.selectAllHV()")
										})
										,lQuery.create("D#Button", {
											caption = "Clear all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.clearAllHV()")
										})
									}
								})
							}
						})
					}
				})
				,lQuery.create("D#Tab", {
					caption = "Individuals"
					,horizontalAlignment = -1
					,verticalAlignment = -1
					,component = {
						lQuery.create("D#CheckBox", {
							caption = "Show individuals"
							,id = "individuals - individuals"
							,enabled = checkEnabled("individuals - individuals")
							,checked = profile:find("/value:has(/parameter[name='individuals - individuals'])"):attr("value")
							,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							--,bottomMargin = 300
							,component = {
								lQuery.create("D#HorizontalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#CheckBox", {
											caption = "Same individuals"
											,id = "individuals - merge same individuals links"
											,enabled = checkEnabled("individuals - merge same individuals links")
											,checked = profile:find("/value:has(/parameter[name='individuals - merge same individuals links'])"):attr("value")
											,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
										})
										,lQuery.create("D#VerticalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,component = {
												lQuery.create("D#RadioButton", {
													caption = "As text"
													,id = "same individuals as text"
													,enabled = checkEnabled("same individuals as text")
													,selected = profile:find("/value:has(/parameter[name='same individuals as text'])"):attr("value")
													,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsAsText()")
												})
												,lQuery.create("D#RadioButton", {
													caption = "Graphically"
													,id = "individuals - pairs of same individuals as lines"
													,enabled = checkEnabled("individuals - pairs of same individuals as lines")
													,selected = profile:find("/value:has(/parameter[name='individuals - pairs of same individuals as lines'])"):attr("value")
													,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsGraphically()")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Group as boxes (if > 2)"
													,leftMargin = 20
													,id = "individuals - mutualy same individuals larger then 2 as boxes"
													,enabled = checkEnabled("individuals - mutualy same individuals larger then 2 as boxes")
													,checked = profile:find("/value:has(/parameter[name='individuals - mutualy same individuals larger then 2 as boxes'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Show others as text"
													,leftMargin = 20
													,id = "same individuals show others as text"
													,enabled = checkEnabled("same individuals show others as text")
													,checked = profile:find("/value:has(/parameter[name='same individuals show others as text'])"):attr("value")
												})
											}
										})
									}
								})
							}
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,component = {
								lQuery.create("D#HorizontalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#CheckBox", {
											caption = "Different individuals"
											,id = "individuals - merge different individuals links"
											,enabled = checkEnabled("individuals - merge different individuals links")
											,checked = profile:find("/value:has(/parameter[name='individuals - merge different individuals links'])"):attr("value")
											,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.enableFields()")
										})
										,lQuery.create("D#VerticalBox", {
											horizontalAlignment = -1
											,verticalAlignment = -1
											,component = {
												lQuery.create("D#RadioButton", {
													caption = "As text"
													,id = "different individuals as text"
													,enabled = checkEnabled("different individuals as text")
													,selected = profile:find("/value:has(/parameter[name='different individuals as text'])"):attr("value")
													,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsAsText()")
												})
												,lQuery.create("D#RadioButton", {
													caption = "Graphically"
													,id = "individuals - pairs of different individuals as lines"
													,enabled = checkEnabled("individuals - pairs of different individuals as lines")
													,selected = profile:find("/value:has(/parameter[name='individuals - pairs of different individuals as lines'])"):attr("value")
													,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.enableFieldsGraphically()")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Group as boxes (if > 2)"
													,leftMargin = 20
													,id = "individuals - mutualy different individuals larger then 2 as boxes"
													,enabled = checkEnabled("individuals - mutualy different individuals larger then 2 as boxes")
													,checked = profile:find("/value:has(/parameter[name='individuals - mutualy different individuals larger then 2 as boxes'])"):attr("value")
												})
												,lQuery.create("D#CheckBox", {
													caption = "Show others as text"
													,leftMargin = 20
													,enabled = checkEnabled("different individuals show others as text")
													,id = "different individuals show others as text"
													,checked = profile:find("/value:has(/parameter[name='different individuals show others as text'])"):attr("value")
												})
											}
										})
									}
								})
							}
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,component = {
								lQuery.create("D#VerticalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#CheckBox", {
											caption = "Annotations"
											,enabled = checkEnabled("individuals - individual annotations")
											,id = "individuals - individual annotations"
											,checked = profile:find("/value:has(/parameter[name='individuals - individual annotations'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Class assertions"
											,id = "individuals - class assertions"
											,enabled = checkEnabled("individuals - class assertions")
											,checked = profile:find("/value:has(/parameter[name='individuals - class assertions'])"):attr("value")
										})
									}
								})
							}
						})
						,lQuery.create("D#GroupBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,bottomMargin = 113
							,minimumWidth = 385
							,component = {
								lQuery.create("D#VerticalBox", {
									horizontalAlignment = -1
									,verticalAlignment = -1
									,rightMargin = 76
									,id = 'VerticalBox'
									,component = {
										lQuery.create("D#CheckBox", {
											caption = "Object property assertions"
											,id = "individuals - object property assertions"
											,type = "CheckBox"
											,enabled = checkEnabled("individuals - object property assertions")
											,checked = profile:find("/value:has(/parameter[name='individuals - object property assertions'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Data property assertions"
											,id = "individuals - data property assertions"
											,type = "CheckBox"
											,enabled = checkEnabled("individuals - data property assertions")
											,checked = profile:find("/value:has(/parameter[name='individuals - data property assertions'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Negative object property assertions"
											,id = "individuals - negative object property assertions"
											,type = "CheckBox"
											,enabled = checkEnabled("individuals - negative object property assertions")
											,checked = profile:find("/value:has(/parameter[name='individuals - negative object property assertions'])"):attr("value")
										})
										,lQuery.create("D#CheckBox", {
											caption = "Negative data property assertions"
											,id = "individuals - negative data property assertions"
											,type = "CheckBox"
											,enabled = checkEnabled("individuals - negative data property assertions")
											,checked = profile:find("/value:has(/parameter[name='individuals - negative data property assertions'])"):attr("value")
										})
									}
								})
								,lQuery.create("D#HorizontalBox", {
									horizontalAlignment = 1
									,verticalAlignment = -1
									,component = {
										lQuery.create("D#Button", {
											caption = "Select all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.selectAllHV()")
										})
										,lQuery.create("D#Button", {
											caption = "Clear all"
											,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.clearAllHV()")
										})
									}
								})
							}
						})
					}
				})
				,lQuery.create("D#Tab", {
					caption = "Data types"
					,horizontalAlignment = -1
					,verticalAlignment = -1
					,component = {
						lQuery.create("D#VerticalBox", {
							horizontalAlignment = -1
							,verticalAlignment = -1
							,component = {
								lQuery.create("D#CheckBox", {
									caption = "Data types"
									,id = "data-type - data types"
									,enabled = checkEnabled("data-type - data types")
									,checked = profile:find("/value:has(/parameter[name='data-type - data types'])"):attr("value")
								})
							}
						})
					}
				})
			}
		})
	return tabs
end

function advancedButtons()
	local buttons = lQuery.create("D#VerticalBox", {
			horizontalAlignment = 1
			,id = "VerticalBoxAdvancedButtonsForm"
			,leftMargin = 10
			,component = {
				  lQuery.create("D#Label", {
					caption = "Saved parameter sets"
					,bottomMargin = 100
				  })
				  ,lQuery.create("D#Button", {
					caption = "<- Load"
					,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.loadParameterProfileForm()")
				  })
				  ,lQuery.create("D#Button", {
					caption = "Update ->"
					,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.updateParameterProfileForm()")
				  })
				  ,lQuery.create("D#Button", {
					caption = "Save as new ->"
					,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.saveAsNewParameterProfileForm()")
				  })
				  ,lQuery.create("D#Button", {
					caption = "Copy"
					,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.copyParameterProfileForm()")
				  })
				  ,lQuery.create("D#Button", {
					caption = "Delete"
					,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.deleteParameterProfileForm()")
					,bottomMargin = 300
				  })
			}
		})
	return buttons
end

function advancedProfiles()
	local profiles = lQuery.create("D#VerticalBox", {
			horizontalAlignment = 1
			,id = "VerticalBoxAdvancedParametersForm"
			,leftMargin = 15
			,minimumWidth = 200
			,component = {
				lQuery.create("D#ListBox", {
					id = 'ListBoxWithParametersProfiles'
					,topMargin = 30
					,item = collectParameterProfiles()
				})
				,lQuery.create("D#Row", {
					horizontalAlignment = -1
					,component = {
						lQuery.create("D#CheckBox", {
							id = 'AutoShowAdvanced'
							,checked = lQuery("ToolType/tag[key='advancedParametersShow']"):attr("value")
							,eventHandler = utilities.d_handler("Change", "lua_engine", "lua.parameters.setAdvancedParametersTag()")
						})
						,lQuery.create("D#Label", {caption = 'Autoshow advanced'})
					}
				})
			}
		})
	return profiles
end

function setAdvancedParametersTag()
	lQuery("ToolType/tag[key='advancedParametersShow']"):attr("value", lQuery("D#Event/source"):attr("checked"))
end

function showAdvancedForm()
	local allForms = lQuery("D#HorizontalBox[id='HorizontalAllForms']")
	if allForms:find("/component[id='VerticalBoxAdvancedButtonsForm']"):is_empty() and allForms:find("/component[id='VerticalBoxAdvancedParametersForm']"):is_empty() then
		allForms:link("component", advancedButtons())
		allForms:link("component", advancedProfiles())
		allForms:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	else
		allForms:find("/component[id='VerticalBoxAdvancedButtonsForm']"):delete()
		allForms:find("/component[id='VerticalBoxAdvancedParametersForm']"):delete()
		allForms:link("command", utilities.enqued_cmd("D#Command", {info = "Refresh"}))
	end
end

function collectParameterProfiles()
	local values = lQuery("OWL_PP#ParamProfile"):map(function(obj)
		if obj:attr("isSystemProfile")~="true" then
			return lQuery(obj):attr("name")
		end
	end)  
		
	return lQuery.map(values, function(value) 
		return lQuery.create("D#Item", {
			value = value
		}) 
	end)
end

function saveAsNewParameterProfileForm()
	local close_button = lQuery.create("D#Button", {
		caption = "Close"
		,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.closeSaveAsNewParameterProfile()")
	})
	local ok_button = lQuery.create("D#Button", {
		caption = "Ok"
		,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.saveAsNewParameterProfile()")
	})
	local form = newParameterProfileForm(close_button, ok_button)
	dialog_utilities.show_form(form)
end

function copyParameterProfileForm()
	local selectedItem = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']/selected")
	if selectedItem:is_not_empty() then
		local close_button = lQuery.create("D#Button", {
			caption = "Close"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.closeSaveAsNewParameterProfile()")
		})
		local ok_button = lQuery.create("D#Button", {
			caption = "Ok"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.copyParameterProfile()")
		})
		local form = newParameterProfileForm(close_button, ok_button)
		dialog_utilities.show_form(form)
	end
end

function loadParameterProfileForm()
	local selectedItem = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']/selected")
	if selectedItem:is_not_empty() then
		local yes_button = lQuery.create("D#Button", {
			caption = "Yes"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.loadParameterProfile()")
		})
		local no_button = lQuery.create("D#Button", {
			caption = "No"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.closeLoadParameterProfileForm()")
		})
		local form = lQuery.create("D#Form", {
			id = "LoadParameterProfile"
			,caption = "Load parameter profile"
			,buttonClickOnClose = false
			,cancelButton = no_button
			,defaultButton = yes_button
			,eventHandler = utilities.d_handler("Close", "lua_engine", "lua.parameters.closeLoadParameterProfileForm()")
			,horizontalAlignment = 1
			,component = {
				lQuery.create("D#HorizontalBox", {component = {
					lQuery.create("D#Label", {caption = "Overwrite current parameters set?"})}
				})
				,lQuery.create("D#HorizontalBox", {component={yes_button ,no_button}})
			}
		})
		dialog_utilities.show_form(form)
	end
end

function updateParameterProfileForm()
		local yes_button = lQuery.create("D#Button", {
			caption = "Yes"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.updateParameterProfile()")
		})
		local no_button = lQuery.create("D#Button", {
			caption = "No"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.closeUpdateParameterProfileForm()")
		})
		local form = lQuery.create("D#Form", {
			id = "UpdateParameterProfile"
			,caption = "Update parameter profile"
			,buttonClickOnClose = false
			,cancelButton = no_button
			,defaultButton = yes_button
			,eventHandler = utilities.d_handler("Close", "lua_engine", "lua.parameters.closeUpdateParameterProfileForm()")
			,horizontalAlignment = 1
			,component = {
				lQuery.create("D#HorizontalBox", {component = {
					lQuery.create("D#Label", {caption = "Overwrite parameters set?"})}
				})
				,lQuery.create("D#HorizontalBox", {component={yes_button ,no_button}})
			}
		})
		dialog_utilities.show_form(form)
end

function deleteParameterProfileForm()
	local selectedItem = lQuery("D#ListBox[id='ListBoxWithParametersProfiles']/selected")
	if selectedItem:is_not_empty() then
		local yes_button = lQuery.create("D#Button", {
			caption = "Yes"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.deleteParameterProfile()")
		})
		local no_button = lQuery.create("D#Button", {
			caption = "No"
			,eventHandler = utilities.d_handler("Click", "lua_engine", "lua.parameters.closeDeleteParameterProfileForm()")
		})
		local form = lQuery.create("D#Form", {
			id = "DeleteParameterProfile"
			,caption = "Delete parameter profile"
			,buttonClickOnClose = false
			,cancelButton = no_button
			,defaultButton = yes_button
			,eventHandler = utilities.d_handler("Close", "lua_engine", "lua.parameters.closeDeleteParameterProfileForm()")
			,horizontalAlignment = 1
			,component = {
				lQuery.create("D#HorizontalBox", {component = {
					lQuery.create("D#Label", {caption = "Delete parameters set?"})}
				})
				,lQuery.create("D#HorizontalBox", {component={yes_button ,no_button}})
			}
		})
		dialog_utilities.show_form(form)
	end
end

function newParameterProfileForm(close_button, ok_button)
	local form = lQuery.create("D#Form", {
		id = "NewParameterProfile"
		,caption = "New parameter profile name"
		,buttonClickOnClose = false
		,cancelButton = close_button
		,defaultButton = ok_button
		,eventHandler = utilities.d_handler("Close", "lua_engine", "lua.parameters.closeSaveAsNewParameterProfile()")
		,horizontalAlignment = 1
		,component = {
			lQuery.create("D#InputField", {id="NewParameterProfileName", text = ""})
			,lQuery.create("D#HorizontalBox", {component={ok_button ,close_button}})
		}
	})
	return form
end

function closeLoadParameterProfileForm()
  lQuery("D#Event"):delete()
  utilities.close_form("LoadParameterProfile")
end

function closeUpdateParameterProfileForm()
  lQuery("D#Event"):delete()
  utilities.close_form("UpdateParameterProfile")
end

function closeDeleteParameterProfileForm()
  lQuery("D#Event"):delete()
  utilities.close_form("DeleteParameterProfile")
end

function closeSaveAsNewParameterProfile()
  lQuery("D#Event"):delete()
  utilities.close_form("NewParameterProfile")
end

function close()
  local allForms = lQuery("D#HorizontalBox[id='HorizontalAllForms']")
  if allForms:find("/component[id='VerticalBoxAdvancedButtonsForm']"):is_empty() and allForms:find("/component[id='VerticalBoxAdvancedParametersForm']"):is_empty() and lQuery("ToolType/tag[key='advancedParametersShow']"):attr("value") == "false" then
	saveParametersToMetamodel()
  end
  lQuery("D#Event"):delete()
  utilities.close_form("visualization_parameters")
  
  --createParametersTable(lQuery("OWL_PP#ParamProfile[isSystemProfile='true']"):attr("name"))
  createParametersTable()
end