Exemple #1
0
        private void OnMenuAddProperties(object sender, EventArgs e)
        {
            NodeShape shapeElement = CurrentSelection.OfType <ClassShape>().FirstOrDefault();

            if (shapeElement?.ModelElement is ModelClass element)
            {
                AddCodeForm codeForm = new AddCodeForm(element);
                if (codeForm.ShowDialog() == DialogResult.OK)
                {
                    using (Transaction tx = element.Store.TransactionManager.BeginTransaction("AddProperties"))
                    {
                        element.Attributes.Clear();
                        IEnumerable <ModelAttribute> modelAttributes =
                            codeForm.Lines
                            .Select(s => ModelAttribute.Parse(element.ModelRoot, s))
                            .Where(attr => attr != null)
                            .Select(parseResult => new ModelAttribute(element.Store,
                                                                      new PropertyAssignment(ModelAttribute.NameDomainPropertyId, parseResult.Name),
                                                                      new PropertyAssignment(ModelAttribute.TypeDomainPropertyId, parseResult.Type ?? "String"),
                                                                      new PropertyAssignment(ModelAttribute.RequiredDomainPropertyId, parseResult.Required ?? true),
                                                                      new PropertyAssignment(ModelAttribute.MaxLengthDomainPropertyId, parseResult.MaxLength ?? 0),
                                                                      new PropertyAssignment(ModelAttribute.InitialValueDomainPropertyId, parseResult.InitialValue),
                                                                      new PropertyAssignment(ModelAttribute.IsIdentityDomainPropertyId, parseResult.IsIdentity),
                                                                      new PropertyAssignment(ModelAttribute.SetterVisibilityDomainPropertyId, parseResult.SetterVisibility ?? SetterAccessModifier.Public)
                                                                      ));
                        element.Attributes.AddRange(modelAttributes);
                        tx.Commit();
                    }
                }
            }
        }
Exemple #2
0
        private void OnMenuAddValues(object sender, EventArgs e)
        {
            NodeShape shapeElement = CurrentSelection.OfType <EnumShape>().FirstOrDefault();

            if (shapeElement?.ModelElement is ModelEnum element)
            {
                AddCodeForm codeForm = new AddCodeForm(element);

                if (codeForm.ShowDialog() == DialogResult.OK)
                {
                    using (Transaction tx = element.Store.TransactionManager.BeginTransaction("AddValues"))
                    {
                        element.Values.Clear();

                        foreach (string codeFormLine in codeForm.Lines)
                        {
                            try
                            {
                                string[] parts = codeFormLine.Replace(",", string.Empty)
                                                 .Replace(";", string.Empty)
                                                 .Split('=')
                                                 .Select(x => x.Trim())
                                                 .ToArray();

                                string message = null;

                                if (parts.Length > 0)
                                {
                                    if (!CodeGenerator.IsValidLanguageIndependentIdentifier(parts[0]))
                                    {
                                        message = $"Could not add '{parts[0]}' to {element.Name}: '{parts[0]}' is not a valid .NET identifier";
                                    }
                                    else if (element.Values.Any(x => x.Name == parts[0]))
                                    {
                                        message = $"Could not add {parts[0]} to {element.Name}: {parts[0]} already in use";
                                    }
                                    else
                                    {
                                        switch (parts.Length)
                                        {
                                        case 1:

                                            element.Values.Add(new ModelEnumValue(element.Store,
                                                                                  new PropertyAssignment(ModelEnumValue.NameDomainPropertyId, parts[0])));

                                            break;

                                        case 2:

                                            element.Values.Add(new ModelEnumValue(element.Store,
                                                                                  new PropertyAssignment(ModelEnumValue.NameDomainPropertyId, parts[0]),
                                                                                  new PropertyAssignment(ModelEnumValue.ValueDomainPropertyId, parts[1])));

                                            break;

                                        default:
                                            message = $"Could not add '{codeFormLine}' to {element.Name}: The string was not in the proper format.";

                                            break;
                                        }
                                    }

                                    if (message != null)
                                    {
                                        Messages.AddWarning(message);
                                    }
                                }
                            }
                            catch (Exception exception)
                            {
                                Messages.AddWarning($"Could not parse '{codeFormLine}'. {exception.Message}. The line will be discarded.");
                            }
                        }

                        tx.Commit();
                    }
                }
            }
        }
Exemple #3
0
        private void OnMenuAddProperties(object sender, EventArgs e)
        {
            NodeShape shapeElement = CurrentSelection.OfType <ClassShape>().FirstOrDefault();

            if (shapeElement?.ModelElement is ModelClass element)
            {
                AddCodeForm codeForm = new AddCodeForm(element);

                if (codeForm.ShowDialog() == DialogResult.OK)
                {
                    using (Transaction tx = element.Store.TransactionManager.BeginTransaction("AddProperties"))
                    {
                        element.Attributes.Clear();

                        foreach (string codeFormLine in codeForm.Lines)
                        {
                            try
                            {
                                ParseResult parseResult = ModelAttribute.Parse(element.ModelRoot, codeFormLine);

                                if (parseResult == null)
                                {
                                    Messages.AddWarning($"Could not parse '{codeFormLine}'. The line will be discarded.");

                                    continue;
                                }

                                string message = null;

                                if (string.IsNullOrEmpty(parseResult.Name) || !CodeGenerator.IsValidLanguageIndependentIdentifier(parseResult.Name))
                                {
                                    message = $"Could not add '{parseResult.Name}' to {element.Name}: '{parseResult.Name}' is not a valid .NET identifier";
                                }
                                else if (element.AllAttributes.Any(x => x.Name == parseResult.Name))
                                {
                                    message = $"Could not add {parseResult.Name} to {element.Name}: {parseResult.Name} already in use";
                                }
                                else if (element.AllNavigationProperties().Any(p => p.PropertyName == parseResult.Name))
                                {
                                    message = $"Could not add {parseResult.Name} to {element.Name}: {parseResult.Name} already in use";
                                }

                                if (message != null)
                                {
                                    Messages.AddWarning(message);

                                    continue;
                                }

                                ModelAttribute modelAttribute = new ModelAttribute(element.Store,
                                                                                   new PropertyAssignment(ModelAttribute.NameDomainPropertyId, parseResult.Name),
                                                                                   new PropertyAssignment(ModelAttribute.TypeDomainPropertyId, parseResult.Type ?? "String"),
                                                                                   new PropertyAssignment(ModelAttribute.RequiredDomainPropertyId, parseResult.Required ?? true),
                                                                                   new PropertyAssignment(ModelAttribute.MaxLengthDomainPropertyId, parseResult.MaxLength ?? -1),
                                                                                   new PropertyAssignment(ModelAttribute.MinLengthDomainPropertyId, parseResult.MinLength ?? 0),
                                                                                   new PropertyAssignment(ModelAttribute.InitialValueDomainPropertyId, parseResult.InitialValue),
                                                                                   new PropertyAssignment(ModelAttribute.IsIdentityDomainPropertyId, parseResult.IsIdentity),
                                                                                   new PropertyAssignment(ModelAttribute.SetterVisibilityDomainPropertyId, parseResult.SetterVisibility ?? SetterAccessModifier.Public));

                                element.Attributes.Add(modelAttribute);
                            }
                            catch (Exception exception)
                            {
                                Messages.AddWarning($"Could not parse '{codeFormLine}'. {exception.Message}. The line will be discarded.");
                            }
                        }

                        tx.Commit();
                    }
                }
            }
        }