public static List<TagItem> GetTagsList(TagFilesDatabase currentDb, TagsCombinaton tagsCombination, string searchText)
        {
            if (currentDb == null) return null;

            List<TagItem> ret = new List<TagItem>();
            // Add each tag from the database
            for (int i = 0; i < currentDb.Tags.Count; i++)
            {
                // Extract the tag
                FileTag tag = currentDb.Tags[i];

                // Show the tag only if it's not in the history of tags being selected - 
                // (You should not show tag 'X' after selecting tag 'X')                                    
                if (!tagsCombination.ContainsByValue(tag, false) && tag.Value.ToLower().Contains(searchText.ToLower()))
                {
                    TagItem item = new TagItem();
                    // Set the tag item (including text of the tag with the tag's file-count                    
                    item.Text = (tag.Value == "" ? No_Tags_String : tag.ToString()) +
                        "\n  [" + currentDb.CountFilesByTag(tag).ToString() + "]";
                    item.RawFileTag = tag;

                    // Map the tag to it's group
                    int groupKeyIndex = currentDb.tagGroupMapping[i];
                    item.GroupName = currentDb.groupsNames[groupKeyIndex];
                    item.GroupKey = currentDb.groupsKeys[groupKeyIndex];

                    ret.Add(item);
                }
            }
            return ret;
        }
        public void UpdateItems(TagFilesDatabase currentDb, TagsCombinaton tagsCombination, string searchText)
        {
            var tagItems = TagsListWraper.GetTagsList(currentDb, tagsCombination, searchText);            
            Dictionary<string, string> groups = new Dictionary<string,string>();
            if (tagItems != null)
            for (int i = 0; i < tagItems.Count; i++)
                groups[tagItems[i].GroupKey] = tagItems[i].GroupName;

            // Create the groups list. (Skip the default group - index 0)
            this.Groups.Clear();
            foreach (var kvp in groups)
                this.Groups.Add(kvp.Key, kvp.Value);            
            
            // Create item for the listView:
            List<ListViewItem> tagsList = new List<ListViewItem>();
            ListViewItem tagItem;
            // Add each tag from the database
            int j = 0;
            foreach (var t in tagItems)
            {
                // Show the tag only if it's not in the history of tags being selected - 
                // (You should not show tag 'X' after selecting tag 'X')
                if (!tagsCombination.ContainsByValue(t.RawFileTag, false) && 
                    t.Value.ToLower().Contains(searchText.ToLower()))
                {
                    // Set the tag item (including text of the tag with the tag's file-count
                    tagItem = new ListViewItem();
                    //tagItem.Text = j++.ToString().PadLeft(9, 'a') + "\n [10]";
                    tagItem.Text = t.Text;
                    tagItem.Tag = t.RawFileTag;

                    // Set image of the tag: special image if the tag's file-count is equals the
                    // database file-count. a regular folder image if not.
                    if (currentDb.Files.Count == currentDb.CountFilesByTag(t.RawFileTag))
                        tagItem.ImageIndex = 1;
                    else
                        tagItem.ImageIndex = 0;

                    // Map the tag to it's group
                    tagItem.Group = this.Groups[t.GroupKey];
                    tagsList.Add(tagItem);
                    //this.Items.Add(tagItem);
                }
            }

            // ** Sort the tags by groups, and inside of each group
            //ListViewItem tmp = new ListViewItem();
            //for (int i = 0; i < tagsList.Count; i++)
            //{
            //    for (int j = i + 1; j < tagsList.Count; j++)
            //    {
            //        int comperator = tagsList[i].Group.Name.CompareTo(tagsList[j].Group.Name);
            //        if (comperator == 1) // item[i].Group > item[j].Group
            //        { // Swap items
            //            tmp = (ListViewItem)tagsList[i].Clone();
            //            tagsList[i] = (ListViewItem)tagsList[j].Clone();
            //            tagsList[j] = tmp;
            //        }
            //        else if (comperator == 0) // Equal groups
            //        { // Sort internally

            //            comperator = ((FileTag)tagsList[i].Tag).ToString().CompareTo(
            //                ((FileTag)tagsList[j].Tag).ToString());

            //            if (comperator == 1)
            //            {
            //                tmp = (ListViewItem)tagsList[i].Clone();
            //                tagsList[i] = (ListViewItem)tagsList[j].Clone();
            //                tagsList[j] = tmp;
            //            }
            //        }
            //    }
            //}


            // ** Add to list view
            this.BeginUpdate();
            this.Clear();
            foreach (ListViewItem item in tagsList)
               this.Items.Add(item);
            
            this.EndUpdate();        
        }
        /// <summary>
        /// 
        /// </summary>
        private void updateTagsListView(ListView tagsListView, TagFilesDatabase database, TagsCombinaton tagsCombination, string searchTagText)
        {
            //for (int i = 0; i < 5; i++)
            //{
            //    tagsListView1.Groups.Add("key_"+i.ToString(), "name_" + i.ToString());
            //}
            //tagsListView.BeginUpdate();
            //tagsListView.Clear();
            //for (int i = 0; i < 100; i++)
            //{
            //    ListViewItem item = new ListViewItem();
            //    item.Text = "T".PadLeft(12, 'a');
            //    item.ImageIndex = i % 2;
            //    item.Group = tagsListView1.Groups[i % 5];
            //    item.Tag = new object();
            //    this.tagsListView1.Items.Add(item);
            //}
            //tagsListView.EndUpdate();
            //return;

            this.setTagsListViewObject();

            this.tagsListView1.UpdateItems(database, tagsCombination, searchTagText);

            // Create the groups list. (Skip the default group - index 0)
            tagsListView.Groups.Clear();
            for (int i = 1; i < database.groupsNames.Count; i++)
            {
                string groupName = database.groupsNames[i];
                string groupKey = database.groupsKeys[i];
                tagsListView.Groups.Add(groupKey, groupName);
            }
            // Add the default group (with index 0) as the last group
            tagsListView.Groups.Add(database.groupsKeys[0], database.groupsNames[0]);

            // Create item for the listView:
            List<ListViewItem> tagsList = new List<ListViewItem>();
            ListViewItem tagItem;
            // Add each tag from the database
            for (int i = 0; i < database.Tags.Count; i++)
            {
                // Extract the tag
                FileTag tag = database.Tags[i];

                // Show the tag only if it's not in the history of tags being selected - 
                // (You should not show tag 'X' after selecting tag 'X')
                bool showTagCondition;
                showTagCondition =                   
                    !tagsCombination.ContainsByValue(tag, false) && 
                    tag.Value.ToLower().Contains(searchTagText.ToLower());
                if (showTagCondition)
                {
                    // Set the tag item (including text of the tag with the tag's file-count
                    tagItem = new ListViewItem();
                    tagItem.Text = (tag.Value == "" ? noTagsString : tag.ToString()) +
                        "\n  [" + database.CountFilesByTag(tag).ToString() + "]";
                    tagItem.Tag = tag;

                    // Set image of the tag: special image if the tag's file-count is equals the
                    // database file-count. a regular folder image if not.
                    if (database.Files.Count == database.CountFilesByTag(tag))
                        tagItem.ImageIndex = 1;
                    else
                        tagItem.ImageIndex = 0;

                    // Map the tag to it's group
                    int groupKeyIndex = database.tagGroupMapping[i];
                    string groupKey = database.groupsKeys[groupKeyIndex];
                    tagItem.Group = tagsListView.Groups[groupKey];
                    tagsList.Add(tagItem);
                }
            }

            // ** Sort the tags by groups, and inside of each group
            ListViewItem tmp = new ListViewItem();
            for (int i = 0; i < tagsList.Count; i++)
            {
                for (int j = i + 1; j < tagsList.Count; j++)
                {
                    int comperator = tagsList[i].Group.Name.CompareTo(tagsList[j].Group.Name);
                    if (comperator == 1) // item[i].Group > item[j].Group
                    { // Swap items
                        tmp = (ListViewItem)tagsList[i].Clone();
                        tagsList[i] = (ListViewItem)tagsList[j].Clone();
                        tagsList[j] = tmp;
                    }
                    else if (comperator == 0) // Equal groups
                    { // Sort internally

                        comperator = ((FileTag)tagsList[i].Tag).ToString().CompareTo(
                            ((FileTag)tagsList[j].Tag).ToString());

                        if (comperator == 1)
                        {
                            tmp = (ListViewItem)tagsList[i].Clone();
                            tagsList[i] = (ListViewItem)tagsList[j].Clone();
                            tagsList[j] = tmp;
                        }
                    }
                }
            }


            // ** Add to list view
            tagsListView.BeginUpdate();
            tagsListView.Clear();
            foreach (ListViewItem item in tagsList)
                tagsListView.Items.Add(item);
            
            tagsListView.EndUpdate();
        }