internal string GetNewComponentName(System.Type compClass)
        {
            IServiceProvider     sp         = this;
            INameCreationService nameCreate = (INameCreationService)sp.GetService(typeof(INameCreationService));

            if (nameCreate != null)
            {
                return(nameCreate.CreateName(this.Container, compClass));
            }
            string        baseName = compClass.Name;
            StringBuilder b        = new StringBuilder(baseName.Length);

            for (int i = 0; i < baseName.Length; i++)
            {
                if (char.IsUpper(baseName[i]) && (((i == 0) || (i == (baseName.Length - 1))) || char.IsUpper(baseName[i + 1])))
                {
                    b.Append(char.ToLower(baseName[i]));
                }
                else
                {
                    b.Append(baseName.Substring(i));
                    break;
                }
            }
            baseName = b.ToString();
            int    idx       = 1;
            string finalName = baseName + idx.ToString();

            while (this.Container.Components[finalName] != null)
            {
                idx++;
                finalName = baseName + idx.ToString();
            }
            return(finalName);
        }
        protected string GetUniqueName(IDesignerSerializationManager manager, object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            string name = manager.GetName(instance);

            if (name == null)
            {
                INameCreationService service = manager.GetService(typeof(INameCreationService)) as INameCreationService;
                name = service.CreateName(null, instance.GetType());
                if (name == null)
                {
                    name = instance.GetType().Name.ToLower();
                }
                manager.SetName(instance, name);
            }
            return(name);
        }
        private string CreateControlName(string prevName, IContainer container, Type dataType, List <EntityCPNode> listAllComps)
        {
            INameCreationService nameService = designerHost.GetService(typeof(INameCreationService)) as INameCreationService;

            bool isValidName = true;

            if (container.Components[prevName] != null)
            {
                isValidName = false;
            }

            if (isValidName)
            {
                return(prevName);
            }
            else
            {
                string currName = nameService.CreateName(container, dataType);
                if (currName == prevName)
                {
                    return(prevName);
                }
                else
                {
                    foreach (EntityCPNode item in listAllComps)
                    {
                        if (item.ControlName == prevName)
                        {
                            item.ControlName = currName;
                        }
                    }
                    return(currName);
                }
            }
        }
        private string CreateListViewGroupName(ListViewGroupCollection lvgCollection)
        {
            string lvgName = "ListViewGroup";
            string resultName;
            INameCreationService ncs       = GetService(typeof(INameCreationService)) as INameCreationService;
            IContainer           container = GetService(typeof(IContainer)) as IContainer;

            if (ncs != null && container != null)
            {
                lvgName = ncs.CreateName(container, typeof(ListViewGroup));
            }

            // strip the digits from the end.
            while (char.IsDigit(lvgName[lvgName.Length - 1]))
            {
                lvgName = lvgName.Substring(0, lvgName.Length - 1);
            }

            int i = 1;

            resultName = lvgName + i.ToString(System.Globalization.CultureInfo.CurrentCulture);

            while (lvgCollection[resultName] != null)
            {
                i++;
                resultName = lvgName + i.ToString(System.Globalization.CultureInfo.CurrentCulture);
            }

            return(resultName);
        }
Exemplo n.º 5
0
 internal void SetComponent(IComponent component)
 {
     this.component = component;
     if (name == null)
     {
         INameCreationService nameService = (INameCreationService)GetService(typeof(INameCreationService));
         name = nameService.CreateName(host.Container, component.GetType());
     }
 }
Exemplo n.º 6
0
        public override DesignerControlDefinition Create(INameCreationService namingService)
        {
            var uniqueName = namingService.CreateName(typeName);

            return(new DesignerControlDefinition
            {
                Control = string.Format(Properties.Snippets.HeaderControlControl, uniqueName),
                Style = string.Format(Properties.Snippets.HeaderControlStyle, uniqueName)
            });
        }
 protected override ISite CreateSite(IComponent component, string name)
 {
     if (name == null)
     {
         INameCreationService nameService = this.GetService(typeof(INameCreationService)) as INameCreationService;
         if (nameService != null)
         {
             name = nameService.CreateName(this, component.GetType());
         }
     }
     return(new DesignModeSite(component, name, this, this));
 }
        private string NameFromText(string text, System.Type itemType, INameCreationService nameCreationService, bool adjustCapitalization)
        {
            string name = null;

            if (text == "-")
            {
                name = "toolStripSeparator";
            }
            else
            {
                string        str2    = itemType.Name;
                StringBuilder builder = new StringBuilder(text.Length + str2.Length);
                bool          flag    = false;
                for (int j = 0; j < text.Length; j++)
                {
                    char c = text[j];
                    if (char.IsLetterOrDigit(c))
                    {
                        if (!flag)
                        {
                            c    = char.ToLower(c, CultureInfo.CurrentCulture);
                            flag = true;
                        }
                        builder.Append(c);
                    }
                }
                builder.Append(str2);
                name = builder.ToString();
                if (adjustCapitalization)
                {
                    string str3 = ToolStripDesigner.NameFromText(null, typeof(ToolStripMenuItem), this._designer.Component.Site);
                    if (!string.IsNullOrEmpty(str3) && char.IsUpper(str3[0]))
                    {
                        name = char.ToUpper(name[0], CultureInfo.InvariantCulture) + name.Substring(1);
                    }
                }
            }
            if (this._host.Container.Components[name] == null)
            {
                if (!nameCreationService.IsValidName(name))
                {
                    return(nameCreationService.CreateName(this._host.Container, itemType));
                }
                return(name);
            }
            string str4 = name;

            for (int i = 1; !nameCreationService.IsValidName(str4); i++)
            {
                str4 = name + i.ToString(CultureInfo.InvariantCulture);
            }
            return(str4);
        }
Exemplo n.º 9
0
 private string GetNewDataSourceName(System.Type dataSourceType)
 {
     if (dataSourceType != null)
     {
         ISite site = this.GetSite();
         if (site != null)
         {
             INameCreationService service = (INameCreationService)site.GetService(typeof(INameCreationService));
             if (service != null)
             {
                 return(service.CreateName(site.Container, dataSourceType));
             }
             return(site.Name + "_DataSource");
         }
     }
     return(string.Empty);
 }
Exemplo n.º 10
0
        private void CreateComponentName(IComponent component, ref string name)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(component);
            PropertyDescriptor           find       = properties.Find("Name", true);

            if (find != null)
            {
                string currentName = find.GetValue(component) as string;
                if (currentName != null && currentName.Length > 0)
                {
                    name = currentName;
                    return;
                }
            }
            if (name == null)
            {
                INameCreationService nameService = (INameCreationService)GetService(typeof(INameCreationService));
                name = nameService.CreateName(this, component.GetType());
            }
        }
        private string CreateListViewGroupName(ListViewGroupCollectionEx lvgCollection)
        {
            string str = "ListViewGroupEx";
            INameCreationService service   = base.GetService(typeof(INameCreationService)) as INameCreationService;
            IContainer           container = base.GetService(typeof(IContainer)) as IContainer;

            if ((service != null) && (container != null))
            {
                str = service.CreateName(container, typeof(ListViewGroupEx));
            }
            while (char.IsDigit(str[str.Length - 1]))
            {
                str = str.Substring(0, str.Length - 1);
            }
            int    num  = 1;
            string str2 = str + num.ToString(System.Globalization.CultureInfo.CurrentCulture);

            while (lvgCollection[str2] != null)
            {
                num++;
                str2 = str + num.ToString(System.Globalization.CultureInfo.CurrentCulture);
            }
            return(str2);
        }
 private string NameFromText(string text, System.Type itemType, INameCreationService nameCreationService, bool adjustCapitalization)
 {
     string name = null;
     if (text == "-")
     {
         name = "toolStripSeparator";
     }
     else
     {
         string str2 = itemType.Name;
         StringBuilder builder = new StringBuilder(text.Length + str2.Length);
         bool flag = false;
         for (int j = 0; j < text.Length; j++)
         {
             char c = text[j];
             if (char.IsLetterOrDigit(c))
             {
                 if (!flag)
                 {
                     c = char.ToLower(c, CultureInfo.CurrentCulture);
                     flag = true;
                 }
                 builder.Append(c);
             }
         }
         builder.Append(str2);
         name = builder.ToString();
         if (adjustCapitalization)
         {
             string str3 = ToolStripDesigner.NameFromText(null, typeof(ToolStripMenuItem), this._designer.Component.Site);
             if (!string.IsNullOrEmpty(str3) && char.IsUpper(str3[0]))
             {
                 name = char.ToUpper(name[0], CultureInfo.InvariantCulture) + name.Substring(1);
             }
         }
     }
     if (this._host.Container.Components[name] == null)
     {
         if (!nameCreationService.IsValidName(name))
         {
             return nameCreationService.CreateName(this._host.Container, itemType);
         }
         return name;
     }
     string str4 = name;
     for (int i = 1; !nameCreationService.IsValidName(str4); i++)
     {
         str4 = name + i.ToString(CultureInfo.InvariantCulture);
     }
     return str4;
 }
Exemplo n.º 13
0
        public void Add(IComponent component, string name)
        {
            // we have to have a component
            if (component == null)
            {
                throw new ArgumentException("component");
            }

            // if we dont have a name, create one
            if (name == null || name.Trim().Length == 0)
            {
                // we need the naming service
                INameCreationService nameCreationService = GetService(typeof(INameCreationService)) as INameCreationService;
                if (nameCreationService == null)
                {
                    throw new Exception("Failed to get INameCreationService.");
                }
                name = nameCreationService.CreateName(this, component.GetType());
            }

            // if we own the component and the name has changed
            // we just rename the component
            if (component.Site != null && component.Site.Container == this &&
                name != null && string.Compare(name, component.Site.Name, true) != 0)
            {
                // name validation and component changing/changed events
                // are fired in the Site.Name property so we don't have
                // to do it here...
                component.Site.Name = name;
                // bail out
                return;
            }
            // create a site for the component
            ISite site = new SiteImpl(component, name, this);

            // create component-site association
            component.Site = site;
            // the container-component association was established when
            // we created the site through site.host.
            // we need to fire adding/added events. create a component event args
            // for the component we are adding.

            ComponentEventArgs evtArgs = new ComponentEventArgs(component);

            // fire off adding event
            if (ComponentAdding != null)
            {
                try
                {
                    ComponentAdding(this, evtArgs);
                }
                catch {}
            }

            // if this is the root component
            IDesigner designer = null;

            if (rootComponent == null)
            {
                // set the root component
                rootComponent = component;
                // create the root designer
                designer = (IRootDesigner)TypeDescriptor.CreateDesigner(component, typeof(IRootDesigner));
            }
            else
            {
                designer = TypeDescriptor.CreateDesigner(component, typeof(IDesigner));
            }
            if (designer != null)
            {
                // add the designer to the list
                designers.Add(component, designer);
                // initialize the designer
                designer.Initialize(component);
            }
            // add to container component list
            sites.Add(site.Name, site);
            // now fire off added event
            if (ComponentAdded != null)
            {
                try
                {
                    ComponentAdded(this, evtArgs);
                }
                catch {}
            }
        }
Exemplo n.º 14
0
        /// <include file='doc\InstanceDescriptorCodeDomSerializer.uex' path='docs/doc[@for="InstanceDescriptorCodeDomSerializer.Serialize"]/*' />
        /// <devdoc>
        ///     Serializes the given object into a CodeDom object.
        /// </devdoc>
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            object expression = null;

            Debug.WriteLineIf(traceSerialization.TraceVerbose, "InstanceDescriptorCodeDomSerializer::Serialize");
            Debug.Indent();

            // To serialize a primitive type, we must assign its value to the current statement.  We get the current
            // statement by asking the context.

            object statement = manager.Context.Current;

            Debug.Assert(statement != null, "Statement is null -- we need a context to be pushed for instance descriptors to serialize");

            Debug.WriteLineIf(traceSerialization.TraceVerbose, "Value: " + value.ToString());
            Debug.WriteLineIf(traceSerialization.TraceVerbose && statement != null, "Statement: " + statement.GetType().Name);

            TypeConverter      converter  = TypeDescriptor.GetConverter(value);
            InstanceDescriptor descriptor = (InstanceDescriptor)converter.ConvertTo(value, typeof(InstanceDescriptor));

            if (descriptor != null)
            {
                expression = SerializeInstanceDescriptor(manager, value, descriptor);
            }
            else
            {
                Debug.WriteLineIf(traceSerialization.TraceError, "*** Converter + " + converter.GetType().Name + " failed to give us an instance descriptor");
            }

            // Ok, we have the "new Foo(arg, arg, arg)" done.  Next, check to see if the instance
            // descriptor has given us a complete representation of the object.  If not, we must
            // go through the additional work of creating a local variable and saving properties.
            //
            if (descriptor != null && !descriptor.IsComplete)
            {
                Debug.WriteLineIf(traceSerialization.TraceVerbose, "Incomplete instance descriptor; creating local variable declaration and serializing properties.");
                CodeStatementCollection statements = (CodeStatementCollection)manager.Context[typeof(CodeStatementCollection)];
                Debug.WriteLineIf(traceSerialization.TraceError && statements == null, "*** No CodeStatementCollection on context stack so we can generate a local variable statement.");

                if (statements != null)
                {
                    MemberInfo mi = descriptor.MemberInfo;
                    Type       targetType;

                    if (mi is PropertyInfo)
                    {
                        targetType = ((PropertyInfo)mi).PropertyType;
                    }
                    else if (mi is MethodInfo)
                    {
                        targetType = ((MethodInfo)mi).ReturnType;
                    }
                    else
                    {
                        targetType = mi.DeclaringType;
                    }

                    string localName = manager.GetName(value);

                    if (localName == null)
                    {
                        string baseName;

                        INameCreationService ns = (INameCreationService)manager.GetService(typeof(INameCreationService));
                        Debug.WriteLineIf(traceSerialization.TraceWarning && (ns == null), "WARNING: Need to generate name for local variable but we have no service.");

                        if (ns != null)
                        {
                            baseName = ns.CreateName(null, targetType);
                        }
                        else
                        {
                            baseName = targetType.Name.ToLower(CultureInfo.InvariantCulture);
                        }

                        int suffixIndex = 1;

                        // Declare this name to the serializer.  If there is already a name defined,
                        // keep trying.
                        //
                        while (true)
                        {
                            localName = baseName + suffixIndex.ToString();

                            if (manager.GetInstance(localName) == null)
                            {
                                manager.SetName(value, localName);
                                break;
                            }

                            suffixIndex++;
                        }
                    }

                    Debug.WriteLineIf(traceSerialization.TraceVerbose, "Named local variable " + localName);

                    CodeVariableDeclarationStatement localStatement = new CodeVariableDeclarationStatement(targetType, localName);
                    localStatement.InitExpression = (CodeExpression)expression;
                    statements.Add(localStatement);

                    expression = new CodeVariableReferenceExpression(localName);

                    // Create a CodeValueExpression to place on the context stack.
                    CodeValueExpression cve = new CodeValueExpression((CodeExpression)expression, value);

                    manager.Context.Push(cve);

                    try {
                        // Now that we have hooked the return expression up and declared the local,
                        // it's time to save off the properties for the object.
                        //
                        SerializeProperties(manager, statements, value, runTimeProperties);
                    }
                    finally {
                        Debug.Assert(manager.Context.Current == cve, "Context stack corrupted");
                        manager.Context.Pop();
                    }
                }
            }

            Debug.Unindent();
            return(expression);
        }
Exemplo n.º 15
0
        /// <summary>
        ///  Computes a name from a text label by removing all spaces and non-alphanumeric characters.
        /// </summary>
        private string NameFromText(string text, Type itemType, INameCreationService nameCreationService, bool adjustCapitalization)
        {
            string baseName;

            // for separators, name them ToolStripSeparator...
            if (text == "-")
            {
                baseName = "toolStripSeparator";
            }
            else
            {
                string nameSuffix = itemType.Name;
                // remove all the non letter and number characters.   Append length of "MenuItem"
                Text.StringBuilder name = new Text.StringBuilder(text.Length + nameSuffix.Length);
                bool firstCharSeen      = false;
                for (int i = 0; i < text.Length; i++)
                {
                    char c = text[i];
                    if (char.IsLetterOrDigit(c))
                    {
                        if (!firstCharSeen)
                        {
                            c             = char.ToLower(c, CultureInfo.CurrentCulture);
                            firstCharSeen = true;
                        }

                        name.Append(c);
                    }
                }

                name.Append(nameSuffix);
                baseName = name.ToString();
                if (adjustCapitalization)
                {
                    string nameOfRandomItem = ToolStripDesigner.NameFromText(null, typeof(ToolStripMenuItem), _designer.Component.Site);
                    if (!string.IsNullOrEmpty(nameOfRandomItem) && char.IsUpper(nameOfRandomItem[0]))
                    {
                        baseName = char.ToUpper(baseName[0], CultureInfo.InvariantCulture) + baseName.Substring(1);
                    }
                }
            }

            // see if this name matches another one in the container..
            object existingComponent = _host.Container.Components[baseName];

            if (existingComponent is null)
            {
                if (!nameCreationService.IsValidName(baseName))
                {
                    // we don't have a name collision but this still isn't a valid name...something is wrong and we can't make a valid identifier out of this so bail.
                    return(nameCreationService.CreateName(_host.Container, itemType));
                }
                else
                {
                    return(baseName);
                }
            }
            else
            {
                // start appending numbers.
                string newName = baseName;
                for (int indexer = 1; !nameCreationService.IsValidName(newName); indexer++)
                {
                    newName = baseName + indexer.ToString(CultureInfo.InvariantCulture);
                }

                return(newName);
            }
        }
Exemplo n.º 16
0
        public void Add(System.ComponentModel.IComponent component, string name)
        {
            IDesigner  designer = null;
            DesignSite site     = null;

            // Check we're not trying to add a null component
            if (component == null)
            {
                throw new ArgumentNullException("Cannot add a null component to the container.");
            }

            // Remove this component from its existing container, if applicable
            if (component.Site != null && component.Site.Container != this)
            {
                component.Site.Container.Remove(component);
            }

            // Make sure we have a name for the component
            if (name == null)
            {
                INameCreationService nameService = (INameCreationService)GetService(typeof(INameCreationService));
                name = nameService.CreateName(this, component.GetType());
            }

            // Make sure there isn't already a component with this name in the container
            if (ContainsName(name))
            {
                throw new ArgumentException("A component with this name already exists in the container.");
            }

            // Give the new component a site
            site = new DesignSite(this, name);
            site.SetComponent(component);
            component.Site = site;

            // Let everyone know there's a component being added
            if (ComponentAdding != null)
            {
                ComponentAdding(this, new ComponentEventArgs(component));
            }

            // Get the designer for this component
            if (components.Count == 0)
            {
                // This is the first component being added and therefore must offer a root designer
                designer      = TypeDescriptor.CreateDesigner(component, typeof(IRootDesigner));
                rootComponent = component;
            }
            else
            {
                designer = TypeDescriptor.CreateDesigner(component, typeof(IDesigner));
            }

            // If we got a designer, initialize it
            if (designer != null)
            {
                designer.Initialize(component);
                designers[component] = designer;
            }
            else
            {
                // This should never happen
                component.Site = null;
                throw new InvalidOperationException("Failed to get designer for this component.");
            }

            // Add to our list of extenderproviders if necessary
            if (component is IExtenderProvider)
            {
                IExtenderProviderService e = (IExtenderProviderService)GetService(typeof(IExtenderProviderService));
                e.AddExtenderProvider((IExtenderProvider)component);
            }

            // Finally we're able to add the component
            components.Add(component.Site.Name, component);
            if (ComponentAdded != null)
            {
                ComponentAdded(this, new ComponentEventArgs(component));
            }
        }
Exemplo n.º 17
0
        public static string BuildNameFromText(
            string text,
            System.Type componentType,
            System.IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                return((string)null);
            }
            INameCreationService service1 = serviceProvider.GetService(typeof(INameCreationService)) as INameCreationService;
            IContainer           service2 = (IContainer)serviceProvider.GetService(typeof(IContainer));

            if (service1 == null || service2 == null)
            {
                return((string)null);
            }
            string name1 = service1.CreateName(service2, componentType);

            if (text == null || text.Length == 0 || text == "-")
            {
                return(name1);
            }
            string        name2         = componentType.Name;
            StringBuilder stringBuilder = new StringBuilder(text.Length + name2.Length);
            bool          flag          = false;

            for (int index = 0; index < text.Length; ++index)
            {
                char c = text[index];
                if (flag)
                {
                    if (char.IsLower(c))
                    {
                        c = char.ToUpper(c, CultureInfo.CurrentCulture);
                    }
                    flag = false;
                }
                if (char.IsLetterOrDigit(c))
                {
                    if (stringBuilder.Length == 0)
                    {
                        if (!char.IsDigit(c))
                        {
                            if (char.IsLower(c) != char.IsLower(name1[0]))
                            {
                                c = !char.IsLower(c) ? char.ToLower(c, CultureInfo.CurrentCulture) : char.ToUpper(c, CultureInfo.CurrentCulture);
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    stringBuilder.Append(c);
                }
                else if (char.IsWhiteSpace(c))
                {
                    flag = true;
                }
            }
            if (stringBuilder.Length == 0)
            {
                return(name1);
            }
            stringBuilder.Append(name2);
            string name3 = stringBuilder.ToString();

            if (service2.Components[name3] == null)
            {
                if (!service1.IsValidName(name3))
                {
                    return(name1);
                }
                return(name3);
            }
            string name4 = name3;
            int    num   = 1;

            while (!service1.IsValidName(name4) || service2.Components[name4] != null)
            {
                name4 = name3 + num.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                ++num;
            }
            return(name4);
        }
Exemplo n.º 18
0
        public void Add(IComponent component, string name)
        {
            IDesigner designer = null;

            //input checks
            if (component == null)
            {
                throw new ArgumentException("Cannot add null component to container", "component");
            }
            if (!(component is Control))
            {
                throw new ArgumentException("This Container only accepts System.Web.UI.Control-derived components", "component");
            }
            if (component.Site != null && component.Site.Container != this)
            {
                component.Site.Container.Remove(component);
            }

            //Check the name and create one if necessary
            INameCreationService nameService = host.GetService(typeof(INameCreationService)) as INameCreationService;

            if (nameService == null)
            {
                throw new Exception("The container must have access to a INameCreationService implementation");
            }

            if (name == null || !nameService.IsValidName(name))
            {
                name = nameService.CreateName(this, component.GetType());
                System.Diagnostics.Trace.WriteLine("Generated name for component: " + name);
            }

            //check we don't already have component with same name
            if (GetComponent(name) != null)
            {
                throw new ArgumentException("There is already a component with this name in the container", "name");
            }

            //we're definately adding it now, so broadcast
            OnComponentAdding(component);

            //get a site and set ID property
            //this way (not PropertyDescriptor.SetValue) won't fire change events
            ((Control)component).ID = name;
            component.Site          = new DesignSite(component, this);

            //Get designer. If first component, designer must be an IRootDesigner
            if (components.Count == 0)
            {
                host.SetRootComponent(component);
                designer = new RootDesigner(component);
            }

            //FIXME: Give Mono some base designers to find! We should never encounter this!
            //else
            //	designer = TypeDescriptor.CreateDesigner (component, typeof(System.ComponentModel.Design.IDesigner));


            if (designer == null)
            {
                //component.Site = null;
                //throw new Exception ("Designer could not be obtained for this component.");
            }
            else
            {
                //track and initialise it
                designers.Add(component, designer);
                designer.Initialize(component);
            }

            //add references to referenceManager, unless root component
            if (components.Count != 1)
            {
                host.WebFormReferenceManager.AddReference(component.GetType());
            }

            //Finally put in container
            components.Add(component);

            //and broadcast completion
            OnComponentAdded(component);
        }