Exemplo n.º 1
0
        private Control Layout(Panel Container, ListSingleField Field, DataForm _)
        {
            this.LayoutControlLabel(Container, Field);

            ComboBox ComboBox = new ComboBox()
            {
                Name       = VarToName(Field.Var),
                IsReadOnly = Field.ReadOnly,
                ToolTip    = Field.Description,
                Margin     = new Thickness(0, 0, 0, 5)
            };

            if (Field.HasError)
            {
                ComboBox.Background = new SolidColorBrush(Colors.PeachPuff);
            }
            else if (Field.NotSame)
            {
                ComboBox.Background = new SolidColorBrush(Colors.LightGray);
            }

            ComboBoxItem Item;

            foreach (KeyValuePair <string, string> P in Field.Options)
            {
                Item = new ComboBoxItem()
                {
                    Content = P.Key,
                    Tag     = P.Value
                };

                ComboBox.Items.Add(Item);
            }

            if (Field.ValidationMethod is Networking.XMPP.DataForms.ValidationMethods.OpenValidation)
            {
                ComboBox.IsEditable = true;
                ComboBox.Text       = Field.ValueString;
                ComboBox.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent,
                                    new System.Windows.Controls.TextChangedEventHandler(ComboBox_TextChanged));
            }
            else
            {
                string s = Field.ValueString;

                ComboBox.IsEditable        = false;
                ComboBox.SelectedIndex     = Array.FindIndex <KeyValuePair <string, string> >(Field.Options, (P) => P.Value.Equals(s));
                ComboBox.SelectionChanged += this.ComboBox_SelectionChanged;
            }

            Container.Children.Add(ComboBox);
            ComboBox.Tag = this.LayoutErrorLabel(Container, Field);

            return(ComboBox);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a data form containing editable parameters from an object.
        /// </summary>
        /// <param name="Client">Client</param>
        /// <param name="e">IQ Event Arguments describing the request.</param>
        /// <param name="EditableObject">Object whose parameters will be edited.</param>
        /// <param name="Title">Title of form.</param>
        /// <returns>Data form containing editable parameters.</returns>
        public static async Task <DataForm> GetEditableForm(XmppClient Client, IqEventArgs e, object EditableObject, string Title)
        {
            Type     T = EditableObject.GetType();
            string   DefaultLanguageCode = GetDefaultLanguageCode(T);
            DataForm Parameters          = new DataForm(Client, FormType.Form, e.To, e.From);
            Language Language            = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode);

            Namespace    Namespace = null;
            List <Field> Fields    = new List <Field>();
            List <Page>  Pages     = new List <Page>();
            Dictionary <string, Page>             PageByLabel = new Dictionary <string, Page>();
            Dictionary <string, Section>          SectionByPageAndSectionLabel = null;
            List <KeyValuePair <string, string> > Options = null;
            string                     NamespaceStr;
            string                     LastNamespaceStr = null;
            string                     Header;
            string                     ToolTip;
            string                     PageLabel;
            string                     SectionLabel;
            string                     s;
            int                        StringId;
            int                        PagePriority;
            int                        PageOrdinal = 0;
            int                        FieldPriority;
            int                        FieldOrdinal = 0;
            bool                       Required;
            bool                       ReadOnly;
            bool                       Masked;
            bool                       Alpha;
            bool                       DateOnly;
            bool                       Nullable;
            HeaderAttribute            HeaderAttribute;
            ToolTipAttribute           ToolTipAttribute;
            PageAttribute              PageAttribute;
            SectionAttribute           SectionAttribute;
            OptionAttribute            OptionAttribute;
            TextAttribute              TextAttribute;
            RegularExpressionAttribute RegularExpressionAttribute;
            LinkedList <TextAttribute> TextAttributes;
            RangeAttribute             RangeAttribute;
            ValidationMethod           ValidationMethod;
            Type                       PropertyType;
            Field                      Field;
            Page                       DefaultPage = null;
            Page                       Page;

            if (Namespace is null)
            {
                Namespace = await Language.CreateNamespaceAsync(T.Namespace);
            }

            LinkedList <KeyValuePair <PropertyInfo, FieldInfo> > Properties = new LinkedList <KeyValuePair <PropertyInfo, FieldInfo> >();

            foreach (PropertyInfo PI in T.GetRuntimeProperties())
            {
                if (PI.CanRead)
                {
                    Properties.AddLast(new KeyValuePair <PropertyInfo, FieldInfo>(PI, null));
                }
            }

            foreach (FieldInfo FI in T.GetRuntimeFields())
            {
                Properties.AddLast(new KeyValuePair <PropertyInfo, FieldInfo>(null, FI));
            }

            foreach (KeyValuePair <PropertyInfo, FieldInfo> Rec in Properties)
            {
                PropertyInfo PropertyInfo = Rec.Key;
                FieldInfo    FieldInfo    = Rec.Value;

                NamespaceStr = (PropertyInfo?.DeclaringType ?? FieldInfo.DeclaringType).Namespace;
                if (Namespace is null || NamespaceStr != LastNamespaceStr)
                {
                    Namespace = await Language.GetNamespaceAsync(NamespaceStr);

                    LastNamespaceStr = NamespaceStr;
                }

                Header           = ToolTip = PageLabel = SectionLabel = null;
                TextAttributes   = null;
                ValidationMethod = null;
                Options          = null;
                Required         = Masked = Alpha = DateOnly = false;
                ReadOnly         = PropertyInfo != null && !PropertyInfo.CanWrite;
                PagePriority     = PageAttribute.DefaultPriority;
                FieldPriority    = HeaderAttribute.DefaultPriority;

                foreach (Attribute Attr in (PropertyInfo?.GetCustomAttributes() ?? FieldInfo.GetCustomAttributes()))
                {
                    if ((HeaderAttribute = Attr as HeaderAttribute) != null)
                    {
                        Header        = HeaderAttribute.Header;
                        StringId      = HeaderAttribute.StringId;
                        FieldPriority = HeaderAttribute.Priority;
                        if (StringId > 0)
                        {
                            Header = await Namespace.GetStringAsync(StringId, Header);
                        }
                    }
                    else if ((ToolTipAttribute = Attr as ToolTipAttribute) != null)
                    {
                        ToolTip  = ToolTipAttribute.ToolTip;
                        StringId = ToolTipAttribute.StringId;
                        if (StringId > 0)
                        {
                            ToolTip = await Namespace.GetStringAsync(StringId, ToolTip);
                        }
                    }
                    else if ((PageAttribute = Attr as PageAttribute) != null)
                    {
                        PageLabel    = PageAttribute.Label;
                        StringId     = PageAttribute.StringId;
                        PagePriority = PageAttribute.Priority;
                        if (StringId > 0)
                        {
                            PageLabel = await Namespace.GetStringAsync(StringId, PageLabel);
                        }
                    }
                    else if ((SectionAttribute = Attr as SectionAttribute) != null)
                    {
                        SectionLabel = SectionAttribute.Label;
                        StringId     = SectionAttribute.StringId;
                        if (StringId > 0)
                        {
                            SectionLabel = await Namespace.GetStringAsync(StringId, SectionLabel);
                        }
                    }
                    else if ((TextAttribute = Attr as TextAttribute) != null)
                    {
                        if (TextAttributes is null)
                        {
                            TextAttributes = new LinkedList <TextAttribute>();
                        }

                        TextAttributes.AddLast(TextAttribute);
                    }
                    else if ((OptionAttribute = Attr as OptionAttribute) != null)
                    {
                        if (Options is null)
                        {
                            Options = new List <KeyValuePair <string, string> >();
                        }

                        StringId = OptionAttribute.StringId;
                        if (StringId > 0)
                        {
                            Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(),
                                                                          await Namespace.GetStringAsync(StringId, OptionAttribute.Label)));
                        }
                        else
                        {
                            Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(), OptionAttribute.Label));
                        }
                    }
                    else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null)
                    {
                        ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern);
                    }
                    else if ((RangeAttribute = Attr as RangeAttribute) != null)
                    {
                        ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max);
                    }
                    else if (Attr is OpenAttribute)
                    {
                        ValidationMethod = new OpenValidation();
                    }
                    else if (Attr is RequiredAttribute)
                    {
                        Required = true;
                    }
                    else if (Attr is ReadOnlyAttribute)
                    {
                        ReadOnly = true;
                    }
                    else if (Attr is MaskedAttribute)
                    {
                        Masked = true;
                    }
                    else if (Attr is AlphaChannelAttribute)
                    {
                        Alpha = true;
                    }
                    else if (Attr is DateOnlyAttribute)
                    {
                        DateOnly = true;
                    }
                }

                if (Header is null)
                {
                    continue;
                }

                PropertyType = PropertyInfo?.PropertyType ?? FieldInfo.FieldType;
                Field        = null;
                Nullable     = false;

                if (PropertyType.GetTypeInfo().IsGenericType)
                {
                    Type GT = PropertyType.GetGenericTypeDefinition();
                    if (GT == typeof(Nullable <>))
                    {
                        Nullable     = true;
                        PropertyType = PropertyType.GenericTypeArguments[0];
                    }
                }

                string PropertyName  = PropertyInfo?.Name ?? FieldInfo.Name;
                object PropertyValue = (PropertyInfo?.GetValue(EditableObject) ?? FieldInfo.GetValue(EditableObject));

                if (PropertyType == typeof(string[]))
                {
                    if (ValidationMethod is null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    if (Options is null)
                    {
                        Field = new TextMultiField(Parameters, PropertyName, Header, Required, (string[])PropertyValue,
                                                   null, ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                    else
                    {
                        Field = new ListMultiField(Parameters, PropertyName, Header, Required, (string[])PropertyValue,
                                                   Options.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                }
                else if (PropertyType.GetTypeInfo().IsEnum)
                {
                    if (ValidationMethod is null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    if (Options is null)
                    {
                        Options = new List <KeyValuePair <string, string> >();

                        foreach (string Option in Enum.GetNames(PropertyType))
                        {
                            Options.Add(new KeyValuePair <string, string>(Option, Option));
                        }
                    }

                    s = PropertyValue?.ToString();
                    if (Nullable && s is null)
                    {
                        s = string.Empty;
                    }

                    Field = new ListSingleField(Parameters, PropertyName, Header, Required, new string[] { s },
                                                Options.ToArray(), ToolTip, null, ValidationMethod, string.Empty, false, ReadOnly, false);
                }
                else if (PropertyType == typeof(bool))
                {
                    if (ValidationMethod is null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    if (Nullable && PropertyValue is null)
                    {
                        s = string.Empty;
                    }
                    else
                    {
                        s = CommonTypes.Encode((bool)PropertyValue);
                    }

                    Field = new BooleanField(Parameters, PropertyName, Header, Required, new string[] { s },
                                             Options?.ToArray(), ToolTip, BooleanDataType.Instance, ValidationMethod,
                                             string.Empty, false, ReadOnly, false);
                }
                else
                {
                    DataType DataType;

                    if (PropertyType == typeof(string))
                    {
                        DataType = StringDataType.Instance;
                    }
                    else if (PropertyType == typeof(sbyte))
                    {
                        DataType = ByteDataType.Instance;
                    }
                    else if (PropertyType == typeof(short))
                    {
                        DataType = ShortDataType.Instance;
                    }
                    else if (PropertyType == typeof(int))
                    {
                        DataType = IntDataType.Instance;
                    }
                    else if (PropertyType == typeof(long))
                    {
                        DataType = LongDataType.Instance;
                    }
                    else if (PropertyType == typeof(byte))
                    {
                        DataType = ShortDataType.Instance;

                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new RangeValidation(byte.MinValue.ToString(), byte.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(ushort))
                    {
                        DataType = IntDataType.Instance;

                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(uint))
                    {
                        DataType = LongDataType.Instance;

                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(ulong))
                    {
                        DataType = IntegerDataType.Instance;

                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(DateTime))
                    {
                        if (DateOnly)
                        {
                            DataType = DateDataType.Instance;
                        }
                        else
                        {
                            DataType = DateTimeDataType.Instance;
                        }
                    }
                    else if (PropertyType == typeof(decimal))
                    {
                        DataType = DecimalDataType.Instance;
                    }
                    else if (PropertyType == typeof(double))
                    {
                        DataType = DoubleDataType.Instance;
                    }
                    else if (PropertyType == typeof(float))
                    {
                        DataType = DoubleDataType.Instance;                            // Use xs:double anyway
                    }
                    else if (PropertyType == typeof(TimeSpan))
                    {
                        DataType = TimeDataType.Instance;
                    }
                    else if (PropertyType == typeof(Uri))
                    {
                        DataType = AnyUriDataType.Instance;
                    }
                    else if (PropertyType == typeof(SKColor))
                    {
                        if (Alpha)
                        {
                            DataType = ColorAlphaDataType.Instance;
                        }
                        else
                        {
                            DataType = ColorDataType.Instance;
                        }
                    }
                    else
                    {
                        DataType = StringDataType.Instance;
                    }

                    if (ValidationMethod is null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    s = PropertyValue?.ToString();
                    if (Nullable && s is null)
                    {
                        s = string.Empty;
                    }

                    if (Masked)
                    {
                        Field = new TextPrivateField(Parameters, PropertyName, Header, Required, new string[] { s },
                                                     Options?.ToArray(), ToolTip, DataType, ValidationMethod,
                                                     string.Empty, false, ReadOnly, false);
                    }
                    else if (Options is null)
                    {
                        Field = new TextSingleField(Parameters, PropertyName, Header, Required, new string[] { s },
                                                    null, ToolTip, DataType, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                    else
                    {
                        Field = new ListSingleField(Parameters, PropertyName, Header, Required, new string[] { s },
                                                    Options.ToArray(), ToolTip, DataType, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                }

                if (Field is null)
                {
                    continue;
                }

                if (string.IsNullOrEmpty(PageLabel))
                {
                    if (DefaultPage is null)
                    {
                        DefaultPage = new Page(Parameters, string.Empty)
                        {
                            Priority = PageAttribute.DefaultPriority,
                            Ordinal  = PageOrdinal++
                        };
                        Pages.Add(DefaultPage);
                        PageByLabel[string.Empty] = DefaultPage;
                    }

                    Page      = DefaultPage;
                    PageLabel = string.Empty;
                }
                else
                {
                    if (!PageByLabel.TryGetValue(PageLabel, out Page))
                    {
                        Page = new Page(Parameters, PageLabel)
                        {
                            Priority = PagePriority,
                            Ordinal  = PageOrdinal++
                        };
                        Pages.Add(Page);
                        PageByLabel[PageLabel] = Page;
                    }
                }

                Field.Priority = FieldPriority;
                Field.Ordinal  = FieldOrdinal++;
                Fields.Add(Field);

                if (string.IsNullOrEmpty(SectionLabel))
                {
                    if (TextAttributes != null)
                    {
                        foreach (TextAttribute TextAttr in TextAttributes)
                        {
                            if (TextAttr.Position == TextPosition.BeforeField)
                            {
                                StringId = TextAttr.StringId;
                                if (StringId > 0)
                                {
                                    Page.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label)));
                                }
                                else
                                {
                                    Page.Add(new TextElement(Parameters, TextAttr.Label));
                                }
                            }
                        }
                    }

                    Page.Add(new FieldReference(Parameters, Field.Var));

                    if (TextAttributes != null)
                    {
                        foreach (TextAttribute TextAttr in TextAttributes)
                        {
                            if (TextAttr.Position == TextPosition.AfterField)
                            {
                                StringId = TextAttr.StringId;
                                if (StringId > 0)
                                {
                                    Page.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label)));
                                }
                                else
                                {
                                    Page.Add(new TextElement(Parameters, TextAttr.Label));
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (SectionByPageAndSectionLabel is null)
                    {
                        SectionByPageAndSectionLabel = new Dictionary <string, Section>();
                    }

                    s = PageLabel + " \xa0 " + SectionLabel;
                    if (!SectionByPageAndSectionLabel.TryGetValue(s, out Section Section))
                    {
                        Section = new Section(Parameters, SectionLabel);
                        SectionByPageAndSectionLabel[s] = Section;

                        Page.Add(Section);
                    }

                    if (TextAttributes != null)
                    {
                        foreach (TextAttribute TextAttr in TextAttributes)
                        {
                            if (TextAttr.Position == TextPosition.BeforeField)
                            {
                                StringId = TextAttr.StringId;
                                if (StringId > 0)
                                {
                                    Section.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label)));
                                }
                                else
                                {
                                    Section.Add(new TextElement(Parameters, TextAttr.Label));
                                }
                            }
                        }
                    }

                    Section.Add(new FieldReference(Parameters, Field.Var));

                    if (TextAttributes != null)
                    {
                        foreach (TextAttribute TextAttr in TextAttributes)
                        {
                            if (TextAttr.Position == TextPosition.AfterField)
                            {
                                StringId = TextAttr.StringId;
                                if (StringId > 0)
                                {
                                    Section.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label)));
                                }
                                else
                                {
                                    Section.Add(new TextElement(Parameters, TextAttr.Label));
                                }
                            }
                        }
                    }
                }
            }

            if (EditableObject is IPropertyFormAnnotation PropertyFormAnnotation)
            {
                FormState State = new FormState()
                {
                    Form        = Parameters,
                    PageByLabel = PageByLabel,
                    SectionByPageAndSectionLabel = SectionByPageAndSectionLabel,
                    DefaultPage  = DefaultPage,
                    LanguageCode = Language.Code,
                    Fields       = Fields,
                    Pages        = Pages,
                    FieldOrdinal = FieldOrdinal,
                    PageOrdinal  = PageOrdinal
                };

                await PropertyFormAnnotation.AnnotatePropertyForm(State);
            }

            Fields.Sort(OrderFields);
            Parameters.Title  = Title;
            Parameters.Fields = Fields.ToArray();

            if (Pages != null)
            {
                Pages.Sort(OrderPages);

                foreach (Page Page2 in Pages)
                {
                    Page2.Sort();
                }
            }

            Parameters.Pages = Pages.ToArray();

            return(Parameters);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets a data form containing editable parameters from an object.
        /// </summary>
        /// <param name="Client">Client</param>
        /// <param name="e">IQ Event Arguments describing the request.</param>
        /// <param name="EditableObject">Object whose parameters will be edited.</param>
        /// <param name="Title">Title of form.</param>
        /// <returns>Data form containing editable parameters.</returns>
        public static async Task <DataForm> GetEditableForm(XmppClient Client, IqEventArgs e, object EditableObject, string Title)
        {
            Type     T = EditableObject.GetType();
            string   DefaultLanguageCode = GetDefaultLanguageCode(T);
            DataForm Parameters          = new DataForm(Client, FormType.Form, e.To, e.From);
            Language Language            = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode);

            Namespace Namespace = await Language.GetNamespaceAsync(T.Namespace);

            List <Field> Fields = new List <Field>();
            List <Page>  Pages  = new List <Page>();
            Dictionary <string, Page>             PageByLabel = new Dictionary <string, Page>();
            Dictionary <string, Section>          SectionByPageAndSectionLabel = null;
            List <KeyValuePair <string, string> > Options = null;
            string                     Header;
            string                     ToolTip;
            string                     PageLabel;
            string                     SectionLabel;
            string                     s;
            int                        StringId;
            bool                       Required;
            bool                       ReadOnly;
            bool                       Masked;
            bool                       Alpha;
            bool                       DateOnly;
            HeaderAttribute            HeaderAttribute;
            ToolTipAttribute           ToolTipAttribute;
            PageAttribute              PageAttribute;
            SectionAttribute           SectionAttribute;
            OptionAttribute            OptionAttribute;
            TextAttribute              TextAttribute;
            RegularExpressionAttribute RegularExpressionAttribute;
            LinkedList <string>        TextAttributes;
            RangeAttribute             RangeAttribute;
            ValidationMethod           ValidationMethod;
            Type                       PropertyType;
            Field                      Field;
            Page                       DefaultPage = null;
            Page                       Page;

            if (Namespace == null)
            {
                Namespace = await Language.CreateNamespaceAsync(T.Namespace);
            }

            foreach (PropertyInfo PI in T.GetRuntimeProperties())
            {
                if (!PI.CanRead || !PI.CanWrite)
                {
                    continue;
                }

                Header           = ToolTip = PageLabel = SectionLabel = null;
                TextAttributes   = null;
                ValidationMethod = null;
                Required         = ReadOnly = Masked = Alpha = DateOnly = false;

                foreach (Attribute Attr in PI.GetCustomAttributes())
                {
                    if ((HeaderAttribute = Attr as HeaderAttribute) != null)
                    {
                        Header   = HeaderAttribute.Header;
                        StringId = HeaderAttribute.StringId;
                        if (StringId > 0)
                        {
                            Header = await Namespace.GetStringAsync(StringId, Header);
                        }
                    }
                    else if ((ToolTipAttribute = Attr as ToolTipAttribute) != null)
                    {
                        ToolTip  = ToolTipAttribute.ToolTip;
                        StringId = ToolTipAttribute.StringId;
                        if (StringId > 0)
                        {
                            ToolTip = await Namespace.GetStringAsync(StringId, ToolTip);
                        }
                    }
                    else if ((PageAttribute = Attr as PageAttribute) != null)
                    {
                        PageLabel = PageAttribute.Label;
                        StringId  = PageAttribute.StringId;
                        if (StringId > 0)
                        {
                            PageLabel = await Namespace.GetStringAsync(StringId, PageLabel);
                        }
                    }
                    else if ((SectionAttribute = Attr as SectionAttribute) != null)
                    {
                        SectionLabel = SectionAttribute.Label;
                        StringId     = SectionAttribute.StringId;
                        if (StringId > 0)
                        {
                            SectionLabel = await Namespace.GetStringAsync(StringId, SectionLabel);
                        }
                    }
                    else if ((TextAttribute = Attr as TextAttribute) != null)
                    {
                        if (TextAttributes == null)
                        {
                            TextAttributes = new LinkedList <string>();
                        }

                        StringId = TextAttribute.StringId;
                        if (StringId > 0)
                        {
                            TextAttributes.AddLast(await Namespace.GetStringAsync(StringId, TextAttribute.Label));
                        }
                        else
                        {
                            TextAttributes.AddLast(TextAttribute.Label);
                        }
                    }
                    else if ((OptionAttribute = Attr as OptionAttribute) != null)
                    {
                        if (Options == null)
                        {
                            Options = new List <KeyValuePair <string, string> >();
                        }

                        StringId = OptionAttribute.StringId;
                        if (StringId > 0)
                        {
                            Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(),
                                                                          await Namespace.GetStringAsync(StringId, TextAttribute.Label)));
                        }
                        else
                        {
                            Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(), OptionAttribute.Label));
                        }
                    }
                    else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null)
                    {
                        ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern);
                    }
                    else if ((RangeAttribute = Attr as RangeAttribute) != null)
                    {
                        ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max);
                    }
                    else if (Attr is OpenAttribute)
                    {
                        ValidationMethod = new OpenValidation();
                    }
                    else if (Attr is RequiredAttribute)
                    {
                        Required = true;
                    }
                    else if (Attr is ReadOnlyAttribute)
                    {
                        ReadOnly = true;
                    }
                    else if (Attr is MaskedAttribute)
                    {
                        Masked = true;
                    }
                    else if (Attr is AlphaChannelAttribute)
                    {
                        Alpha = true;
                    }
                    else if (Attr is DateOnlyAttribute)
                    {
                        DateOnly = true;
                    }
                }

                if (Header == null)
                {
                    continue;
                }

                PropertyType = PI.PropertyType;
                Field        = null;

                if (PropertyType == typeof(string[]))
                {
                    if (ValidationMethod == null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    if (Options == null)
                    {
                        Field = new TextMultiField(Parameters, PI.Name, Header, Required, (string[])PI.GetValue(EditableObject),
                                                   null, ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                    else
                    {
                        Field = new ListMultiField(Parameters, PI.Name, Header, Required, (string[])PI.GetValue(EditableObject),
                                                   Options.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                }
                else if (PropertyType == typeof(Enum))
                {
                    if (ValidationMethod == null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    if (Options == null)
                    {
                        Options = new List <KeyValuePair <string, string> >();

                        foreach (string Option in Enum.GetNames(PropertyType))
                        {
                            Options.Add(new KeyValuePair <string, string>(Option, Option));
                        }
                    }

                    Field = new ListSingleField(Parameters, PI.Name, Header, Required, new string[] { PI.GetValue(EditableObject).ToString() },
                                                Options.ToArray(), ToolTip, null, ValidationMethod, string.Empty, false, ReadOnly, false);
                }
                else if (PropertyType == typeof(bool))
                {
                    if (ValidationMethod == null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    Field = new BooleanField(Parameters, PI.Name, Header, Required,
                                             new string[] { CommonTypes.Encode((bool)PI.GetValue(EditableObject)) },
                                             Options?.ToArray(), ToolTip, BooleanDataType.Instance, ValidationMethod,
                                             string.Empty, false, ReadOnly, false);
                }
                else
                {
                    DataType DataType;

                    if (PropertyType == typeof(string))
                    {
                        DataType = StringDataType.Instance;
                    }
                    else if (PropertyType == typeof(byte))
                    {
                        DataType = ByteDataType.Instance;
                    }
                    else if (PropertyType == typeof(short))
                    {
                        DataType = ShortDataType.Instance;
                    }
                    else if (PropertyType == typeof(int))
                    {
                        DataType = IntDataType.Instance;
                    }
                    else if (PropertyType == typeof(long))
                    {
                        DataType = LongDataType.Instance;
                    }
                    else if (PropertyType == typeof(sbyte))
                    {
                        DataType = ShortDataType.Instance;

                        if (ValidationMethod == null)
                        {
                            ValidationMethod = new RangeValidation(sbyte.MinValue.ToString(), sbyte.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(ushort))
                    {
                        DataType = IntDataType.Instance;

                        if (ValidationMethod == null)
                        {
                            ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(uint))
                    {
                        DataType = LongDataType.Instance;

                        if (ValidationMethod == null)
                        {
                            ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(ulong))
                    {
                        DataType = IntegerDataType.Instance;

                        if (ValidationMethod == null)
                        {
                            ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString());
                        }
                    }
                    else if (PropertyType == typeof(DateTime))
                    {
                        if (DateOnly)
                        {
                            DataType = DateDataType.Instance;
                        }
                        else
                        {
                            DataType = DateTimeDataType.Instance;
                        }
                    }
                    else if (PropertyType == typeof(decimal))
                    {
                        DataType = DecimalDataType.Instance;
                    }
                    else if (PropertyType == typeof(double))
                    {
                        DataType = DoubleDataType.Instance;
                    }
                    else if (PropertyType == typeof(float))
                    {
                        DataType = DoubleDataType.Instance;                            // Use xs:double anyway
                    }
                    else if (PropertyType == typeof(TimeSpan))
                    {
                        DataType = TimeDataType.Instance;
                    }
                    else if (PropertyType == typeof(Uri))
                    {
                        DataType = AnyUriDataType.Instance;
                    }
                    else if (PropertyType == typeof(SKColor))
                    {
                        if (Alpha)
                        {
                            DataType = ColorAlphaDataType.Instance;
                        }
                        else
                        {
                            DataType = ColorDataType.Instance;
                        }
                    }
                    else
                    {
                        DataType = null;
                    }

                    if (ValidationMethod == null)
                    {
                        ValidationMethod = new BasicValidation();
                    }

                    if (Masked)
                    {
                        Field = new TextPrivateField(Parameters, PI.Name, Header, Required, new string[] { (string)PI.GetValue(EditableObject) },
                                                     Options?.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod,
                                                     string.Empty, false, ReadOnly, false);
                    }
                    else if (Options == null)
                    {
                        Field = new TextSingleField(Parameters, PI.Name, Header, Required, new string[] { (string)PI.GetValue(EditableObject) },
                                                    null, ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                    else
                    {
                        Field = new ListSingleField(Parameters, PI.Name, Header, Required, new string[] { (string)PI.GetValue(EditableObject) },
                                                    Options.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false);
                    }
                }

                if (Field == null)
                {
                    continue;
                }

                Fields.Add(Field);

                if (string.IsNullOrEmpty(PageLabel))
                {
                    if (DefaultPage == null)
                    {
                        DefaultPage = new Page(Parameters, string.Empty);
                        Pages.Add(DefaultPage);
                        PageByLabel[string.Empty] = DefaultPage;
                    }

                    Page      = DefaultPage;
                    PageLabel = string.Empty;
                }
                else
                {
                    if (!PageByLabel.TryGetValue(PageLabel, out Page))
                    {
                        Page = new Page(Parameters, PageLabel);
                        Pages.Add(Page);
                        PageByLabel[PageLabel] = Page;
                    }
                }

                if (string.IsNullOrEmpty(SectionLabel))
                {
                    if (TextAttributes != null)
                    {
                        foreach (string Text in TextAttributes)
                        {
                            Page.Add(new TextElement(Parameters, Text));
                        }
                    }

                    Page.Add(new FieldReference(Parameters, Field.Var));
                }
                else
                {
                    if (SectionByPageAndSectionLabel == null)
                    {
                        SectionByPageAndSectionLabel = new Dictionary <string, Section>();
                    }

                    s = PageLabel + " \xa0 " + SectionLabel;
                    if (!SectionByPageAndSectionLabel.TryGetValue(s, out Section Section))
                    {
                        Section = new Section(Parameters, SectionLabel);
                        SectionByPageAndSectionLabel[s] = Section;

                        Page.Add(Section);
                    }

                    if (TextAttributes != null)
                    {
                        foreach (string Text in TextAttributes)
                        {
                            Section.Add(new TextElement(Parameters, Text));
                        }
                    }

                    Section.Add(new FieldReference(Parameters, Field.Var));
                }
            }

            Parameters.Title  = Title;
            Parameters.Fields = Fields.ToArray();
            Parameters.Pages  = Pages.ToArray();

            return(Parameters);
        }
Exemplo n.º 4
0
        private Field ParseField(XmlElement E)
        {
            string        Label        = XML.Attribute(E, "label");
            string        Type         = XML.Attribute(E, "type");
            string        Var          = XML.Attribute(E, "var");
            List <string> ValueStrings = null;
            List <KeyValuePair <string, string> > OptionStrings = null;
            string           Description      = string.Empty;
            string           DataTypeName     = null;
            DataType         DataType         = null;
            ValidationMethod ValidationMethod = null;
            Media            Media            = null;
            Field            Field;
            string           Error    = null;
            bool             Required = false;
            bool             PostBack = false;
            bool             ReadOnly = false;
            bool             NotSame  = false;

            foreach (XmlNode N2 in E.ChildNodes)
            {
                switch (N2.LocalName)
                {
                case "desc":
                    Description = N2.InnerText;
                    break;

                case "required":
                    Required = true;
                    break;

                case "value":
                    if (ValueStrings == null)
                    {
                        ValueStrings = new List <string>();
                    }

                    ValueStrings.Add(N2.InnerText);
                    break;

                case "option":
                    if (OptionStrings == null)
                    {
                        OptionStrings = new List <KeyValuePair <string, string> >();
                    }

                    string OptionLabel = XML.Attribute((XmlElement)N2, "label");
                    string OptionValue = string.Empty;

                    foreach (XmlNode N3 in N2.ChildNodes)
                    {
                        if (N3.LocalName == "value")
                        {
                            OptionValue = N3.InnerText;
                            break;
                        }
                    }

                    OptionStrings.Add(new KeyValuePair <string, string>(OptionLabel, OptionValue));
                    break;

                case "validate":
                    DataTypeName = XML.Attribute((XmlElement)N2, "datatype");

                    foreach (XmlNode N3 in N2.ChildNodes)
                    {
                        switch (N3.LocalName)
                        {
                        case "basic":
                            ValidationMethod = new BasicValidation();
                            break;

                        case "open":
                            ValidationMethod = new OpenValidation();
                            break;

                        case "range":
                            XmlElement E3 = (XmlElement)N3;

                            ValidationMethod = new RangeValidation(
                                XML.Attribute(E3, "min"),
                                XML.Attribute(E3, "max"));
                            break;

                        case "regex":
                            ValidationMethod = new RegexValidation(N3.InnerText);
                            break;

                        case "list-range":
                            E3 = (XmlElement)N3;

                            ValidationMethod = new ListRangeValidation(ValidationMethod,
                                                                       XML.Attribute(E3, "min", 0),
                                                                       XML.Attribute(E3, "max", int.MaxValue));
                            break;
                        }
                    }
                    break;

                case "media":
                    Media = new Media((XmlElement)N2);
                    break;

                case "postBack":
                    PostBack = true;
                    break;

                case "readOnly":
                    ReadOnly = true;
                    break;

                case "notSame":
                    NotSame = true;
                    break;

                case "error":
                    Error = N2.InnerText;
                    break;
                }
            }

            if (string.IsNullOrEmpty(DataTypeName))
            {
                if (Type == "boolean")
                {
                    DataTypeName = "xs:boolean";
                }
                else
                {
                    DataTypeName = "xs:string";
                }
            }

            switch (DataTypeName.ToLower())
            {
            case "xs:boolean":
                DataType = BooleanDataType.Instance;
                break;

            case "xs:string":
            default:
                DataType = StringDataType.Instance;
                break;

            case "anyuri":
                DataType = AnyUriDataType.Instance;
                break;

            case "xs:byte":
                DataType = ByteDataType.Instance;
                break;

            case "xs:date":
                DataType = DateDataType.Instance;
                break;

            case "xs:datetime":
                DataType = DateTimeDataType.Instance;
                break;

            case "xs:decimal":
                DataType = DecimalDataType.Instance;
                break;

            case "xs:double":
                DataType = DoubleDataType.Instance;
                break;

            case "xs:int":
                DataType = IntDataType.Instance;
                break;

            case "xs:integer":
                DataType = IntegerDataType.Instance;
                break;

            case "xs:language":
                DataType = LanguageDataType.Instance;
                break;

            case "xs:long":
                DataType = LongDataType.Instance;
                break;

            case "xs:short":
                DataType = ShortDataType.Instance;
                break;

            case "xs:time":
                DataType = TimeDataType.Instance;
                break;

            case "xdc:Color":
                DataType = ColorDataType.Instance;
                break;

            case "xdc:ColorAlpha":
                DataType = ColorAlphaDataType.Instance;
                break;
            }

            if (ValidationMethod == null)
            {
                ValidationMethod = new BasicValidation();
            }

            switch (Type)
            {
            case "boolean":
                Field = new BooleanField(this, Var, Label, Required,
                                         ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                         Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "fixed":
                Field = new FixedField(this, Var, Label, Required,
                                       ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                       Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "hidden":
                Field = new HiddenField(this, Var, Label, Required,
                                        ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                        Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "jid-multi":
                Field = new JidMultiField(this, Var, Label, Required,
                                          ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                          Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "jid-single":
                Field = new JidSingleField(this, Var, Label, Required,
                                           ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                           Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "list-multi":
                Field = new ListMultiField(this, Var, Label, Required,
                                           ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                           Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "list-single":
                Field = new ListSingleField(this, Var, Label, Required,
                                            ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                            Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "text-multi":
                Field = new TextMultiField(this, Var, Label, Required,
                                           ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                           Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "text-private":
                Field = new TextPrivateField(this, Var, Label, Required,
                                             ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                             Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            case "text-single":
                Field = new TextSingleField(this, Var, Label, Required,
                                            ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                            Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                break;

            default:
                if (Media == null)
                {
                    Field = new TextSingleField(this, Var, Label, Required,
                                                ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                                Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame);
                }
                else
                {
                    Field = new MediaField(this, Var, Label, Required,
                                           ValueStrings?.ToArray(), OptionStrings?.ToArray(),
                                           Description, DataType, ValidationMethod, Media, Error, PostBack, ReadOnly, NotSame);
                }
                break;
            }

            return(Field);
        }