File: EntitySetDataBindingList.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (108 lines) | stat: -rw-r--r-- 3,760 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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Linq {
    internal class EntitySetBindingList<TEntity> : SortableBindingList<TEntity>
        where TEntity : class {
        private EntitySet<TEntity> data;
        private TEntity addNewInstance;
        private TEntity cancelNewInstance;
        private bool addingNewInstance;

        internal EntitySetBindingList(IList<TEntity> sequence, EntitySet<TEntity> data)
            : base(sequence) {
            if (sequence == null) {
                throw Error.ArgumentNull("sequence");
            }
            if (data == null) {
                throw Error.ArgumentNull("data");
            }

            this.data = data;
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
        private void ThrowEntitySetErrorsIfTypeInappropriate() {
            Type type = typeof(TEntity);

            if (type.IsAbstract) {
                throw Error.EntitySetDataBindingWithAbstractBaseClass(type.Name);
            }
            if (type.GetConstructor(System.Type.EmptyTypes) == null) {
                throw Error.EntitySetDataBindingWithNonPublicDefaultConstructor(type.Name);
            }
        }

        protected override object AddNewCore() {
            ThrowEntitySetErrorsIfTypeInappropriate();
            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.Insert(index, item);
            }
        }

        protected override void RemoveItem(int index) {
            if (index >= 0 && index < Count && this[index] == cancelNewInstance) {
                cancelNewInstance = null;
            }
            else {
                this.data.Remove(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.Remove(removedItem);
                }
                this.data.Insert(index,item);
            }

        }

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

        public override void EndNew(int itemIndex) {
            if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) {
                this.data.Add(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);
        }
    }
}