private void filed_changed(object sender, SelectionChangedEventArgs e)
        {
            var controlop  = new ControlAndOperator();
            var pinfo      = field_cb.SelectedValue as PropertyInfo;
            var ctlcontext = new FormItemContext(_type, pinfo);

            controlop.InputControl = InputControlBuilder.CreateControl(ctlcontext);
            if (pinfo.PropertyType.Name.Equals("string", StringComparison.OrdinalIgnoreCase))
            {
                controlop.UseStringOperator = true;
            }
            else
            {
                controlop.UseNumberOperator = true;
            }

            if (CreateControlCallback != null)
            {
                controlop = CreateControlCallback(ctlcontext, controlop);
            }

            _bindobject = Activator.CreateInstance(_type);
            controlop.InputControl.DataContext = _bindobject;
            input_br.Child = controlop.InputControl;

            operator_cb.ItemsSource = null;
            if (controlop.UseStringOperator)
            {
                operator_cb.ItemsSource = EnumNameValuePair.EnumToList(typeof(StringOperator));
            }
            else if (controlop.UseNumberOperator)
            {
                operator_cb.ItemsSource = EnumNameValuePair.EnumToList(typeof(NumberOperator));
            }

            operator_cb.SelectedIndex = 0;

            if (pinfo.PropertyType.IsEnum)
            {
                operator_cb.IsEnabled = false;
            }
            else
            {
                operator_cb.IsEnabled = true;
            }
        }
Ejemplo n.º 2
0
        public override FrameworkElement CreateControl(FormItemContext context)
        {
            ComboBox cb       = new ComboBox();
            var      enumtype = context.PropertyInfo.PropertyType;
            var      source   = EnumNameValuePair.EnumToList(enumtype);

            cb.ItemsSource       = source;
            cb.SelectedValuePath = "Value";

            var binding = new Binding(context.PropertyInfo.Name)
            {
                Mode = BindingMode.TwoWay,
            };

            binding.ValidationRules.Add(new AnnotationValidationRule(context.PropertyInfo));
            cb.SetBinding(ComboBox.SelectedValueProperty, binding);
            CustomValidation.SetValidationProperty(cb, ComboBox.SelectedValueProperty);
            StyleHelper.ApplyStyle(cb, FormControlConstrants.EDIT_COMBO_STYLE);
            return(cb);
        }
        void Initialize(Type datatype)
        {
            var logiclist = EnumNameValuePair.EnumToList(typeof(Logical));

            logic_cb.ItemsSource = logiclist;
            if (IsFirstRow)
            {
                logic_cb.IsEnabled = false;
            }
            else
            {
                logic_cb.IsEnabled     = true;
                logic_cb.SelectedIndex = 0;
            }

            var proplist = PropertyNameValuePair.TypeToList(datatype);

            if (DetermineFieldCallback != null)
            {
                proplist = DetermineFieldCallback(datatype, proplist);
            }
            field_cb.ItemsSource   = proplist;
            field_cb.SelectedIndex = 0;
        }
Ejemplo n.º 4
0
        private void btnEnumValues_Click(object sender, EventArgs e)
        {
            List <string> enumFieldNames = new List <string>();
            //Dictionary<string, StringBuilder> enumFieldComments = new Dictionary<string, StringBuilder>();
            Dictionary <string, List <string> >            enumFieldsPaths  = new Dictionary <string, List <string> >();
            Dictionary <string, List <EnumNameValuePair> > enumFieldsValues = new Dictionary <string, List <EnumNameValuePair> >();
            XNamespace ns             = "http://www.w3.org/2001/XMLSchema";
            XElement   fnSchema       = XElement.Load(txtSchemaFile.Text);
            var        enumCodeFields = from fld in fnSchema.Descendants(ns + "element")
                                        where fld.Attribute("type")?.Value == EnumCodeType
                                        select fld;

            foreach (var fld in enumCodeFields)
            {
                var fldName = fld.Attribute("name").Value;
                var path    = string.Join("/", fld.AncestorsAndSelf().Where(x => x.Attribute("name") != null).Select(x => x.Attribute("name")?.Value).Reverse().ToArray());
                //StringBuilder sbComments;
                if (!enumFieldNames.Contains(fldName))
                {
                    var fldComments = fld.Element(ns + "annotation").Element(ns + "documentation").Value;
                    enumFieldNames.Add(fldName);
                    //sbComments = new StringBuilder();
                    //sbComments.Append("    /// Field Name: ");
                    //sbComments.AppendLine(fldName);
                    //sbComments.Append("    /// Path://");
                    //sbComments.AppendLine( string.Join("/", fld.AncestorsAndSelf().Where(x => x.Attribute("name") != null).Select(x => x.Attribute("name")?.Value).Reverse().ToArray()));
                    //enumFieldComments.Add(fldName, sbComments);
                    enumFieldsPaths.Add(fldName, new List <string>());
                    enumFieldsValues.Add(fldName, new List <EnumNameValuePair>());
                    var comLines = fldComments.Split('\n');
                    foreach (var line in comLines)
                    {
                        var trimLine = line.Trim();
                        if (trimLine.Length == 0)
                        {
                            continue;
                        }
                        char firstChar = trimLine.ToArray()[0];
                        if (!Char.IsNumber(firstChar))
                        {
                            continue;
                        }
                        var lineArr = trimLine.Split(' ');
                        var val     = int.Parse(lineArr[0].Trim(':').Trim('-'));
                        var name    = trimLine.Substring(lineArr[0].Length);
                        name = name.Trim();
                        name = name.Trim('-');
                        name = name.Trim(',');
                        name = name.Trim('.');
                        name = name.Trim('\r');
                        name = name.Trim();
                        var pair = new EnumNameValuePair()
                        {
                            Name = name, Value = val
                        };
                        enumFieldsValues[fldName].Add(pair);
                    }
                }
                else
                {
                    //sbComments = enumFieldComments[fldName];
                    //sbComments.Append("    /// Path://");
                    //sbComments.AppendLine(string.Join("/", fld.AncestorsAndSelf().Where(x => x.Attribute("name") != null).Select(x => x.Attribute("name")?.Value).Reverse().ToArray()));
                }
                enumFieldsPaths[fldName].Add(path);
            }
            StreamWriter fileCs = new StreamWriter(Path.Combine(txtTargetFolder.Text, CSharpFileName));

            fileCs.WriteLine(string.Format(CodeHeader, DateTime.Now));
            fileCs.WriteLine("{");
            XmlDocument doc  = new XmlDocument();
            var         root = doc.CreateElement("enumFields");

            doc.AppendChild(root);
            var xmlComments = doc.CreateElement("comments");

            xmlComments.InnerText = string.Format("This document is generated from FN_Schema.xsd by CodeGeneration utility on {0}", DateTime.Now);
            root.AppendChild(xmlComments);
            //StreamWriter fileSql = new StreamWriter(Path.Combine(txtTargetFolder.Text, SqlFileName));
            foreach (var fldName in enumFieldNames)
            {
                var xmlFld = doc.CreateElement("enumField");
                root.AppendChild(xmlFld);
                var xmlAttrFldName = doc.CreateAttribute("fieldName");
                xmlAttrFldName.Value = fldName;
                xmlFld.Attributes.Append(xmlAttrFldName);

                fileCs.WriteLine("    /// <summary>");
                fileCs.Write("    /// <fieldName>");
                fileCs.Write(fldName);
                fileCs.WriteLine("</fieldName>");
                var xmlPaths = doc.CreateElement("paths");
                xmlFld.AppendChild(xmlPaths);
                var xmlOptions = doc.CreateElement("options");
                xmlFld.AppendChild(xmlOptions);
                foreach (var path in enumFieldsPaths[fldName])
                {
                    var xmlPath = doc.CreateElement("path");
                    xmlPath.InnerText = path;
                    xmlPaths.AppendChild(xmlPath);
                    fileCs.Write("    /// <path>");
                    fileCs.Write(path);
                    fileCs.WriteLine("</path>");
                }
                //fileCs.Write(enumFieldComments[fldName].ToString());
                fileCs.WriteLine("    /// </summary>");
                fileCs.Write("    public enum ");
                fileCs.WriteLine(fldName);
                fileCs.WriteLine("    {");
                foreach (var pair in enumFieldsValues[fldName])
                {
                    var xmlOption = doc.CreateElement("option");
                    xmlOptions.AppendChild(xmlOption);
                    var xmlAttrName  = doc.CreateAttribute("name");
                    var xmlAttrValue = doc.CreateAttribute("value");
                    xmlOption.Attributes.Append(xmlAttrValue);
                    xmlOption.Attributes.Append(xmlAttrName);
                    xmlAttrName.Value  = pair.Name;
                    xmlAttrValue.Value = pair.Value.ToString();
                    fileCs.Write("        ");
                    var enumName = pair.Name.Replace("+", "and");
                    enumName = enumName.Replace("N/A", "NA");
                    enumName = enumName.Replace("%", "");
                    enumName = enumName.Replace("'", "");
                    enumName = enumName.Replace(".", "");
                    enumName = enumName.Replace("(", "");
                    enumName = enumName.Replace(")", "");
                    enumName = enumName.Replace('-', '_');
                    enumName = enumName.Replace(' ', '_');
                    enumName = enumName.Replace("/", "_or_");
                    enumName = enumName.Replace("__", "_");
                    enumName = enumName.Replace("__", "_");
                    enumName = enumName.Replace("__", "_");
                    if (char.IsDigit(enumName.ToArray()[0]))
                    {
                        enumName = "_" + enumName;
                    }
                    fileCs.Write(enumName);
                    fileCs.Write(" = ");
                    fileCs.Write(pair.Value);
                    fileCs.WriteLine(",");
                }
                fileCs.WriteLine("    }");
                fileCs.WriteLine();
                fileCs.WriteLine();
            }
            fileCs.WriteLine("}");
            fileCs.Close();
            fileCs.Dispose();
            doc.Save(Path.Combine(txtTargetFolder.Text, XmlDocFileName));
            if (MessageBox.Show("Code and Documents generated sucessfully, quit?", "Generated", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Close();
            }
        }