Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web.Entity.Design / System / Data / WebControls / Design / EntityDataSourceStatementEditorForm.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="EntityDataSourceStatementEditorForm.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //
9 // Enables a user to edit CommandText, OrderBy, Select, and
10 // Where properties and parameters
11 //------------------------------------------------------------------------------
12 using System.Collections.Generic;
13 using System.Web.UI.Design.WebControls.Util;
14 using System.Drawing;
15 using System.Globalization;
16 using System.Reflection;
17 using System.Web.UI.Design.WebControls;
18 using System.Web.UI.WebControls;
19 using System.Windows.Forms;
20
21 namespace System.Web.UI.Design.WebControls
22 {
23     internal class EntityDataSourceStatementEditorForm : DesignerForm
24     {
25         private System.Windows.Forms.Panel _checkBoxPanel;
26         private System.Windows.Forms.CheckBox _autoGenerateCheckBox;
27         private System.Windows.Forms.Panel _statementPanel;
28         private System.Windows.Forms.Label _statementLabel;
29         private System.Windows.Forms.TextBox _statementTextBox;
30         private ParameterEditorUserControl _parameterEditorUserControl;
31         private System.Windows.Forms.Button _okButton;
32         private System.Windows.Forms.Button _cancelButton;
33
34         private System.Web.UI.Control _entityDataSource;
35         private ParameterCollection _parameters;
36
37         private string _cachedStatementText;
38         private readonly string _helpTopic;
39
40         public EntityDataSourceStatementEditorForm(System.Web.UI.Control entityDataSource, IServiceProvider serviceProvider,
41             bool hasAutoGen, bool isAutoGen, string propertyName, string statementLabelText, string statementAccessibleName,
42             string helpTopic, string statement, ParameterCollection parameters)
43             : base(serviceProvider)
44         {
45
46             _entityDataSource = entityDataSource;
47             InitializeComponent();
48             InitializeUI(propertyName, statementLabelText, statementAccessibleName);
49             InitializeTabIndexes();
50             InitializeAnchors();
51
52             _helpTopic = helpTopic;
53
54             if (!hasAutoGen)
55             {
56                 HideCheckBox();
57             }
58
59             _parameters = parameters;
60
61             _autoGenerateCheckBox.Checked = isAutoGen;
62             _statementPanel.Enabled = !isAutoGen;
63
64             _statementTextBox.Text = statement;
65             _statementTextBox.Select(0, 0);
66
67             List<Parameter> paramList = new List<Parameter>();
68             foreach (Parameter p in parameters)
69             {
70                 paramList.Add(p);
71             }
72             _parameterEditorUserControl.AddParameters(paramList.ToArray());
73
74             _cachedStatementText = null;
75         }
76
77         public bool AutoGen
78         {
79             get
80             {
81                 return _autoGenerateCheckBox.Checked;
82             }
83         }
84
85         protected override string HelpTopic
86         {
87             get
88             {
89                 return _helpTopic;
90             }
91         }
92
93         public ParameterCollection Parameters
94         {
95             get
96             {
97                 return _parameters;
98             }
99         }
100
101         public string Statement
102         {
103             get
104             {
105                 return _statementTextBox.Text;
106             }
107         }
108
109         private void HideCheckBox()
110         {
111             _autoGenerateCheckBox.Checked = false;
112             _checkBoxPanel.Visible = false;
113
114             int moveUp = _statementPanel.Location.Y - _checkBoxPanel.Location.Y;
115
116             Point loc = _statementPanel.Location;
117             loc.Y -= moveUp;
118             _statementPanel.Location = loc;
119
120             loc = _parameterEditorUserControl.Location;
121             loc.Y -= moveUp;
122             _parameterEditorUserControl.Location = loc;
123
124             Size size = _parameterEditorUserControl.Size;
125             size.Height += moveUp;
126             _parameterEditorUserControl.Size = size;
127
128             size = this.MinimumSize;
129             size.Height -= moveUp;
130             this.MinimumSize = size;
131             this.Size = size;
132         }
133
134         private void InitializeAnchors()
135         {
136             _checkBoxPanel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
137             _autoGenerateCheckBox.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
138
139             _statementPanel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
140             _statementLabel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
141             _statementTextBox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
142             
143             _parameterEditorUserControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
144
145             _okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
146             _cancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
147         }
148
149         #region Windows Form Designer generated code
150         /// <summary>
151         /// Required method for Designer support - do not modify
152         /// the contents of this method with the code editor.
153         /// </summary>
154         private void InitializeComponent()
155         {
156             this._okButton = new System.Windows.Forms.Button();
157             this._cancelButton = new System.Windows.Forms.Button();
158             this._statementLabel = new System.Windows.Forms.Label();
159             this._statementTextBox = new System.Windows.Forms.TextBox();
160             this._autoGenerateCheckBox = new System.Windows.Forms.CheckBox();
161             this._parameterEditorUserControl = (ParameterEditorUserControl)Activator.CreateInstance(typeof(ParameterEditorUserControl), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { ServiceProvider, _entityDataSource }, null);
162             this._checkBoxPanel = new System.Windows.Forms.Panel();
163             this._statementPanel = new System.Windows.Forms.Panel();
164             this._checkBoxPanel.SuspendLayout();
165             this._statementPanel.SuspendLayout();
166             this.SuspendLayout();
167             this.InitializeSizes();
168             // 
169             // _okButton
170             // 
171             this._okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
172             this._okButton.Name = "_okButton";            
173             this._okButton.Click += new System.EventHandler(this.OnOkButtonClick);
174             // 
175             // _cancelButton
176             // 
177             this._cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
178             this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
179             this._cancelButton.Name = "_cancelButton";            
180             this._cancelButton.Click += new System.EventHandler(this.OnCancelButtonClick);
181             // 
182             // _commandLabel
183             // 
184             this._statementLabel.Name = "_commandLabel";            
185             // 
186             // _statementTextBox
187             // 
188             this._statementTextBox.AcceptsReturn = true;
189             this._statementTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
190                         | System.Windows.Forms.AnchorStyles.Right)));
191             this._statementTextBox.Multiline = true;
192             this._statementTextBox.Name = "_statementTextBox";
193             this._statementTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;            
194             // 
195             // _autoGenerateCheckBox
196             // 
197             this._autoGenerateCheckBox.CheckAlign = ContentAlignment.TopLeft;
198             this._autoGenerateCheckBox.TextAlign = ContentAlignment.TopLeft;
199             this._autoGenerateCheckBox.Name = "_autoGenerateCheckBox";
200             this._autoGenerateCheckBox.UseVisualStyleBackColor = true;
201             this._autoGenerateCheckBox.CheckedChanged += new EventHandler(OnAutoGenerateCheckBoxCheckedChanged);
202             // 
203             // _checkBoxPanel
204             // 
205             this._checkBoxPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
206                         | System.Windows.Forms.AnchorStyles.Right)));
207             this._checkBoxPanel.Controls.Add(this._autoGenerateCheckBox);
208             this._checkBoxPanel.Name = "_radioPanel";            
209             // 
210             // _statementPanel
211             // 
212             this._statementPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
213                         | System.Windows.Forms.AnchorStyles.Right)));
214             this._statementPanel.Controls.Add(this._statementLabel);
215             this._statementPanel.Controls.Add(this._statementTextBox);
216             this._statementPanel.Name = "_statementPanel";            
217             // 
218             // _parameterEditorUserControl
219             // 
220             this._parameterEditorUserControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
221                         | System.Windows.Forms.AnchorStyles.Left)
222                         | System.Windows.Forms.AnchorStyles.Right)));
223             this._parameterEditorUserControl.Name = "_parameterEditorUserControl";            
224             // 
225             // EntityDataSourceStatementEditorForm
226             // 
227             this.AcceptButton = this._okButton;
228             this.CancelButton = this._cancelButton;
229             this.Controls.Add(this._statementPanel);
230             this.Controls.Add(this._checkBoxPanel);
231             this.Controls.Add(this._cancelButton);
232             this.Controls.Add(this._okButton);
233             this.Controls.Add(this._parameterEditorUserControl);
234             this.Name = "EntityDataSourceStatementEditorForm";
235             this._checkBoxPanel.ResumeLayout(false);
236             this._checkBoxPanel.PerformLayout();
237             this._statementPanel.ResumeLayout(false);
238             this._statementPanel.PerformLayout();
239             this.ResumeLayout(false);
240
241             InitializeForm();
242         }
243
244         #endregion
245
246         private void InitializeSizes()
247         {
248             int top = 0;
249
250             _checkBoxPanel.Location = new Point(12, 12);
251             _checkBoxPanel.Size = new Size(456, 32);
252             _autoGenerateCheckBox.Location = new Point(0, 0);
253             _autoGenerateCheckBox.Size = new Size(456, 30);
254             top = _checkBoxPanel.Bottom;
255
256             _statementPanel.Location = new Point(12, top + 4);
257             _statementPanel.Size = new Size(456, 124);
258
259             top = 0;
260             _statementLabel.Location = new Point(0, 0);
261             _statementLabel.Size = new Size(200, 16);
262             top = _statementLabel.Bottom;
263
264             _statementTextBox.Location = new Point(0, top + 3);
265             _statementTextBox.Size = new Size(456, 78);
266             top = _statementPanel.Bottom;
267
268             _parameterEditorUserControl.Location = new Point(12, top + 5);
269             _parameterEditorUserControl.Size = new Size(460, 216);
270             top = _parameterEditorUserControl.Bottom;
271
272             _okButton.Location = new Point(313, top + 6);
273             _okButton.Size = new Size(75, 23);
274             _cancelButton.Location = new Point(393, top + 6);
275             _cancelButton.Size = new Size(75, 23);
276             top = _cancelButton.Bottom;
277
278             ClientSize = new Size(480, top + 12);
279             MinimumSize = new Size(480 + 8, top + 12 + 27);
280         }
281
282         private void InitializeTabIndexes()
283         {
284             _checkBoxPanel.TabStop = false;
285             _autoGenerateCheckBox.TabStop = true;
286
287             _statementPanel.TabStop = false;
288             _statementLabel.TabStop = false;
289             _statementTextBox.TabStop = true;
290
291             _parameterEditorUserControl.TabStop = true;
292
293             _okButton.TabStop = true;
294             _cancelButton.TabStop = true;
295
296             int tabIndex = 0;
297
298             _checkBoxPanel.TabIndex = tabIndex += 10;
299             _autoGenerateCheckBox.TabIndex = tabIndex += 10;
300
301             _statementPanel.TabIndex = tabIndex += 10;
302             _statementLabel.TabIndex = tabIndex += 10;
303             _statementTextBox.TabIndex = tabIndex += 10;
304
305             _parameterEditorUserControl.TabIndex = tabIndex += 10;
306
307             _okButton.TabIndex = tabIndex += 10;
308             _cancelButton.TabIndex = tabIndex += 10;
309         }
310
311         private void InitializeUI(string propertyName, string labelText, string accessibleName)
312         {
313             this.Text = Strings.ExpressionEditor_Caption;
314             this.AccessibleName = Strings.ExpressionEditor_Caption;
315             _okButton.Text = Strings.OKButton;
316             _okButton.AccessibleName = Strings.OKButtonAccessibleName;
317             _cancelButton.Text = Strings.CancelButton;
318             _cancelButton.AccessibleName = Strings.CancelButtonAccessibleName;
319             _statementLabel.Text = labelText;
320             _statementTextBox.AccessibleName = accessibleName;
321             if (String.Equals(propertyName, "Where", StringComparison.OrdinalIgnoreCase))
322             {
323                 _autoGenerateCheckBox.Text = Strings.ExpressionEditor_AutoGenerateWhereCheckBox;
324                 _autoGenerateCheckBox.AccessibleName = Strings.ExpressionEditor_AutoGenerateWhereCheckBoxAccessibleName;
325             }
326             else if (String.Equals(propertyName, "OrderBy", StringComparison.OrdinalIgnoreCase))
327             {
328                 _autoGenerateCheckBox.Text = Strings.ExpressionEditor_AutoGenerateOrderByCheckBox;
329                 _autoGenerateCheckBox.AccessibleName = Strings.ExpressionEditor_AutoGenerateOrderByCheckBoxAccessibleName;
330             }
331         }
332
333         private void OnAutoGenerateCheckBoxCheckedChanged(object sender, EventArgs e)
334         {
335             if (_autoGenerateCheckBox.Checked)
336             {
337                 _cachedStatementText = _statementTextBox.Text;
338                 _statementTextBox.Text = null;
339             }
340             else if (!String.IsNullOrEmpty(_cachedStatementText))
341             {
342                 _statementTextBox.Text = _cachedStatementText;
343             }
344             _statementPanel.Enabled = !_autoGenerateCheckBox.Checked;
345         }
346
347         private void OnCancelButtonClick(System.Object sender, System.EventArgs e)
348         {
349             DialogResult = DialogResult.Cancel;
350             Close();
351         }
352
353         private void OnOkButtonClick(System.Object sender, System.EventArgs e)
354         {
355             _parameters.Clear();
356             Parameter[] paramList = _parameterEditorUserControl.GetParameters();
357             foreach (Parameter p in paramList)
358             {
359                 _parameters.Add(p);
360             }
361
362             DialogResult = DialogResult.OK;
363             Close();
364         }
365     }
366 }
367