void CreateTreeForList(TreeViewItem root, IAutosarTreeList list)
        {
            /* */
            bool         needAdd = false;
            TreeViewItem newTreeItem;

            if (list.CreateRoot() || (root == null))
            {
                newTreeItem            = new TreeViewItem();
                newTreeItem.Header     = list.GetName();
                newTreeItem.FontWeight = FontWeights.Normal;
                newTreeItem.Tag        = list;
                needAdd = true;
            }
            else
            {
                needAdd     = false;
                newTreeItem = root;
            }


            /* Create items for all properties */
            List <IGUID> guids = list.GetItems();

            if (guids != null)
            {
                foreach (IGUID guid in guids)
                {
                    TreeViewItem item = CreateItemsForGuid(newTreeItem, guid);

                    /* Create leafes for the lists*/
                    List <IAutosarTreeList> lists = guid.GetLists();
                    if (lists != null)
                    {
                        if (lists.Count > 0)
                        {
                            foreach (IAutosarTreeList treeList in lists)
                            {
                                CreateTreeForList(item, treeList);
                            }
                        }
                    }
                }
            }

            if (needAdd)
            {
                if (root != null)
                {
                    root.Items.Add(newTreeItem);
                }
                else
                {
                    newTreeItem.FontWeight = FontWeights.Bold;
                    this.Items.Add(newTreeItem);
                }
            }
        }
        TreeViewItem CreateItemsForList(TreeViewItem root, IAutosarTreeList list)
        {
            TreeViewItem newTreeItem = new TreeViewItem();

            newTreeItem.Header     = list.GetName();
            newTreeItem.FontWeight = FontWeights.Normal;
            newTreeItem.Tag        = list;
            root.Items.Add(newTreeItem);
            return(newTreeItem);
        }
        //public TreeViewItem FindItem(TreeViewItem root, IGUID guidObject)
        //{
        //    if (root == null)
        //    {
        //        foreach (TreeViewItem treeViewItem in Items)
        //        {
        //            /* Item is not found. Try to find in their children */
        //            return FindItem(treeViewItem, guidObject);
        //        }
        //    }
        //    else
        //    {
        //        IGUID treeGuidObj = root.Tag as IGUID;
        //        if (treeGuidObj != null)
        //        {
        //            if (treeGuidObj.GUID.Equals(guidObject.GUID))
        //            {
        //                return root;
        //            }
        //        }

        //        foreach (TreeViewItem treeViewItem in root.Items)
        //        {
        //            treeGuidObj = treeViewItem.Tag as IGUID;
        //            if (treeGuidObj != null)
        //            {
        //                if (treeGuidObj.GUID.Equals(guidObject.GUID))
        //                {
        //                    return treeViewItem;
        //                }
        //            }

        //            /* Item is not found. Try to find in their children */
        //            return FindItem(treeViewItem, guidObject);
        //        }
        //    }
        //    return null;
        //}

        public bool DoesGuidExistInProperties(List <PropertyInfo> properties, IGUID guidObject)
        {
            foreach (PropertyInfo property in properties)
            {
                IAutosarTreeList treeList = property.GetValue(AutosarApplication.GetInstance()) as IAutosarTreeList;

                foreach (IGUID guid in treeList.GetItems())
                {
                    if (DoesGuidExists(treeList, guidObject) == true)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
 bool DoesGuidExists(IAutosarTreeList treeList, IGUID guidToBeFound)
 {
     foreach (IGUID guid in treeList.GetItems())
     {
         if (guid.Equals(guidToBeFound))
         {
             return(true);
         }
         else
         {
             List <IAutosarTreeList> lists = guid.GetLists();
             foreach (IAutosarTreeList list in lists)
             {
                 if (DoesGuidExists(list, guidToBeFound) == true)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
        public void UpdateAutosarTreeView()
        {
            /* Find all properties in Autosar Application */
            /* Find all IGuidObjects */
            String res = "";

            PropertyInfo[]      allProperties = AutosarApplication.GetInstance().GetType().GetProperties();
            List <PropertyInfo> properties    = new List <PropertyInfo>();

            foreach (PropertyInfo property in allProperties)
            {
                if (property.GetValue(AutosarApplication.GetInstance()) as IAutosarTreeList != null)
                {
                    properties.Add(property);
                }
            }

            properties.Sort(delegate(PropertyInfo x, PropertyInfo y)
            {
                IAutosarTreeList list1 = x.GetValue(AutosarApplication.GetInstance()) as IAutosarTreeList;
                IAutosarTreeList list2 = y.GetValue(AutosarApplication.GetInstance()) as IAutosarTreeList;
                String name1           = list1.GetName();
                String name2           = list2.GetName();
                if ((name1 != null) && (name2 != null))
                {
                    string xName = name1.ToString();
                    string yName = name2.ToString();

                    if (xName == null && yName == null)
                    {
                        return(0);
                    }
                    else if (xName == null)
                    {
                        return(-1);
                    }
                    else if (yName == null)
                    {
                        return(1);
                    }
                    else
                    {
                        return(xName.CompareTo(yName));
                    }
                }

                throw new Exception("Sort exception! Properties not exists!");
            });

            /* Delete unexist */
            //DeleteUnexists(properties);


            /* Add new */
            TreeViewItem root = new TreeViewItem();

            foreach (PropertyInfo property in properties)
            {
                res += property.Name + " " + Environment.NewLine;
                IAutosarTreeList treeList = property.GetValue(AutosarApplication.GetInstance()) as IAutosarTreeList;
                if (treeList != null)
                {
                    /* Create root for the list */
                    CreateTreeForList(root, treeList);
                }
            }

            /* Sort */
            for (int i = 0; i < root.Items.Count; i++)
            {
                SortChilds(root.Items[i] as TreeViewItem);
            }

            /* syncronize root and items */
            SyncronizeRoot(root.Items.Count);
            for (int i = 0; i < root.Items.Count; i++)
            {
                Syncronize(root.Items[i] as TreeViewItem, Items[i] as TreeViewItem);
            }

            /* Sort */
            for (int i = 0; i < root.Items.Count; i++)
            {
                SortChilds(root.Items[i] as TreeViewItem);
            }
        }