Example #1
0
        private void ApplyEyedropperSceneElement(SceneElement hitElement)
        {
            PropertyManager   propertyManager  = (PropertyManager)this.ToolBehaviorContext.PropertyManager;
            IPlatform         platform         = this.ActiveDocument.ProjectContext.Platform;
            IPlatformMetadata platformMetadata = (IPlatformMetadata)platform.Metadata;

            this.EnsureEditTransaction();
            foreach (IPropertyId propertyId in PropertyToolBehavior.PropertyList)
            {
                ReferenceStep singleStep = platformMetadata.ResolveProperty(propertyId) as ReferenceStep;
                if (singleStep != null && singleStep.PropertyType.PlatformMetadata == platform.Metadata)
                {
                    PropertyReference propertyReference1 = new PropertyReference(singleStep);
                    PropertyReference propertyReference2 = propertyManager.FilterProperty((SceneNode)hitElement, propertyReference1);
                    if (propertyReference2 != null)
                    {
                        object second        = propertyManager.GetValue(propertyReference2);
                        object computedValue = hitElement.GetComputedValue(propertyReference2);
                        if (computedValue != MixedProperty.Mixed && !PropertyUtilities.Compare(computedValue, second, hitElement.ViewModel.DefaultView))
                        {
                            propertyManager.SetValue(propertyReference2, computedValue);
                        }
                    }
                }
            }
            this.UpdateEditTransaction();
        }
        /// <summary>
        /// Adds a random value to a property.
        /// </summary>
        /// <typeparam name="T">Type to add the property value to.</typeparam>
        /// <param name="obj">Object to add the property value to.</param>
        /// <param name="property">Property to add the value to.</param>
        public static void PopulatePropertyWithRandomValue<T>(T obj, PropertyInfo property)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            else if (property == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (PropertyUtilities.CheckIfPropertyIsList(property) || PropertyUtilities.CheckIfPropertyIsClass(property))
            {
                if (obj.GetType() == property.PropertyType)
                {
                    throw new ArgumentException($"The property {property.Name} is trying to add the type {property.PropertyType}, which is the same type.", nameof(property));
                }

                if (PropertyUtilities.CheckIfPropertyIsReadWrite(property))
                {
                    property.SetValue(obj, Activator.CreateInstance(property.PropertyType));
                }

                PopulateObjectWithRandomValues(property.GetValue(obj));

                return;
            }

            if (!PropertyUtilities.CheckIfPropertyIsReadWrite(property))
            {
                return;
            }

            switch (property.PropertyType)
            {
                case Type t when t == typeof(string):
                    property.SetValue(obj, RandomUtilities.GetRandomString((string)property.GetValue(obj)));
                    break;
                case Type t when t.IsEnum:
                    property.SetValue(obj, RandomUtilities.GetRandomEnum((Enum)property.GetValue(obj)));
                    break;
                case Type t when t == typeof(bool):
                    property.SetValue(obj, RandomUtilities.GetRandomBoolean((bool)property.GetValue(obj)));
                    break;
                case Type t when t == typeof(int):
                    property.SetValue(obj, RandomUtilities.GetRandomInteger((int)property.GetValue(obj)));
                    break;
                case Type t when t == typeof(DateTime):
                    property.SetValue(obj, RandomUtilities.GetRandomDateTime((DateTime)property.GetValue(obj)));
                    break;
                case Type t when t == typeof(double):
                    property.SetValue(obj, RandomUtilities.GetRandomDouble((double)property.GetValue(obj)));
                    break;
                case Type t when t == typeof(decimal):
                    property.SetValue(obj, RandomUtilities.GetRandomDecimal((decimal)property.GetValue(obj)));
                    break;
                default:
                    throw new ArgumentException($"The property {property.Name} uses an unsupported value type of {property.PropertyType} for this method.", nameof(property));
            }
        }
Example #3
0
        /// <summary>
        /// Tests that a property raises the <see cref="PropertyChangedEventHandler"/>.
        /// </summary>
        /// <typeparam name="T">Type that implements <see cref="INotifyPropertyChanged"/>.</typeparam>
        /// <param name="obj">Object with property to test.</param>
        /// <param name="property">Property to test.</param>
        public static void NotifiesPropertyChanged <T>(T obj, PropertyInfo property) where T : INotifyPropertyChanged
        {
            List <string> propertiesChanged = new List <string>();

            obj.PropertyChanged += (sender, args) => propertiesChanged.Add(args.PropertyName);

            int count = new Random().Next(5, 20);

            for (int i = 0; i < count; i++)
            {
                ObjectUtilities.PopulatePropertyWithRandomValue(obj, property);
            }

            Assert.AreEqual(count, propertiesChanged.Count(propertyName => propertyName == property.Name));

            if (PropertyUtilities.CheckIfPropertyIsValue(property))
            {
                for (int i = 0; i < new Random().Next(5, 20); i++)
                {
                    ObjectUtilities.PopulateObjectWithRandomValues(obj);
                    T copiedObject = ObjectUtilities.GetSerializedCopyOfObject(obj);

                    propertiesChanged.Clear();

                    property.SetValue(obj, property.GetValue(copiedObject));

                    Assert.AreEqual(property.GetValue(obj), property.GetValue(copiedObject));
                    Assert.AreEqual(0, propertiesChanged.Count);
                }
            }
        }
        public T DynamicObject(T entity)
        {
            var methodName = ((int)PropertyUtilities.TryGetProperty(entity, "Id") != 0) ?
                             "Update" : "Create";
            var method        = _dynamicObjectService.GetType().GetMethod(methodName);
            var genericMethod = method.MakeGenericMethod(typeof(T));
            var result        = genericMethod.Invoke(_dynamicObjectService, new object[] { entity });

            return((T)result);
        }
        private IList <object> GetPropertyValues(IList <object> objects, string propertyName)
        {
            List <object> values = new List <object>();

            foreach (var o in objects)
            {
                object value = PropertyUtilities.GetPropertyValue(o, propertyName);
                values.Add(value);
            }
            return(values);
        }
        public T Create <T>(T entity) where T : class
        {
            PropertyUtilities.TrySetProperty(entity, "Created", DateTime.Now);
            PropertyUtilities.TrySetProperty(entity, "CreatedBy", 1);
            PropertyUtilities.TrySetProperty(entity, "Modified", DateTime.Now);
            PropertyUtilities.TrySetProperty(entity, "ModifiedBy", 1);

            _context.Set <T>().Add(entity);
            _context.SaveChanges();

            return(entity);
        }
        private void storePartcipantEditField(TextBox control, IList <ParticipantEdit> items, string propertyName)
        {
            if (control.Text == "<Verschiedene>")
            {
                return;
            }

            foreach (var item in items.Cast <ParticipantEdit>())
            {
                PropertyUtilities.SetPropertyValue(item, propertyName, control.Text);
            }
        }
Example #8
0
        /// <summary>
        /// Tests that a specific property of an object changes values.
        /// </summary>
        /// <typeparam name="T">Type of object to test.</typeparam>
        /// <param name="obj">Object with property to test.</param>
        /// <param name="property">Property to test.</param>
        public static void ChangesValue <T>(T obj, PropertyInfo property)
        {
            for (int i = 0; i < new Random().Next(5, 20); i++)
            {
                var randomObject = ObjectUtilities.CreateInstanceWithRandomValues(obj.GetType());
                property.SetValue(obj, property.GetValue(randomObject));
                Assert.AreEqual(property.GetValue(randomObject), property.GetValue(obj));
            }

            if (PropertyUtilities.CheckIfPropertyIsClass(property))
            {
                ChangesValues(property.GetValue(obj));
            }
        }
Example #9
0
        public override object GetValue(PropertyReference propertyReference, PropertyReference.GetValueFlags getValueFlags)
        {
            object second = null;
            bool   flag   = false;

            foreach (SceneNode sceneNode in this.Objects)
            {
                PropertyReference propertyReference1 = SceneNodeObjectSet.FilterProperty(sceneNode, propertyReference);
                if (propertyReference1 != null)
                {
                    object first;
                    if ((getValueFlags & PropertyReference.GetValueFlags.Computed) != PropertyReference.GetValueFlags.Local)
                    {
                        if (SceneNodeObjectSetBase.IsValidForGetComputedValue(sceneNode))
                        {
                            first = sceneNode.GetComputedValue(propertyReference1);
                        }
                        else
                        {
                            SceneNode         ancestor           = (SceneNode)null;
                            PropertyReference propertyReference2 = propertyReference1;
                            first = !this.ShouldWalkParentsForGetValue || !this.FindAncestor(sceneNode, out ancestor, ref propertyReference2, new Predicate <SceneNode>(SceneNodeObjectSetBase.IsValidForGetComputedValue)) ? sceneNode.GetLocalOrDefaultValue(propertyReference1) : ancestor.GetComputedValue(propertyReference2);
                        }
                    }
                    else
                    {
                        first = sceneNode.GetLocalOrDefaultValue(propertyReference1);
                    }
                    if (!flag)
                    {
                        second = first;
                        flag   = true;
                    }
                    else if (!PropertyUtilities.Compare(first, second, sceneNode.ViewModel.DefaultView))
                    {
                        second = MixedProperty.Mixed;
                        break;
                    }
                }
            }
            if (!flag)
            {
                ReferenceStep referenceStep = propertyReference[propertyReference.Count - 1];
                if (this.designerContext.ActiveView != null)
                {
                    second = referenceStep.GetDefaultValue(referenceStep.TargetType);
                }
            }
            return(second);
        }
        private void storePartcipantComboBox(ComboBox control, IList <ParticipantEdit> items, string propertyName)
        {
            if (control.SelectedValue == null)
            {
                return;
            }

            var value = control.SelectedValue;

            foreach (var item in items.Cast <ParticipantEdit>())
            {
                PropertyUtilities.SetPropertyValue(item, propertyName, value);
            }
        }
Example #11
0
        /// <summary>
        /// Populates an object's properties with random values.
        /// </summary>
        /// <typeparam name="T"><see cref="Type"/> of <see cref="object"/> to populate.</typeparam>
        /// <param name="obj"><see cref="object"/> to populate with random values.</param>
        public static void PopulateObjectWithRandomValues<T>(T obj)
        {
            if (typeof(IList).IsAssignableFrom(obj.GetType()))
            {
                PopulateListWithRandomValues((IList)obj);
            }
            else
            {
                // Populates all of the readwrite properties including values, classes and lists with values.
                PopulatePropertiesWithRandomValues(obj, PropertyUtilities.GetListOfReadWriteProperties(obj));

                // populates all of the readonly classes and lists with values.
                PopulatePropertiesWithRandomValues(obj, PropertyUtilities.GetListOfProperties(obj.GetType(), false, true, true, false, true, true));
            }
        }
Example #12
0
 public static void ValidateMandatory(object obj)
 {
     foreach (var field in obj.GetType().GetProperties(PropertyUtilities.PropertiesInCurrentClass))
     {
         bool isMandatory   = field.GetCustomAttributes(typeof(Mandatory), true).Length > 0;
         var  propertyValue = field.GetValue(obj);
         if (isMandatory && (PropertyUtilities.IsPrimitive(propertyValue) || propertyValue == null))
         {
             Assert.IsTrue(propertyValue != null, $"Mandatory value of property '{field.Name}' is null");
         }
         if (!PropertyUtilities.IsPrimitive(propertyValue) && propertyValue != null)
         {
             ValidateMandatory(propertyValue);
         }
     }
 }
        public T Update <T>(T entity) where T : class
        {
            var fieldsToIgnore = new string[] { "Id", "Created", "CreatedBy", "Modified", "ModifiedBy" };
            int id             = (int)PropertyUtilities.TryGetProperty(entity, "Id");
            T   found          = FindById <T>(id);

            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                if (!fieldsToIgnore.Contains(property.Name))
                {
                    PropertyUtilities.TrySetProperty(found, property.Name, property.GetValue(entity));
                }
            }

            PropertyUtilities.TrySetProperty(found, "Modified", DateTime.Now);
            PropertyUtilities.TrySetProperty(found, "ModifiedBy", 1);

            _context.SaveChanges();

            return(found);
        }
Example #14
0
    public static bool CompareArrays(this SerializedProperty property, object[] array, object target)
    {
        if (!property.IsCollection())
        {
            return(false);
        }
        if (property.arraySize != array.Length)
        {
            return(false);
        }
        if (property.arraySize < 1)
        {
            return(true);
        }
        var propertyType  = property.GetArrayElementAtIndex(0).propertyType;
        var propertyArray = PropertyUtilities.SetArrayGenericValue(property, propertyType, target);

        if (propertyArray == null)
        {
            return(false);
        }
        switch (propertyType)
        {
        case SerializedPropertyType.Enum:
        {
            var comparer = new EnumComparer <object>();
            return(new HashSet <object>(propertyArray, comparer).SetEquals(new HashSet <object>(array, comparer)));
        }

        case SerializedPropertyType.Generic:
        {
            var comparer = new GenericComparer <object>();
            return(new HashSet <object>(propertyArray, comparer).SetEquals(new HashSet <object>(array, comparer)));
        }

        default:
            return(new HashSet <object>(propertyArray).SetEquals(array));
        }
    }
Example #15
0
    public static bool CompareArrays(this SerializedProperty property, SerializedProperty other, object target)
    {
        if (!property.IsCollection() || !other.IsCollection())
        {
            return(false);
        }
        if (property.arraySize != other.arraySize)
        {
            return(false);
        }
        if (property.propertyType != other.propertyType)
        {
            return(false);
        }
        if (property.arraySize < 1)
        {
            return(true);
        }

        var propertyType  = property.GetArrayElementAtIndex(0).propertyType;
        var propertyArray = PropertyUtilities.SetArrayGenericValue(property, propertyType, target);

        if (propertyArray == null)
        {
            return(false);
        }
        var otherArray = PropertyUtilities.SetArrayGenericValue(other, propertyType, target);

        if (otherArray == null)
        {
            return(false);
        }

        return(propertyType == SerializedPropertyType.Generic
            ? new HashSet <object>(propertyArray, new GenericComparer <object>()).SetEquals(
                   new HashSet <object>(propertyArray, new GenericComparer <object>()))
            : new HashSet <object>(propertyArray).SetEquals(otherArray));
    }
Example #16
0
 /// <summary>
 /// Test that an object's property values can be changed.
 /// </summary>
 /// <typeparam name="T">Type to test properties.</typeparam>
 /// <param name="obj">Object with properties to test properties.</param>
 public static void ChangesValues <T>(T obj)
 {
     ChangesValues(obj, PropertyUtilities.GetListOfReadWriteProperties(obj));
 }
Example #17
0
 /// <summary>
 /// Tests that all properties within an object raise the <see cref="PropertyChangedEventHandler"/>.
 /// </summary>
 /// <typeparam name="T">Type that implements <see cref="INotifyPropertyChanged"/>.</typeparam>
 /// <param name="obj">Object to test.</param>
 public static void NotifiesPropertiesChanged <T>(T obj) where T : INotifyPropertyChanged
 {
     NotifiesPropertiesChanged(obj, PropertyUtilities.GetListOfReadWriteProperties(obj));
 }
Example #18
0
 internal void ApplyAfterInsertionDefaultsToElements(IList <SceneNode> nodes, SceneNode rootNode)
 {
     foreach (SceneNode node in (IEnumerable <SceneNode>)nodes)
     {
         SceneElement element = node as SceneElement;
         if (element != null)
         {
             string name = element.Name;
             if (name == null)
             {
                 StyleAsset relatedUserThemeAsset = this.GetRelatedUserThemeAsset(node, rootNode);
                 if (relatedUserThemeAsset != null)
                 {
                     DocumentCompositeNode documentCompositeNode = relatedUserThemeAsset.ResourceModel.ValueNode as DocumentCompositeNode;
                     if (documentCompositeNode != null)
                     {
                         name = documentCompositeNode.GetValue <string>(DesignTimeProperties.StyleDefaultContentProperty);
                         double num1 = documentCompositeNode.GetValue <double>(DesignTimeProperties.ExplicitWidthProperty);
                         if (num1 > 0.0)
                         {
                             DefaultTypeInstantiator.SetIfUnset(node, BaseFrameworkElement.WidthProperty, (object)num1);
                         }
                         double num2 = documentCompositeNode.GetValue <double>(DesignTimeProperties.ExplicitHeightProperty);
                         if (num2 > 0.0)
                         {
                             DefaultTypeInstantiator.SetIfUnset(node, BaseFrameworkElement.HeightProperty, (object)num2);
                         }
                     }
                 }
             }
             if (name == null)
             {
                 name = element.TargetType.Name;
             }
             if (element.Name == null && this.ViewModel.DesignerContext.ProjectManager.OptionsModel.NameInteractiveElementsByDefault && Enumerable.FirstOrDefault <ITypeId>((IEnumerable <ITypeId>)DefaultTypeInstantiator.InteractiveElementTypes, (Func <ITypeId, bool>)(i =>
             {
                 IType type = this.ViewModel.ProjectContext.ResolveType(i);
                 if (type != null)
                 {
                     return(type.IsAssignableFrom((ITypeId)element.Type));
                 }
                 return(false);
             })) != null)
             {
                 element.EnsureNamed();
             }
             if (ProjectNeutralTypes.HeaderedContentControl.IsAssignableFrom((ITypeId)node.Type))
             {
                 DefaultTypeInstantiator.SetIfUnset(node, HeaderedControlProperties.HeaderedContentHeaderProperty, (object)name);
             }
             else if (ProjectNeutralTypes.HeaderedItemsControl.IsAssignableFrom((ITypeId)node.Type) && !PlatformTypes.ToolBar.IsAssignableFrom((ITypeId)node.Type))
             {
                 DefaultTypeInstantiator.SetIfUnset(node, HeaderedControlProperties.HeaderedItemsHeaderProperty, (object)name);
             }
             else if (PlatformTypes.ContentControl.IsAssignableFrom((ITypeId)node.Type) && !PlatformTypes.ScrollViewer.IsAssignableFrom((ITypeId)node.Type) && !PlatformTypes.UserControl.IsAssignableFrom((ITypeId)node.Type))
             {
                 if (ProjectNeutralTypes.TabItem.IsAssignableFrom((ITypeId)node.Type) && this.ViewModel.ProjectContext.IsCapabilitySet(PlatformCapability.UseHeaderOnTabItem))
                 {
                     IPropertyId property = (IPropertyId)ProjectNeutralTypes.TabItem.GetMember(MemberType.LocalProperty, "Header", MemberAccessTypes.Public);
                     DefaultTypeInstantiator.SetIfUnset(node, property, (object)name);
                 }
                 else
                 {
                     DefaultTypeInstantiator.SetIfUnset(node, ContentControlElement.ContentProperty, (object)name);
                 }
             }
             else
             {
                 BaseTextElement baseTextElement;
                 if ((baseTextElement = node as BaseTextElement) != null)
                 {
                     if (string.IsNullOrEmpty(baseTextElement.Text.Trim()))
                     {
                         baseTextElement.Text = name;
                     }
                     if (PlatformTypes.TextBox.IsAssignableFrom((ITypeId)node.Type))
                     {
                         DefaultTypeInstantiator.SetAsWpfIfUnset(node, TextBoxElement.TextWrappingProperty, (object)TextWrapping.Wrap);
                     }
                     if (!node.ProjectContext.IsCapabilitySet(PlatformCapability.IsWpf) && PlatformTypes.RichTextBox.IsAssignableFrom((ITypeId)node.Type))
                     {
                         DefaultTypeInstantiator.SetAsWpfIfUnset(node, RichTextBoxElement.TextWrappingProperty, (object)TextWrapping.Wrap);
                     }
                 }
                 else if (PlatformTypes.TextBlock.IsAssignableFrom((ITypeId)node.Type))
                 {
                     DefaultTypeInstantiator.SetIfUnset(node, TextBlockElement.TextProperty, (object)name);
                     DefaultTypeInstantiator.SetAsWpfIfUnset(node, TextBlockElement.TextWrappingProperty, (object)TextWrapping.Wrap);
                 }
                 else if (PlatformTypes.ListView.IsAssignableFrom((ITypeId)node.Type))
                 {
                     GridViewElement gridViewElement = (GridViewElement)node.ViewModel.CreateSceneNode(PlatformTypes.GridView);
                     SceneNode       sceneNode       = node.ViewModel.CreateSceneNode(PlatformTypes.GridViewColumn);
                     gridViewElement.Columns.Add(sceneNode);
                     node.SetValueAsSceneNode(DefaultTypeInstantiator.ListViewViewProperty, (SceneNode)gridViewElement);
                 }
                 else if (PlatformTypes.Border.IsAssignableFrom((ITypeId)node.Type))
                 {
                     DefaultTypeInstantiator.SetAsWpfIfUnset(node, BorderElement.BorderBrushProperty, (object)Brushes.Black);
                     DefaultTypeInstantiator.SetAsWpfIfUnset(node, BorderElement.BorderThicknessProperty, (object)new Thickness(1.0));
                 }
                 else if (typeof(FlowDocumentScrollViewer).IsAssignableFrom(node.TargetType))
                 {
                     DefaultTypeInstantiator.SetIfUnset(node, FlowDocumentScrollViewerElement.DocumentProperty, (object)new FlowDocument((Block) new Paragraph((Inline) new Run(name))));
                 }
                 else if (typeof(Glyphs).IsAssignableFrom(node.TargetType))
                 {
                     DefaultTypeInstantiator.SetIfUnset(node, DefaultTypeInstantiator.GlyphsUnicodeStringProperty, (object)name);
                     DefaultTypeInstantiator.SetIfUnset(node, DefaultTypeInstantiator.GlyphsFillProperty, (object)Brushes.Black);
                     DefaultTypeInstantiator.SetIfUnset(node, DefaultTypeInstantiator.GlyphsFontRenderingSizeEmProperty, (object)12.0);
                 }
                 else if (typeof(Viewport3D).IsAssignableFrom(node.TargetType))
                 {
                     Viewport3DElement viewport3Delement = node as Viewport3DElement;
                     if (viewport3Delement != null)
                     {
                         Camera camera1 = (Camera)Viewport3D.CameraProperty.DefaultMetadata.DefaultValue;
                         Camera camera2 = (Camera)viewport3Delement.GetComputedValue(Viewport3DElement.CameraProperty);
                         if (camera2 == null || PropertyUtilities.Compare((object)camera1, (object)camera2, this.sceneView))
                         {
                             Camera perspectiveCamera = Helper3D.CreateEnclosingPerspectiveCamera(45.0, 1.0, new Rect3D(-1.0, -1.0, -1.0, 2.0, 2.0, 2.0), 1.0);
                             DefaultTypeInstantiator.SetIfUnset((SceneNode)viewport3Delement, Viewport3DElement.CameraProperty, (object)perspectiveCamera);
                         }
                     }
                 }
             }
         }
     }
 }