IfcDoc.FormEdit.InitInstanceList C# (CSharp) Method

InitInstanceList() private method

private InitInstanceList ( ) : void
return void
        private void InitInstanceList()
        {
            this.ctlConcept.CurrentInstance = null;
            this.ctlProperties.CurrentInstance = null;

            this.listViewValidate.Items.Clear();

            if (this.m_formatTest == null || this.m_assembly == null)
            {
                this.listViewValidate.Items.Clear();
                return;
            }

            this.toolStripLabelValidateFile.Text = String.Empty;
            foreach (object o in this.m_formatTest.Headers)
            {
                if (o is IfcDoc.Schema.FILE_NAME)
                {
                    IfcDoc.Schema.FILE_NAME fn = (IfcDoc.Schema.FILE_NAME)o;
                    this.toolStripLabelValidateFile.Text = fn.Name;
                }
            }

            // check for scope based on selected node...
            DocEntity docEntity = null;
            DocTemplateDefinition docTemplate = null;
            DocTemplateUsage docUsage = null;
            Type typeFilter = null;
            TreeNode tnSelect = this.treeView.SelectedNode;
            if (tnSelect != null)
            {
                if (tnSelect.Tag is DocTemplateDefinition)
                {
                    docTemplate = (DocTemplateDefinition)tnSelect.Tag;
                    docEntity = this.m_project.GetDefinition(docTemplate.Type) as DocEntity;
                }
                else if(tnSelect.Tag is DocTemplateUsage)
                {
                    docUsage = (DocTemplateUsage)tnSelect.Tag;
                    if (docUsage.Validation == null)
                        return;

                    docTemplate = (DocTemplateDefinition)docUsage.Definition;

                    TreeNode tnTest = tnSelect.Parent;
                    while(tnTest.Tag is DocTemplateUsage)
                    {
                        tnTest = tnTest.Parent;
                    }
                    docEntity = (DocEntity)tnTest.Parent.Tag;
                }
                else if (tnSelect.Tag is DocConceptRoot)
                {
                    docEntity = (DocEntity)tnSelect.Parent.Tag;
                }
                else if (tnSelect.Tag is DocEntity)
                {
                    docEntity = (DocEntity)tnSelect.Tag;
                }

                if (docEntity != null)
                {
                    DocSchema docSchema = this.m_project.GetSchemaOfDefinition(docEntity);
                    if (docSchema != null)
                    {
                        string typename = docSchema.Name + "." + docEntity.Name;
                        typeFilter = this.m_assembly.GetType(typename);
                    }
                }
            }

            //if (this.treeView.Tag == typeFilter)
            //    return;

            this.treeView.Tag = typeFilter;

            if (typeFilter == null)
                return;

            List<SEntity> population = new List<SEntity>();
            foreach(SEntity entity in this.m_formatTest.Instances.Values)
            {
                if (typeFilter == null || typeFilter.IsInstanceOfType(entity))
                {
                    DocTemplateUsage docUsageForEntity = docUsage;

                    // find the leaf-most template usage for the entity
                    if (docUsageForEntity == null && docTemplate != null && this.m_filterviews != null)
                    {
                        DocEntity docSuper = this.m_project.GetDefinition(entity.GetType().Name) as DocEntity;// docEntity;
                        while (docSuper != null && docUsageForEntity == null)
                        {
                            foreach (DocModelView docView in this.m_filterviews)
                            {
                                foreach (DocConceptRoot docRoot in docView.ConceptRoots)
                                {
                                    if (docRoot.ApplicableEntity == docSuper)
                                    {
                                        foreach (DocTemplateUsage docEachUsage in docRoot.Concepts)
                                        {
                                            if (docEachUsage.Definition == docTemplate && docEachUsage.ValidationStructure.Count > 0)
                                            {
                                                docUsageForEntity = docEachUsage;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            docSuper = this.m_project.GetDefinition(docSuper.BaseDefinition) as DocEntity;
                        }
                    }

                    // show rule results
                    if (docUsageForEntity != null)
                    {
                        population.Add(entity);

                        ListViewItem lvi = new ListViewItem();
                        lvi.Tag = entity;
                        lvi.Text = entity.OID.ToString();
                        lvi.SubItems.Add(entity.GetType().Name);

                        bool? result = docUsageForEntity.GetResultForObject(entity);

                        if (result == null)
                        {
                            lvi.BackColor = Color.Gray;
                        }
                        else if(result != null && result.Value)
                        {
                            lvi.BackColor = Color.Lime;
                        }
                        else
                        {
                            lvi.BackColor = Color.Red;
                        }

                        System.Reflection.FieldInfo field = entity.GetType().GetField("Name");
                        if (field != null)
                        {
                            object oname = field.GetValue(entity);
                            if (oname != null)
                            {
                                if (oname.GetType().IsValueType)
                                {
                                    string name = oname.GetType().GetFields()[0].GetValue(oname) as string;
                                    lvi.SubItems.Add(name);
                                }
                            }
                        }

                        //... set icon as pass/fail...

                        this.listViewValidate.Items.Add(lvi);
                    }
                }
            }

            this.ctlProperties.CurrentPopulation = population.ToArray();
        }
FormEdit