aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Components/SelectedObjectCollection.cs
blob: 8aa087ed32d601e5bc0608fa5b01787ccabfe5cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subhe
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.SharedUI.Components
{
    public class SelectedObjectCollection<T> : ObservableCollection<SelectedObject<T>>
    {
        private Func<T, T, bool> _compareFunc;

        public event EventHandler SelectionChanged;
        public ObservableCollection<T> Source { get; set; }
        public ObservableCollection<T> SynchedSource { get; set; }

        public SelectedObjectCollection(ObservableCollection<T> source, ObservableCollection<T> synchedSource, Func<T, T, bool> compareFunc)
        {
            _compareFunc = compareFunc;

            SynchedSource = synchedSource;
            Source = source;

            foreach (var item in source)
            {
                var selectedItem = new SelectedObject<T>(item, _compareFunc == null ? synchedSource.Contains(item) : synchedSource.ToList().Exists(x => _compareFunc(x, item)));
                this.Add(selectedItem);
                selectedItem.IsSelectedChanged += SelectedItem_IsSelectedChanged;
            }
        }

        public SelectedObjectCollection(ObservableCollection<T> source, ObservableCollection<T> synchedSource) : this(source, synchedSource, null)
        {

        }

        private void SelectedItem_IsSelectedChanged(object sender, EventArgs e)
        {
            SelectedObject<T> item = sender as SelectedObject<T>;

            if (item.IsSelected)
            {
                if (_compareFunc == null)
                {
                    if (!SynchedSource.Contains(item.Data))
                    {
                        SynchedSource.Add(item.Data);
                    }
                }
                else
                {
                    if (!SynchedSource.ToList().Exists(x => _compareFunc(x, item.Data)))
                    {
                        SynchedSource.Add(item.Data);
                    }
                }
            }
            else
            {
                if (_compareFunc == null)
                {
                    SynchedSource.Remove(item.Data);
                }
                else
                {
                    foreach (var s in SynchedSource.ToList())
                    {
                        if (_compareFunc(s, item.Data))
                        {
                            SynchedSource.Remove(s);
                        }
                    }
                }
            }

            SelectionChanged?.Invoke(this, new EventArgs());
        }
    }
}