Esempio n. 1
0
        /////<summary>
        ///// Возвращает редактируемый объект
        /////</summary>
        //public BaseEntityObject CurrentObject
        //{
        //    get { return _typeToImport; }
        //}
        #endregion

        #region Methods

        #region protected List<PropertyInfo> GetTypeProperties(Type type)
        protected List <PropertyInfo> GetTypeProperties(Type type)
        {
            if (type == null)
            {
                return(null);
            }
            //определение своиств, имеющих атрибут "отображаемое в списке"
            List <PropertyInfo> properties =
                type.GetProperties().Where(p => p.GetCustomAttributes(typeof(ExcelImportAttribute), false).Length != 0).ToList();

            //поиск своиств у которых задан порядок отображения
            //своиства, имеющие порядок отображения
            Dictionary <int, PropertyInfo> orderedProperties = new Dictionary <int, PropertyInfo>();
            //своиства, НЕ имеющие порядок отображения
            List <PropertyInfo> unOrderedProperties = new List <PropertyInfo>();

            foreach (PropertyInfo propertyInfo in properties)
            {
                ExcelImportAttribute lvda = (ExcelImportAttribute)
                                            propertyInfo.GetCustomAttributes(typeof(ExcelImportAttribute), false).FirstOrDefault();
                if (lvda.Order > 0)
                {
                    orderedProperties.Add(lvda.Order, propertyInfo);
                }
                else
                {
                    unOrderedProperties.Add(propertyInfo);
                }
            }

            var ordered = orderedProperties.OrderBy(p => p.Key).ToList();

            properties.Clear();
            properties.AddRange(ordered.Select(keyValuePair => keyValuePair.Value));
            properties.AddRange(unOrderedProperties);

            return(properties);
        }
Esempio n. 2
0
        /// <summary>
        /// Возвращает значение, показывающее является ли значение элемента управления допустимым
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidateData(out string message)
        {
            message = "";
            foreach (Control control in panelControls.Controls)
            {
                if (control is ThresholdControl)
                {
                    return(((ThresholdControl)control).ValidateData());
                }
                if (control is ComboBox)
                {
                    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    NotNullAttribute notNullAttribute =
                        (NotNullAttribute)propertyInfo.GetCustomAttributes(typeof(NotNullAttribute), false).FirstOrDefault();

                    object controlVal = ((ComboBox)control).SelectedItem;
                    if (controlVal == null && notNullAttribute != null)
                    {
                        if (message != "")
                        {
                            message += "\n ";
                        }
                        ExcelImportAttribute fca = (ExcelImportAttribute)
                                                   propertyInfo.GetCustomAttributes(typeof(ExcelImportAttribute), false).First();
                        message += $"'{fca.Title}' should not be empty";
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Возвращает значение, показывающее является ли значение элемента управления допустимым
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidateData(out string message)
        {
            message = "";
            foreach (Control control in _importingControls)
            {
                if (control is TextBox)
                {
                    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    if (propertyInfo.PropertyType.Name.ToLower() != "string")
                    {
                        continue;
                    }

                    string controlVal = control.Text;

                    NotNullAttribute notNullAttribute =
                        (NotNullAttribute)propertyInfo.GetCustomAttributes(typeof(NotNullAttribute), false).FirstOrDefault();
                    if (controlVal == null || (notNullAttribute != null && controlVal.Trim() == ""))
                    {
                        if (message != "")
                        {
                            message += "\n ";
                        }
                        ExcelImportAttribute fca = (ExcelImportAttribute)
                                                   propertyInfo.GetCustomAttributes(typeof(ExcelImportAttribute), false).First();
                        message += $"'{fca.Title}' should not be empty";
                        return(false);
                    }
                }
                if (control is RichTextBox)
                {
                    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    if (propertyInfo.PropertyType.Name.ToLower() != "string")
                    {
                        continue;
                    }

                    string controlVal = ((RichTextBox)control).Rtf;

                    NotNullAttribute notNullAttribute =
                        (NotNullAttribute)propertyInfo.GetCustomAttributes(typeof(NotNullAttribute), false).FirstOrDefault();
                    if (controlVal == null || (notNullAttribute != null && controlVal.Trim() == ""))
                    {
                        if (message != "")
                        {
                            message += "\n ";
                        }
                        ExcelImportAttribute fca = (ExcelImportAttribute)
                                                   propertyInfo.GetCustomAttributes(typeof(ExcelImportAttribute), false).First();
                        message += $"'{fca.Title}' should not be empty";
                        return(false);
                    }
                }
                //if (control is NumericUpDown && control.Tag != null)
                //{
                //    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                //    if (propertyInfo == null) continue;

                //    string typeName = propertyInfo.PropertyType.Name.ToLower();
                //    switch (typeName)
                //    {
                //        case "int32":
                //            {
                //                int val = (int)propertyInfo.GetValue(obj, null);
                //                int controlVal = (int)((NumericUpDown)control).Value;
                //                if (val != controlVal)
                //                    return true;

                //                break;
                //            }
                //        case "int16":
                //            {
                //                Int16 val = (Int16)propertyInfo.GetValue(obj, null);
                //                Int16 controlVal = (Int16)((NumericUpDown)control).Value;
                //                if (val != controlVal)
                //                    return true;

                //                break;
                //            }
                //        case "double":
                //            {
                //                double val = (double)propertyInfo.GetValue(obj, null);
                //                double controlVal = (double)((NumericUpDown)control).Value;
                //                if (val != controlVal)
                //                    return true;

                //                break;
                //            }
                //    }
                //}
                //if (control is DateTimePicker)
                //{
                //    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                //    if (propertyInfo == null) continue;

                //    DateTime val = (DateTime)propertyInfo.GetValue(obj, null);
                //    if (val != ((DateTimePicker)control).Value)
                //        return true;
                //}
                //if (control is CheckBox)
                //{
                //    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                //    if (propertyInfo == null) continue;

                //    bool val = (bool)propertyInfo.GetValue(obj, null);
                //    bool controlVal = ((CheckBox)control).Checked;
                //    if (val != controlVal)
                //        return true;
                //}
                if (control is AttachedFileControl)
                {
                    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    string m;
                    if (((AttachedFileControl)control).ValidateData(out m))
                    {
                        return(true);
                    }
                    if (m != "")
                    {
                        message += "\n ";
                    }

                    message += "Not set " + propertyInfo.Name;
                    return(false);
                }
                //if (control is LifelengthViewer)
                //{
                //    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                //    if (propertyInfo == null) continue;

                //    Lifelength val = (Lifelength)propertyInfo.GetValue(obj, null);
                //    Lifelength controlVal = ((LifelengthViewer)control).Lifelength;
                //    if (!val.IsEqual(controlVal))
                //        return true;
                //}
                if (control is DictionaryComboBox)
                {
                    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    object controlVal = ((DictionaryComboBox)control).SelectedItem;
                    if (controlVal == null)
                    {
                        if (message != "")
                        {
                            message += "\n ";
                        }

                        message += "Not set " + propertyInfo.Name;
                        return(false);
                    }
                }
                //if (control is ThresholdControl)
                //{
                //    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                //    if (propertyInfo == null) continue;

                //    object val = propertyInfo.GetValue(obj, null);
                //    object controlVal = ((ThresholdControl)control).Threshold;
                //    if (val.ToString() != controlVal.ToString())
                //        return true;
                //}
                if (control is ComboBox)
                {
                    PropertyInfo propertyInfo = control.Tag as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    object controlVal = ((ComboBox)control).SelectedItem;
                    if (controlVal == null)
                    {
                        if (message != "")
                        {
                            message += "\n ";
                        }

                        message += "Not set " + propertyInfo.Name;
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        protected void UpdateControl()
        {
            Text = $"{_typeToImport.Name} Import Form";

            panelControls.Controls.Clear();

            if (_typeToImport == null)
            {
                return;
            }

            List <PropertyInfo> properties = GetTypeProperties(_typeToImport);

            if (properties.Count == 0)
            {
                return;
            }

            int columnCount = 1;//количество колонок ЭУ

            List <Label>   labels   = new List <Label>();
            List <Control> controls = new List <Control>();
            Dictionary <PropertyInfo, Control> pairControls = new Dictionary <PropertyInfo, Control>();
            string errorMessage = "";

            foreach (PropertyInfo t in properties)
            {
                PropertyInfo         pairProperty = null;
                ExcelImportAttribute attr         =
                    (ExcelImportAttribute)t.GetCustomAttributes(typeof(ExcelImportAttribute), false).First();
                labels.Add(new Label {
                    Text = attr.Title, AutoSize = true
                });

                //Если значение должно быть не пустым или не NULL
                //то шрифт леибла утснанавливается в ЖИРНЫЙ(BOLD)
                NotNullAttribute notNullAttribute =
                    (NotNullAttribute)t.GetCustomAttributes(typeof(NotNullAttribute), false).FirstOrDefault();
                if (notNullAttribute != null)
                {
                    labels.Last().Font = new Font(labels.Last().Font, FontStyle.Bold);
                }

                Control c = null;
                if (!string.IsNullOrEmpty(attr.PairControlPropertyName))
                {
                    pairProperty = _typeToImport.GetProperty(attr.PairControlPropertyName);
                }

                if (pairProperty != null)
                {
                    try
                    {
                        c = GetControl(pairProperty, attr.PairControlWidht, attr.PairControlEnabled);
                    }
                    catch (Exception ex)
                    {
                        if (errorMessage != "")
                        {
                            errorMessage += "\n ";
                        }
                        errorMessage += $"Pair property '{attr.PairControlPropertyName}' raise error {ex.Message}";
                    }
                    if (c is ThresholdControl)
                    {
                        columnCount = 2;
                    }
                    pairControls.Add(t, c);
                }

                c = null;

                try
                {
                    int cw = pairProperty != null ? attr.Width - attr.PairControlWidht - 5 : attr.Width;
                    c = GetControl(t, cw, true);
                }
                catch (Exception ex)
                {
                    if (errorMessage != "")
                    {
                        errorMessage += "\n ";
                    }
                    errorMessage += $"'{attr.Title}' raise error {ex.Message}";
                }
                if (c is LifelengthViewer)
                {
                    ((LifelengthViewer)c).LeftHeader = attr.Title;
                }
                if (c is ThresholdControl)
                {
                    columnCount = 2;
                }
                controls.Add(c);
            }

            #region Компоновка контролов в одну колонку
            if (columnCount == 1)
            {
                #region расчет длины лейблов и контролов

                int maxLabelXSize   = 0;
                int maxControlXSize = 0;//максимальная длиня ЭУ
                for (int i = 0; i < labels.Count; i++)
                {
                    //на каждый лейбл приходится по одному контролу,
                    //поэтому обе коллекции просматриваются одновременно
                    if (controls[i] != null && controls[i] is LifelengthViewer)
                    {
                        LifelengthViewer llv = (LifelengthViewer)controls[i];
                        if (llv.LeftHeaderWidth > maxLabelXSize)
                        {
                            maxLabelXSize = llv.LeftHeaderWidth;
                        }
                        if (llv.WidthWithoutLeftHeader > maxControlXSize)
                        {
                            maxControlXSize = llv.WidthWithoutLeftHeader;
                        }
                        if (i > 0 && controls[i - 1] != null && controls[i - 1] is LifelengthViewer)
                        {
                            llv.ShowHeaders = false;
                        }
                        continue;
                    }
                    //выше рассматривались контролы, имеющие собственные лейблы
                    //теперь идет просмотр самих леблов
                    if (labels[i].PreferredWidth > maxLabelXSize)
                    {
                        maxLabelXSize = labels[i].PreferredWidth;
                    }
                    //размеры контролов
                    if (controls[i] != null && controls[i].Size.Width > maxControlXSize)
                    {
                        maxControlXSize = controls[i].Size.Width;
                    }
                }
                #endregion

                for (int i = 0; i < properties.Count; i++)
                {
                    Control pairControl = null;
                    if (pairControls.ContainsKey(properties[i]))
                    {
                        pairControl = pairControls[properties[i]];
                    }

                    if (i == 0)
                    {
                        labels[i].Location = new Point(3, 3);
                        if (controls[i] != null)
                        {
                            if (controls[i] is LifelengthViewer)
                            {
                                controls[i].Location =
                                    new Point((maxLabelXSize + labels[i].Location.X) - ((LifelengthViewer)controls[i]).LeftHeaderWidth, 3);
                            }
                            else
                            {
                                if (pairControl != null)
                                {
                                    pairControl.Location = new Point(maxLabelXSize + labels[i].Location.X + 5, 3);

                                    controls[i].Location = new Point(pairControl.Location.X + pairControl.Size.Width + 5, 3);
                                    controls[i].Width    = maxControlXSize - (pairControl.Size.Width + 5);
                                }
                                else
                                {
                                    controls[i].Location = new Point(maxLabelXSize + labels[i].Location.X + 5, 3);
                                    controls[i].Width    = maxControlXSize;
                                }
                            }
                        }
                    }
                    else
                    {
                        Point labelLocation = new Point(3, 0);
                        if (controls[i - 1] != null)
                        {
                            labelLocation.Y = controls[i - 1].Location.Y + controls[i - 1].Size.Height + 5;
                        }
                        else
                        {
                            labelLocation.Y = labels[i - 1].Location.Y + labels[i - 1].Size.Height + 5;
                        }

                        labels[i].Location = labelLocation;
                        if (controls[i] != null)
                        {
                            if (controls[i] is LifelengthViewer)
                            {
                                controls[i].Location =
                                    new Point((maxLabelXSize + labelLocation.X) - ((LifelengthViewer)controls[i]).LeftHeaderWidth, labelLocation.Y);
                            }
                            else
                            {
                                if (pairControl != null)
                                {
                                    pairControl.Location = new Point(maxLabelXSize + labelLocation.X + 5, labelLocation.Y);

                                    controls[i].Location = new Point(pairControl.Location.X + pairControl.Size.Width + 5, labelLocation.Y);
                                    controls[i].Width    = maxControlXSize - (pairControl.Size.Width + 5);
                                }
                                else
                                {
                                    controls[i].Location = new Point(maxLabelXSize + labelLocation.X + 5, labelLocation.Y);
                                    controls[i].Width    = maxControlXSize;
                                }
                            }
                        }
                    }


                    if (controls[i] == null || (controls[i] != null && !(controls[i] is LifelengthViewer)))
                    {
                        //Если контрол не является LifelengthViewer-ом то нужно добавить лейбл
                        panelControls.Controls.Add(labels[i]);
                    }
                    if (pairControl != null)
                    {
                        panelControls.Controls.Add(pairControl);
                    }

                    panelControls.Controls.Add(controls[i]);
                }
            }
            #endregion

            #region Компоновка контролов в две колонки
            if (columnCount == 2)
            {
                #region расчет длины лейблов и контролов

                int  fMaxLabelXSize = 0, sMaxLabelXSize = 0;
                int  fMaxControlXSize = 0, sMaxControlXSize = 0;
                bool checkFirst = true;//флаг проверяемой колонки

                for (int i = 0; i < labels.Count; i++)
                {
                    //на каждый лейбл приходится по одному контролу,
                    //поэтому обе коллекции просматриваются одновременно
                    if (controls[i] != null && controls[i] is ThresholdControl)
                    {
                        //контрол порога выполнения директивы
                        //влияет на длину лейблов обоих колонок
                        ThresholdControl ddtc = (ThresholdControl)controls[i];
                        //размеры заголовков
                        if (ddtc.MaxFirstColLabelWidth > fMaxLabelXSize)
                        {
                            fMaxLabelXSize = ddtc.MaxFirstColLabelWidth;
                        }
                        if (ddtc.MaxSecondColLabelWidth > sMaxLabelXSize)
                        {
                            sMaxLabelXSize = ddtc.MaxSecondColLabelWidth;
                        }

                        //размеры контролов
                        if (ddtc.MaxFirstColControlWidth > fMaxControlXSize)
                        {
                            fMaxControlXSize = ddtc.MaxFirstColControlWidth;
                        }
                        if (ddtc.MaxSecondColControlWidth > sMaxControlXSize)
                        {
                            sMaxControlXSize = ddtc.MaxSecondColControlWidth;
                        }

                        //т.к. DetailDirectiveThresholdControl занимает 2 колонки,
                        //то следующий контрол всегда будет располагаться в первой колонке
                        checkFirst = true;
                        continue;
                    }

                    if (controls[i] != null && controls[i] is LifelengthViewer)
                    {
                        LifelengthViewer llv = (LifelengthViewer)controls[i];

                        if (checkFirst && llv.LeftHeaderWidth > fMaxLabelXSize)
                        {
                            fMaxLabelXSize = llv.LeftHeaderWidth;
                        }
                        if (!checkFirst && llv.LeftHeaderWidth > sMaxLabelXSize)
                        {
                            sMaxLabelXSize = llv.LeftHeaderWidth;
                        }

                        //размеры контролов
                        if (checkFirst && llv.WidthWithoutLeftHeader > fMaxControlXSize)
                        {
                            fMaxControlXSize = llv.WidthWithoutLeftHeader;
                        }
                        if (!checkFirst && llv.WidthWithoutLeftHeader > sMaxControlXSize)
                        {
                            sMaxControlXSize = llv.WidthWithoutLeftHeader;
                        }

                        checkFirst = !checkFirst;
                        continue;
                    }

                    //выше рассматривались контролы, имеющие собственные лейблы
                    //теперь идет просмотр самих леблов
                    if (checkFirst && labels[i].PreferredWidth > fMaxLabelXSize)
                    {
                        fMaxLabelXSize = labels[i].PreferredWidth;
                    }
                    if (!checkFirst && labels[i].PreferredWidth > sMaxLabelXSize)
                    {
                        sMaxLabelXSize = labels[i].PreferredWidth;
                    }

                    //размеры контролов
                    if (controls[i] != null)
                    {
                        if (checkFirst && controls[i].Size.Width > fMaxControlXSize)
                        {
                            fMaxControlXSize = controls[i].Size.Width;
                        }
                        if (!checkFirst && controls[i].Size.Width > sMaxControlXSize)
                        {
                            sMaxControlXSize = controls[i].Size.Width;
                        }
                    }

                    checkFirst = !checkFirst;
                }
                #endregion

                checkFirst = true;
                const int firstCol  = 3;
                int       secondCol = (3 + fMaxLabelXSize + 5 + fMaxControlXSize + 50);
                for (int i = 0; i < labels.Count; i++)
                {
                    int top, left, labelSize, controlsize;
                    if (i == 0)
                    {
                        top         = 3;
                        left        = firstCol;
                        labelSize   = fMaxLabelXSize;
                        controlsize = fMaxControlXSize;
                    }
                    else
                    {
                        if (checkFirst ||
                            (controls[i] != null && controls[i] is ThresholdControl))
                        {
                            left        = firstCol;
                            labelSize   = fMaxLabelXSize;
                            controlsize = fMaxControlXSize;

                            //определение самого нижнего Bottoma 2-х предыдущих контролов
                            int bottom1, bottom2;
                            //определение нижней точки предыдущего контрола
                            //(он будет либо во второй колонке предыдущего ряда, либо занимать весь ряд)
                            if (controls[i - 1] != null)
                            {
                                bottom2 = controls[i - 1].Bottom + 5;
                            }
                            else
                            {
                                bottom2 = labels[i - 1].Bottom + 5;
                            }
                            //определение нижней точки пред-предыдущего контрола
                            //он может и отсутствовать
                            if ((i - 2) >= 0)
                            {
                                if (controls[i - 2] != null)
                                {
                                    bottom1 = controls[i - 2].Bottom + 5;
                                }
                                else
                                {
                                    bottom1 = labels[i - 2].Bottom + 5;
                                }
                            }
                            else
                            {
                                bottom1 = 0;
                            }

                            top = bottom1 > bottom2 ? bottom1 : bottom2;
                        }
                        else
                        {
                            left        = secondCol;
                            labelSize   = sMaxLabelXSize;
                            controlsize = sMaxControlXSize;

                            top = controls[i - 1] != null ? controls[i - 1].Location.Y : labels[i - 1].Location.Y;
                        }
                    }

                    if (controls[i] != null && controls[i] is ThresholdControl)
                    {
                        ThresholdControl ddtc = (ThresholdControl)controls[i];
                        controls[i].Location = new Point(3, top);
                        //выравнивание первой колонки
                        ddtc.SetFirstColumnPos(firstCol + fMaxLabelXSize);
                        //выравнивание второй колонки
                        ddtc.SetSecondColumnPos(secondCol + sMaxLabelXSize);

                        panelControls.Controls.Add(controls[i]);
                        checkFirst = true;
                        continue;
                    }

                    if (controls[i] != null && controls[i] is LifelengthViewer)
                    {
                        controls[i].Location =
                            new Point((labelSize + left) - ((LifelengthViewer)controls[i]).LeftHeaderWidth, top);
                        panelControls.Controls.Add(controls[i]);
                        checkFirst = !checkFirst;
                        continue;
                    }

                    labels[i].Location = new Point(left, top);
                    if (controls[i] != null)
                    {
                        controls[i].Location = new Point(labelSize + left + 5, top);
                        controls[i].Width    = controlsize;
                    }
                    panelControls.Controls.Add(labels[i]);
                    panelControls.Controls.Add(controls[i]);
                    checkFirst = !checkFirst;
                }
            }
            #endregion

            if (errorMessage != "")
            {
                MessageBox.Show(errorMessage, (string)new GlobalTermsProvider()["SystemName"], MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }