/// <summary> /// Create command method used to create components /// </summary> /// <param name="t"></param> private void CreateComponent(Type t) { Component comp = Model.CreateComponent(t, SelectedComponentFieldsList.Select(field => field.fieldValue).ToArray()); if (ComponentCollection.Where(c => c.GetName() == comp.GetName() && c.GetType() == t).Count() == 0) { ComponentCollection.Add(comp); if (comp.GetType() == typeof(Computer)) { ComputerCollection.Add(new ComputerItem(comp as Computer, SelectedComponentFieldsList)); } } else { MessageBox.Show("Component of such type with such name is already added!", "Error", MessageBoxButton.OK); } }
/// <summary> /// Updates component instance from user input /// </summary> /// <param name="t"></param> private void UpdateComponent(Component t) { int index = 0; if (t is Computer) //Required to find match in computer collection because it might be impossible to find it after update call { index = ComputerCollection.IndexOf(ComputerCollection.Single(i => i.Pc == t)); } t.Update(SelectedComponentFieldsList.Select(field => field.fieldValue).ToArray()); SelectedComponentType = null; SelectedComponentType = t.GetType(); SelectedComponentInstance = t; List <ComputerItem> tmp = ComputerCollection.Where(i => i.Fields.Where(f => f.fieldInfo.FieldType == t.GetType()).Count() != 0).ToList(); tmp.ForEach(i => i.Fields.Where(f => f.fieldInfo.FieldType == t.GetType()).ToList().ForEach(f => { f.fieldInfo.SetValue(i.Pc, t); f.fieldValue = t; })); if (t is Computer) { ComputerCollection[index] = new ComputerItem(t as Computer, SelectedComponentFieldsList); } var a = ComputerCollection; ComputerCollection = null; ComputerCollection = a; }
/// <summary> /// Check if component instance is selected and all fields are valid /// </summary> /// <returns></returns>5 private bool CanUpdateComponent() { return((SelectedComponentType == null) || (SelectedComponentFieldsList.Where(f => f.fieldInfo.FieldType.BaseType == typeof(Component) ? f.fieldValue == null : string.IsNullOrEmpty(f.fieldValue?.ToString())).Count() != 0) || (SelectedComponentInstance == null) ? false : true); }