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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
//---------------------------------------------------------------------
// <copyright file="EntityDataSourceStatementEditor.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI.Design.WebControls.Util;
using System.Diagnostics;
using System.Drawing.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Windows.Forms;
namespace System.Web.UI.Design.WebControls
{
internal class EntityDataSourceStatementEditor : UITypeEditor
{
private bool EditQueryChangeCallback(object pair)
{
// pair.First is a wrapper that contains the EntityDataSource instance and property name being edited
// pair.Second contains the value of the property being edited
ITypeDescriptorContext context = (ITypeDescriptorContext)((Pair)pair).First;
string value = (string)((Pair)pair).Second;
EntityDataSource entityDataSource = (EntityDataSource)context.Instance;
IServiceProvider serviceProvider = entityDataSource.Site;
IDesignerHost designerHost = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost));
Debug.Assert(designerHost != null, "Did not get DesignerHost service.");
EntityDataSourceDesigner designer = (EntityDataSourceDesigner)designerHost.GetDesigner(entityDataSource);
// Configure the dialog for the specified property and display it
return Initialize(designer, entityDataSource, context.PropertyDescriptor.Name, serviceProvider, value);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
ControlDesigner.InvokeTransactedChange(
(IComponent)context.Instance,
new TransactedChangeCallback(EditQueryChangeCallback),
new Pair(context, value),
Strings.ExpressionEditorTransactionDescription);
return value;
}
// Determines if the specified property is one that has an associated "AutoGenerateXXXClause" property
private static bool GetAutoGen(string operation, EntityDataSourceDesigner entityDataSourceDesigner)
{
if (String.Equals("Where", operation, StringComparison.Ordinal))
{
return entityDataSourceDesigner.AutoGenerateWhereClause;
}
else if (String.Equals("OrderBy", operation, StringComparison.Ordinal))
{
return entityDataSourceDesigner.AutoGenerateOrderByClause;
}
return false;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
// Gets the name of the AutoGenerateXXXProperty associated with the specified property name
private static string GetOperationAutoGenerateProperty(string propertyName)
{
if (String.Equals("Where", propertyName, StringComparison.Ordinal))
{
return "AutoGenerateWhereClause";
}
else if (String.Equals("OrderBy", propertyName, StringComparison.Ordinal))
{
return "AutoGenerateOrderByClause";
}
return null;
}
// Gets the label text or accessible name to display over the textbox for editing the specified property name
private static string GetStatementLabel(string propertyName, bool accessible)
{
switch (propertyName)
{
case "CommandText":
return accessible ? Strings.ExpressionEditor_CommandTextLabelAccessibleName :
Strings.ExpressionEditor_CommandTextLabel;
case "OrderBy":
case "Select":
case "Where":
return accessible ? Strings.ExpressionEditor_ExpressionStatementLabelAccessibleName(propertyName) :
Strings.ExpressionEditor_ExpressionStatementLabel(propertyName);
default:
Debug.Fail("Unknown property name in EntityDataSourceStatementEditor: " + propertyName);
return null;
}
}
// Gets the F1 help topic for each of the dialogs using the specified property name
private static string GetHelpTopic(string propertyName)
{
switch (propertyName)
{
case "CommandText": return "net.Asp.EntityDataSource.CommandTextExpression";
case "OrderBy": return "net.Asp.EntityDataSource.OrderByExpression";
case "Select": return "net.Asp.EntityDataSource.SelectExpression";
case "Where": return "net.Asp.EntityDataSource.WhereExpression";
default:
Debug.Fail("Unknown property name in EntityDataSourceStatementEditor: " + propertyName);
return String.Empty;
}
}
// Gets the property name for the parameters property associated with the specified property name
private static string GetOperationParameterProperty(string propertyName)
{
switch (propertyName)
{
case "CommandText":
return "CommandParameters";
case "OrderBy":
return "OrderByParameters";
case "Select":
return "SelectParameters";
case "Where":
return "WhereParameters";
default:
Debug.Fail("Unknown property name in EntityDataSourceStatementEditor: " + propertyName);
return null;
}
}
// Gets a clone of the parameters collection associated with the specified property name
private static ParameterCollection GetParameters(string propertyName, EntityDataSourceDesigner designer)
{
switch (propertyName)
{
case "CommandText":
return designer.CloneCommandParameters();
case "OrderBy":
return designer.CloneOrderByParameters();
case "Select":
return designer.CloneSelectParameters();
case "Where":
return designer.CloneWhereParameters();
default:
Debug.Fail("Unknown property name in EntityDataSourceStatementEditor: " + propertyName);
return null;
}
}
// Updates the parameters collection associated with the specified property name
private static void SetParameters(string propertyName, EntityDataSourceDesigner designer, ParameterCollection parameters)
{
switch (propertyName)
{
case "CommandText":
designer.SetCommandParameterContents(parameters);
break;
case "OrderBy":
designer.SetOrderByParameterContents(parameters);
break;
case "Select":
designer.SetSelectParameterContents(parameters);
break;
case "Where":
designer.SetWhereParameterContents(parameters);
break;
default:
Debug.Fail("Unknown property name in EntityDataSourceStatementEditor: " + propertyName);
break;
}
}
// Configures and displays the editor dialog based on the specified property name
private bool Initialize(EntityDataSourceDesigner designer, EntityDataSource entityDataSource, string propertyName, IServiceProvider serviceProvider, string statement)
{
string propertyParameters = GetOperationParameterProperty(propertyName);
string autoGenProperty = GetOperationAutoGenerateProperty(propertyName);
bool hasAutoGen = (autoGenProperty != null);
bool autoGen = GetAutoGen(propertyName, designer);
ParameterCollection parameters = GetParameters(propertyName, designer);
string label = GetStatementLabel(propertyName, false);
string accessibleName = GetStatementLabel(propertyName, true);
string helpTopic = GetHelpTopic(propertyName);
EntityDataSourceStatementEditorForm form = new EntityDataSourceStatementEditorForm(entityDataSource, serviceProvider,
hasAutoGen, autoGen, propertyName, label, accessibleName, helpTopic, statement, parameters);
DialogResult result = UIHelper.ShowDialog(serviceProvider, form);
if (result == DialogResult.OK)
{
// We use the property descriptors to reset the values to
// make sure we clear out any databindings or expressions that
// may be set.
PropertyDescriptor propDesc = null;
if (autoGenProperty != null)
{
propDesc = TypeDescriptor.GetProperties(entityDataSource)[autoGenProperty];
propDesc.ResetValue(entityDataSource);
propDesc.SetValue(entityDataSource, form.AutoGen);
}
if (propertyName != null)
{
propDesc = TypeDescriptor.GetProperties(entityDataSource)[propertyName];
propDesc.ResetValue(entityDataSource);
propDesc.SetValue(entityDataSource, form.Statement);
}
if (propertyParameters != null)
{
SetParameters(propertyName, designer, form.Parameters);
}
return true;
}
else
{
return false;
}
}
}
}
|