Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / UI / WebParts / RowToParametersTransformer.cs / 1305376 / RowToParametersTransformer.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls.WebParts {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.UI.WebControls;
[WebPartTransformer(typeof(IWebPartRow), typeof(IWebPartParameters))]
public sealed class RowToParametersTransformer : WebPartTransformer, IWebPartParameters {
private IWebPartRow _provider;
private string[] _consumerFieldNames;
private string[] _providerFieldNames;
private PropertyDescriptorCollection _consumerSchema;
private ParametersCallback _callback;
public override Control CreateConfigurationControl() {
return new RowToParametersConfigurationWizard(this);
}
[
TypeConverterAttribute(typeof(StringArrayConverter)),
]
public string[] ConsumerFieldNames {
get {
return (_consumerFieldNames != null) ? (string[])_consumerFieldNames.Clone() : new string[0];
}
set {
_consumerFieldNames = (value != null) ? (string[])value.Clone() : null;
}
}
private PropertyDescriptorCollection ConsumerSchema {
get {
return _consumerSchema;
}
}
[
TypeConverterAttribute(typeof(StringArrayConverter)),
]
public string[] ProviderFieldNames {
get {
return (_providerFieldNames != null) ? (string[])_providerFieldNames.Clone() : new string[0];
}
set {
_providerFieldNames = (value != null) ? (string[])value.Clone() : null;
}
}
private PropertyDescriptorCollection ProviderSchema {
get {
return (_provider != null) ? _provider.Schema : null;
}
}
// Schema containing property descriptors associated with each name in ProviderFieldNames
private PropertyDescriptorCollection SelectedProviderSchema {
get {
PropertyDescriptorCollection props = new PropertyDescriptorCollection(null);
PropertyDescriptorCollection providerSchema = ProviderSchema;
if (providerSchema != null && _providerFieldNames != null && _providerFieldNames.Length > 0) {
foreach (string fieldName in _providerFieldNames) {
PropertyDescriptor prop = providerSchema.Find(fieldName, /* ignoreCase */ true);
if (prop == null) {
// If any provider field name is not in the schema, return an empty schema
return new PropertyDescriptorCollection(null);
}
else {
props.Add(prop);
}
}
}
return props;
}
}
// Throws an exception if the ConsumerFieldNames and ProviderFieldNames have different length
private void CheckFieldNamesLength() {
int consumerFieldNamesLength = (_consumerFieldNames != null) ? _consumerFieldNames.Length : 0;
int providerFieldNamesLength = (_providerFieldNames != null) ? _providerFieldNames.Length : 0;
if (consumerFieldNamesLength != providerFieldNamesLength) {
throw new InvalidOperationException(SR.GetString(SR.RowToParametersTransformer_DifferentFieldNamesLength));
}
}
private void GetRowData(object rowData) {
Debug.Assert(_callback != null);
// Only return null if rowData is null. Else, return an empty dictionary. (VSWhidbey 381264)
IDictionary parametersData = null;
// For perf, check in decreasing order of likeliness that an object is null
if (rowData != null) {
PropertyDescriptorCollection consumerSchema = ((IWebPartParameters)this).Schema;
parametersData = new HybridDictionary(consumerSchema.Count);
if (consumerSchema.Count > 0) {
PropertyDescriptorCollection providerSchema = SelectedProviderSchema;
if (providerSchema != null && providerSchema.Count > 0) {
if (providerSchema.Count == consumerSchema.Count) {
for (int i=0; i < providerSchema.Count; i++) {
PropertyDescriptor providerProp = providerSchema[i];
PropertyDescriptor consumerProp = consumerSchema[i];
parametersData[consumerProp.Name] = providerProp.GetValue(rowData);
}
}
}
}
}
_callback(parametersData);
}
protected internal override void LoadConfigurationState(object savedState) {
if (savedState != null) {
string[] fieldNames = (string[])savedState;
int fieldNamesLength = fieldNames.Length;
if (fieldNamesLength % 2 != 0) {
throw new InvalidOperationException(SR.GetString(SR.RowToParametersTransformer_DifferentFieldNamesLength));
}
int length = fieldNamesLength / 2;
_consumerFieldNames = new string[length];
_providerFieldNames = new string[length];
for (int i=0; i < length; i++) {
_consumerFieldNames[i] = fieldNames[2*i];
_providerFieldNames[i] = fieldNames[2*i + 1];
}
}
}
protected internal override object SaveConfigurationState() {
CheckFieldNamesLength();
int consumerFieldNamesLength = (_consumerFieldNames != null) ? _consumerFieldNames.Length : 0;
if (consumerFieldNamesLength > 0) {
string[] fieldNames = new string[consumerFieldNamesLength * 2];
for (int i=0; i < consumerFieldNamesLength; i++) {
fieldNames[2*i] = _consumerFieldNames[i];
fieldNames[2*i + 1] = _providerFieldNames[i];
}
return fieldNames;
}
return null;
}
public override object Transform(object providerData) {
_provider = (IWebPartRow)providerData;
return this;
}
#region Implementation of IWebPartParameters
void IWebPartParameters.GetParametersData(ParametersCallback callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
CheckFieldNamesLength();
if (_provider != null) {
_callback = callback;
_provider.GetRowData(new RowCallback(GetRowData));
}
else {
callback(null);
}
}
PropertyDescriptorCollection IWebPartParameters.Schema {
get {
CheckFieldNamesLength();
PropertyDescriptorCollection props = new PropertyDescriptorCollection(null);
if (_consumerSchema != null && _consumerFieldNames != null && _consumerFieldNames.Length > 0) {
foreach (string fieldName in _consumerFieldNames) {
PropertyDescriptor prop = _consumerSchema.Find(fieldName, true);
if (prop == null) {
// If any consumer field name is not in the schema, return an empty schema
return new PropertyDescriptorCollection(null);
}
else {
props.Add(prop);
}
}
}
return props;
}
}
void IWebPartParameters.SetConsumerSchema(PropertyDescriptorCollection schema) {
_consumerSchema = schema;
}
#endregion
private sealed class RowToParametersConfigurationWizard : TransformerConfigurationWizardBase {
private DropDownList[] _consumerFieldNames;
private RowToParametersTransformer _owner;
private const string consumerFieldNameID = "ConsumerFieldName";
public RowToParametersConfigurationWizard(RowToParametersTransformer owner) {
Debug.Assert(owner != null);
_owner = owner;
}
protected override PropertyDescriptorCollection ConsumerSchema {
get {
return _owner.ConsumerSchema;
}
}
protected override PropertyDescriptorCollection ProviderSchema {
get {
return _owner.ProviderSchema;
}
}
protected override void CreateWizardSteps() {
// The WizardSteps should be empty when this is called
Debug.Assert(WizardSteps.Count == 0);
int oldProviderNamesLength = (OldProviderNames != null) ? OldProviderNames.Length : 0;
if (oldProviderNamesLength > 0) {
_consumerFieldNames = new DropDownList[oldProviderNamesLength / 2];
ListItem[] consumerItems = null;
int oldConsumerNamesLength = (OldConsumerNames != null) ? OldConsumerNames.Length : 0;
if (oldConsumerNamesLength > 0) {
consumerItems = new ListItem[oldConsumerNamesLength / 2];
for (int i=0; i < oldConsumerNamesLength / 2; i++) {
consumerItems[i] = new ListItem(OldConsumerNames[2*i], OldConsumerNames[2*i + 1]);
}
}
for (int i=0; i < oldProviderNamesLength / 2; i++) {
WizardStep s = new WizardStep();
s.Controls.Add(new LiteralControl(
SR.GetString(SR.RowToParametersTransformer_ProviderFieldName) + " "));
Label label = new Label();
// HtmlEncode the string, since it comes from the provider schema and it may contain
// unsafe characters.
label.Text = HttpUtility.HtmlEncode(OldProviderNames[2*i]);
label.Font.Bold = true;
s.Controls.Add(label);
s.Controls.Add(new LiteralControl("
"));
DropDownList consumerFieldName = new DropDownList();
consumerFieldName.ID = consumerFieldNameID + i;
if (consumerItems != null) {
consumerFieldName.Items.Add(new ListItem());
// Calculate consumerFieldValue based on the current providerFieldValue,
// and the ProviderFieldNames and ConsumerFieldNames on the Transformer
string[] providerFieldNames = _owner._providerFieldNames;
string[] consumerFieldNames = _owner._consumerFieldNames;
string providerFieldValue = OldProviderNames[2*i + 1];
string consumerFieldValue = null;
if (providerFieldNames != null) {
for (int j=0; j < providerFieldNames.Length; j++) {
// Ignore case when getting the value, since we ignore case when
// returing the connection data. (VSWhidbey 434566)
if (String.Equals(providerFieldNames[j], providerFieldValue,
StringComparison.OrdinalIgnoreCase) &&
consumerFieldNames != null && consumerFieldNames.Length > j) {
consumerFieldValue = consumerFieldNames[j];
break;
}
}
}
foreach (ListItem consumerItem in consumerItems) {
ListItem item = new ListItem(consumerItem.Text, consumerItem.Value);
// Ignore case when setting selected value, since we ignore case when
// returing the connection data. (VSWhidbey 434566)
if (String.Equals(item.Value, consumerFieldValue, StringComparison.OrdinalIgnoreCase)) {
item.Selected = true;
}
consumerFieldName.Items.Add(item);
}
}
else {
consumerFieldName.Items.Add(new ListItem(
SR.GetString(SR.RowToParametersTransformer_NoConsumerSchema)));
consumerFieldName.Enabled = false;
}
_consumerFieldNames[i] = consumerFieldName;
Label consumerFieldNameLabel = new Label();
consumerFieldNameLabel.Text = SR.GetString(SR.RowToParametersTransformer_ConsumerFieldName);
consumerFieldNameLabel.AssociatedControlID = consumerFieldName.ID;
s.Controls.Add(consumerFieldNameLabel);
s.Controls.Add(new LiteralControl(" "));
s.Controls.Add(consumerFieldName);
WizardSteps.Add(s);
}
}
else {
WizardStep s = new WizardStep();
s.Controls.Add(new LiteralControl(SR.GetString(SR.RowToParametersTransformer_NoProviderSchema)));
WizardSteps.Add(s);
}
// We should always have at least 1 WizardStep when we return
Debug.Assert(WizardSteps.Count > 0);
}
protected override void OnFinishButtonClick(WizardNavigationEventArgs e) {
ArrayList providerFieldNames = new ArrayList();
ArrayList consumerFieldNames = new ArrayList();
int oldProviderNamesLength = (OldProviderNames != null) ? OldProviderNames.Length : 0;
if (oldProviderNamesLength > 0) {
Debug.Assert(_consumerFieldNames != null);
Debug.Assert(oldProviderNamesLength == 2 * _consumerFieldNames.Length);
for (int i=0; i < _consumerFieldNames.Length; i++) {
DropDownList consumerFieldName = _consumerFieldNames[i];
if (consumerFieldName.Enabled) {
string selectedConsumerFieldName = consumerFieldName.SelectedValue;
if (!String.IsNullOrEmpty(selectedConsumerFieldName)) {
providerFieldNames.Add(OldProviderNames[2*i + 1]);
consumerFieldNames.Add(selectedConsumerFieldName);
}
}
}
}
_owner.ConsumerFieldNames = (string[])consumerFieldNames.ToArray(typeof(string));
_owner.ProviderFieldNames = (string[])providerFieldNames.ToArray(typeof(string));
base.OnFinishButtonClick(e);
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CFStream.cs
- TextSchema.cs
- ZipFileInfo.cs
- PseudoWebRequest.cs
- StorageFunctionMapping.cs
- PenContext.cs
- DoubleLink.cs
- DynamicValidatorEventArgs.cs
- PriorityBindingExpression.cs
- MD5CryptoServiceProvider.cs
- HttpModuleAction.cs
- OutputScopeManager.cs
- DashStyles.cs
- ConstructorBuilder.cs
- ManualResetEventSlim.cs
- File.cs
- ReflectEventDescriptor.cs
- ProcessProtocolHandler.cs
- DataRowView.cs
- SafeHandles.cs
- RegexParser.cs
- SimpleBitVector32.cs
- Track.cs
- AssociationTypeEmitter.cs
- DataGridViewCheckBoxColumn.cs
- GeometryModel3D.cs
- LiteralDesigner.cs
- Mapping.cs
- RoleManagerModule.cs
- XmlDataLoader.cs
- XPathExpr.cs
- NetworkInformationPermission.cs
- ClientScriptManager.cs
- RedirectionProxy.cs
- WSDualHttpSecurity.cs
- DbConnectionPool.cs
- VisualStyleInformation.cs
- DirectoryInfo.cs
- LifetimeServices.cs
- StreamGeometry.cs
- mansign.cs
- OciLobLocator.cs
- QueryStringParameter.cs
- HandlerBase.cs
- DataObject.cs
- HelpInfo.cs
- ClassHandlersStore.cs
- MembershipSection.cs
- CodeDelegateInvokeExpression.cs
- Events.cs
- DataGridViewCellContextMenuStripNeededEventArgs.cs
- ToolStripPanelDesigner.cs
- DateTimeConstantAttribute.cs
- UInt64Storage.cs
- XPathNode.cs
- TableLayoutPanelCellPosition.cs
- Baml2006SchemaContext.cs
- StorageAssociationTypeMapping.cs
- PageCanvasSize.cs
- KeyedCollection.cs
- ScriptComponentDescriptor.cs
- BuildResult.cs
- SearchForVirtualItemEventArgs.cs
- ContractUtils.cs
- ScriptMethodAttribute.cs
- CodeArgumentReferenceExpression.cs
- PolyLineSegmentFigureLogic.cs
- ConfigXmlWhitespace.cs
- WinFormsSecurity.cs
- UnknownBitmapDecoder.cs
- NullableBoolConverter.cs
- FormViewRow.cs
- WebPartVerbCollection.cs
- ListControlConvertEventArgs.cs
- SafeProcessHandle.cs
- FailedToStartupUIException.cs
- ProgressiveCrcCalculatingStream.cs
- IndentTextWriter.cs
- EditorPartChrome.cs
- UTF32Encoding.cs
- MessageBox.cs
- COM2EnumConverter.cs
- EncodingDataItem.cs
- ProviderConnectionPoint.cs
- HtmlCalendarAdapter.cs
- XmlSubtreeReader.cs
- BufferedGraphicsManager.cs
- WebBaseEventKeyComparer.cs
- EventHandlerList.cs
- Cursors.cs
- EmptyWithCancelationCheckWorkItem.cs
- DateTimePicker.cs
- MapPathBasedVirtualPathProvider.cs
- ObjectManager.cs
- RightsManagementInformation.cs
- StreamedFramingRequestChannel.cs
- ReflectEventDescriptor.cs
- TraceData.cs
- BinaryUtilClasses.cs
- PixelFormatConverter.cs