public void SetName(IComponent comp, string newName)
        {
            // trim any spaces off of the name
            newName = newName.Trim();

            SampleDesignSite cs = (SampleDesignSite)comp.Site;

            if (newName.Equals(cs.Name))
            {
                return;
            }

            // If the rename is only a case change
            if (string.Compare(newName, cs.Name, true) != 0)
            {
                Host.CheckName(newName);
            }

            // Find the name property provided by us.
            //
            ((IComponentChangeService)Host).OnComponentChanging(comp, TypeDescriptor.GetProperties(comp, designerNameAttribute)["Name"]);

            Host.Sites.Remove(cs.Name);
            Host.Sites[newName] = cs;

            string oldName = cs.Name;

            cs.SetName(newName);

            Host.OnComponentRename(new ComponentRenameEventArgs(comp, oldName, newName));
            ((IComponentChangeService)Host).OnComponentChanged(comp, TypeDescriptor.GetProperties(comp, designerNameAttribute)["Name"], oldName, newName);
        }
        public IComponent CreateComponent(System.Type componentClass, string name)
        {
            object     obj  = null;
            IComponent comp = null;

            this.newComponentSite = new SampleDesignSite(this, name);
            try
            {
                try
                {
                    object[]      args     = new object[] { this };
                    System.Type[] argTypes = new System.Type[] { typeof(IContainer) };
                    obj = this.CreateObject(componentClass, args, argTypes, false);
                }
                catch (Exception)
                {
                }
                if (null == obj)
                {
                    obj = this.CreateObject(componentClass, null, null, false);
                }
                comp = obj as Component;
                if (comp == null)
                {
                    Exception ex = new Exception("The class is not a component " + componentClass.FullName);
                    throw ex;
                }
                SampleDesignSite site = comp.Site as SampleDesignSite;
                if (site == null)
                {
                    ((IContainer)this).Add(comp);
                }
                Debug.Assert(this.newComponentSite == null, "add didn't use newComponentSite");
            }
            catch (Exception)
            {
                if (comp != null)
                {
                    try
                    {
                        this.DestroyComponent(comp);
                    }
                    catch (Exception)
                    {
                    }
                }
                throw;
            }
            return(comp);
        }
        public void SetName(IComponent comp, string newName)
        {
            newName = newName.Trim();
            SampleDesignSite cs = (SampleDesignSite)comp.Site;

            if (!newName.Equals(cs.Name))
            {
                if (string.Compare(newName, cs.Name, true) != 0)
                {
                    this.Host.CheckName(newName);
                }
                ((IComponentChangeService)this.Host).OnComponentChanging(comp, TypeDescriptor.GetProperties(comp, designerNameAttribute)["Name"]);
                this.Host.Sites.Remove(cs.Name);
                this.Host.Sites[newName] = cs;
                string oldName = cs.Name;
                cs.SetName(newName);
                this.Host.OnComponentRename(new ComponentRenameEventArgs(comp, oldName, newName));
                ((IComponentChangeService)this.Host).OnComponentChanged(comp, TypeDescriptor.GetProperties(comp, designerNameAttribute)["Name"], oldName, newName);
            }
        }
 private void Remove(IComponent component)
 {
     if (component != null)
     {
         ISite site = component.Site;
         if ((this.sites.ContainsValue(site) && ((site != null) && (site.Container == this))) && (site is SampleDesignSite))
         {
             ComponentEventArgs ce = new ComponentEventArgs(component);
             this.OnComponentRemoving(ce);
             SampleDesignSite csite = (SampleDesignSite)site;
             if ((csite.Component != this.rootComponent) && (component is IExtenderProvider))
             {
                 ((IExtenderProviderService)this).RemoveExtenderProvider((IExtenderProvider)component);
             }
             IDesigner designer = (IDesigner)this.designerTable[component];
             if (designer != null)
             {
                 designer.Dispose();
             }
             this.designerTable.Remove(component);
             this.sites.Remove(csite.Name);
             if (this.components != null)
             {
                 this.components.Remove(site);
             }
             try
             {
                 this.OnComponentRemoved(ce);
             }
             catch (Exception)
             {
             }
             component.Site = null;
         }
     }
 }
        public IComponent CreateComponent(Type componentClass, string name) {

            object obj = null;
            IComponent comp = null;

            // Create the site we are going to use here so that our Container.Add implementation 
            // can pick it up - need to do this here because name is not passed to Container.Add
            // by Foo(IContainer) constructor below.
            newComponentSite = new SampleDesignSite(this,name);

            // Create the Component - there are 2 possible ways to do this:
            // Foo(IComponent) or Foo().

            try {
                // See if we can create the component using an IContainer constructor...
                try {
                    object[] args = new object[] {this};
                    Type[] argTypes = new Type[] {typeof(IContainer)};
                    obj = CreateObject(componentClass, args, argTypes, false);
                }
                catch (Exception) {
                }

                // If it failed, try to create it with a default constructor
                if (null == obj) {
                    obj = CreateObject(componentClass, null, null, false);
                }

                //Make sure we have a component
                comp = obj as Component ;
                if (comp == null) {
                    Exception ex = new Exception("The class is not a component " + componentClass.FullName);
                    throw ex;
                }

                // If we didn't have a constructor that took a Container or the site is not 
                // a SampleDesignSite the we will do the Container.Add() work 
                SampleDesignSite site = comp.Site as SampleDesignSite;
                if (site == null) {
                    ((IContainer)this).Add(comp);
                }

                // At this point, our add call should have used the new site we gave it (if there was
                // one), and nulled out the holder pointer.
                Debug.Assert(newComponentSite == null, "add didn't use newComponentSite");
            }
            catch (Exception) {
                if (comp != null) {
                    try {
                        DestroyComponent(comp);
                    }
                    catch (Exception) {
                    }
                }

                throw;
            }
            if (comp != null)
            {
                AddDesignedComponent(comp);
            }
            return comp;
        }
        void IContainer.Add(IComponent component, string name) {
            
            if (null == component) return;

            if (rootComponent != null) {
                // Compare to the class the root component represents, not it's actual class!
                if (String.Compare(component.GetType().FullName, rootComponentClassName, true) == 0) {
                    Exception ex = new Exception("Can't add a component to itself " + component.GetType().FullName);
                    throw ex;
                }
            }

            ISite site = component.Site;

            // If the component is already added to us, all we have to do is check
            // the name.  If the name is different we rename it, otherwise we do
            // nothing.
            if (site != null && site.Container == this) {
                if (name != null && !name.Equals(site.Name)) {
                    CheckName(name);
                    site.Name = name;
                }
                return;
            }

            // Check to see if someone has already configured a site for us.  If so,
            // use it.  Otherwise, fabricate a new site.
            // newComponentSite is created by CreateComponent.
            SampleDesignSite newSite = newComponentSite;
            newComponentSite = null;

            if (newSite != null && name == null) {
                name = newSite.Name;
            }

            // Make sure the name is valid.
            if (name != null) {
                CheckName(name);
            }

            ComponentEventArgs ce = new ComponentEventArgs(component);
            OnComponentAdding(ce);

            // Remove this component from its current site
            if (site != null) site.Container.Remove(component);

            if (newSite == null) {
                newSite = new SampleDesignSite(this, name);
            }

            // And set the relationship between this site and it's component.  If the
            // site has no name at this point, it will fabricate one.
            newSite.SetComponent(component);

            // If we were given a site, the name we're given should always be null,
            // or at least be the same name as that stored in the new site.
            Debug.Assert(name == null || name.Equals(newSite.Name), "Name should match the one in newComponentSite");
            
            if (component is IExtenderProvider &&
                !TypeDescriptor.GetAttributes(component).Contains(InheritanceAttribute.InheritedReadOnly)) {
                ((IExtenderProviderService)this).AddExtenderProvider((IExtenderProvider)component);
            }

            // And establish the component/site relationship
            sites[newSite.Name] = newSite;
            component.Site = newSite;
            if (components != null) {
                components.Add(newSite);
            }

            try {

                CreateComponentDesigner(component);

                // The component has been added.  Note that it is tempting to move this above the
                // designer because the designer will never need to know that its own component just
                // got added, but this would be bad because the designer is needed to extract
                // shadowed properties from the component.
                //
                OnComponentAdded(ce);
            }
            catch (Exception) {
                // If we're loading, then don't remove the component.  We are about to
                // fail the load anyway here, and we don't want to be firing remove events during
                // a load.
                if (!loadingDesigner) {
                    ((IContainer)this).Remove(component);
                }
                throw;
            }
        }
        // This is called during Dispose and Reload methods to unload the current designer.
        private void UnloadDocument() {

            if (helpService != null && rootDesigner != null) {
                helpService.RemoveContextAttribute("Keyword", "Designer_" + rootDesigner.GetType().FullName);
            }
            
            // Note: Because this can be called during Dispose, we are very anal here
            // about checking for null references.

            // If we can get a selection service, clear the selection...
            // we don't want the property browser browsing disposed components...
            // or components who's designer has been destroyed.
            IServiceProvider sp = (IServiceProvider)this;
            ISelectionService selectionService = (ISelectionService)sp.GetService(typeof(ISelectionService));
            Debug.Assert(selectionService != null, "ISelectionService not found");
            if (selectionService != null) {
                selectionService.SetSelectedComponents(null);
            }

            // Stash off the base designer and component.  We are
            // going to be destroying these and we don't want them
            // to be accidently referenced after they're dead.
            //
            IDesigner rootDesignerHolder = rootDesigner;
            IComponent rootComponentHolder = rootComponent;

            rootDesigner = null;
            rootComponent = null;
            rootComponentClassName = null;

            SampleDesignSite[] siteArray = new SampleDesignSite[sites.Values.Count];
            sites.Values.CopyTo(siteArray, 0);

            // Destroy all designers.  We save the base designer for last.
            //
            IDesigner[] designers = new IDesigner[designerTable.Values.Count];
            designerTable.Values.CopyTo(designers, 0);
            designerTable.Clear();

            // Loading, unloading, it's all the same.  It indicates that you
            // shouldn't dirty or otherwise mess with the buffer.  We also
            // create a transaction here to limit the effects of making
            // so many changes.
            loadingDesigner = true;
            DesignerTransaction trans = CreateTransaction();
            
            try {
                for (int i = 0; i < designers.Length; i++) {
                    if (designers[i] != rootDesignerHolder) {
                        try {
                            designers[i].Dispose();
                        }
                        catch {
                            Debug.Fail("Designer " + designers[i].GetType().Name + " threw an exception during Dispose.");
                        }
                    }
                }
    
                // Now destroy all components.
                for (int i = 0; i < siteArray.Length; i++) {
                    SampleDesignSite site = siteArray[i];
                    IComponent comp = site.Component;
                    if (comp != null && comp != rootComponentHolder) {
                        try {
                            comp.Dispose();
                        }
                        catch {
                            Debug.Fail("Component " + site.Name + " threw during dispose.  Bad component!!");
                        }
                        if (comp.Site != null) {
                            Debug.Fail("Component " + site.Name + " did not remove itself from its container");
                            Remove(comp);
                        }
                    }
                }
    
                // Finally, do the base designer and component.
                //
                if (rootDesignerHolder != null) {
                    try {
                        rootDesignerHolder.Dispose();
                    }
                    catch {
                        Debug.Fail("Designer " + rootDesignerHolder.GetType().Name + " threw an exception during Dispose.");
                    }
                }
    
                if (rootComponentHolder != null) {
                    try {
                        rootComponentHolder.Dispose();
                    }
                    catch {
                        Debug.Fail("Component " + rootComponentHolder.GetType().Name + " threw during dispose.  Bad component!!");
                    }
                    
                    if (rootComponentHolder.Site != null) {
                        Debug.Fail("Component " + rootComponentHolder.Site.Name + " did not remove itself from its container");
                        Remove(rootComponentHolder);
                    }
                }
            
                sites.Clear();
                if (components != null) {
                    components.Clear();
                }
            }
            finally {
                loadingDesigner = false;
                trans.Commit();
            }
            
            // And clear the document window
            //
            if (documentWindow != null) {
                documentWindow.SetDesigner(null);
            }
        }
        private void UnloadDocument()
        {
            if ((this.helpService != null) && (this.rootDesigner != null))
            {
                this.helpService.RemoveContextAttribute("Keyword", "Designer_" + this.rootDesigner.GetType().FullName);
            }
            IServiceProvider  sp = this;
            ISelectionService selectionService = (ISelectionService)sp.GetService(typeof(ISelectionService));

            Debug.Assert(selectionService != null, "ISelectionService not found");
            if (selectionService != null)
            {
                selectionService.SetSelectedComponents(null);
            }
            IDesigner  rootDesignerHolder  = this.rootDesigner;
            IComponent rootComponentHolder = this.rootComponent;

            this.rootDesigner           = null;
            this.rootComponent          = null;
            this.rootComponentClassName = null;
            SampleDesignSite[] siteArray = new SampleDesignSite[this.sites.Values.Count];
            this.sites.Values.CopyTo(siteArray, 0);
            IDesigner[] designers = new IDesigner[this.designerTable.Values.Count];
            this.designerTable.Values.CopyTo(designers, 0);
            this.designerTable.Clear();
            this.loadingDesigner = true;
            DesignerTransaction trans = this.CreateTransaction();

            try
            {
                int i;
                for (i = 0; i < designers.Length; i++)
                {
                    if (designers[i] != rootDesignerHolder)
                    {
                        try
                        {
                            designers[i].Dispose();
                        }
                        catch
                        {
                            Debug.Fail("Designer " + designers[i].GetType().Name + " threw an exception during Dispose.");
                        }
                    }
                }
                for (i = 0; i < siteArray.Length; i++)
                {
                    SampleDesignSite site = siteArray[i];
                    IComponent       comp = site.Component;
                    if ((comp != null) && (comp != rootComponentHolder))
                    {
                        try
                        {
                            comp.Dispose();
                        }
                        catch
                        {
                            Debug.Fail("Component " + site.Name + " threw during dispose.  Bad component!!");
                        }
                        if (comp.Site != null)
                        {
                            Debug.Fail("Component " + site.Name + " did not remove itself from its container");
                            this.Remove(comp);
                        }
                    }
                }
                if (rootDesignerHolder != null)
                {
                    try
                    {
                        rootDesignerHolder.Dispose();
                    }
                    catch
                    {
                        Debug.Fail("Designer " + rootDesignerHolder.GetType().Name + " threw an exception during Dispose.");
                    }
                }
                if (rootComponentHolder != null)
                {
                    try
                    {
                        rootComponentHolder.Dispose();
                    }
                    catch
                    {
                        Debug.Fail("Component " + rootComponentHolder.GetType().Name + " threw during dispose.  Bad component!!");
                    }
                    if (rootComponentHolder.Site != null)
                    {
                        Debug.Fail("Component " + rootComponentHolder.Site.Name + " did not remove itself from its container");
                        this.Remove(rootComponentHolder);
                    }
                }
                this.sites.Clear();
                if (this.components != null)
                {
                    this.components.Clear();
                }
            }
            finally
            {
                this.loadingDesigner = false;
                trans.Commit();
            }
            if (this.documentWindow != null)
            {
                this.documentWindow.SetDesigner(null);
            }
        }
 void IContainer.Add(IComponent component, string name)
 {
     if (null != component)
     {
         if ((this.rootComponent != null) && (string.Compare(component.GetType().FullName, this.rootComponentClassName, true) == 0))
         {
             Exception ex = new Exception("Can't add a component to itself " + component.GetType().FullName);
             throw ex;
         }
         ISite site = component.Site;
         if ((site != null) && (site.Container == this))
         {
             if ((name != null) && !name.Equals(site.Name))
             {
                 try
                 {
                     this.CheckName(name);
                 }
                 catch (Exception)
                 {
                     name      = this.GetNewComponentName(site.GetType());
                     site.Name = name;
                 }
                 site.Name = name;
             }
         }
         else
         {
             SampleDesignSite newSite = this.newComponentSite;
             this.newComponentSite = null;
             if ((newSite != null) && (name == null))
             {
                 name = newSite.Name;
             }
             if ((name != null) && !this.CheckName(name))
             {
             }
             ComponentEventArgs ce = new ComponentEventArgs(component);
             this.OnComponentAdding(ce);
             if (site != null)
             {
                 site.Container.Remove(component);
             }
             if (newSite == null)
             {
                 newSite = new SampleDesignSite(this, name);
             }
             if (this.components[name] != null)
             {
                 name         = this.GetNewComponentName(component.GetType());
                 newSite.Name = name;
             }
             newSite.SetComponent(component);
             Debug.Assert((name == null) || name.Equals(newSite.Name), "Name should match the one in newComponentSite");
             if (!(!(component is IExtenderProvider) || TypeDescriptor.GetAttributes(component).Contains(InheritanceAttribute.InheritedReadOnly)))
             {
                 ((IExtenderProviderService)this).AddExtenderProvider((IExtenderProvider)component);
             }
             this.sites[newSite.Name] = newSite;
             component.Site           = newSite;
             if (this.components != null)
             {
                 this.components.Add(newSite);
             }
             try
             {
                 this.CreateComponentDesigner(component);
                 this.OnComponentAdded(ce);
             }
             catch (Exception)
             {
                 if (!this.loadingDesigner)
                 {
                     ((IContainer)this).Remove(component);
                 }
                 throw;
             }
         }
     }
 }
 void IContainer.Add(IComponent component, string name)
 {
     if (null != component)
     {
         if ((this.rootComponent != null) && (string.Compare(component.GetType().FullName, this.rootComponentClassName, true) == 0))
         {
             Exception ex = new Exception("Can't add a component to itself " + component.GetType().FullName);
             throw ex;
         }
         ISite site = component.Site;
         if ((site != null) && (site.Container == this))
         {
             if ((name != null) && !name.Equals(site.Name))
             {
                 try
                 {
                     this.CheckName(name);
                 }
                 catch (Exception)
                 {
                     name = this.GetNewComponentName(site.GetType());
                     site.Name = name;
                 }
                 site.Name = name;
             }
         }
         else
         {
             SampleDesignSite newSite = this.newComponentSite;
             this.newComponentSite = null;
             if ((newSite != null) && (name == null))
             {
                 name = newSite.Name;
             }
             if ((name != null) && !this.CheckName(name))
             {
             }
             ComponentEventArgs ce = new ComponentEventArgs(component);
             this.OnComponentAdding(ce);
             if (site != null)
             {
                 site.Container.Remove(component);
             }
             if (newSite == null)
             {
                 newSite = new SampleDesignSite(this, name);
             }
             if (this.components[name] != null)
             {
                 name = this.GetNewComponentName(component.GetType());
                 newSite.Name = name;
             }
             newSite.SetComponent(component);
             Debug.Assert((name == null) || name.Equals(newSite.Name), "Name should match the one in newComponentSite");
             if (!(!(component is IExtenderProvider) || TypeDescriptor.GetAttributes(component).Contains(InheritanceAttribute.InheritedReadOnly)))
             {
                 ((IExtenderProviderService) this).AddExtenderProvider((IExtenderProvider) component);
             }
             this.sites[newSite.Name] = newSite;
             component.Site = newSite;
             if (this.components != null)
             {
                 this.components.Add(newSite);
             }
             try
             {
                 this.CreateComponentDesigner(component);
                 this.OnComponentAdded(ce);
             }
             catch (Exception)
             {
                 if (!this.loadingDesigner)
                 {
                     ((IContainer) this).Remove(component);
                 }
                 throw;
             }
         }
     }
 }
 public IComponent CreateComponent(System.Type componentClass, string name)
 {
     object obj = null;
     IComponent comp = null;
     this.newComponentSite = new SampleDesignSite(this, name);
     try
     {
         try
         {
             object[] args = new object[] { this };
             System.Type[] argTypes = new System.Type[] { typeof(IContainer) };
             obj = this.CreateObject(componentClass, args, argTypes, false);
         }
         catch (Exception)
         {
         }
         if (null == obj)
         {
             obj = this.CreateObject(componentClass, null, null, false);
         }
         comp = obj as Component;
         if (comp == null)
         {
             Exception ex = new Exception("The class is not a component " + componentClass.FullName);
             throw ex;
         }
         SampleDesignSite site = comp.Site as SampleDesignSite;
         if (site == null)
         {
             ((IContainer) this).Add(comp);
         }
         Debug.Assert(this.newComponentSite == null, "add didn't use newComponentSite");
     }
     catch (Exception)
     {
         if (comp != null)
         {
             try
             {
                 this.DestroyComponent(comp);
             }
             catch (Exception)
             {
             }
         }
         throw;
     }
     return comp;
 }
 private void UnloadDocument()
 {
     if ((this.helpService != null) && (this.rootDesigner != null))
     {
         this.helpService.RemoveContextAttribute("Keyword", "Designer_" + this.rootDesigner.GetType().FullName);
     }
     IServiceProvider sp = this;
     ISelectionService selectionService = (ISelectionService) sp.GetService(typeof(ISelectionService));
     Debug.Assert(selectionService != null, "ISelectionService not found");
     if (selectionService != null)
     {
         selectionService.SetSelectedComponents(null);
     }
     IDesigner rootDesignerHolder = this.rootDesigner;
     IComponent rootComponentHolder = this.rootComponent;
     this.rootDesigner = null;
     this.rootComponent = null;
     this.rootComponentClassName = null;
     SampleDesignSite[] siteArray = new SampleDesignSite[this.sites.Values.Count];
     this.sites.Values.CopyTo(siteArray, 0);
     IDesigner[] designers = new IDesigner[this.designerTable.Values.Count];
     this.designerTable.Values.CopyTo(designers, 0);
     this.designerTable.Clear();
     this.loadingDesigner = true;
     DesignerTransaction trans = this.CreateTransaction();
     try
     {
         int i;
         for (i = 0; i < designers.Length; i++)
         {
             if (designers[i] != rootDesignerHolder)
             {
                 try
                 {
                     designers[i].Dispose();
                 }
                 catch
                 {
                     Debug.Fail("Designer " + designers[i].GetType().Name + " threw an exception during Dispose.");
                 }
             }
         }
         for (i = 0; i < siteArray.Length; i++)
         {
             SampleDesignSite site = siteArray[i];
             IComponent comp = site.Component;
             if ((comp != null) && (comp != rootComponentHolder))
             {
                 try
                 {
                     comp.Dispose();
                 }
                 catch
                 {
                     Debug.Fail("Component " + site.Name + " threw during dispose.  Bad component!!");
                 }
                 if (comp.Site != null)
                 {
                     Debug.Fail("Component " + site.Name + " did not remove itself from its container");
                     this.Remove(comp);
                 }
             }
         }
         if (rootDesignerHolder != null)
         {
             try
             {
                 rootDesignerHolder.Dispose();
             }
             catch
             {
                 Debug.Fail("Designer " + rootDesignerHolder.GetType().Name + " threw an exception during Dispose.");
             }
         }
         if (rootComponentHolder != null)
         {
             try
             {
                 rootComponentHolder.Dispose();
             }
             catch
             {
                 Debug.Fail("Component " + rootComponentHolder.GetType().Name + " threw during dispose.  Bad component!!");
             }
             if (rootComponentHolder.Site != null)
             {
                 Debug.Fail("Component " + rootComponentHolder.Site.Name + " did not remove itself from its container");
                 this.Remove(rootComponentHolder);
             }
         }
         this.sites.Clear();
         if (this.components != null)
         {
             this.components.Clear();
         }
     }
     finally
     {
         this.loadingDesigner = false;
         trans.Commit();
     }
     if (this.documentWindow != null)
     {
         this.documentWindow.SetDesigner(null);
     }
 }