Exemple #1
0
        //Runs when item is clicked.
        //We should be able to avoid loading actual ToolboxItem instances and their
        //associated assemblies until they are activated.
        public override void Activate(object host)
        {
            IDesignerHost desHost = host as IDesignerHost;

            if (desHost == null)
            {
                throw new Exception("This ToolboxItem should not have been shown for this host. System.Drawing.Design.ToolboxItem requires a host of type System.ComponentModel.Design.IDesignerHost");
            }

            //web controls have sample HTML that need to be deserialised
            //TODO: Fix WebControlToolboxItem so we don't have to mess around with type lookups and attributes here
            if ((item is System.Web.UI.Design.WebControlToolboxItem) && host is AspNetEdit.Editor.ComponentModel.DesignerHost)
            {
                AspNetEdit.Editor.ComponentModel.DesignerHost aspDesHost = (AspNetEdit.Editor.ComponentModel.DesignerHost)desHost;

                if (item.AssemblyName != null && item.TypeName != null)
                {
                    //look up and register the type
                    ITypeResolutionService typeRes = (ITypeResolutionService)aspDesHost.GetService(typeof(ITypeResolutionService));
                    typeRes.ReferenceAssembly(item.AssemblyName);
                    Type controlType = typeRes.GetType(item.TypeName, true);

                    //read the WebControlToolboxItem data from the attribute
                    AttributeCollection atts = TypeDescriptor.GetAttributes(controlType);
                    System.Web.UI.ToolboxDataAttribute tda = (System.Web.UI.ToolboxDataAttribute)atts[typeof(System.Web.UI.ToolboxDataAttribute)];

                    //if it's present
                    if (tda != null && tda.Data.Length > 0)
                    {
                        //look up the tag's prefix and insert it into the data
                        System.Web.UI.Design.IWebFormReferenceManager webRef = aspDesHost.GetService(typeof(System.Web.UI.Design.IWebFormReferenceManager)) as System.Web.UI.Design.IWebFormReferenceManager;
                        if (webRef == null)
                        {
                            throw new Exception("Host does not provide an IWebFormReferenceManager");
                        }
                        string aspText = String.Format(tda.Data, webRef.GetTagPrefix(controlType));
                        System.Diagnostics.Trace.WriteLine("Toolbox processing ASP.NET item data: " + aspText);

                        //and add it to the document
                        aspDesHost.RootDocument.DeserializeAndAdd(aspText);
                        return;
                    }
                }
            }

            //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself
            item.CreateComponents(desHost);
        }
        private void handleToolboxNode(ItemToolboxNode node)
        {
            ToolboxItemToolboxNode tiNode = node as ToolboxItemToolboxNode;

            if (tiNode != null)
            {
                //load the type into this process and get the ToolboxItem
                tiNode.Type.Load();
                System.Drawing.Design.ToolboxItem ti = tiNode.GetToolboxItem();

                //web controls have sample HTML that need to be deserialised, in a ToolboxDataAttribute
                //TODO: Fix WebControlToolboxItem and (mono classlib's use of it) so we don't have to mess around with type lookups and attributes here
                if (ti.AssemblyName != null && ti.TypeName != null)
                {
                    //look up and register the type
                    ITypeResolutionService typeRes = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService));
                    typeRes.ReferenceAssembly(ti.AssemblyName);
                    Type controlType = typeRes.GetType(ti.TypeName, true);

                    //read the WebControlToolboxItem data from the attribute
                    AttributeCollection atts = TypeDescriptor.GetAttributes(controlType);

                    System.Web.UI.ToolboxDataAttribute tda = (System.Web.UI.ToolboxDataAttribute)atts[typeof(System.Web.UI.ToolboxDataAttribute)];

                    //if it's present
                    if (tda != null && tda.Data.Length > 0)
                    {
                        //look up the tag's prefix and insert it into the data
                        System.Web.UI.Design.IWebFormReferenceManager webRef = host.GetService(typeof(System.Web.UI.Design.IWebFormReferenceManager)) as System.Web.UI.Design.IWebFormReferenceManager;
                        if (webRef == null)
                        {
                            throw new Exception("Host does not provide an IWebFormReferenceManager");
                        }
                        string aspText = String.Format(tda.Data, webRef.GetTagPrefix(controlType));
                        System.Diagnostics.Trace.WriteLine("Toolbox processing ASP.NET item data: " + aspText);

                        //and add it to the document
                        host.RootDocument.InsertFragment(aspText);
                        return;
                    }
                }

                //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself
                ti.CreateComponents(host);
            }
        }