File: DataBindingList.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (114 lines) | stat: -rw-r--r-- 4,240 bytes parent folder | download | duplicates (7)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace System.Data.Linq.Provider {
    internal static class BindingList {
        [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
        internal static IBindingList Create<T>(DataContext context, IEnumerable<T> sequence) {
            List<T> list = sequence.ToList();
            MetaTable metaTable = context.Services.Model.GetTable(typeof(T));
            if (metaTable != null) {
                ITable table = context.GetTable(metaTable.RowType.Type);
                Type bindingType = typeof(DataBindingList<>).MakeGenericType(metaTable.RowType.Type);
                return (IBindingList)Activator.CreateInstance(bindingType,
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
                    new object[] { list, table }, null
                    );
            } else {
                return new SortableBindingList<T>(list);
            }
        }
    }

    internal class DataBindingList<TEntity> : SortableBindingList<TEntity> 
        where TEntity : class {
        private Table<TEntity> data;
        private TEntity addNewInstance;
        private TEntity cancelNewInstance;
        private bool addingNewInstance;

        internal DataBindingList(IList<TEntity> sequence, Table<TEntity> data)
            : base(sequence != null ? sequence : new List<TEntity>()) {
            if (sequence == null) {
                throw Error.ArgumentNull("sequence");
            }
            if (data == null) {
                throw Error.ArgumentNull("data");
            }

            this.data = data;
        }

        protected override object AddNewCore() {
            addingNewInstance = true;
            addNewInstance = (TEntity)base.AddNewCore();
            return addNewInstance;
        }

        protected override void InsertItem(int index, TEntity item) {
            base.InsertItem(index, item);
            if (!addingNewInstance && index >= 0 && index <= Count) {
                this.data.InsertOnSubmit(item);
            }
        }

        protected override void RemoveItem(int index) {
            if (index >= 0 && index < Count && this[index] == cancelNewInstance) {
                cancelNewInstance = null;
            }
            else {
                this.data.DeleteOnSubmit(this[index]);
            }

            base.RemoveItem(index);
        }

        protected override void SetItem(int index, TEntity item) {
            TEntity removedItem = this[index];
            base.SetItem(index, item);
            if (index >= 0 && index < Count) {
                //Check to see if the user is trying to set an item that is currently being added via AddNew
                //If so then the list should not continue the AddNew; but instead add the item
                //that is being passed in.
                if (removedItem == addNewInstance) {
                    addNewInstance = null;
                    addingNewInstance = false;
                }
                else {
                    this.data.DeleteOnSubmit(removedItem);
                }
                this.data.InsertOnSubmit(item);
            }
        }

        protected override void ClearItems() {
            this.data.DeleteAllOnSubmit(this.data.ToList());
            base.ClearItems();
        }

        public override void EndNew(int itemIndex) {
            if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) {
                this.data.InsertOnSubmit(addNewInstance);
                addNewInstance = null;
                addingNewInstance = false;
            }

            base.EndNew(itemIndex);
        }

        public override void CancelNew(int itemIndex) {
            if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) {
                cancelNewInstance = addNewInstance;
                addNewInstance = null;
                addingNewInstance = false;
            }
            
            base.CancelNew(itemIndex);
        }
    }
}