private bool CanConnectWebPartsCore(WebPart provider, ProviderConnectionPoint providerConnectionPoint, WebPart consumer, ConsumerConnectionPoint consumerConnectionPoint, WebPartTransformer transformer, bool throwOnError)
 {
     if (!this.Personalization.IsModifiable)
     {
         if (!throwOnError)
         {
             return false;
         }
         this.Personalization.EnsureEnabled(true);
     }
     if (provider == null)
     {
         throw new ArgumentNullException("provider");
     }
     if (!this.Controls.Contains(provider))
     {
         throw new ArgumentException(System.Web.SR.GetString("UnknownWebPart"), "provider");
     }
     if (consumer == null)
     {
         throw new ArgumentNullException("consumer");
     }
     if (!this.Controls.Contains(consumer))
     {
         throw new ArgumentException(System.Web.SR.GetString("UnknownWebPart"), "consumer");
     }
     if (providerConnectionPoint == null)
     {
         throw new ArgumentNullException("providerConnectionPoint");
     }
     if (consumerConnectionPoint == null)
     {
         throw new ArgumentNullException("consumerConnectionPoint");
     }
     Control control = provider.ToControl();
     Control control2 = consumer.ToControl();
     if (providerConnectionPoint.ControlType != control.GetType())
     {
         throw new ArgumentException(System.Web.SR.GetString("WebPartManager_InvalidConnectionPoint"), "providerConnectionPoint");
     }
     if (consumerConnectionPoint.ControlType != control2.GetType())
     {
         throw new ArgumentException(System.Web.SR.GetString("WebPartManager_InvalidConnectionPoint"), "consumerConnectionPoint");
     }
     if (provider == consumer)
     {
         if (throwOnError)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("WebPartManager_CantConnectToSelf"));
         }
         return false;
     }
     if (provider.IsClosed)
     {
         if (throwOnError)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("WebPartManager_CantConnectClosed", new object[] { provider.ID }));
         }
         return false;
     }
     if (consumer.IsClosed)
     {
         if (throwOnError)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("WebPartManager_CantConnectClosed", new object[] { consumer.ID }));
         }
         return false;
     }
     if (!providerConnectionPoint.GetEnabled(control))
     {
         if (throwOnError)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_DisabledConnectionPoint", new object[] { providerConnectionPoint.ID, provider.ID }));
         }
         return false;
     }
     if (!consumerConnectionPoint.GetEnabled(control2))
     {
         if (throwOnError)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_DisabledConnectionPoint", new object[] { consumerConnectionPoint.ID, consumer.ID }));
         }
         return false;
     }
     if (!providerConnectionPoint.AllowsMultipleConnections)
     {
         foreach (WebPartConnection connection in this.Connections)
         {
             if ((connection.Provider == provider) && (connection.ProviderConnectionPoint == providerConnectionPoint))
             {
                 if (throwOnError)
                 {
                     throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_Duplicate", new object[] { providerConnectionPoint.ID, provider.ID }));
                 }
                 return false;
             }
         }
     }
     if (!consumerConnectionPoint.AllowsMultipleConnections)
     {
         foreach (WebPartConnection connection2 in this.Connections)
         {
             if ((connection2.Consumer == consumer) && (connection2.ConsumerConnectionPoint == consumerConnectionPoint))
             {
                 if (throwOnError)
                 {
                     throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_Duplicate", new object[] { consumerConnectionPoint.ID, consumer.ID }));
                 }
                 return false;
             }
         }
     }
     if (transformer == null)
     {
         if (providerConnectionPoint.InterfaceType != consumerConnectionPoint.InterfaceType)
         {
             if (throwOnError)
             {
                 throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_NoCommonInterface", new string[] { providerConnectionPoint.DisplayName, provider.ID, consumerConnectionPoint.DisplayName, consumer.ID }));
             }
             return false;
         }
         ConnectionInterfaceCollection secondaryInterfaces = providerConnectionPoint.GetSecondaryInterfaces(control);
         if (!consumerConnectionPoint.SupportsConnection(control2, secondaryInterfaces))
         {
             if (throwOnError)
             {
                 throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_IncompatibleSecondaryInterfaces", new string[] { consumerConnectionPoint.DisplayName, consumer.ID, providerConnectionPoint.DisplayName, provider.ID }));
             }
             return false;
         }
     }
     else
     {
         Type type = transformer.GetType();
         if (!this.AvailableTransformers.Contains(type))
         {
             throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_TransformerNotAvailable", new object[] { type.FullName }));
         }
         Type consumerType = WebPartTransformerAttribute.GetConsumerType(type);
         Type providerType = WebPartTransformerAttribute.GetProviderType(type);
         if (providerConnectionPoint.InterfaceType != consumerType)
         {
             if (throwOnError)
             {
                 throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_IncompatibleProviderTransformer", new object[] { providerConnectionPoint.DisplayName, provider.ID, type.FullName }));
             }
             return false;
         }
         if (providerType != consumerConnectionPoint.InterfaceType)
         {
             if (throwOnError)
             {
                 throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_IncompatibleConsumerTransformer", new object[] { type.FullName, consumerConnectionPoint.DisplayName, consumer.ID }));
             }
             return false;
         }
         if (!consumerConnectionPoint.SupportsConnection(control2, ConnectionInterfaceCollection.Empty))
         {
             if (throwOnError)
             {
                 throw new InvalidOperationException(System.Web.SR.GetString("WebPartConnection_ConsumerRequiresSecondaryInterfaces", new object[] { consumerConnectionPoint.DisplayName, consumer.ID }));
             }
             return false;
         }
     }
     return true;
 }
        internal void Activate()
        {
            this.Transformers.SetReadOnly();
            WebPart provider = this.Provider;
            WebPart consumer = this.Consumer;
            Control control  = provider.ToControl();
            Control control2 = consumer.ToControl();

            System.Web.UI.WebControls.WebParts.ProviderConnectionPoint providerConnectionPoint = this.ProviderConnectionPoint;
            if (!providerConnectionPoint.GetEnabled(control))
            {
                consumer.SetConnectErrorMessage(System.Web.SR.GetString("WebPartConnection_DisabledConnectionPoint", new object[] { providerConnectionPoint.DisplayName, provider.DisplayTitle }));
            }
            else
            {
                System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint consumerConnectionPoint = this.ConsumerConnectionPoint;
                if (!consumerConnectionPoint.GetEnabled(control2))
                {
                    consumer.SetConnectErrorMessage(System.Web.SR.GetString("WebPartConnection_DisabledConnectionPoint", new object[] { consumerConnectionPoint.DisplayName, consumer.DisplayTitle }));
                }
                else if (!provider.IsClosed && !consumer.IsClosed)
                {
                    WebPartTransformer transformer = this.Transformer;
                    if (transformer == null)
                    {
                        if (providerConnectionPoint.InterfaceType == consumerConnectionPoint.InterfaceType)
                        {
                            ConnectionInterfaceCollection secondaryInterfaces = providerConnectionPoint.GetSecondaryInterfaces(control);
                            if (consumerConnectionPoint.SupportsConnection(control2, secondaryInterfaces))
                            {
                                object data = providerConnectionPoint.GetObject(control);
                                consumerConnectionPoint.SetObject(control2, data);
                                this._isActive = true;
                            }
                            else
                            {
                                consumer.SetConnectErrorMessage(System.Web.SR.GetString("WebPartConnection_IncompatibleSecondaryInterfaces", new string[] { consumerConnectionPoint.DisplayName, consumer.DisplayTitle, providerConnectionPoint.DisplayName, provider.DisplayTitle }));
                            }
                        }
                        else
                        {
                            consumer.SetConnectErrorMessage(System.Web.SR.GetString("WebPartConnection_NoCommonInterface", new string[] { providerConnectionPoint.DisplayName, provider.DisplayTitle, consumerConnectionPoint.DisplayName, consumer.DisplayTitle }));
                        }
                    }
                    else
                    {
                        Type type = transformer.GetType();
                        if (!this._webPartManager.AvailableTransformers.Contains(type))
                        {
                            string str;
                            if ((this._webPartManager.Context != null) && this._webPartManager.Context.IsCustomErrorEnabled)
                            {
                                str = System.Web.SR.GetString("WebPartConnection_TransformerNotAvailable");
                            }
                            else
                            {
                                str = System.Web.SR.GetString("WebPartConnection_TransformerNotAvailableWithType", new object[] { type.FullName });
                            }
                            consumer.SetConnectErrorMessage(str);
                        }
                        Type consumerType = WebPartTransformerAttribute.GetConsumerType(type);
                        Type providerType = WebPartTransformerAttribute.GetProviderType(type);
                        if ((providerConnectionPoint.InterfaceType == consumerType) && (providerType == consumerConnectionPoint.InterfaceType))
                        {
                            if (consumerConnectionPoint.SupportsConnection(control2, ConnectionInterfaceCollection.Empty))
                            {
                                object providerData = providerConnectionPoint.GetObject(control);
                                object obj4         = transformer.Transform(providerData);
                                consumerConnectionPoint.SetObject(control2, obj4);
                                this._isActive = true;
                            }
                            else
                            {
                                consumer.SetConnectErrorMessage(System.Web.SR.GetString("WebPartConnection_ConsumerRequiresSecondaryInterfaces", new object[] { consumerConnectionPoint.DisplayName, consumer.DisplayTitle }));
                            }
                        }
                        else if (providerConnectionPoint.InterfaceType != consumerType)
                        {
                            string str2;
                            if ((this._webPartManager.Context != null) && this._webPartManager.Context.IsCustomErrorEnabled)
                            {
                                str2 = System.Web.SR.GetString("WebPartConnection_IncompatibleProviderTransformer", new object[] { providerConnectionPoint.DisplayName, provider.DisplayTitle });
                            }
                            else
                            {
                                str2 = System.Web.SR.GetString("WebPartConnection_IncompatibleProviderTransformerWithType", new object[] { providerConnectionPoint.DisplayName, provider.DisplayTitle, type.FullName });
                            }
                            consumer.SetConnectErrorMessage(str2);
                        }
                        else
                        {
                            string str3;
                            if ((this._webPartManager.Context != null) && this._webPartManager.Context.IsCustomErrorEnabled)
                            {
                                str3 = System.Web.SR.GetString("WebPartConnection_IncompatibleConsumerTransformer", new object[] { consumerConnectionPoint.DisplayName, consumer.DisplayTitle });
                            }
                            else
                            {
                                str3 = System.Web.SR.GetString("WebPartConnection_IncompatibleConsumerTransformerWithType", new object[] { type.FullName, consumerConnectionPoint.DisplayName, consumer.DisplayTitle });
                            }
                            consumer.SetConnectErrorMessage(str3);
                        }
                    }
                }
            }
        }
Пример #3
0
        private bool CanConnectWebPartsCore(WebPart provider, ProviderConnectionPoint providerConnectionPoint,
                                            WebPart consumer, ConsumerConnectionPoint consumerConnectionPoint,
                                            WebPartTransformer transformer, bool throwOnError) {
            if (!Personalization.IsModifiable) {
                if (throwOnError) {
                    // Will throw appropriate exception
                    Personalization.EnsureEnabled(/* ensureModifiable */ true);
                }
                else {
                    return false;
                }
            }

            if (provider == null) {
                throw new ArgumentNullException("provider");
            }
            if (!Controls.Contains(provider)) {
                throw new ArgumentException(SR.GetString(SR.UnknownWebPart), "provider");
            }

            if (consumer == null) {
                throw new ArgumentNullException("consumer");
            }
            if (!Controls.Contains(consumer)) {
                throw new ArgumentException(SR.GetString(SR.UnknownWebPart), "consumer");
            }

            if (providerConnectionPoint == null) {
                throw new ArgumentNullException("providerConnectionPoint");
            }
            if (consumerConnectionPoint == null) {
                throw new ArgumentNullException("consumerConnectionPoint");
            }

            Control providerControl = provider.ToControl();
            Control consumerControl = consumer.ToControl();

            if (providerConnectionPoint.ControlType != providerControl.GetType()) {
                throw new ArgumentException(SR.GetString(SR.WebPartManager_InvalidConnectionPoint), "providerConnectionPoint");
            }
            if (consumerConnectionPoint.ControlType != consumerControl.GetType()) {
                throw new ArgumentException(SR.GetString(SR.WebPartManager_InvalidConnectionPoint), "consumerConnectionPoint");
            }

            if (provider == consumer) {
                if (throwOnError) {
                    throw new InvalidOperationException(SR.GetString(SR.WebPartManager_CantConnectToSelf));
                }
                else {
                    return false;
                }
            }

            if (provider.IsClosed) {
                if (throwOnError) {
                    throw new InvalidOperationException(SR.GetString(SR.WebPartManager_CantConnectClosed, provider.ID));
                }
                else {
                    return false;
                }
            }

            if (consumer.IsClosed) {
                if (throwOnError) {
                    throw new InvalidOperationException(SR.GetString(SR.WebPartManager_CantConnectClosed, consumer.ID));
                }
                else {
                    return false;
                }
            }

            if (!providerConnectionPoint.GetEnabled(providerControl)) {
                if (throwOnError) {
                    throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_DisabledConnectionPoint, providerConnectionPoint.ID, provider.ID));
                }
                else {
                    return false;
                }
            }

            if (!consumerConnectionPoint.GetEnabled(consumerControl)) {
                if (throwOnError) {
                    throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_DisabledConnectionPoint, consumerConnectionPoint.ID, consumer.ID));
                }
                else {
                    return false;
                }
            }

            // Check AllowsMultipleConnections on each ConnectionPoint
            if (!providerConnectionPoint.AllowsMultipleConnections) {
                foreach (WebPartConnection c in Connections) {
                    if (c.Provider == provider && c.ProviderConnectionPoint == providerConnectionPoint) {
                        if (throwOnError) {
                            throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_Duplicate, providerConnectionPoint.ID, provider.ID));
                        }
                        else {
                            return false;
                        }
                    }
                }
            }

            if (!consumerConnectionPoint.AllowsMultipleConnections) {
                foreach (WebPartConnection c in Connections) {
                    if (c.Consumer == consumer && c.ConsumerConnectionPoint == consumerConnectionPoint) {
                        if (throwOnError) {
                            throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_Duplicate, consumerConnectionPoint.ID, consumer.ID));
                        }
                        else {
                            return false;
                        }
                    }
                }
            }

            if (transformer == null) {
                if (providerConnectionPoint.InterfaceType != consumerConnectionPoint.InterfaceType) {
                    if (throwOnError) {
                        throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_NoCommonInterface,
                            new string[] {providerConnectionPoint.DisplayName, provider.ID,
                                consumerConnectionPoint.DisplayName, consumer.ID}));
                    }
                    else {
                        return false;
                    }
                }

                ConnectionInterfaceCollection secondaryInterfaces = providerConnectionPoint.GetSecondaryInterfaces(providerControl);
                if (!consumerConnectionPoint.SupportsConnection(consumerControl, secondaryInterfaces)) {
                    if (throwOnError) {
                        throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_IncompatibleSecondaryInterfaces, new string[] {
                                consumerConnectionPoint.DisplayName, consumer.ID,
                                providerConnectionPoint.DisplayName, provider.ID}));
                    }
                    else {
                        return false;
                    }
                }
            }
            else {
                Type transformerType = transformer.GetType();

                if (!AvailableTransformers.Contains(transformerType)) {
                    throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_TransformerNotAvailable, transformerType.FullName));
                }

                // Check matching interfaces on connection points and transformer attribute.
                // Note that we require the connection interfaces to match exactly.  We do not match
                // a derived interface type.  This is because we want to simplify the interface matching
                // algorithm when transformers are involved.  If we allowed derived interfaces to match,
                // then we would to take into account the "closest" match if multiple transformers
                // have compatible interfaces.
                Type transformerConsumerType = WebPartTransformerAttribute.GetConsumerType(transformerType);
                Type transformerProviderType = WebPartTransformerAttribute.GetProviderType(transformerType);
                if (providerConnectionPoint.InterfaceType != transformerConsumerType) {
                    if (throwOnError) {
                        throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_IncompatibleProviderTransformer,
                            providerConnectionPoint.DisplayName, provider.ID, transformerType.FullName));
                    }
                    else {
                        return false;
                    }
                }
                if (transformerProviderType != consumerConnectionPoint.InterfaceType) {
                    if (throwOnError) {
                        throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_IncompatibleConsumerTransformer,
                            transformerType.FullName, consumerConnectionPoint.DisplayName, consumer.ID));
                    }
                    else {
                        return false;
                    }
                }

                // A transformer never provides any secondary interfaces
                if (!consumerConnectionPoint.SupportsConnection(consumerControl, ConnectionInterfaceCollection.Empty)) {
                    if (throwOnError) {
                        throw new InvalidOperationException(SR.GetString(SR.WebPartConnection_ConsumerRequiresSecondaryInterfaces,
                            consumerConnectionPoint.DisplayName, consumer.ID));
                    }
                    else {
                        return false;
                    }
                }

            }

            return true;
        }
Пример #4
0
        internal void Activate()
        {
            // This method should only be called on WebPartConnections in the WebPartManager, so
            // _webPartManager should never be null.
            Debug.Assert(_webPartManager != null);

            Transformers.SetReadOnly();

            WebPart providerWebPart = Provider;

            // Cannot be null because Activate() is only called on valid Connections
            Debug.Assert(providerWebPart != null);

            WebPart consumerWebPart = Consumer;

            // Cannot be null because Activate() is only called on valid Connections
            Debug.Assert(consumerWebPart != null);

            Control providerControl = providerWebPart.ToControl();
            Control consumerControl = consumerWebPart.ToControl();

            ProviderConnectionPoint providerConnectionPoint = ProviderConnectionPoint;

            // Cannot be null because Activate() is only called on valid Connections
            Debug.Assert(providerConnectionPoint != null);

            if (!providerConnectionPoint.GetEnabled(providerControl))
            {
                consumerWebPart.SetConnectErrorMessage(SR.GetString(SR.WebPartConnection_DisabledConnectionPoint, providerConnectionPoint.DisplayName, providerWebPart.DisplayTitle));
                return;
            }

            ConsumerConnectionPoint consumerConnectionPoint = ConsumerConnectionPoint;

            // Cannot be null because Activate() is only called on valid Connections
            Debug.Assert(consumerConnectionPoint != null);

            if (!consumerConnectionPoint.GetEnabled(consumerControl))
            {
                consumerWebPart.SetConnectErrorMessage(SR.GetString(SR.WebPartConnection_DisabledConnectionPoint, consumerConnectionPoint.DisplayName, consumerWebPart.DisplayTitle));
                return;
            }

            // Do not activate connections involving closed WebParts
            if (!providerWebPart.IsClosed && !consumerWebPart.IsClosed)
            {
                WebPartTransformer transformer = Transformer;
                if (transformer == null)
                {
                    if (providerConnectionPoint.InterfaceType == consumerConnectionPoint.InterfaceType)
                    {
                        ConnectionInterfaceCollection secondaryInterfaces = providerConnectionPoint.GetSecondaryInterfaces(providerControl);
                        if (consumerConnectionPoint.SupportsConnection(consumerControl, secondaryInterfaces))
                        {
                            object dataObject = providerConnectionPoint.GetObject(providerControl);
                            consumerConnectionPoint.SetObject(consumerControl, dataObject);
                            _isActive = true;
                        }
                        else
                        {
                            consumerWebPart.SetConnectErrorMessage(SR.GetString(SR.WebPartConnection_IncompatibleSecondaryInterfaces, new string[] {
                                consumerConnectionPoint.DisplayName, consumerWebPart.DisplayTitle,
                                providerConnectionPoint.DisplayName, providerWebPart.DisplayTitle
                            }));
                        }
                    }
                    else
                    {
                        consumerWebPart.SetConnectErrorMessage(SR.GetString(SR.WebPartConnection_NoCommonInterface, new string[] {
                            providerConnectionPoint.DisplayName, providerWebPart.DisplayTitle,
                            consumerConnectionPoint.DisplayName, consumerWebPart.DisplayTitle
                        }));
                    }
                }
                else
                {
                    Type transformerType = transformer.GetType();

                    if (!_webPartManager.AvailableTransformers.Contains(transformerType))
                    {
                        string errorMessage;
                        if (_webPartManager.Context != null && _webPartManager.Context.IsCustomErrorEnabled)
                        {
                            errorMessage = SR.GetString(SR.WebPartConnection_TransformerNotAvailable);
                        }
                        else
                        {
                            errorMessage = SR.GetString(SR.WebPartConnection_TransformerNotAvailableWithType, transformerType.FullName);
                        }
                        consumerWebPart.SetConnectErrorMessage(errorMessage);

                        //
                    }

                    // Check matching interfaces on connection points and transformer attribute
                    Type transformerConsumerType = WebPartTransformerAttribute.GetConsumerType(transformerType);
                    Type transformerProviderType = WebPartTransformerAttribute.GetProviderType(transformerType);

                    if (providerConnectionPoint.InterfaceType == transformerConsumerType &&
                        transformerProviderType == consumerConnectionPoint.InterfaceType)
                    {
                        // A transformer never provides any secondary interfaces
                        if (consumerConnectionPoint.SupportsConnection(consumerControl, ConnectionInterfaceCollection.Empty))
                        {
                            object dataObject        = providerConnectionPoint.GetObject(providerControl);
                            object transformedObject = transformer.Transform(dataObject);
                            consumerConnectionPoint.SetObject(consumerControl, transformedObject);
                            _isActive = true;
                        }
                        else
                        {
                            consumerWebPart.SetConnectErrorMessage(SR.GetString(SR.WebPartConnection_ConsumerRequiresSecondaryInterfaces,
                                                                                consumerConnectionPoint.DisplayName, consumerWebPart.DisplayTitle));
                        }
                    }
                    else if (providerConnectionPoint.InterfaceType != transformerConsumerType)
                    {
                        string errorMessage;
                        if (_webPartManager.Context != null && _webPartManager.Context.IsCustomErrorEnabled)
                        {
                            errorMessage = SR.GetString(SR.WebPartConnection_IncompatibleProviderTransformer,
                                                        providerConnectionPoint.DisplayName, providerWebPart.DisplayTitle);
                        }
                        else
                        {
                            errorMessage = SR.GetString(SR.WebPartConnection_IncompatibleProviderTransformerWithType,
                                                        providerConnectionPoint.DisplayName, providerWebPart.DisplayTitle, transformerType.FullName);
                        }
                        consumerWebPart.SetConnectErrorMessage(errorMessage);
                    }
                    else
                    {
                        string errorMessage;
                        if (_webPartManager.Context != null && _webPartManager.Context.IsCustomErrorEnabled)
                        {
                            errorMessage = SR.GetString(SR.WebPartConnection_IncompatibleConsumerTransformer,
                                                        consumerConnectionPoint.DisplayName, consumerWebPart.DisplayTitle);
                        }
                        else
                        {
                            errorMessage = SR.GetString(SR.WebPartConnection_IncompatibleConsumerTransformerWithType,
                                                        transformerType.FullName, consumerConnectionPoint.DisplayName, consumerWebPart.DisplayTitle);
                        }
                        consumerWebPart.SetConnectErrorMessage(errorMessage);
                    }
                }
            }
        }