Exemplo n.º 1
0
        /// <summary>
        /// Method that <see cref="IPropertyBuildContext{TSubject}.AddEntry(string,IValueGetter,Demo.yFiles.Option.DataBinding.IValueSetter)">adds entries</see>
        /// for the <see cref="INode.Layout"/> of a node.
        /// </summary>
        /// <remarks>
        /// This implementation create a <see cref="IPropertyBuildContext{TSubject}.CreateChildContext{TChild}">child context</see>
        /// and adds properties for x,y,width, and height.
        /// </remarks>
        /// <param name="context">The context to use.</param>
        protected virtual void BuildLayoutProperties(IPropertyBuildContext <INode> context)
        {
            IPropertyBuildContext <IMutableRectangle> childContext = context.CreateChildContext <IMutableRectangle>(LayoutPropertyName,
                                                                                                                    delegate {
                return((IMutableRectangle)context.CurrentInstance.Lookup(typeof(IMutableRectangle)));
            },
                                                                                                                    delegate {
                //we work directly on the mutable rectangle
            }, AssignmentPolicy.ModifyInstance);

            childContext.AddEntry <double>(LayoutXName, delegate { return(childContext.CurrentInstance.X); },
                                           delegate(double value) {
                childContext.CurrentInstance.X = value;
            });
            childContext.AddEntry <double>(LayoutYName, delegate { return(childContext.CurrentInstance.Y); },
                                           delegate(double value) {
                childContext.CurrentInstance.Y = value;
            });
            childContext.AddEntry <double>(LayoutWidthName, delegate { return(childContext.CurrentInstance.Width); },
                                           delegate(double value) {
                childContext.CurrentInstance.Width = value;
            });
            childContext.AddEntry <double>(LayoutHeightName, delegate { return(childContext.CurrentInstance.Height); },
                                           delegate(double value) {
                childContext.CurrentInstance.Height = value;
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds the properties for the labels's <see cref="ILabelModel"/> type.
        /// </summary>
        protected virtual void BuildModelProperties(IPropertyBuildContext <ILabel> context)
        {
            ValueGetterDelegate <Type> labelModelGetter = new ValueGetterDelegate <Type>(
                delegate {
                var type = context.CurrentInstance.LayoutParameter.Model.GetType();
                while (!type.IsPublic)
                {
                    type = type.BaseType;
                }
                return(type);
            });
            ValueSetterDelegate <Type> labelModelSetter = new ValueSetterDelegate <Type>(
                delegate(Type value) {
                IGraph graph = context.Lookup(typeof(IGraph)) as IGraph;
                if (graph != null)
                {
                    ILabelModel model = Activator.CreateInstance(value) as ILabelModel;
                    if (model != null)
                    {
                        ILabelModelParameterFinder finder =
                            model.Lookup(typeof(ILabelModelParameterFinder)) as ILabelModelParameterFinder;
                        ILabelModelParameter parameter;
                        ILabel subject = context.CurrentInstance;
                        if (finder != null)
                        {
                            parameter = finder.FindBestParameter(subject, model, subject.GetLayout());
                        }
                        else
                        {
                            parameter = model.CreateDefaultParameter();
                        }
                        graph.SetLabelLayoutParameter(subject, parameter);
                    }
                }
            });
            ILabel currentLabel;

            currentLabel = context.CurrentInstance;
            if (currentLabel == null)
            {
                return;
            }
            if (currentLabel.Owner is IEdge)
            {
                context.AddEntry(EdgeLabelModelProperty,
                                 labelModelGetter,
                                 labelModelSetter, null);
            }

            if (currentLabel.Owner is INode)
            {
                context.AddEntry(NodeLabelModelProperty, labelModelGetter, labelModelSetter, null);
            }

            if (currentLabel.Owner is IPort)
            {
                context.AddEntry(PortLabelModelProperty, labelModelGetter, labelModelSetter, null);
            }
        }
Exemplo n.º 3
0
        protected override void BuildPropertyMapImpl(IPropertyBuildContext <StringFormat> context)
        {
            context.AddEntry(DirectionProperty,
                             new DelegateGetter <object>(delegate {
                StringFormat format = context.CurrentInstance;
                if ((format.FormatFlags & StringFormatFlags.DirectionRightToLeft) ==
                    StringFormatFlags.DirectionRightToLeft)
                {
                    return(StringFormatFlags.DirectionRightToLeft);
                }
                if ((format.FormatFlags & StringFormatFlags.DirectionVertical) ==
                    StringFormatFlags.DirectionVertical)
                {
                    return(StringFormatFlags.DirectionVertical);
                }
                return(null);
            }),
                             new DelegateSetter <object>(
                                 delegate(object value) {
                if (value == null)
                {
                    context.CurrentInstance.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
                    context.CurrentInstance.FormatFlags &= ~StringFormatFlags.DirectionVertical;
                }
                else if (value is StringFormatFlags)
                {
                    if ((StringFormatFlags)value == StringFormatFlags.DirectionRightToLeft)
                    {
                        context.CurrentInstance.FormatFlags &= ~StringFormatFlags.DirectionVertical;
                        context.CurrentInstance.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                    }
                    else if ((StringFormatFlags)value == StringFormatFlags.DirectionVertical)
                    {
                        context.CurrentInstance.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
                        context.CurrentInstance.FormatFlags |= StringFormatFlags.DirectionVertical;
                    }
                }
            }));
            context.AddEntry <StringAlignment>(AlignmentProperty, delegate { return(context.CurrentInstance.Alignment); },
                                               delegate(StringAlignment value) { context.CurrentInstance.Alignment = value; });

            context.AddEntry <StringAlignment>(LineAlignmentProperty,
                                               delegate { return(context.CurrentInstance.LineAlignment); },
                                               delegate(StringAlignment value) { context.CurrentInstance.LineAlignment = value; });
            context.AddEntry <StringTrimming>(TrimmingProperty, delegate { return(context.CurrentInstance.Trimming); },
                                              delegate(StringTrimming value) { context.CurrentInstance.Trimming = value; });
        }
Exemplo n.º 4
0
 /// <summary>
 /// Builds the property for the labels's <see cref="ILabel.Text"/>.
 /// </summary>
 protected virtual void BuildLabelTextProperty(IPropertyBuildContext <ILabel> context)
 {
     context.AddEntry(TextProperty
                      , new ValueGetterDelegate <string>(delegate { return(context.CurrentInstance.Text); })
                      , new ValueSetterDelegate <string>(delegate(string value) {
         IGraph graph = context.Lookup(typeof(IGraph)) as IGraph;
         if (graph != null)
         {
             graph.SetLabelText(context.CurrentInstance, value);
         }
     }), null);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Builds the properties for the labels's <see cref="ILabel.PreferredSize"/>.
 /// </summary>
 protected virtual void BuildPreferredSizeProperties(IPropertyBuildContext <ILabel> context)
 {
     context.AddEntry(PreferredSizeProperty,
                      new ValueGetterDelegate <SizeD>(delegate() {
         return(context.CurrentInstance.PreferredSize);
     }),
                      new ValueSetterDelegate <SizeD>(delegate(SizeD value) {
         IGraph graph = context.Lookup(typeof(IGraph)) as IGraph;
         if (graph != null)
         {
             graph.SetLabelPreferredSize(context.CurrentInstance, value);
         }
     }), null);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Builds the property for the port's <see cref="IPort.Style"/>.
 /// </summary>
 protected virtual void BuildPortStylesProperty(IPropertyBuildContext <IPort> context)
 {
     context.AddEntry(PortStyleProperty,
                      new ValueGetterDelegate <Type>(delegate {
         var type = context.CurrentInstance.Style.GetType();
         while (!type.IsPublic)
         {
             type = type.BaseType;
         }
         return(type);
     })
                      , new ValueSetterDelegate <Type>(delegate(Type value) {
         IGraph graph     = context.Lookup(typeof(IGraph)) as IGraph;
         IPortStyle style = Activator.CreateInstance(value) as IPortStyle;
         if (graph != null && style != null)
         {
             graph.SetStyle(context.CurrentInstance, style);
         }
     }), null);
 }
 public IPropertyItem AddEntry(string virtualPropertyName, IValueGetter getter, IValueSetter setter, IEqualityComparer comparer)
 {
     return(childContext.AddEntry(virtualPropertyName, getter, wrapper.decorate(setter), comparer));
 }
        private void AddProperty(IPropertyBuildContext <TSubject> context, MethodInfo buildChildContextMethod, PropertyInfo d)
        {
            PropertyInfo descriptor   = d;
            Type         propertyType = descriptor.PropertyType;

            AssignmentPolicyAttribute assignmentPolicyAttribute = descriptor.GetAttribute <AssignmentPolicyAttribute>();

            NullableAttribute nullableAttribute = descriptor.GetAttribute <NullableAttribute>();
            bool nullable = nullableAttribute == null || nullableAttribute.IsNullable;

            AssignmentPolicy policy;

            if (assignmentPolicyAttribute != null && context.Policy == AssignmentPolicy.Default)
            {
                policy = assignmentPolicyAttribute.Policy;
            }
            else
            {
                policy = context.Policy == AssignmentPolicy.Default ? AssignmentPolicy.CreateNewInstance : context.Policy;
            }

            object childbuilder;
            object currentPropertyValue = descriptor.GetValue(context.CurrentInstance, null);

            if (currentPropertyValue == null && nullable)
            {
                //todo: make this dependent on yet-to-implement nullable attribute
                childbuilder = context.GetPropertyMapBuilder(propertyType, currentPropertyValue);
            }
            else
            {
                childbuilder = context.GetPropertyMapBuilder(currentPropertyValue);
            }

            string name = descriptor.GetDisplayName();

            if (childbuilder != null)
            {
                // Create the SetInstanceDelegate that uses the builders' CurrentInstance for the set operation
                Delegate setMemberInstanceDelegate;
                {
                    Type helperSetInstanceDelegateType = typeof(HelperSetInstanceDelegate <,>)
                                                         .MakeGenericType(propertyType, typeof(TSubject));
                    ConstructorInfo setterConstructorInfo = helperSetInstanceDelegateType
                                                            .GetConstructor(new[] { typeof(PropertyInfo), typeof(IPropertyBuildContext <TSubject>) });
                    object helperSetInstanceDelegateInstance = setterConstructorInfo
                                                               .Invoke(new object[] { descriptor, context });

                    MethodInfo method =
                        helperSetInstanceDelegateType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                 BindingFlags.Instance)[0];
                    setMemberInstanceDelegate =
                        Delegate.CreateDelegate(typeof(SetInstanceDelegate <>).MakeGenericType(propertyType),
                                                helperSetInstanceDelegateInstance, method);
                }

                // Create the SetInstanceDelegate that uses the builders' CurrentInstance for the set operation
                Delegate getMemberInstanceDelegate;
                {
                    Type helperInstanceDelegateType = typeof(HelperGetInstanceDelegate <,>)
                                                      .MakeGenericType(propertyType, typeof(TSubject));
                    ConstructorInfo getterConstructorInfo = helperInstanceDelegateType
                                                            .GetConstructor(new[] { typeof(PropertyInfo), typeof(IPropertyBuildContext <TSubject>) });
                    object helperGetInstanceDelegateInstance = getterConstructorInfo
                                                               .Invoke(new object[] { descriptor, context });

                    MethodInfo method =
                        helperInstanceDelegateType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                              BindingFlags.Instance)[0];
                    getMemberInstanceDelegate =
                        Delegate.CreateDelegate(typeof(GetInstanceDelegate <>).MakeGenericType(propertyType),
                                                helperGetInstanceDelegateInstance, method);
                }
                object childContext = buildChildContextMethod.MakeGenericMethod(propertyType).Invoke(context,
                                                                                                     new object[]
                {
                    name,
                    getMemberInstanceDelegate
                    ,
                    setMemberInstanceDelegate
                    ,
                    policy
                });

                MethodInfo buildPropertyMapMethod =
                    typeof(IPropertyMapBuilder).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                           BindingFlags.Instance)[0];

                buildPropertyMapMethod.MakeGenericMethod(propertyType).Invoke(childbuilder, new[] { childContext });
            }
            else
            {
                context.AddEntry(name, new ReflectionGetter <TSubject>(descriptor, context),
                                 new ReflectionSetter <TSubject>(descriptor, context));
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Method that populates the <see cref="IPropertyMap"/> for edge styles in a given context.
        /// </summary>
        /// <remarks>
        /// This implementation uses <see cref="GetStylePropertyMapBuilder"/> to retrieve the builder for the style.
        /// </remarks>
        /// <param name="context">The context to use for queries.</param>
        protected virtual void BuildStyleProperties(IPropertyBuildContext <INode> context)
        {
            INode node = context.CurrentInstance;

            if (node != null)
            {
                INodeStyle style = node.Style;
                if (style != null)
                {
                    GetInstanceDelegate <INodeStyle> styleGetter = delegate { return(context.CurrentInstance.Style); };
                    SetInstanceDelegate <INodeStyle> styleSetter = delegate(INodeStyle newValue) {
                        IGraph graph       = context.Lookup(typeof(IGraph)) as IGraph;
                        INode  currentNode = context.CurrentInstance;
                        if (graph != null)
                        {
                            graph.SetStyle(currentNode, newValue);
                            var foldingView = graph.Lookup(typeof(IFoldingView)) as IFoldingView;
                            if (foldingView != null)
                            {
                                var masterNode = foldingView.GetMasterItem(currentNode);
                                if (foldingView.IsInFoldingState(currentNode))
                                {
                                    // update non-dummy node
                                    foldingView.Manager.MasterGraph.SetStyle(masterNode, newValue);
                                }
                                else if (foldingView.IsExpanded(currentNode))
                                {
                                    // update dummy node
                                    if (foldingView.Manager.HasFolderNodeState(masterNode))
                                    {
                                        foldingView.Manager.GetFolderNodeState(masterNode).Style = newValue;
                                    }
                                }
                            }
                        }
                    };
                    IPropertyBuildContext <INodeStyle> styleContext =
                        context.CreateChildContext(StylePropertyName, styleGetter, styleSetter, styleAssignmentPolicy);
                    styleContext.AddEntry("Shadow",
                                          new DelegateGetter <bool>(
                                              delegate { return(context.CurrentInstance.Style is ShadowNodeStyleDecorator); },
                                              delegate {
                        //show item only for !PanelNodeStyle...
                        return(!(context.CurrentInstance.Style is PanelNodeStyle));
                    }),
                                          new DelegateSetter <bool>(delegate(bool isShadow) {
                        INodeStyle currentStyle = context.CurrentInstance.Style;
                        if (isShadow && !(currentStyle is ShadowNodeStyleDecorator))
                        {
                            //don't decorate if already decorated...
                            INodeStyle newStyle =
                                new ShadowNodeStyleDecorator(currentStyle);
                            styleContext.SetNewInstance(newStyle);
                        }
                        else
                        {
                            if (!isShadow && currentStyle is ShadowNodeStyleDecorator)
                            {
                                //remove decoration
                                styleContext.SetNewInstance(
                                    ((ShadowNodeStyleDecorator)currentStyle).Wrapped);
                            }
                        }
                    },
                                                                    delegate { return(!(context.CurrentInstance.Style is PanelNodeStyle)); }
                                                                    ));

                    IPropertyMapBuilder propertyBuilder = GetStylePropertyMapBuilder(context, style);
                    if (propertyBuilder != null)
                    {
                        GetInstanceDelegate <INodeStyle> innerStyleGetter = delegate { return(styleContext.CurrentInstance); };
                        SetInstanceDelegate <INodeStyle> innerStyleSetter = delegate(INodeStyle newValue) { styleContext.SetNewInstance(newValue); };
                        propertyBuilder.BuildPropertyMap(styleContext.CreateChildContext(string.Empty, innerStyleGetter, innerStyleSetter, styleAssignmentPolicy));
                    }
                }
            }
        }
Exemplo n.º 10
0
        protected override void BuildPropertyMapImpl(IPropertyBuildContext <Brush> context)
        {
            IValueGetter fillTypeGetter = new DelegateGetter <object>(
                delegate() {
                Brush brush = context.CurrentInstance;
                if (brush == null)
                {
                    return(null);
                }
                if (brush is SolidBrush)
                {
                    return(BrushTypes.SolidBrush);
                }
                if (brush is HatchBrush)
                {
                    return(BrushTypes.HatchBrush);
                }
                if (brush is LinearGradientBrush)
                {
                    return(BrushTypes.LinearGradientBrush);
                }
                if (brush is TextureBrush)
                {
                    return(BrushTypes.TextureBrush);
                }
                return(BrushTypes.Custom);
            });

            IValueSetter fillTypeSetter = new DelegateSetter <object>(
                delegate(object value) {
                if (value != null)
                {
                    if (!(value is BrushTypes))
                    {
                        return;
                    }
                    BrushTypes type = (BrushTypes)value;
                    Brush brush     = context.CurrentInstance;
                    if (type == BrushTypes.Custom)
                    {
                        context.SetNewInstance(brush);
                        return;
                    }
                    //todo: this code should be replaced by a composite brush editor some day
                    Color fg = Color.Black;
                    Color bg = Color.Empty;

                    if (brush != null)
                    {
                        if (brush is SolidBrush)
                        {
                            fg = ((SolidBrush)brush).Color;
                        }
                        if (brush is HatchBrush)
                        {
                            HatchBrush hb = (HatchBrush)brush;
                            fg            = hb.ForegroundColor;
                            bg            = hb.BackgroundColor;
                        }
                        if (brush is LinearGradientBrush)
                        {
                            LinearGradientBrush lb = (LinearGradientBrush)brush;
                            Color[] colors         = lb.LinearColors;
                            if (colors != null)
                            {
                                fg = colors[0];
                                bg = colors[1];
                            }
                        }
                    }
                    switch (type)
                    {
                    case BrushTypes.SolidBrush:
                        if (!(brush is SolidBrush))
                        {
                            context.SetNewInstance(new SolidBrush(fg));
                            return;
                        }
                        break;

                    case BrushTypes.HatchBrush:
                        if (!(brush is HatchBrush))
                        {
                            context.SetNewInstance(new HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, fg, bg));
                            return;
                        }
                        break;

                    case BrushTypes.TextureBrush:
                        if (!(brush is TextureBrush))
                        {
                            Image img = new Bitmap(1, 1);
                            context.SetNewInstance(new TextureBrush(img));
                            return;
                        }
                        break;

                    case BrushTypes.LinearGradientBrush:
                        if (!(brush is LinearGradientBrush))
                        {
                            //Accomodate for rounding errors
                            LinearGradientBrush lgb = new LinearGradientBrush(new PointF(), new PointF(1.01f, 0), fg, bg);
                            context.SetNewInstance(lgb);
                            return;
                        }
                        break;
                    }
                }
                else
                {
                    context.SetNewInstance(null);
                }
            });

            context.AddEntry(FillType, fillTypeGetter, fillTypeSetter, null);


            DelegateGetter <Color> foregroundColorGetter = new DelegateGetter <Color>(
                delegate() {
                Brush brush = context.CurrentInstance;
                if (brush == null)
                {
                    return(Color.Empty);
                }
                if (brush is SolidBrush)
                {
                    return(((SolidBrush)brush).Color);
                }
                if (brush is HatchBrush)
                {
                    return
                    (((HatchBrush)brush).ForegroundColor);
                }
                if (brush is LinearGradientBrush)
                {
                    Color[] colors =
                        ((LinearGradientBrush)brush).
                        LinearColors;
                    return
                    (colors == null
                ? Color.Empty
                : colors[0]);
                }
                return(Color.Empty);
            });

            DelegateSetter <Color> foregroundColorSetter =
                new DelegateSetter <Color>(
                    delegate(Color value) {
                Brush brush = context.CurrentInstance;
                if (brush is SolidBrush)
                {
                    SolidBrush newBrush = new SolidBrush(value);
                    context.SetNewInstance(newBrush);
                    return;
                }

                if (brush is HatchBrush)
                {
                    HatchBrush hb       = (HatchBrush)brush;
                    HatchBrush newBrush = new HatchBrush(hb.HatchStyle, value, hb.BackgroundColor);
                    context.SetNewInstance(newBrush);
                    return;
                }

                if (brush is LinearGradientBrush)
                {
                    LinearGradientBrush lb       = (LinearGradientBrush)brush;
                    Color[] colors               = lb.LinearColors;
                    LinearGradientBrush newBrush = new LinearGradientBrush(lb.Rectangle, value, colors[1], 0f);
                    newBrush.WrapMode            = lb.WrapMode;
                    newBrush.Transform           = lb.Transform;
                    //(LinearGradientBrush)lb.Clone();

                    //            newBrush.LinearColors = new Color[] { colors[0], value };
                    context.SetNewInstance(newBrush);
                    return;
                }

//          if (brush is LinearGradientBrush) {
//            LinearGradientBrush lb = (LinearGradientBrush)brush;
//            Color[] colors = lb.LinearColors;
//            LinearGradientBrush newBrush = (LinearGradientBrush)lb.Clone();
//            newBrush.LinearColors = new Color[] { value, colors[1] };
//
//            context.SetNewInstance(newBrush);
//            return;
//          }
            }, delegate() {
                Brush brush = context.CurrentInstance;
                if (brush is SolidBrush || brush is HatchBrush)
                {
                    return(true);
                }

                if (brush is LinearGradientBrush)
                {
                    LinearGradientBrush lb = (LinearGradientBrush)brush;
                    Color[] colors         = lb.LinearColors;
                    return(colors != null);
                }
                return(false);
            });

            context.AddEntry(ForegroundColor,
                             foregroundColorGetter, foregroundColorSetter, new ArgbEqualityComparer());

            DelegateGetter <Color> backgroundColorGetter = new DelegateGetter <Color>(
                delegate() {
                Brush brush = context.CurrentInstance;
                if (brush == null)
                {
                    return(Color.Empty);
                }
                if (brush is HatchBrush)
                {
                    return
                    (((HatchBrush)brush).BackgroundColor);
                }
                if (brush is LinearGradientBrush)
                {
                    Color[] colors =
                        ((LinearGradientBrush)brush).
                        LinearColors;
                    return
                    (colors == null
                ? Color.Empty
                : colors[1]);
                }
                return(Color.Empty);
            });

            DelegateSetter <Color> backgroundColorSetter =
                new DelegateSetter <Color>(
                    delegate(Color value) {
                Brush brush = context.CurrentInstance;
                if (brush is HatchBrush)
                {
                    HatchBrush hb       = (HatchBrush)brush;
                    HatchBrush newBrush = new HatchBrush(hb.HatchStyle, hb.ForegroundColor, value);
                    context.SetNewInstance(newBrush);
                    return;
                }

                if (brush is LinearGradientBrush)
                {
                    LinearGradientBrush lb       = (LinearGradientBrush)brush;
                    Color[] colors               = lb.LinearColors;
                    LinearGradientBrush newBrush = new LinearGradientBrush(lb.Rectangle, colors[0], value, 0f);
                    newBrush.Transform           = lb.Transform;
                    newBrush.WrapMode            = lb.WrapMode;
                    //(LinearGradientBrush)lb.Clone();

//            newBrush.LinearColors = new Color[] { colors[0], value };
                    context.SetNewInstance(newBrush);
                    return;
                }
            }, delegate() {
                Brush brush = context.CurrentInstance;
                if (brush is SolidBrush || brush is HatchBrush)
                {
                    return(true);
                }

                if (brush is LinearGradientBrush)
                {
                    LinearGradientBrush lb = (LinearGradientBrush)brush;
                    Color[] colors         = lb.LinearColors;
                    return(colors != null);
                }
                return(false);
            });

            context.AddEntry(BackgroundColor, backgroundColorGetter, backgroundColorSetter, new ArgbEqualityComparer());

            context.AddEntry(HatchStyle,
                             new DelegateGetter <object>(delegate() {
                Brush brush   = context.CurrentInstance;
                HatchBrush hb = brush as HatchBrush;
                if (hb != null)
                {
                    return(hb.HatchStyle);
                }
                else
                {
                    return(OptionItem.VALUE_UNDEFINED);
                }
            }),
                             new DelegateSetter <HatchStyle>(
                                 delegate(HatchStyle value) {
                HatchBrush hb = context.CurrentInstance as HatchBrush;
                if (hb != null)
                {
                    HatchBrush newBrush = new HatchBrush(value, hb.ForegroundColor, hb.BackgroundColor);
                    context.SetNewInstance(newBrush);
                }
            }));

            context.AddEntry(Rotation,
                             new DelegateGetter <float>(delegate() {
                LinearGradientBrush lb = context.CurrentInstance as LinearGradientBrush;
                if (lb != null)
                {
                    return((float)CalculateAngle(lb.Transform));
                }
                else
                {
                    return(0);
                }
            }),
                             new DelegateSetter <float>(
                                 delegate(float value) {
                LinearGradientBrush lb = context.CurrentInstance as LinearGradientBrush;
                if (lb != null)
                {
                    context.SetNewInstance(RotateBrush(lb, value));
                }
            }));
            context.AddEntry(Image,
                             new DelegateGetter <Image>(delegate() {
                TextureBrush textureBrush = context.CurrentInstance as TextureBrush;
                if (textureBrush != null)
                {
                    return(textureBrush.Image);
                }
                else
                {
                    return(null);
                }
            }),
                             new DelegateSetter <Image>(
                                 delegate(Image value) {
                TextureBrush newBrush = new TextureBrush(value);
                context.SetNewInstance(newBrush);
            }));

            #endregion
        }
        protected override void BuildPropertyMapImpl(IPropertyBuildContext <TSubject> builder)
        {
            MethodInfo[] infos = typeof(IPropertyBuildContext <>).MakeGenericType(typeof(TSubject)).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            MethodInfo   buildChildContextMethod = null;

            foreach (MethodInfo info in infos)
            {
                if (info.IsGenericMethod && info.ReturnType.IsGenericType)
                {
                    buildChildContextMethod = info;
                    break;
                }
            }
            if (buildChildContextMethod == null)
            {
                throw new InvalidDataException("Method not found!");
            }

            TSubject instance = builder.CurrentInstance;

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

            foreach (PropertyDescriptor d  in properties)
            {
                PropertyDescriptor descriptor = d;

                AttributeBuilderAttribute mapAttribute =
                    (AttributeBuilderAttribute)descriptor.Attributes[typeof(AttributeBuilderAttribute)];
                if (mapAttribute != null && mapAttribute.Invisible)
                {
                    continue;
                }

                DisplayNameAttribute displayNameAttribute =
                    (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
                string propertyName;
                if (displayNameAttribute == null || displayNameAttribute.DisplayName.Length < 1)
                {
                    propertyName = descriptor.Name;
                }
                else
                {
                    propertyName = displayNameAttribute.DisplayName;
                }
                AssignmentPolicyAttribute assignmentPolicyAttribute =
                    (AssignmentPolicyAttribute)descriptor.Attributes[typeof(AssignmentPolicyAttribute)];
                Type propertyType = descriptor.PropertyType;

                NullableAttribute nullableAttribute = (NullableAttribute)descriptor.Attributes[typeof(NullableAttribute)];
                bool nullable = nullableAttribute == null || nullableAttribute.IsNullable;

                AssignmentPolicy policy;

                if (assignmentPolicyAttribute != null && builder.Policy == AssignmentPolicy.Default)
                {
                    policy = assignmentPolicyAttribute.Policy;
                }
                else
                {
                    policy = builder.Policy == AssignmentPolicy.Default ? AssignmentPolicy.ModifyInstance : builder.Policy;
                }

                object childbuilder;
                object currentPropertyValue = descriptor.GetValue(builder.CurrentInstance);

                if (currentPropertyValue == null && nullable)
                {
                    //todo: make this dependent on yet-to-implement nullable attribute
                    childbuilder = builder.GetPropertyMapBuilder(propertyType, currentPropertyValue);
                }
                else
                {
                    childbuilder = builder.GetPropertyMapBuilder(currentPropertyValue);
                }

                if (childbuilder != null)
                {
                    // Create the SetInstanceDelegate that uses the builders' CurrentInstance for the set operation
                    Delegate setMemberInstanceDelegate;
                    {
                        Type helperSetInstanceDelegateType = typeof(HelperSetInstanceDelegate <,>)
                                                             .MakeGenericType(propertyType, typeof(TSubject));
                        ConstructorInfo setterConstructorInfo = helperSetInstanceDelegateType
                                                                .GetConstructor(new Type[] { typeof(PropertyDescriptor), typeof(IPropertyBuildContext <TSubject>) });
                        object helperSetInstanceDelegateInstance = setterConstructorInfo
                                                                   .Invoke(new object[] { descriptor, builder });

                        MethodInfo method = helperSetInstanceDelegateType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)[0];
                        setMemberInstanceDelegate = Delegate.CreateDelegate(typeof(SetInstanceDelegate <>).MakeGenericType(propertyType),
                                                                            helperSetInstanceDelegateInstance, method);
                    }

                    // Create the SetInstanceDelegate that uses the builders' CurrentInstance for the set operation
                    Delegate getMemberInstanceDelegate;
                    {
                        Type helperInstanceDelegateType = typeof(HelperGetInstanceDelegate <,>)
                                                          .MakeGenericType(propertyType, typeof(TSubject));
                        ConstructorInfo getterConstructorInfo = helperInstanceDelegateType
                                                                .GetConstructor(new Type[] { typeof(PropertyDescriptor), typeof(IPropertyBuildContext <TSubject>) });
                        object helperGetInstanceDelegateInstance = getterConstructorInfo
                                                                   .Invoke(new object[] { descriptor, builder });

                        MethodInfo method = helperInstanceDelegateType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)[0];
                        getMemberInstanceDelegate = Delegate.CreateDelegate(typeof(GetInstanceDelegate <>).MakeGenericType(propertyType),
                                                                            helperGetInstanceDelegateInstance, method);
                    }
                    object childContext = buildChildContextMethod.MakeGenericMethod(propertyType).Invoke(builder,
                                                                                                         new object[] { propertyName, getMemberInstanceDelegate,
                                                                                                                        setMemberInstanceDelegate,
                                                                                                                        policy });

                    MethodInfo buildPropertyMapMethod = typeof(IPropertyMapBuilder).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                                               BindingFlags.Instance)[0];

                    buildPropertyMapMethod.MakeGenericMethod(propertyType).Invoke(childbuilder, new object[] { childContext });
                }
                else
                {
                    builder.AddEntry(propertyName, new ReflectionGetter <TSubject>(descriptor, builder),
                                     new ReflectionSetter <TSubject>(descriptor, builder));
                }
            }
        }
Exemplo n.º 12
0
        protected override void BuildPropertyMapImpl(IPropertyBuildContext <Pen> context)
        {
            Brush b = context.CurrentInstance == null ? null : context.CurrentInstance.Brush;
            IPropertyMapBuilder brushBuilder;

            if (b == null)
            {
                brushBuilder = context.GetPropertyMapBuilder(typeof(Brush), b);
            }
            else
            {
                brushBuilder = context.GetPropertyMapBuilder(b);
            }

            if (brushBuilder != null)
            {
                brushBuilder.BuildPropertyMap(context.CreateChildContext <Brush>("",
                                                                                 delegate() {
                    Pen p = context.CurrentInstance;
                    return(p == null ? null:context.CurrentInstance.Brush);
                },
                                                                                 delegate(Brush newInstance) {
                    Pen p = context.CurrentInstance;
                    if (newInstance == null)
                    {
                        context.SetNewInstance(null);
                        return;
                    }
                    if (p == null)
                    {
                        context.SetNewInstance(new Pen(newInstance));
                    }
                    else
                    {
                        Pen clone   = (Pen)p.Clone();
                        clone.Brush = newInstance;
                        context.SetNewInstance(clone);
                    }
                }, AssignmentPolicy.CreateNewInstance));
            }

            context.AddEntry <float>(Width,
                                     delegate {
                Pen pen = context.CurrentInstance;
                return(pen == null ? 0 : pen.Width);
            },
                                     delegate(float value) {
                Pen pen   = context.CurrentInstance;
                Pen clone = pen.Clone() as Pen;
                if (clone != null)
                {
                    clone.Width = value;
                    context.SetNewInstance(clone);
                }
            });
            context.AddEntry(DashStyle,
                             new DelegateGetter <object>(
                                 delegate() {
                Pen pen = context.CurrentInstance;
                return(pen == null ? OptionItem.VALUE_UNDEFINED : pen.DashStyle);
            }),
                             new DelegateSetter <DashStyle>(delegate(DashStyle value) {
                Pen pen   = context.CurrentInstance;
                Pen clone = pen.Clone() as Pen;
                if (clone != null)
                {
                    clone.DashStyle = value;
                    context.SetNewInstance(clone);
                }
            }));
        }