private void MovePropertyDown()
        {
            foreach (object selectedObject in this.CurrentSelection)
            {
                AttributeProperty attribute = selectedObject as AttributeProperty;
                if (attribute != null)
                {
                    LinkedElementCollection <AttributeProperty> attributes = attribute.ConfigurationElement.AttributeProperties;
                    int curIndex = attributes.IndexOf(attribute);
                    if (curIndex == attributes.Count - 1)
                    {
                        return;
                    }
                }
                ElementProperty element = selectedObject as ElementProperty;
                if (element != null)
                {
                    LinkedElementCollection <ElementProperty> elements = element.ConfigurationElement.ElementProperties;
                    int curIndex = elements.IndexOf(element);
                    if (curIndex == elements.Count - 1)
                    {
                        return;
                    }
                }
            }

            using (Transaction transaction = ((ModelElement)this.SingleDocumentSelection).Store.TransactionManager.BeginTransaction())
            {
                try
                {
                    foreach (object selectedObject in this.CurrentSelection)
                    {
                        AttributeProperty attribute = selectedObject as AttributeProperty;
                        if (attribute != null)
                        {
                            LinkedElementCollection <AttributeProperty> attributes = attribute.ConfigurationElement.AttributeProperties;
                            int curIndex = attributes.IndexOf(attribute);

                            attributes.Move(attribute, curIndex + 1);
                        }
                        ElementProperty element = selectedObject as ElementProperty;
                        if (element != null)
                        {
                            LinkedElementCollection <ElementProperty> elements = element.ConfigurationElement.ElementProperties;
                            int curIndex = elements.IndexOf(element);

                            elements.Move(element, curIndex + 1);
                        }
                    }
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
                transaction.Commit();
            }
        }
Esempio n. 2
0
        private void GenerateProperty(ConfigurationElement element, CodeTypeDeclaration elementClass, ConfigurationProperty property)
        {
            CodeAttributeDeclarationCollection customAttributes = new CodeAttributeDeclarationCollection();

            foreach (Attribute customAttribute in property.Attributes)
            {
                CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(GlobalReference(customAttribute.Name));
                foreach (AttributeParameter parameter in customAttribute.Parameters)
                {
                    if (!string.IsNullOrEmpty(parameter.Name))
                    {
                        attribute.Arguments.Add(
                            new CodeAttributeArgument(parameter.Name, new CodeSnippetExpression(parameter.Value)));
                    }
                    else
                    {
                        attribute.Arguments.Add(new CodeAttributeArgument(new CodeSnippetExpression(parameter.Value)));
                    }
                }
                customAttributes.Add(attribute);
            }

            string xmlPropertyName = string.Format("{0}PropertyName", property.Name);

            // Property's XML name field
            CodeMemberField xmlPropertyNameField = new CodeMemberField(_string, xmlPropertyName);

            elementClass.Members.Add(xmlPropertyNameField);
            xmlPropertyNameField.StartDirectives.Add(Region(string.Format("{0} Property", property.Name)));
            xmlPropertyNameField.Comments.Add(DocComment("<summary>"));
            xmlPropertyNameField.Comments.Add(DocComment(string.Format("The XML name of the <see cref=\"{0}\"/> property.", property.Name)));
            xmlPropertyNameField.Comments.Add(DocComment("</summary>"));
            xmlPropertyNameField.CustomAttributes.Add(_generatedCodeAttribute);
            xmlPropertyNameField.Attributes     = MemberAttributes.Assembly | MemberAttributes.Const;
            xmlPropertyNameField.InitExpression = new CodePrimitiveExpression(property.XmlName);

            // Property's property
            CodeMemberProperty propertyProperty = new CodeMemberProperty();

            elementClass.Members.Add(propertyProperty);

            // Set modifiers, type and name
            propertyProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            propertyProperty.Type       = (property is ElementProperty) ? GlobalSelfReference(property.TypeName) : GlobalReference(property.TypeName);
            propertyProperty.Name       = property.Name;

            propertyProperty.Comments.Add(DocComment("<summary>"));
            propertyProperty.Comments.Add(DocComment(property.DocumentationPropertyText));
            propertyProperty.Comments.Add(DocComment("</summary>"));
            propertyProperty.CustomAttributes.Add(_generatedCodeAttribute);
            propertyProperty.CustomAttributes.AddRange(customAttributes);

            // Add conditional metadata attributes
            if (!string.IsNullOrEmpty(property.DocumentationText))
            {
                propertyProperty.CustomAttributes.Add(new CodeAttributeDeclaration(GlobalReference(typeof(System.ComponentModel.DescriptionAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(property.DocumentationText))));
            }
            if (!property.Browsable)
            {
                propertyProperty.CustomAttributes.Add(new CodeAttributeDeclaration(GlobalReference(typeof(System.ComponentModel.BrowsableAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(false))));
            }
            if (property.IsReadOnly)
            {
                propertyProperty.CustomAttributes.Add(new CodeAttributeDeclaration(GlobalReference(typeof(System.ComponentModel.ReadOnlyAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(true))));
            }
            if (!string.IsNullOrEmpty(property.Category))
            {
                propertyProperty.CustomAttributes.Add(new CodeAttributeDeclaration(GlobalReference(typeof(System.ComponentModel.CategoryAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(property.Category))));
            }
            if (!string.IsNullOrEmpty(property.DisplayName))
            {
                propertyProperty.CustomAttributes.Add(new CodeAttributeDeclaration(GlobalReference(typeof(System.ComponentModel.DisplayNameAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(property.DisplayName))));
            }
            if (property is AttributeProperty)
            {
                AttributeProperty attributeProperty = (AttributeProperty)property;
                if (attributeProperty.CustomEditor == CustomEditors.FileNameEditor)
                {
                    propertyProperty.CustomAttributes.Add(
                        new CodeAttributeDeclaration(
                            GlobalReference(typeof(System.ComponentModel.EditorAttribute)),
                            new CodeAttributeArgument(_typeof("System.Windows.Forms.Design.FileNameEditor")),
                            new CodeAttributeArgument(_typeof("System.Drawing.Design.UITypeEditor"))
                            )
                        );
                }
                else if (attributeProperty.CustomEditor == CustomEditors.FolderNameEditor)
                {
                    propertyProperty.CustomAttributes.Add(
                        new CodeAttributeDeclaration(
                            GlobalReference(typeof(System.ComponentModel.EditorAttribute)),
                            new CodeAttributeArgument(_typeof("System.Windows.Forms.Design.FolderNameEditor")),
                            new CodeAttributeArgument(_typeof("System.Drawing.Design.UITypeEditor"))
                            )
                        );
                }
            }

            // Add validator
            if (property.Validator != null)
            {
                GenerateValidator(element, elementClass, propertyProperty, property);
            }

            // Add converter
            if (property.TypeConverter != TypeConverters.None)
            {
                GenerateConverter(propertyProperty, property);
            }

            // Add ConfigurationPropertyAttribute
            CodeAttributeDeclaration configPropertyAttribute = new CodeAttributeDeclaration(GlobalReference(typeof(System.Configuration.ConfigurationPropertyAttribute)),
                                                                                            new CodeAttributeArgument(new CodeFieldReferenceExpression(GlobalSelfReferenceExpression(string.Format("{0}.{1}", element.ActualNamespace, element.Name)), xmlPropertyNameField.Name)),
                                                                                            new CodeAttributeArgument("IsRequired", new CodePrimitiveExpression(property.IsRequired)),
                                                                                            new CodeAttributeArgument("IsKey", new CodePrimitiveExpression(property.IsKey)),
                                                                                            new CodeAttributeArgument("IsDefaultCollection", new CodePrimitiveExpression(property.IsDefaultCollection)));

            if (property is AttributeProperty)
            {
                AttributeProperty attributeProperty = (AttributeProperty)property;
                if (!string.IsNullOrEmpty(attributeProperty.DefaultValue))
                {
                    configPropertyAttribute.Arguments.Add(new CodeAttributeArgument("DefaultValue", new CodeSnippetExpression(attributeProperty.DefaultValue)));
                }
            }
            propertyProperty.CustomAttributes.Add(configPropertyAttribute);

            CodeTypeReference castType;

            if (property is ElementProperty)
            {
                castType = GlobalSelfReference(property.TypeName);
            }
            else
            {
                castType = GlobalReference(property.TypeName);
            }

            // Get method
            propertyProperty.HasGet = true;
            propertyProperty.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeCastExpression(
                        castType,
                        new CodeIndexerExpression(
                            _base,
                            new CodeFieldReferenceExpression(
                                GlobalSelfReferenceExpression(string.Format("{0}.{1}", element.ActualNamespace, element.Name)),
                                xmlPropertyName
                                )
                            )
                        )
                    )
                );

            if (!property.IsReadOnly)
            {
                // Set method
                propertyProperty.HasSet = true;
                propertyProperty.SetStatements.Add(
                    new CodeAssignStatement(
                        new CodeIndexerExpression(
                            _base,
                            new CodeFieldReferenceExpression(
                                GlobalSelfReferenceExpression(string.Format("{0}.{1}", element.ActualNamespace, element.Name)),
                                xmlPropertyName
                                )
                            ),
                        new CodePropertySetValueReferenceExpression()
                        )
                    );
            }

            propertyProperty.EndDirectives.Add(EndRegion());
        }