~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/Data/ICSharpCode.Data.EDMDesigner.Core.UI/UserControls/DesignerCanvas.Commands.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;
 
8
using System.Linq;
 
9
using System.Windows;
 
10
using System.Windows.Controls;
 
11
using System.Windows.Data;
 
12
using System.Windows.Input;
 
13
using ICSharpCode.Data.EDMDesigner.Core.UI.Windows;
 
14
using ICSharpCode.Data.EDMDesigner.Core.UI.UserControls.Common;
 
15
using System.Windows.Media;
 
16
using ICSharpCode.Data.EDMDesigner.Core.UI.DisplayBinding;
 
17
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL;
 
18
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer;
 
19
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL.Type;
 
20
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.CSDL.Association;
 
21
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.CSDL.Property;
 
22
 
 
23
#endregion
 
24
 
 
25
namespace ICSharpCode.Data.EDMDesigner.Core.UI.UserControls
 
26
{
 
27
    partial class DesignerCanvas
 
28
    {
 
29
        private const string CONTEXTMENU_RESOURCE_NAME = "ContextMenu";
 
30
        private const string ZOOM_STACKPANEL = "zoomStackPanel";
 
31
        private const string ZOOM_TEXTBOX = "zoomTextBox";
 
32
 
 
33
        private static RoutedCommand _addEntityTypeCommand;
 
34
        public static RoutedCommand AddEntityTypeCommand
 
35
        {
 
36
            get
 
37
            {
 
38
                if (_addEntityTypeCommand == null)
 
39
                    _addEntityTypeCommand = new RoutedCommand();
 
40
                return _addEntityTypeCommand;
 
41
            }
 
42
        }
 
43
        public static string AddEntityTypeText
 
44
        {
 
45
            get
 
46
            {
 
47
                return "Add entity type";
 
48
            }
 
49
        }
 
50
 
 
51
        private static RoutedCommand _addComplexTypeCommand;
 
52
        public static RoutedCommand AddComplexTypeCommand
 
53
        {
 
54
            get
 
55
            {
 
56
                if (_addComplexTypeCommand == null)
 
57
                    _addComplexTypeCommand = new RoutedCommand();
 
58
                return _addComplexTypeCommand;
 
59
            }
 
60
        }
 
61
        public static string AddComplexTypeText
 
62
        {
 
63
            get
 
64
            {
 
65
                return "Add complex type";
 
66
            }
 
67
        }
 
68
 
 
69
        private static RoutedCommand _addAssociationCommand;
 
70
        public static RoutedCommand AddAssociationCommand
 
71
        {
 
72
            get
 
73
            {
 
74
                if (_addAssociationCommand == null)
 
75
                    _addAssociationCommand = new RoutedCommand();
 
76
                return _addAssociationCommand;
 
77
            }
 
78
        }
 
79
        public static string AddAssociationText
 
80
        {
 
81
            get
 
82
            {
 
83
                return "Add association";
 
84
            }
 
85
        }
 
86
 
 
87
        private static RoutedCommand _removeFromDesignerCommand;
 
88
        public static RoutedCommand RemoveFromDesignerCommand
 
89
        {
 
90
            get
 
91
            {
 
92
                if (_removeFromDesignerCommand == null)
 
93
                    _removeFromDesignerCommand = new RoutedCommand();
 
94
                return _removeFromDesignerCommand;
 
95
            }
 
96
        }
 
97
        public static string RemoveFromDesignerText
 
98
        {
 
99
            get
 
100
            {
 
101
                return "Remove from designer";
 
102
            }
 
103
        }
 
104
 
 
105
        private static RoutedCommand _selectAllCommand;
 
106
        public static RoutedCommand SelectAllCommand
 
107
        {
 
108
            get
 
109
            {
 
110
                if (_selectAllCommand == null)
 
111
                    _selectAllCommand = new RoutedCommand();
 
112
                return _selectAllCommand;
 
113
            }
 
114
        }
 
115
        public static string SelectAllText
 
116
        {
 
117
            get
 
118
            {
 
119
                return "Select all";
 
120
            }
 
121
        }
 
122
 
 
123
        public static string ZoomText
 
124
        {
 
125
            get
 
126
            {
 
127
                return "Zoom";
 
128
            }
 
129
        }
 
130
        public int Zoom
 
131
        {
 
132
            get { return (int)GetValue(ZoomProperty); }
 
133
            set { SetValue(ZoomProperty, value); }
 
134
        }
 
135
        public static readonly DependencyProperty ZoomProperty =
 
136
            DependencyProperty.Register("Zoom", typeof(int), typeof(DesignerCanvas),
 
137
                new UIPropertyMetadata(100,
 
138
                    (sender, e) =>
 
139
                    {
 
140
                        var designerCanvas = (DesignerCanvas)sender;
 
141
                        if ((int)e.NewValue < 10)
 
142
                            designerCanvas.Zoom = 10;
 
143
                        else if ((int)e.NewValue > 1000)
 
144
                            designerCanvas.Zoom = 1000;
 
145
                        else
 
146
                        {
 
147
                            int zoom = (int)e.NewValue;
 
148
                            designerCanvas.DesignerView.Zoom = zoom;
 
149
                            var scale = (double)zoom / 100.0;
 
150
                            designerCanvas.LayoutTransform = new ScaleTransform(scale, scale);
 
151
                            designerCanvas.Resize();
 
152
                        }
 
153
                    }));
 
154
 
 
155
        public new InputBindingCollection InputBindings
 
156
        {
 
157
            get { return base.InputBindings; }
 
158
            set { }
 
159
        }
 
160
        public static readonly DependencyProperty InputBindingsProperty = DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(DesignerCanvas), new PropertyMetadata((sender, e) => ((DesignerCanvas)sender).InputBindings.AddRange((InputBindingCollection)e.NewValue)));
 
161
 
 
162
        private EDMDesignerViewContent _container;
 
163
        internal EDMDesignerViewContent Container
 
164
        {
 
165
            get
 
166
            {
 
167
                return _container;
 
168
            }
 
169
        }
 
170
 
 
171
        private new ContextMenu.ContextMenu ContextMenu
 
172
        {
 
173
            get
 
174
            {
 
175
                return Style == null ? (ContextMenu.ContextMenu)null : (ContextMenu.ContextMenu)Style.Resources.OfType<DictionaryEntry>().FirstOrDefault(de => ((string)de.Key) == "ContextMenu").Value;
 
176
            }
 
177
        }
 
178
 
 
179
        private void InitContextMenuCommandBindings()
 
180
        {
 
181
            Loaded +=
 
182
                delegate
 
183
                {
 
184
                    ContextMenu.ContextMenu contextMenu;
 
185
                    StackPanel stackPanel;
 
186
                    TextBox textBox;
 
187
                    if ((contextMenu = ContextMenu) != null && (stackPanel = contextMenu.Items.OfType<StackPanel>().FirstOrDefault(sp => sp.Name == ZOOM_STACKPANEL)) != null && (textBox = stackPanel.Children.OfType<TextBox>().FirstOrDefault(etb => etb.Name == ZOOM_TEXTBOX)) != null)
 
188
                        textBox.SetBinding(TextBox.TextProperty, new Binding { Source = this, Path = new PropertyPath("Zoom") });
 
189
                };
 
190
 
 
191
            CommandBindings.AddRange(
 
192
                new[]
 
193
                {
 
194
                    new CommandBinding(AddEntityTypeCommand, 
 
195
                        delegate
 
196
                        {
 
197
                            var csdlView = EDMView.CSDL;
 
198
                            var addEntityTypeWindow = new AddEntityTypeWindow(csdlView) { Owner = Container.Window };
 
199
                            switch (addEntityTypeWindow.ShowDialog())
 
200
                            {
 
201
                                case true:
 
202
                                    UITypeToAdd = csdlView.AddEntityType(addEntityTypeWindow.EntityTypeName, addEntityTypeWindow.BaseEntityType);
 
203
                                break;
 
204
                            }
 
205
                        }
 
206
                    ),
 
207
                    new CommandBinding(AddComplexTypeCommand, 
 
208
                        delegate
 
209
                        {
 
210
                            var addComplexTypeWindow = new AddComplexTypeWindow { Owner = Container.Window };
 
211
                            switch (addComplexTypeWindow.ShowDialog())
 
212
                            {
 
213
                                case true:
 
214
                                    UITypeToAdd = EDMView.CSDL.AddComplexType(addComplexTypeWindow.ComplexTypeName);
 
215
                                break;
 
216
                            }
 
217
                        }
 
218
                    ),
 
219
                    new CommandBinding(AddAssociationCommand, 
 
220
                        AddAssociationExecuted(this, () => EDMView.CSDL, () => DesignerView, null, null)
 
221
                    ),
 
222
                    new CommandBinding(RemoveFromDesignerCommand, 
 
223
                        delegate { RemoveFromDesigner(); },
 
224
                        (sender, e) => e.CanExecute = TypesSelected.Any()
 
225
                    ),
 
226
                    new CommandBinding(SelectAllCommand, 
 
227
                        delegate
 
228
                        {
 
229
                            foreach(var types in TypesVisibles)
 
230
                                types.IsSelected = true;
 
231
                        },
 
232
                        (sender, e) => e.CanExecute = TypesVisibles.Any()
 
233
                    )
 
234
                }
 
235
            );
 
236
        }
 
237
 
 
238
        protected override void OnMouseDown(MouseButtonEventArgs e)
 
239
        {
 
240
            base.OnMouseDown(e);
 
241
            Focus();
 
242
            if (e.LeftButton == MouseButtonState.Pressed)
 
243
                OnMouseLeftButtonDown(e);
 
244
            else
 
245
                OnMouseRightButtonDown(e);
 
246
            e.Handled = true;
 
247
        }
 
248
 
 
249
        public static ExecutedRoutedEventHandler AddAssociationExecuted(UIElement parent, Func<CSDLView> getCSDLView, Func<DesignerView> getDesignerView, UIEntityType defaultEntityType1, Action<UIAssociation> specialActionToDoWhenNewItemAdded)
 
250
        {
 
251
            return (object sender, ExecutedRoutedEventArgs e) =>
 
252
             {
 
253
                 var csdlView = getCSDLView();
 
254
                 var designerView = getDesignerView();
 
255
                 var addAssociationWindow = new AddAssociationWindow { Owner = VisualTreeHelperUtil.GetControlAscendant<Window>(parent), DataContext = csdlView, NavigationProperty1EntityType = defaultEntityType1 };
 
256
                 switch (addAssociationWindow.ShowDialog())
 
257
                 {
 
258
                     case true:
 
259
                         var uiAssociation = csdlView.AddAssociation(addAssociationWindow.AssociationName, addAssociationWindow.NavigationProperty1Name, addAssociationWindow.NavigationProperty1EntityType, addAssociationWindow.NavigationProperty1Cardinality.Value, addAssociationWindow.NavigationProperty2Name, addAssociationWindow.NavigationProperty2EntityType, addAssociationWindow.NavigationProperty2Cardinality.Value);
 
260
                         SizeChangedEventHandler actionToDoWhenNewItemAdded = null;
 
261
                         bool drawAssociation = false;
 
262
                         DesignerCanvas designerCanvas = parent as DesignerCanvas;
 
263
                         if (designerCanvas == null)
 
264
                             designerCanvas = VisualTreeHelperUtil.GetControlAscendant<DesignerCanvas>(parent);
 
265
                         var navigationProperty1EntityTypeDesigner = designerCanvas.GetTypeDesigner(uiAssociation.NavigationProperty1.ParentType);
 
266
                         var navigationProperty2EntityTypeDesigner = designerCanvas.GetTypeDesigner(uiAssociation.NavigationProperty2.ParentType);
 
267
                         actionToDoWhenNewItemAdded =
 
268
                             (object o, SizeChangedEventArgs scea) =>
 
269
                             {
 
270
                                 if (designerView.ContainsEntityType(uiAssociation.NavigationProperty1.ParentType) && designerView.ContainsEntityType(uiAssociation.NavigationProperty2.ParentType))
 
271
                                 {
 
272
                                     if (drawAssociation)
 
273
                                         navigationProperty1EntityTypeDesigner.DrawAssociation(navigationProperty2EntityTypeDesigner, uiAssociation.NavigationProperty1.BusinessInstance as NavigationProperty);
 
274
                                     else
 
275
                                         drawAssociation = true;
 
276
                                 }
 
277
                                 if (specialActionToDoWhenNewItemAdded != null)
 
278
                                     specialActionToDoWhenNewItemAdded(uiAssociation);
 
279
                                 ((ListView)o).SizeChanged -= actionToDoWhenNewItemAdded;
 
280
                             };
 
281
                         navigationProperty1EntityTypeDesigner.propertiesListView.SizeChanged += actionToDoWhenNewItemAdded;
 
282
                         if (navigationProperty2EntityTypeDesigner != null)
 
283
                             navigationProperty2EntityTypeDesigner.propertiesListView.SizeChanged += actionToDoWhenNewItemAdded;
 
284
                         break;
 
285
                 }
 
286
             };
 
287
        }
 
288
    }
 
289
}