示例#1
0
        private void bApply_Click(object sender, RoutedEventArgs e)
        {
            bApply.Focus();
            if (isPSM)
            {
                throw new NotImplementedException("Method or operation is not implemented.");
            }

            modelController.BeginMacro();
            modelController.CreatedMacro.Description = CommandDescription.UPDATE_CLASS_MACRO;
            if (tbName.ValueChanged)
            {
                classController.RenameElement(tbName.Text, modelClass.Package.Classes.Cast <Class>());
                tbName.ForgetOldValue();
            }
            if (tbOnto.ValueChanged)
            {
                classController.ChangeOntologyEquivalent(tbOnto.Text);
                tbOnto.ForgetOldValue();
            }
            if (cbPackages.SelectedItem != modelClass.Package)
            {
                classController.MoveToPackage((Package)cbPackages.SelectedItem);
            }

            // check for deleted attributes
            List <Property>      removedAttributes = new List <Property>();
            List <FakeAttribute> addedAttributes   = new List <FakeAttribute>();

            foreach (Property attribute in classController.Class.Attributes)
            {
                bool found = false;
                foreach (FakeAttribute fakeAttribute in fakeAttributes)
                {
                    if (fakeAttribute.SourceAttribute == attribute)
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    removedAttributes.Add(attribute);
                    classController.RemoveAttribute(attribute);
                }
            }

            List <FakeAttribute> toRemove = new List <FakeAttribute>();

            foreach (FakeAttribute fakeAttribute in fakeAttributes)
            {
                if (String.IsNullOrEmpty(fakeAttribute.Name))
                {
                    if (fakeAttribute.SourceAttribute != null)
                    {
                        removedAttributes.Add(fakeAttribute.SourceAttribute);
                        classController.RemoveAttribute(fakeAttribute.SourceAttribute);
                    }
                    toRemove.Add(fakeAttribute);
                }
            }

            foreach (FakeAttribute attribute in toRemove)
            {
                fakeAttributes.Remove(attribute);
            }

            // check for changes and new attributes
            foreach (FakeAttribute fakeAttribute in fakeAttributes)
            {
                if (fakeAttribute.SourceAttribute != null && !removedAttributes.Contains(fakeAttribute.SourceAttribute))
                {
                    // editing old attribute
                    Property sourceAttribute = fakeAttribute.SourceAttribute;
                    if (fakeAttribute.Name != sourceAttribute.Name)
                    {
                        classController.RenameAttribute(sourceAttribute, fakeAttribute.Name);
                    }
                    if (fakeAttribute.Type != sourceAttribute.Type)
                    {
                        classController.ChangeAttributeType(sourceAttribute, new ElementHolder <DataType>(fakeAttribute.Type));
                    }
                    if (fakeAttribute.Default != sourceAttribute.Default)
                    {
                        classController.ChangeAttributeDefaultValue(sourceAttribute, fakeAttribute.Default);
                    }
                    if (fakeAttribute.Multiplicity != sourceAttribute.MultiplicityString)
                    {
                        if (!String.IsNullOrEmpty(fakeAttribute.Multiplicity))
                        {
                            uint?            lower;
                            UnlimitedNatural upper;
                            if (!MultiplicityElementController.ParseMultiplicityString(fakeAttribute.Multiplicity, out lower, out upper))
                            {
                                return;
                            }
                            MultiplicityElementController.ChangeMultiplicityOfElement(sourceAttribute, classController.Class, lower, upper, classController.DiagramController.ModelController);
                        }
                        else
                        {
                            MultiplicityElementController.ChangeMultiplicityOfElement(sourceAttribute, classController.Class, null, 1, classController.DiagramController.ModelController);
                        }
                    }
                }
                else
                {
                    // new attribute
                    if (!string.IsNullOrEmpty(fakeAttribute.Name))
                    {
                        uint?            lower = 1;
                        UnlimitedNatural upper = 1;
                        if (!String.IsNullOrEmpty(fakeAttribute.Multiplicity))
                        {
                            if (!MultiplicityElementController.ParseMultiplicityString(fakeAttribute.Multiplicity, out lower, out upper))
                            {
                                return;
                            }
                        }
                        classController.AddNewAttribute(fakeAttribute.Name, fakeAttribute.Type, lower, upper, fakeAttribute.Default);
                        addedAttributes.Add(fakeAttribute);
                    }
                }
            }

            CommandBase tmp = (CommandBase)modelController.CreatedMacro;

            modelController.CommitMacro();
            if (string.IsNullOrEmpty(tmp.ErrorDescription))
            {
                foreach (FakeAttribute attribute in addedAttributes)
                {
                    attribute.SourceAttribute =
                        classController.Class.Attributes.Where(property => property.Name == attribute.Name).SingleOrDefault();
                }
                addedAttributes.RemoveAll(attribute => attribute.SourceAttribute == null);
                bApply.IsEnabled = false;
                dialogReady      = true;
                error            = false;
            }
            else
            {
                error = true;
            }
        }