~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/Data/ICSharpCode.Data.EDMDesigner.Core/EDMObjects/Designer/CSDL/CSDLView.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
#region Usings
 
5
 
 
6
using System;
 
7
using System.Collections.ObjectModel;
 
8
using System.Linq;
 
9
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.CSDL;
 
10
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.CSDL.Function;
 
11
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.CSDL.Type;
 
12
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.Common;
 
13
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL.Association;
 
14
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL.Property;
 
15
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL.Type;
 
16
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Common;
 
17
 
 
18
#endregion
 
19
 
 
20
namespace ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL
 
21
{
 
22
    public class CSDLView
 
23
    {
 
24
        private IndexableUIBusinessTypeObservableCollection<EntityType, UIEntityType> _entityTypes;
 
25
        private UIEntityType _hierarchyType;
 
26
        private ObservableCollection<UIEntityType> _hierarchyTypes;
 
27
        private IndexableUIBusinessTypeObservableCollection<ComplexType, UIComplexType> _complexTypes;
 
28
 
 
29
        public EDMView EDMView { get; internal set; }
 
30
        public CSDLContainer CSDL { get; internal set; }
 
31
 
 
32
        #region Entity Types
 
33
        public IndexableUIBusinessTypeObservableCollection<EntityType, UIEntityType> EntityTypes
 
34
        {
 
35
            get
 
36
            {
 
37
                if (_entityTypes == null)
 
38
                {
 
39
                    _entityTypes = new IndexableUIBusinessTypeObservableCollection<EntityType, UIEntityType>(CSDL.EntityTypes.Select(et => new UIEntityType(this, et)));
 
40
                    CSDL.EntityTypes.ItemAdded += newEntityType => _entityTypes.GetAndAddIfNotExist(newEntityType, net => new UIEntityType(this, net));
 
41
                    CSDL.EntityTypes.ItemRemoved += oldEntityType => _entityTypes.Remove(_entityTypes[oldEntityType]);
 
42
                }
 
43
                return _entityTypes;
 
44
            }
 
45
        }
 
46
 
 
47
        internal void EntityTypesPropertyChanged()
 
48
        {
 
49
            EntityTypes.Refresh();
 
50
        }
 
51
 
 
52
        public UIEntityType HierarchyType
 
53
        {
 
54
            get { return _hierarchyType; }
 
55
            set
 
56
            {
 
57
                _hierarchyType = value;
 
58
                HierarchyTypes.Clear();
 
59
                if (value == null)
 
60
                    return;
 
61
                value.Bold = true;
 
62
                EntityType baseType;
 
63
                while ((baseType = value.BusinessInstance.BaseType) != null)
 
64
                    value = new UIEntityType(this, baseType, value);
 
65
                HierarchyTypes.Add(value);
 
66
            }
 
67
        }
 
68
 
 
69
        public ObservableCollection<UIEntityType> HierarchyTypes
 
70
        {
 
71
            get
 
72
            {
 
73
                if (_hierarchyTypes == null)
 
74
                    _hierarchyTypes = new ObservableCollection<UIEntityType>();
 
75
                return _hierarchyTypes;
 
76
            }
 
77
        }
 
78
        #endregion Entity Types
 
79
 
 
80
        #region Complex Types
 
81
        public IndexableUIBusinessTypeObservableCollection<ComplexType, UIComplexType> ComplexTypes
 
82
        {
 
83
            get
 
84
            {
 
85
                if (_complexTypes == null)
 
86
                {
 
87
                    _complexTypes = new IndexableUIBusinessTypeObservableCollection<ComplexType, UIComplexType>(CSDL.ComplexTypes.Select(ct => new UIComplexType(this, ct)));
 
88
                    CSDL.ComplexTypes.ItemAdded += newComplexType => _complexTypes.GetAndAddIfNotExist(newComplexType, nct => new UIComplexType(this, nct));
 
89
                    CSDL.ComplexTypes.ItemRemoved += oldComplexType => _complexTypes.Remove(_complexTypes[oldComplexType]);
 
90
                }
 
91
                return _complexTypes;
 
92
            }
 
93
        }
 
94
        #endregion Complex Types
 
95
 
 
96
        #region CSDL Functions
 
97
        public ObservableCollection<Function> Functions
 
98
        {
 
99
            get
 
100
            {
 
101
                return CSDL.Functions;
 
102
            }
 
103
        }
 
104
        #endregion CSDL Functions
 
105
 
 
106
        public UIAssociation AddAssociation(string associationName, string navigationProperty1Name, UIEntityType navigationProperty1EntityType, Cardinality navigationProperty1Cardinality, string navigationProperty2Name, UIEntityType navigationProperty2EntityType, Cardinality navigationProperty2Cardinality)
 
107
        {
 
108
            var association = CSDL.AddAssociation(associationName, navigationProperty1Name, navigationProperty1EntityType.BusinessInstance, navigationProperty1Cardinality, navigationProperty2Name, navigationProperty2EntityType.BusinessInstance, navigationProperty2Cardinality);
 
109
            return new UIAssociation { NavigationProperty1 = navigationProperty1EntityType.Properties[association.PropertyEnd1] as UIRelatedProperty, NavigationProperty2 = navigationProperty2EntityType.Properties[association.PropertyEnd2] as UIRelatedProperty };
 
110
        }
 
111
 
 
112
        public UIEntityType AddEntityType(string entityTypeName, UIEntityType baseType)
 
113
        {
 
114
            return EntityTypes[CSDL.AddEntityType(entityTypeName, baseType != null ? null : string.Concat(entityTypeName, "Set"), baseType == null ? null : baseType.BusinessInstance)];
 
115
        }
 
116
 
 
117
        public UIComplexType AddComplexType(string complexTypeName)
 
118
        {
 
119
            return ComplexTypes[CSDL.AddComplexType(complexTypeName)];
 
120
        }
 
121
 
 
122
        public void DeleteType(IUIType uiType)
 
123
        {
 
124
            var entityType = uiType.BusinessInstance as EntityType;
 
125
            if (entityType != null)
 
126
                CSDL.EntityTypes.Remove(entityType);
 
127
            var complexType = uiType.BusinessInstance as ComplexType;
 
128
            if (complexType != null)
 
129
                CSDL.ComplexTypes.Remove(complexType);
 
130
            OnTypeDeleted(uiType);
 
131
        }
 
132
 
 
133
        protected virtual void OnTypeDeleted(IUIType uiType)
 
134
        {
 
135
            if (TypeDeleted != null)
 
136
                TypeDeleted(uiType);
 
137
        }
 
138
 
 
139
        public event Action<IUIType> TypeDeleted;
 
140
    }
 
141
}