/// <summary> /// Analyse class and save analyse into dictionary /// </summary> /// <param name="analysedClass">Type of class to by analysed</param> private void DoAnalysis(Type analysedClass) { Dictionary <string, PropertyAnalyze> analysedClassDictionary = new Dictionary <string, PropertyAnalyze>(); List <Type> linkedTablesRelations = new List <Type>(); foreach (var prop in analysedClass.GetProperties()) { var attributes = prop.GetCustomAttributes(true).ToList(); if (attributes.Contains(typeof(DbIgnoreAttribute)) && attributes.Contains(typeof(LinkedTableAttribute))) { throw new NotSupportedAttributeCombinationException("Linked tables properties can't be ignored in DB."); } LinkedTableAttribute linkedTableInfo = attributes.FirstOrDefault(x => x.GetType() == typeof(LinkedTableAttribute)) as LinkedTableAttribute; if (linkedTableInfo != null) { if ((linkedTableInfo.LinkedTableRelation == Enum.LinkedTableRelation.Many && prop.PropertyType != typeof(List <int>)) || (linkedTableInfo.LinkedTableRelation == Enum.LinkedTableRelation.One && prop.PropertyType != typeof(int))) { throw new NotSupportedAttributeCombinationException("Property type can't be used with this type of Linked Table attribute"); } if (linkedTablesRelations.Contains(linkedTableInfo.LinkedTableType)) { throw new NotSupportedAttributeCombinationException("Only one linked table relation for linked table."); } else { linkedTablesRelations.Add(linkedTableInfo.LinkedTableType); } } UIParamsAttribute uiParams = attributes.FirstOrDefault(x => x.GetType() == typeof(UIParamsAttribute)) as UIParamsAttribute; if (uiParams != null && string.IsNullOrEmpty(uiParams.LabelDescription) && uiParams.UseLabelDescription) { throw new NotSupportedAttributeCombinationException("UIParams attribute must specify LabelDescription if UseLabelDescription is true."); } var analyze = new PropertyAnalyze(prop.Name, prop.PropertyType, attributes); analysedClassDictionary.Add(prop.Name, analyze); } analysedClassesDictionary.Add(analysedClass, analysedClassDictionary); }
/// <summary> /// Create editable controls /// </summary> /// <param name="controlAnalyze">Analyze of property</param> /// <param name="previousControl">Previous control</param> /// <param name="uiModule">UI module</param> /// <returns>New control</returns> public static UIElement CreateEditableControl(KeyValuePair <string, PropertyAnalyze> controlAnalyze, ref UIElement previousControl, UIModule uiModule) { string controlName = controlAnalyze.Key; PropertyAnalyze controlData = controlAnalyze.Value; PropertyType controlTypeName = controlData.PropertyType; UIElement control; UIParamsAttribute customization = controlData.PropertyAttributes.FirstOrDefault(x => x.GetType() == typeof(UIParamsAttribute)) as UIParamsAttribute; var linkedTableAttribute = controlData.PropertyAttributes.FirstOrDefault(x => x.GetType() == typeof(LinkedTableAttribute)) as LinkedTableAttribute; if (linkedTableAttribute != null) { switch (linkedTableAttribute.LinkedTableRelation) { case LinkedTableRelation.One: control = LinkedTableSingleSelectorControl.CreateLinkedTableControl(controlName, controlData, controlTypeName, linkedTableAttribute); break; case LinkedTableRelation.Many: control = LinkedTableMultiSelectorControl.CreateLinkedTableControl(controlName, controlData, controlTypeName, linkedTableAttribute); break; default: throw new NotSupportedPropertyTypeException("Not supported LinkedTableRelation."); } } else { switch (controlTypeName) { case PropertyType.String: case PropertyType.Int: case PropertyType.Int32: case PropertyType.Double: case PropertyType.Char: case PropertyType.Decimal: case PropertyType.Float: control = new TextBox() { Name = controlName + Constants.DATA_CONTROL_IDENTIFIER, Margin = new Thickness(10), TextWrapping = TextWrapping.Wrap, PlaceholderText = "Insert " + controlName }; if (controlTypeName == PropertyType.Char) { (control as TextBox).MaxLength = 1; } if (customization != null && customization.UseLongTextInput) { (control as TextBox).Height = 150; } break; case PropertyType.Boolean: control = new CheckBox() { Content = controlName, Margin = new Thickness(10), Name = controlName + Constants.DATA_CONTROL_IDENTIFIER }; break; case PropertyType.DateTime: if (customization == null) { throw new MissingRequiredAdditionalDataException("Property DateTime require UIParams attribute for specificating design."); } control = new Grid() { Margin = new Thickness(10) }; RowDefinition labelRow = new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }; RowDefinition dateTimeRow = new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }; (control as Grid).RowDefinitions.Add(labelRow); (control as Grid).RowDefinitions.Add(dateTimeRow); TextBlock label = new TextBlock() { Name = controlName + Constants.LABEL_CONTROL_IDENTIFIER, Text = customization == null?controlTypeName.ToString() : customization.LabelDescription, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 5) }; Grid.SetRow(label, 0); (control as Grid).Children.Add(label); //if (customization.ReadOnlyMode) //{ // TextBox data = new TextBox() // { // Text = "", // VerticalAlignment = VerticalAlignment.Center, // Margin = new Thickness(0, 0, 0, 5), // Name = controlName + DATA_CONTROL_IDENTIFIER // }; //} //else //{ // TextBlock data = new TextBlock() // { // Text = "", // VerticalAlignment = VerticalAlignment.Center, // Margin = new Thickness(0, 0, 0, 5), // Name = controlName + DATA_CONTROL_IDENTIFIER // }; //} UIElement dateTimeControl; switch (customization.DateTimeMode) { case DatePickerMode.Date: dateTimeControl = new CalendarDatePicker() { Date = DateTime.Today, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new Thickness(0, 5, 0, 0), PlaceholderText = "Select a date", Name = controlName + Constants.DATA_CONTROL_IDENTIFIER }; break; case DatePickerMode.Time: dateTimeControl = new TimePicker() { Time = DateTime.Now.TimeOfDay, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new Thickness(0, 5, 0, 0), Name = controlName + Constants.DATA_CONTROL_IDENTIFIER }; break; case DatePickerMode.DateAndTime: throw new Base.Exceptions.NotSupportedException("DateTime combination is not supported."); default: throw new Base.Exceptions.NotSupportedException("Not supported DatePickerMode."); } Grid.SetRow(dateTimeControl as FrameworkElement, 1); (control as Grid).Children.Add(dateTimeControl); break; case PropertyType.notImplementedYet: control = new Grid() { Background = new SolidColorBrush(Colors.Red), Margin = new Thickness(10), Height = 25 }; break; default: throw new NotSupportedPropertyTypeException("Not supported PropertyType."); } } RelativePanel.SetAlignLeftWithPanel(control, true); RelativePanel.SetAlignRightWithPanel(control, true); AddControlUnder(control, ref previousControl); return(control); }
/// <summary> /// Create readonly controls /// </summary> /// <param name="controlAnalyze">Analyze of property</param> /// <param name="previousControl">Previous control</param> /// <returns>New control</returns> internal static UIElement CreateDetailControl(KeyValuePair <string, PropertyAnalyze> controlAnalyze, ref UIElement previousControl) { string controlName = controlAnalyze.Key; PropertyAnalyze controlData = controlAnalyze.Value; PropertyType controlTypeName = controlData.PropertyType; UIElement control; UIParamsAttribute customization = controlData.PropertyAttributes.FirstOrDefault(x => x.GetType() == typeof(UIParamsAttribute)) as UIParamsAttribute; var linkedTableAttribute = controlData.PropertyAttributes.FirstOrDefault(x => x.GetType() == typeof(LinkedTableAttribute)) as LinkedTableAttribute; switch (controlTypeName) { case PropertyType.String: case PropertyType.Int: case PropertyType.Int32: case PropertyType.Double: case PropertyType.Char: case PropertyType.DateTime: case PropertyType.Boolean: if (customization == null && controlTypeName == PropertyType.DateTime) { throw new MissingRequiredAdditionalDataException("Property DateTime require UIParams attribute for specificating design."); } control = new Grid() { Margin = new Thickness(10), VerticalAlignment = VerticalAlignment.Stretch }; RowDefinition labelRow = new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }; RowDefinition dateTimeRow = new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }; (control as Grid).RowDefinitions.Add(labelRow); (control as Grid).RowDefinitions.Add(dateTimeRow); ColumnDefinition labelColumn = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) }; ColumnDefinition dataColumn = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) }; (control as Grid).ColumnDefinitions.Add(labelColumn); (control as Grid).ColumnDefinitions.Add(dataColumn); TextBlock label = new TextBlock() { VerticalAlignment = VerticalAlignment.Bottom, FontWeight = FontWeights.Bold, Margin = new Thickness(0, 0, 5, 0) }; if (customization.UseLabelDescription) { label.Text = customization.LabelDescription ?? ""; } else { label.Text = controlData.PropertyName; } Grid.SetRow(label, 0); (control as Grid).Children.Add(label); if (linkedTableAttribute != null && linkedTableAttribute.LinkedTableRelation == LinkedTableRelation.Many) { ListView linkedTableSelectedIds = new ListView() { Name = controlName + Constants.DATA_CONTROL_IDENTIFIER, VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new Thickness(0, 5, 0, 5), Height = 250, SelectionMode = ListViewSelectionMode.None }; Grid.SetRow(linkedTableSelectedIds, 1); Grid.SetColumnSpan(linkedTableSelectedIds, 2); (control as Grid).Children.Add(linkedTableSelectedIds); } else { TextBlock data = new TextBlock() { Text = "", VerticalAlignment = VerticalAlignment.Bottom, Margin = new Thickness(0, 5, 0, 0), Name = controlName + Constants.DATA_CONTROL_IDENTIFIER }; (control as Grid).Children.Add(data); if (customization.ShowDetailOnOneLine) { data.Margin = new Thickness(5, 5, 0, 0); Grid.SetColumn(data, 1); } else { Grid.SetRow(data, 1); } } break; case PropertyType.notImplementedYet: control = new Grid() { Background = new SolidColorBrush(Colors.Red), Margin = new Thickness(10), Height = 25 }; break; default: throw new NotSupportedPropertyTypeException("Not supported PropertyType."); } RelativePanel.SetAlignLeftWithPanel(control, true); RelativePanel.SetAlignRightWithPanel(control, true); AddControlUnder(control, ref previousControl); return(control); }