Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Web.Entity.Design / System / Data / WebControls / Design / EntityDataSourceConfigureObjectContextPanel.cs
blobba12b645410df3aa1f640f2dc8e2ac8e0b376b08
1 //------------------------------------------------------------------------------
2 // <copyright file="EntityDataSourceConfigureObjectContextPanel.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //------------------------------------------------------------------------------
10 using System.Collections.Generic;
11 using System.Web.UI.Design.WebControls.Util;
12 using System.Diagnostics;
13 using System.Drawing;
14 using System.Windows.Forms;
16 namespace System.Web.UI.Design.WebControls
18 internal partial class EntityDataSourceConfigureObjectContextPanel : WizardPanel
20 private EntityDataSourceConfigureObjectContext _configureObjectContext;
21 private bool _ignoreEvents; // used when a control value is set by the wizard, tells the event handlers to do nothing
22 private bool _connectionInEdit; // indicates that a change has been made to the connection and it has not yet been validated
24 #region Constructors
25 internal EntityDataSourceConfigureObjectContextPanel()
27 InitializeComponent();
28 InitializeUI();
29 InitializeTabIndexes();
31 #endregion
33 #region General initialization
34 internal void Register(EntityDataSourceConfigureObjectContext configureObjectContext)
36 _configureObjectContext = configureObjectContext;
38 #endregion
40 #region Control Initialization
41 private void InitializeSizes()
43 int top = 25;
45 _databaseConnectionGroupLabel.Location = new Point(10, top);
46 _databaseConnectionGroupLabel.Size = new Size(500, 13);
47 top = _databaseConnectionGroupLabel.Bottom;
49 _databaseConnectionGroupBox.Location = new Point(13, top);
50 _databaseConnectionGroupBox.Size = new Size(503, 124);
51 top = 0; // rest of controls in this group are positioned relative to the group box, so top resets
53 _namedConnectionRadioButton.Location = new Point(9, top + 20);
54 _namedConnectionRadioButton.Size = new Size(116, 17);
55 top = _namedConnectionRadioButton.Bottom;
57 _namedConnectionComboBox.Location = new Point(25, top + 6);
58 _namedConnectionComboBox.Size = new Size(454, 21);
59 top = _namedConnectionComboBox.Bottom;
61 _connectionStringRadioButton.Location = new Point(9, top + 6);
62 _connectionStringRadioButton.Size = new Size(109, 17);
63 top = _connectionStringRadioButton.Bottom;
65 _connectionStringTextBox.Location = new Point(25, top + 6);
66 _connectionStringTextBox.Size = new Size(454, 20);
67 top = _databaseConnectionGroupBox.Bottom;
69 _containerNameLabel.Location = new Point(10, top + 30);
70 _containerNameLabel.Size = new Size(117, 13);
71 top = _containerNameLabel.Bottom;
73 _containerNameComboBox.Location = new Point(13, top + 3);
74 _containerNameComboBox.Size = new Size(502, 21);
75 // if any controls are added, top should be reset to _containerNameComboBox.Bottom before adding them here
78 private void InitializeTabIndexes()
80 _databaseConnectionGroupLabel.TabStop = false;
81 _databaseConnectionGroupBox.TabStop = false;
82 _namedConnectionComboBox.TabStop = true;
83 _connectionStringTextBox.TabStop = true;
84 _containerNameLabel.TabStop = false;
85 _containerNameComboBox.TabStop = true;
87 int tabIndex = 0;
88 _databaseConnectionGroupLabel.TabIndex = tabIndex += 10;
89 _databaseConnectionGroupBox.TabIndex = tabIndex += 10;
90 _namedConnectionRadioButton.TabIndex = tabIndex += 10;
91 _namedConnectionComboBox.TabIndex = tabIndex += 10;
92 _connectionStringRadioButton.TabIndex = tabIndex += 10;
93 _connectionStringTextBox.TabIndex = tabIndex += 10;
94 _containerNameLabel.TabIndex = tabIndex += 10;
95 _containerNameComboBox.TabIndex = tabIndex += 10;
98 private void InitializeUI()
100 this._databaseConnectionGroupLabel.Text = Strings.Wizard_ObjectContextPanel_ConnectionStringGroupDescription;
101 this._connectionStringRadioButton.Text = Strings.Wizard_ObjectContextPanel_ConnectionStringRadioButton;
102 this._connectionStringRadioButton.AccessibleName = Strings.Wizard_ObjectContextPanel_ConnectionStringRadioButtonAccessibleName;
103 this._connectionStringTextBox.AccessibleName = Strings.Wizard_ObjectContextPanel_ConnectionStringRadioButtonAccessibleName;
104 this._namedConnectionRadioButton.Text = Strings.Wizard_ObjectContextPanel_NamedConnectionRadioButton;
105 this._namedConnectionRadioButton.AccessibleName = Strings.Wizard_ObjectContextPanel_NamedConnectionRadioButtonAccessibleName;
106 this._namedConnectionComboBox.AccessibleName = Strings.Wizard_ObjectContextPanel_NamedConnectionRadioButtonAccessibleName;
107 this._containerNameLabel.Text = Strings.Wizard_ObjectContextPanel_DefaultContainerName;
108 this._containerNameComboBox.AccessibleName = Strings.Wizard_ObjectContextPanel_DefaultContainerNameAccessibleName;
109 this.Caption = Strings.Wizard_ObjectContextPanel_Caption;
110 this.AccessibleName = Strings.Wizard_ObjectContextPanel_Caption;
112 #endregion
114 #region Control Event Handlers
115 private void OnConnectionStringRadioButton_CheckedChanged(object sender, EventArgs e)
117 if (!_ignoreEvents)
119 if (_connectionStringRadioButton.Checked)
121 // Update the state of the controls that are associated with the radio buttons
122 _namedConnectionComboBox.Enabled = false;
123 _connectionStringTextBox.Enabled = true;
125 EnterConnectionEditMode();
127 // Update the flag to track if we have text in the box
128 _configureObjectContext.SelectConnectionStringHasValue(!String.IsNullOrEmpty(_connectionStringTextBox.Text));
130 // Move the focus to the associated TextBox
131 _connectionStringTextBox.Select();
132 _connectionStringTextBox.Select(0, _connectionStringTextBox.TextLength);
135 // else it's being unchecked, so that means another radio button is being checked and that handler will take care of updating the state
138 private void OnConnectionStringTextBox_TextChanged(object sender, EventArgs e)
140 if (!_ignoreEvents)
142 // If we aren't already in edit mode, move to it that we will know we need to reload metadata if it's needed later
143 if (!_connectionInEdit)
145 EnterConnectionEditMode();
148 // Update the state of the flag that tracks if there is anything in this TextBox.
149 // This will cause the Next button to be disabled if all of the text is removed from the box, otherwise it is enabled
150 _configureObjectContext.SelectConnectionStringHasValue(!String.IsNullOrEmpty(_connectionStringTextBox.Text));
154 private void OnNamedConnectionRadioButton_CheckedChanged(object sender, EventArgs e)
156 if (!_ignoreEvents)
158 if (_namedConnectionRadioButton.Checked)
160 // update the controls that are associated with the radio buttons
161 _namedConnectionComboBox.Enabled = true;
162 _connectionStringTextBox.Enabled = false;
164 EnterConnectionEditMode();
166 // Update flag to indicate if there is a value selected in this box
167 _configureObjectContext.SelectConnectionStringHasValue(_namedConnectionComboBox.SelectedIndex != -1);
169 // Move the focus to the associated ComboBox
170 _namedConnectionComboBox.Select();
172 // If there is a selected NamedConnection, validate the connection string right away
173 // so that we can potentially select the default container name if there is one
174 if (_namedConnectionComboBox.SelectedIndex != -1)
176 VerifyConnectionString();
179 // else it's being unchecked, so that means another radio button is being checked and that handler will take care of updating the state
183 private void OnNamedConnectionComboBox_SelectedIndexChanged(object sender, EventArgs e)
185 if (!_ignoreEvents)
187 EnterConnectionEditMode();
189 // Update flag to indicate if there is a value selected in this box
190 _configureObjectContext.SelectConnectionStringHasValue(_namedConnectionComboBox.SelectedIndex != -1);
192 // If there is a selected NamedConnection, validate the connection string right away
193 // so that we can potentially select the default container name if there is one
194 if (_namedConnectionComboBox.SelectedIndex != -1)
196 VerifyConnectionString();
201 private void OnContainerNameComboBox_Enter(object sender, EventArgs e)
203 if (!_ignoreEvents)
205 VerifyConnectionString();
209 private void OnContainerNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
211 if (!_ignoreEvents)
213 _configureObjectContext.SelectContainerName(_containerNameComboBox.SelectedItem as EntityDataSourceContainerNameItem);
217 // Move to edit mode so that we will know we need to reload metadata if it's needed later
218 private void EnterConnectionEditMode()
220 _connectionInEdit = true;
221 _containerNameComboBox.SelectedIndex = -1;
224 private void LeaveConnectionEditMode()
226 _connectionInEdit = false;
229 /// <summary>
230 /// Verify the selected connection string and load the metadata for it if it is successfully verified
231 /// </summary>
232 /// <returns>True if the metadata was successfully loaded from the connection string</returns>
233 private bool VerifyConnectionString()
237 Cursor.Current = Cursors.WaitCursor;
238 if (_connectionInEdit)
240 bool isNamedConnection = _namedConnectionRadioButton.Checked;
241 Debug.Assert(!isNamedConnection ? _connectionStringRadioButton.Checked : true, "only expecting either named connection or connection string radio button options");
243 EntityConnectionStringBuilderItem selectedConnection = null;
244 if (isNamedConnection)
246 if (_namedConnectionComboBox.SelectedIndex != -1)
248 selectedConnection = _namedConnectionComboBox.SelectedItem as EntityConnectionStringBuilderItem;
251 else
253 // Make a builder item out of the specified connection string. This will do some basic verification on the string.
254 selectedConnection = _configureObjectContext.GetEntityConnectionStringBuilderItem(_connectionStringTextBox.Text);
257 if (selectedConnection != null)
259 bool metadataLoaded = _configureObjectContext.SelectConnectionStringBuilder(selectedConnection, true /*resetContainer*/);
261 // If verification failed, try to move the focus back to the appropriate control.
262 if (!metadataLoaded)
264 if (_namedConnectionRadioButton.Checked)
266 _namedConnectionComboBox.Select();
268 else
270 _connectionStringTextBox.Select();
271 _connectionStringTextBox.Select(0, _connectionStringTextBox.TextLength);
276 // Leave connection edit mode regardless of whether the metadata was loaded or not, because there is no need to keep trying
277 // to validated it over and over again unless the user makes a change that puts it back into edit mode again
278 LeaveConnectionEditMode();
281 return true;
283 finally
285 Cursor.Current = Cursors.Default;
288 #endregion
290 #region Wizard state management
291 public override bool OnNext()
293 Debug.Assert(_configureObjectContext.CanEnableNext, "OnNext called when CanEnableNext is false");
295 return VerifyConnectionString();
298 protected override void OnVisibleChanged(EventArgs e)
300 base.OnVisibleChanged(e);
302 if (Visible)
304 _configureObjectContext.UpdateWizardState();
307 #endregion
309 #region Methods for setting control values
310 // Expects that the specified builder item is a named connection already in the list, is a full connection string, or is empty
311 // If empty, the default is to select the named connection option and don't select anything in the list
312 internal void SetConnectionString(EntityConnectionStringBuilderItem connStrBuilderItem)
314 Debug.Assert(connStrBuilderItem != null, "expected non-null connStrBuilderItem");
316 _ignoreEvents = true;
318 // set the state of the ConnectionString radio buttons and associated controls
319 bool isNamedConnection = connStrBuilderItem.IsEmpty || connStrBuilderItem.IsNamedConnection;
321 _namedConnectionRadioButton.Checked = isNamedConnection;
322 _namedConnectionComboBox.Enabled = isNamedConnection;
323 _connectionStringRadioButton.Checked = !isNamedConnection;
324 _connectionStringTextBox.Enabled = !isNamedConnection;
326 // set the value of the control that was just enabled
327 if (connStrBuilderItem.IsEmpty)
329 _namedConnectionComboBox.SelectedIndex = -1;
330 _configureObjectContext.SelectConnectionStringHasValue(false /*connectionStringHasValue*/);
332 else if (connStrBuilderItem.IsNamedConnection)
334 _namedConnectionComboBox.SelectedItem = connStrBuilderItem;
335 _configureObjectContext.SelectConnectionStringHasValue(true /*connectionStringHasValue*/);
337 else
339 _connectionStringTextBox.Text = connStrBuilderItem.ConnectionString;
340 _configureObjectContext.SelectConnectionStringHasValue(!connStrBuilderItem.IsEmpty);
343 _ignoreEvents = false;
346 internal void SetContainerNames(List<EntityDataSourceContainerNameItem> containerNames)
348 _ignoreEvents = true;
349 _containerNameComboBox.Items.Clear();
350 _containerNameComboBox.Items.AddRange(containerNames.ToArray());
351 _ignoreEvents = false;
354 internal void SetNamedConnections(List<EntityConnectionStringBuilderItem> namedConnections)
356 _ignoreEvents = true;
357 _namedConnectionComboBox.Items.AddRange(namedConnections.ToArray());
358 _ignoreEvents = false;
361 /// <summary>
362 /// Expects that selectedContainer is already in the ComboBox list, or should be null
363 /// </summary>
364 /// <param name="selectedContainer"></param>
365 /// <param name="initialLoad">If this is the initial load, we want to suppress events so that the change does
366 /// not cause additional work in panels that listen to the container name changed event</param>
367 internal void SetSelectedContainerName(EntityDataSourceContainerNameItem selectedContainer, bool initialLoad)
369 if (initialLoad)
371 _ignoreEvents = true;
373 if (selectedContainer == null)
375 _containerNameComboBox.SelectedIndex = -1;
377 else
379 _containerNameComboBox.SelectedItem = selectedContainer;
381 _ignoreEvents = false;
383 #endregion