private void SetCategory(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<CategoryAttribute>();
     if (attr != null)
         item.Category = attr.Category;
     else if (Items != null && Items.Count > 0)
         item.Category = Items[Items.Count - 1].Category;
 }
        private void SetEditing(IPropertySource source, PropertyItem item)
        {
            var editorTypeAttr = source.GetAttribute<EditorTypeAttribute>();

            if (editorTypeAttr == null)
            {
                item.EditorType = AutoDetermineEditorType(source.ValueType, source.EnumType, item.EditorParams);
            }
            else
            {
                item.EditorType = editorTypeAttr.EditorType;
                editorTypeAttr.SetParams(item.EditorParams);
            }

            if (source.EnumType != null)
                item.EditorParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);

            if (!ReferenceEquals(null, source.BasedOnField))
            {
                if (item.EditorType == "Decimal" &&
                    (source.BasedOnField is DoubleField ||
                        source.BasedOnField is DecimalField) &&
                        source.BasedOnField.Size > 0 &&
                        source.BasedOnField.Scale < source.BasedOnField.Size)
                {
                    var minVal = new String('0', source.BasedOnField.Size - source.BasedOnField.Scale);
                    if (source.BasedOnField.Scale > 0)
                        minVal += "." + new String('0', source.BasedOnField.Scale);
                    var maxVal = minVal.Replace('0', '9');
                    item.EditorParams["minValue"] = minVal;
                    item.EditorParams["maxValue"] = maxVal;
                }
                else if (source.BasedOnField.Size > 0)
                {
                    item.EditorParams["maxLength"] = source.BasedOnField.Size;
                    item.MaxLength = source.BasedOnField.Size;
                }
            }

            var maxLengthAttr = source.GetAttribute<MaxLengthAttribute>();
            if (maxLengthAttr != null)
            {
                item.MaxLength = maxLengthAttr.MaxLength;
                item.EditorParams["maxLength"] = maxLengthAttr.MaxLength;
            }

            foreach (var param in source.GetAttributes<EditorOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);

                item.EditorParams[key] = param.Value;
            }
        }
        private void SetWidth(IPropertySource source, PropertyItem item)
        {
            var widthAttr = source.GetAttribute<WidthAttribute>();
            var basedOnField = source.BasedOnField;
            item.Width = widthAttr == null ? (!ReferenceEquals(null, basedOnField) ? AutoWidth(basedOnField) : 80) : widthAttr.Value;
            if (widthAttr != null && (widthAttr.Min != 0))
                item.MinWidth = widthAttr.Min;

            if (widthAttr != null && (widthAttr.Max != 0))
                item.MaxWidth = widthAttr.Max;
        }
 private void SetRequired(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<RequiredAttribute>();
     if (attr != null)
     {
         if (attr.IsRequired)
             item.Required = true;
     }
     else if (!ReferenceEquals(null, source.BasedOnField) &&
         (source.BasedOnField.Flags & FieldFlags.NotNull) == FieldFlags.NotNull)
     {
         item.Required = true;
     }
 }
        private void SetDefaultValue(IPropertySource source, PropertyItem item)
        {
            if (source.Property != null)
            {
                var attr = source.Property.GetAttribute<DefaultValueAttribute>(false);
                if (attr != null)
                {
                    item.DefaultValue = attr.Value;
                    return;
                }
            }

            if (!ReferenceEquals(null, source.BasedOnField) && source.BasedOnField.DefaultValue != null)
                item.DefaultValue = source.BasedOnField.DefaultValue;
        }
        private void SetUpdatable(IPropertySource source, PropertyItem item)
        {
            if (source.Property != null)
            {
                var attr = source.Property.GetAttribute<UpdatableAttribute>(false);
                if (attr != null)
                {
                    if (!attr.Value)
                        item.Updatable = false;

                    return;
                }
            }

            if (!ReferenceEquals(null, source.BasedOnField))
            {
                if ((source.BasedOnField.Flags & FieldFlags.Updatable) != FieldFlags.Updatable)
                    item.Updatable = false;
            }
        }
        public static List<PropertyItem> GetPropertyItemsFor(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            var list = new List<PropertyItem>();

            var basedOnRow = GetBasedOnRow(type);
            var processors = ProcessorTypes.Select(x => (IPropertyProcessor)Activator.CreateInstance(x))
                .OrderBy(x => x.Priority).ToList();

            foreach (var processor in processors)
            {
                processor.Items = list;
                processor.Type = type;
                processor.BasedOnRow = basedOnRow;
                processor.Initialize();
            }

            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .OrderBy(x => x.MetadataToken))
            {
                if (property.GetCustomAttribute<IgnoreAttribute>(false) != null)
                    continue;

                var source = new PropertyInfoSource(property, basedOnRow);

                var pi = new PropertyItem();
                pi.Name = property.Name;

                foreach (var processor in processors)
                    processor.Process(source, pi);

                list.Add(pi);
            }

            foreach (var processor in processors)
                processor.PostProcess();

            return list;
        }
        private void SetTitle(IPropertySource source, PropertyItem item)
        {
            if (source.Property != null)
            {
                var attr = source.Property.GetCustomAttribute<DisplayNameAttribute>(false);
                if (attr != null)
                    item.Title = attr.DisplayName;
            }

            if (item.Title == null)
            {
                var basedOnField = source.BasedOnField;

                if (!ReferenceEquals(null, basedOnField))
                {
                    item.Title = !object.ReferenceEquals(null, basedOnField.Caption) ?
                        basedOnField.Caption.Key : basedOnField.Title;
                }
                else
                    item.Title = item.Name;
            }
        }
 public override void Process(IPropertySource source, PropertyItem item)
 {
     SetAlignment(source, item);
     SetCategory(source, item);
     SetCssClass(source, item);
     SetDefaultValue(source, item);
     SetEditLink(source, item);
     SetEditing(source, item);
     SetFiltering(source, item);
     SetFormatting(source, item);
     SetHint(source, item);
     SetInsertable(source, item);
     SetOneWay(source, item);
     SetPlaceholder(source, item);
     SetReadOnly(source, item);
     SetRequired(source, item);
     SetResizable(source, item);
     SetSortOrder(source, item);
     SetTitle(source, item);
     SetUpdatable(source, item);
     SetVisible(source, item);
     SetWidth(source, item);
 }
        private void SetEditLink(IPropertySource source, PropertyItem item)
        {
            var attr = source.GetAttribute<EditLinkAttribute>();
            if (attr == null)
                return;

            if (attr.Value)
                item.EditLink = true;

            if (attr.ItemType != null)
                item.EditLinkItemType = attr.ItemType;

            if (attr.IdField != null)
                item.EditLinkIdField = attr.IdField;

            if (attr.CssClass != null)
                item.EditLinkCssClass = attr.CssClass;

            if (item.EditLinkItemType != null &&
                item.EditLinkIdField == null)
            {
                item.EditLinkIdField = AutoDetermineIdField(source.BasedOnField);
            }
        }
 private void SetVisible(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<VisibleAttribute>();
     if (attr != null && attr.Value == false)
         item.Visible = false;
 }
 private void SetOneWay(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<OneWayAttribute>();
     if (attr != null)
         item.OneWay = true;
 }
 private void SetHint(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<HintAttribute>();
     if (attr != null)
         item.Hint = attr.Hint;
 }
 private void SetReadOnly(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<ReadOnlyAttribute>();
     if (attr != null)
         item.ReadOnly = true;
 }
 private void SetAlignment(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<AlignmentAttribute>();
     if (attr != null)
         item.Alignment = attr.Value;
 }
        private static void SetFormatting(IPropertySource source, PropertyItem item)
        {
            var formatterTypeAttr = source.GetAttribute<FormatterTypeAttribute>();
            var enumType = source.EnumType;
            var valueType = source.ValueType;
            var basedOnField = source.BasedOnField;

            if (formatterTypeAttr == null)
            {
                if (enumType != null)
                {
                    item.FormatterType = "Enum";
                    item.FormatterParams["enumKey"] = EnumMapper.GetEnumTypeKey(enumType);
                }
                else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
                {
                    if (!ReferenceEquals(null, basedOnField) && basedOnField is DateTimeField)
                    {
                        switch (((DateTimeField)basedOnField).DateTimeKind)
                        {
                            case DateTimeKind.Unspecified:
                                item.FormatterType = "Date";
                                break;
                            default:
                                item.FormatterType = "DateTime";
                                break;
                        }
                    }
                    else
                        item.FormatterType = "Date";
                }
                else if (valueType == typeof(Boolean))
                    item.FormatterType = "Checkbox";
                else if (valueType == typeof(Decimal) ||
                    valueType == typeof(Double) ||
                    valueType == typeof(Single) ||
                    valueType == typeof(Int32))
                {
                    item.FormatterType = "Number";
                }
            }
            else
            {
                item.FormatterType = formatterTypeAttr.FormatterType;
                formatterTypeAttr.SetParams(item.FormatterParams);
            }

            var displayFormatAttr = source.GetAttribute<DisplayFormatAttribute>();
            if (displayFormatAttr != null)
            {
                item.DisplayFormat = displayFormatAttr.Value;
                item.FormatterParams["displayFormat"] = displayFormatAttr.Value;
            }

            foreach (var param in source.GetAttributes<FormatterOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);

                item.FormatterParams[key] = param.Value;
            }
        } 
Example #17
0
 public virtual void Process(IPropertySource source, PropertyItem item)
 {
 }
 private void SetPlaceholder(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<PlaceholderAttribute>();
     if (attr != null)
         item.Placeholder = attr.Value;
 }
        private static void SetFiltering(IPropertySource source, PropertyItem item)
        {
            var filterOnlyAttr = source.GetAttribute<FilterOnlyAttribute>();
            var notFilterableAttr = source.GetAttribute<NotFilterableAttribute>();

            if (filterOnlyAttr != null && filterOnlyAttr.Value)
                item.FilterOnly = true;

            if (notFilterableAttr != null && notFilterableAttr.Value)
                item.NotFilterable = true;

            if (item.NotFilterable == true)
                return;

            var basedOnField = source.BasedOnField;

            Field idField;
            string idFieldName;
            var filteringIdField = source.GetAttribute<FilteringIdFieldAttribute>();
            if (filteringIdField != null)
            {
                idFieldName = filteringIdField.Value;
                idField = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
            }
            else
            {
                idFieldName = AutoDetermineIdField(basedOnField);

                idField = null;
                if (idFieldName != null)
                {
                    idField = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
                    if (Object.ReferenceEquals(idField, null) ||
                        (idField.TextualField != basedOnField.PropertyName &&
                         idField.TextualField != basedOnField.Name))
                    {
                        idField = null;
                        idFieldName = null;
                    }
                }
            }

            var valueType = source.ValueType;

            var filteringTypeAttr = source.GetAttribute<FilteringTypeAttribute>() ??
                idField.GetAttribute<FilteringTypeAttribute>();

            if (filteringTypeAttr == null)
            {
                var editorAttr = source.GetAttribute<EditorTypeAttribute>() ??
                    idField.GetAttribute<EditorTypeAttribute>();

                if (idFieldName != null)
                {
                    item.FilteringParams["idField"] = idFieldName;
                    item.FilteringIdField = idFieldName;
                }

                if (editorAttr != null && !standardFilteringEditors.Contains(editorAttr.EditorType))
                {
                    if (editorAttr is LookupEditorAttribute ||
                        editorAttr is AsyncLookupEditorAttribute)
                    {
                        var async = editorAttr as AsyncLookupEditorAttribute;
                        item.FilteringType = async != null ? "AsyncLookup" : "Lookup";
                        item.FilteringParams["lookupKey"] = async != null ? async.LookupKey : ((LookupEditorAttribute)editorAttr).LookupKey;
                    }
                    else
                    {
                        item.FilteringType = "Editor";
                        item.FilteringParams["editorType"] = editorAttr.EditorType;
                        item.FilteringParams["useLike"] = source.ValueType == typeof(String);
                    }
                }
                else if (source.EnumType != null)
                {
                    item.FilteringType = "Enum";
                    item.FilteringParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
                }
                else if (valueType == typeof(DateTime))
                {
                    if (!ReferenceEquals(null, basedOnField) && basedOnField is DateTimeField)
                    {
                        switch (((DateTimeField)basedOnField).DateTimeKind)
                        {
                            case DateTimeKind.Unspecified:
                                item.FilteringType = "Date";
                                break;
                            default:
                                item.FilteringType = "DateTime";
                                break;
                        }
                    }
                    else
                        item.FilteringType = "Date";
                }
                else if (valueType == typeof(Boolean))
                    item.FilteringType = "Boolean";
                else if (valueType == typeof(Decimal) ||
                    valueType == typeof(Double) ||
                    valueType == typeof(Single))
                {
                    item.FilteringType = "Decimal";
                }
                else if (valueType == typeof(Int32) ||
                    valueType == typeof(Int16) ||
                    valueType == typeof(Int64))
                {
                    item.FilteringType = "Integer";
                }
                else
                    item.FilteringType = "String";
            }
            else
            {
                item.FilteringType = filteringTypeAttr.FilteringType;
                filteringTypeAttr.SetParams(item.FilteringParams);

                if (item.FilteringType == "Editor")
                {
                    if (!item.FilteringParams.ContainsKey("editorType"))
                    {
                        var editorAttr =source.GetAttribute<EditorTypeAttribute>() ??
                            idField.GetAttribute<EditorTypeAttribute>();

                        if (editorAttr != null)
                            item.FilteringParams["editorType"] = editorAttr.EditorType;
                    }

                    if (!item.FilteringParams.ContainsKey("useLike"))
                    {
                        if (valueType == typeof(String))
                            item.FilteringParams["useLike"] = true;
                    }
                }

                object idFieldObj;
                if (item.FilteringParams.TryGetValue("idField", out idFieldObj) && idFieldObj is string)
                    item.FilteringIdField = (idFieldObj as string).TrimToNull();
                else
                    item.FilteringIdField = idFieldName;
            }

            var displayFormatAttr = source.GetAttribute<DisplayFormatAttribute>();
            if (displayFormatAttr != null)
                item.FilteringParams["displayFormat"] = displayFormatAttr.Value;

            foreach (var param in
                idField.GetAttributes<FilteringOptionAttribute>().Concat(
                source.GetAttributes<FilteringOptionAttribute>()))
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);

                if (key == "idField")
                    item.FilteringIdField = (param.Value as string) ?? item.FilteringIdField;

                item.FilteringParams[key] = param.Value;
            }
        }
        public static PropertyItem GetCustomFieldPropertyItem(ICustomFieldDefinition definition, Field basedOnField)
        {
            var pi = new PropertyItem();
            pi.Name = !ReferenceEquals(null, basedOnField) ? (basedOnField.PropertyName ?? basedOnField.Name) : definition.Name;
            pi.Category = definition.Category.TrimToNull();
            pi.ReadOnly = false;
            pi.Title = !ReferenceEquals(null, basedOnField) ? basedOnField.Title : definition.Title;
            pi.DefaultValue = definition.DefaultValue;
            pi.Insertable = ReferenceEquals(null, basedOnField) || ((basedOnField.Flags & FieldFlags.Insertable) == FieldFlags.Insertable);
            pi.Updatable = ReferenceEquals(null, basedOnField) || ((basedOnField.Flags & FieldFlags.Updatable) == FieldFlags.Updatable);
            pi.Localizable = definition.IsLocalizable;

            Type enumType = null;
            if (!ReferenceEquals(null, basedOnField) && basedOnField is IEnumTypeField)
            {
                enumType = (basedOnField as IEnumTypeField).EnumType;
                if (enumType != null && !enumType.IsEnum)
                    enumType = null;
            }

            if (!definition.EditorType.IsTrimmedEmpty())
            {
                pi.EditorType = definition.EditorType.TrimToNull();
            }
            else
            {
                if (enumType != null)
                    pi.EditorType = "Enum";
                else if (definition.FieldType == CustomFieldType.Date ||
                    definition.FieldType == CustomFieldType.DateTime)
                    pi.EditorType = "Date";
                else if (definition.FieldType == CustomFieldType.Boolean)
                    pi.EditorType = "Boolean";
                else if (definition.FieldType == CustomFieldType.Decimal)
                    pi.EditorType = "Decimal";
                else if (definition.FieldType == CustomFieldType.Int32 || definition.FieldType == CustomFieldType.Int64)
                    pi.EditorType = "Integer";
                else
                    pi.EditorType = "String";
            }

            if (enumType != null)
            {
                pi.EditorParams["enumKey"] = EnumMapper.GetEnumTypeKey(enumType);
            }

            if (!ReferenceEquals(null, basedOnField))
            {
                if (basedOnField is StringField &&
                    basedOnField.Size > 0)
                {
                    pi.EditorParams["maxLength"] = basedOnField.Size;
                    pi.MaxLength = basedOnField.Size;
                }

                if ((basedOnField.Flags & FieldFlags.NotNull) == FieldFlags.NotNull)
                    pi.Required = true;
            }

            if (definition.IsRequired)
                pi.Required = true;

            if (definition.Size != 0 &&
                definition.FieldType == CustomFieldType.String)
            {
                pi.MaxLength = definition.Size;
                pi.EditorParams["maxLength"] = definition.Size;
            }

            var editorOptionsJson = definition.EditorOptions.TrimToNull();
            if (editorOptionsJson != null &&
                editorOptionsJson.StartsWith("{"))
            {
                var editorOptions = JsonConvert.DeserializeObject<Dictionary<string, object>>(editorOptionsJson, JsonSettings.Tolerant);
                foreach (var option in editorOptions)
                    pi.EditorParams[option.Key] = option.Value;
            }

            return pi;
        }
 private void SetSortOrder(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<SortOrderAttribute>();
     if (attr != null && attr.SortOrder != 0)
         item.SortOrder = attr.SortOrder;
 }
 private void SetCssClass(IPropertySource source, PropertyItem item)
 {
     var attr = source.GetAttribute<CssClassAttribute>();
     if (attr != null)
         item.CssClass = attr.CssClass;
 }