private void bApply_Click(object sender, RoutedEventArgs e) { bApply.Focus(); error = false; controller.BeginMacro(); //controller.CreatedMacro.Description = string.Format("PSM Classs '{0}' was updated. ", psmClass); if (tbName.ValueChanged) { acmdRenameComponent renameCommand = new acmdRenameComponent(controller, psmClass, tbName.Text); controller.CreatedMacro.Commands.Add(renameCommand); tbName.ForgetOldValue(); } //if (psmClass.IsAbstract != cbAbstract.IsChecked) //{ // psmClassController.ChangeAbstract(cbAbstract.IsChecked == true); //} //if (psmClass.AllowAnyAttribute != cbAnyAttribute.IsChecked) //{ // psmClassController.ChangeAllowAnyAttributeDefinition(cbAnyAttribute.IsChecked == true); //} #region check for deleted attributes List <PSMAttribute> removedAttributes = new List <PSMAttribute>(); List <FakePSMAttribute> addedAttributes = new List <FakePSMAttribute>(); foreach (PSMAttribute psmAttribute in psmClass.PSMAttributes) { bool found = false; foreach (FakePSMAttribute fakeAttribute in fakeAttributes) { if (fakeAttribute.SourceAttribute == psmAttribute && fakeAttribute.Checked) { found = true; break; } else if (fakeAttribute.SourceAttribute == psmAttribute && !fakeAttribute.Checked) { fakeAttribute.SourceAttribute = null; } } if (!found) { removedAttributes.Add(psmAttribute); cmdDeletePSMAttribute deleteCommand = new cmdDeletePSMAttribute(controller); deleteCommand.Set(psmAttribute); controller.CreatedMacro.Commands.Add(deleteCommand); } } #endregion #region remove dummy entries in fake collection List <FakePSMAttribute> toRemove = new List <FakePSMAttribute>(); foreach (FakePSMAttribute fakeAttribute in fakeAttributes) { if (String.IsNullOrEmpty(fakeAttribute.Name)) { if (fakeAttribute.SourceAttribute != null) { removedAttributes.Add(fakeAttribute.SourceAttribute); cmdDeletePSMAttribute deleteCommand = new cmdDeletePSMAttribute(controller); deleteCommand.Set(fakeAttribute.SourceAttribute); controller.CreatedMacro.Commands.Add(deleteCommand); } toRemove.Add(fakeAttribute); } } foreach (FakePSMAttribute attribute in toRemove) { fakeAttributes.Remove(attribute); } #endregion Dictionary <PSMAttribute, string> namesDict = new Dictionary <PSMAttribute, string>(); foreach (PSMAttribute a in psmClass.PSMAttributes) { if (!removedAttributes.Contains(a)) { namesDict.Add(a, a.Name); } } // check for changes and new attributes var modified = from FakePSMAttribute a in fakeAttributes where a.SourceAttribute != null && !removedAttributes.Contains(a.SourceAttribute) && a.SomethingChanged() select a; var added = from FakePSMAttribute a in fakeAttributes where a.SourceAttribute == null select a; // editing exisiting attribute foreach (FakePSMAttribute modifiedAttribute in modified) { PSMAttribute sourceAttribute = modifiedAttribute.SourceAttribute; uint lower; UnlimitedInt upper; if ( !IHasCardinalityExt.ParseMultiplicityString(modifiedAttribute.Multiplicity, out lower, out upper)) { error = true; } cmdUpdatePSMAttribute updateCommand = new cmdUpdatePSMAttribute(controller); updateCommand.Set(sourceAttribute, modifiedAttribute.Type, modifiedAttribute.Name, lower, upper, modifiedAttribute.XFormElement, modifiedAttribute.DefaultValue); updateCommand.InterpretedAttribute = modifiedAttribute.RepresentedAttribute; controller.CreatedMacro.Commands.Add(updateCommand); namesDict[sourceAttribute] = modifiedAttribute.Name; } List <string> names = namesDict.Values.ToList(); // new attribute foreach (FakePSMAttribute addedAttribute in added) { if (!string.IsNullOrEmpty(addedAttribute.Name) && addedAttribute.Checked) { uint lower = 1; UnlimitedInt upper = 1; if (!String.IsNullOrEmpty(addedAttribute.Multiplicity)) { if (!IHasCardinalityExt.ParseMultiplicityString(addedAttribute.Multiplicity, out lower, out upper)) { error = true; } } cmdCreateNewPSMAttribute createNewPsmAttribute = new cmdCreateNewPSMAttribute(controller); createNewPsmAttribute.Set(psmClass, addedAttribute.Type, addedAttribute.Name, lower, upper, addedAttribute.XFormElement); if (addedAttribute.RepresentedAttribute != null) { createNewPsmAttribute.InterpretedAttribute = addedAttribute.RepresentedAttribute; } controller.CreatedMacro.Commands.Add(createNewPsmAttribute); addedAttributes.Add(addedAttribute); names.Add(addedAttribute.Name); } } if (error) { controller.CancelMacro(); } else { CommandBase tmp = (CommandBase)controller.CreatedMacro; controller.CommitMacro(); if (string.IsNullOrEmpty(tmp.ErrorDescription)) { foreach (FakePSMAttribute attribute in addedAttributes) { if (attribute.RepresentedAttribute != null) { attribute.SourceAttribute = (PSMAttribute)psmClass.PSMAttributes.Where (property => ((PSMAttribute)property). Interpretation == attribute.RepresentedAttribute). SingleOrDefault(); } else { attribute.SourceAttribute = psmClass.PSMAttributes.Where (property => property.Name == attribute.Name).SingleOrDefault(); } //else //{ // attribute.SourceAttribute = (PSMAttribute)psmClassController.Class.AllAttributes.Where // (property => (property.RepresentedAttribute == attribute.RepresentedAttribute).SingleOrDefault(); //} //if (attribute.SourceAttribute.RepresentedAttribute != null) // attribute.RepresentedAttribute = attribute.SourceAttribute.RepresentedAttribute; } addedAttributes.RemoveAll(attribute => attribute.SourceAttribute == null); bApply.IsEnabled = false; dialogReady = true; error = false; } else { error = true; } } //gridAttributes.ItemsSource.Refresh(); }