private static TestObjectNurse BuildTestObjectsHierarchy(List <AutomationElement> ancestorElements,
                                                                 BreadcrumbControl breadcrumbControl,
                                                                 LAPListViewControl listView,
                                                                 Bitmap bmpDeskTop
                                                                 )
        {
            int ancestorCount            = ancestorElements.Count;
            int breadcrumbCount          = breadcrumbControl.Count;
            int indexOfAutomationElement = 0;

            UIATestObject   topTestObject      = UIAUtility.CreateTestObject(ancestorElements[0]);
            TestObjectNurse topNurseObject     = new TestObjectNurse(topTestObject);
            TestObjectNurse currentNurseObject = topNurseObject;

            if (ancestorCount > breadcrumbCount)
            {
                for (indexOfAutomationElement = 1; indexOfAutomationElement < ancestorCount && breadcrumbCount < ancestorCount - indexOfAutomationElement; ++indexOfAutomationElement)
                {
                    AutomationElement element = ancestorElements[indexOfAutomationElement];
                    if (!string.IsNullOrEmpty(element.Current.Name))
                    {
                        UIATestObject childTestObject = UIAUtility.CreateTestObject(element);
                        currentNurseObject = (TestObjectNurse)currentNurseObject.AddChild(childTestObject);
                    }
                }
            }

            Breadcrumb[] breadcrumbs = breadcrumbControl.GetItems();
            foreach (Breadcrumb breadcrumb in breadcrumbs)
            {
                if (breadcrumb.Checked)
                {
                    UIATestObject childTestObject = UIAUtility.CreateTestObject((breadcrumb.Tag as ElementProperties).AutomationElement);
                    currentNurseObject = (TestObjectNurse)currentNurseObject.AddChild(childTestObject);
                }
            }

            ListView.CheckedListViewItemCollection selectedItems = listView.CheckedItems;

            foreach (ListViewItem item in selectedItems)
            {
                if (null == item.Tag)
                {
                    continue;
                }
                ElementProperties ep       = item.Tag as ElementProperties;
                TestObjectNurse   subNurse = ep.ToNurseObject();

                subNurse.ImageFile = SnapshotHelper.SnapshotFileFromBitmap(subNurse.TestObject, bmpDeskTop);
                currentNurseObject.AddChild(subNurse);
            }
            return(topNurseObject);
        }
        internal static void BuildListviewControl(List <AutomationElement> childElements,
                                                  LAPListViewControl lapListViewObjects,
                                                  EventHandler linkItemClick
                                                  )
        {
            int[] checkedIndexes = lapListViewObjects.CheckedIndicesArray;

            ListView.SelectedIndexCollection selectedIndexs = lapListViewObjects.SelectedIndices;

            int selectIndex = selectedIndexs.Count == 0 ? 0 : selectedIndexs[0];

            lapListViewObjects.BeginUpdate();

            foreach (Control control in lapListViewObjects.Controls)
            {
                if (control.Name.IndexOf("linkLabel") >= 0)
                {
                    control.Click -= linkItemClick;// lvItem_lnLinkItemClick;
                    control.Dispose();
                }
            }


            lapListViewObjects.Items.Clear();
            if (0 == childElements.Count)
            {
                lapListViewObjects.EndUpdate();
                return;
            }


            int index = 0;
            int i = 0, j = 0;

            ElementNotAvailableException elementNotAvailableException = null;

            List <ElementProperties> propertiesList = null;

            Utility.AsyncCall(() =>
            {
                try
                {
                    propertiesList = childElements.ConvertAll <ElementProperties>((element) =>
                    {
                        System.Windows.Rect rect = element.Cached.BoundingRectangle;
                        if (rect.IsEmpty)
                        {
                            return(null);
                        }
                        return(new ElementProperties(element));
                    });
                }
                catch (ElementNotAvailableException ex)
                {
                    lapListViewObjects.Invoke(new Action(() => { lapListViewObjects.EndUpdate(); }));
                    //TODO, still not being handled by some handler
                    elementNotAvailableException = ex;
                }
            });

            if (elementNotAvailableException != null)
            {
                lapListViewObjects.EndUpdate();
                throw elementNotAvailableException;
            }

            foreach (var properties in propertiesList)
            {
                //zero size control will be skipped.
                if (properties == null)
                {
                    continue;
                }

                ListViewSubItem[] lvSubItem = new ListViewSubItem[3];
                Graphics          graphics  = lapListViewObjects.CreateGraphics();

                string    controlType = properties.ControlType.ControlTypeToString();
                LinkLabel linkLabel   = new LinkLabel();
                linkLabel.Name     = string.Format("linkLabel{0}", j++);
                linkLabel.Text     = controlType;
                linkLabel.AutoSize = true;
                linkLabel.Click   += linkItemClick; //lvItem_lnLinkItemClick;

                lvSubItem[0] = new ListViewSubItem()
                {
                    Text = string.Empty
                };
                lvSubItem[1] = new ListViewSubItem()
                {
                    Text = controlType, Tag = linkLabel
                };
                lvSubItem[2] = new ListViewSubItem()
                {
                    Text = properties.Name
                };

                graphics.Dispose();

                ListViewItem lvItem = new ListViewItem(lvSubItem, 0);

                if (checkedIndexes.Length > 0 && i < checkedIndexes.Length)
                {
                    if (index == checkedIndexes[i])
                    {
                        lvItem.Checked = true;
                        ++i;
                    }
                }
                //   lvItem.Checked = true;
                lvItem.Tag = properties;
                lapListViewObjects.Items.Add(lvItem);
                ++index;
            }

            if (lapListViewObjects.Items.Count > 0)
            {
                lapListViewObjects.Focus();
                ListViewItem lvItem = selectIndex >= lapListViewObjects.Items.Count ? lapListViewObjects.Items[0] : lapListViewObjects.Items[selectIndex];
                lvItem.Selected = true;
                lvItem.Focused  = true;
            }

            lapListViewObjects.EndUpdate();
        }