public void ChangeBlueprintId(BlueprintViewModel blueprintViewModel, string newBlueprintId) { if (this.blueprintManager == null) { return; } var oldBlueprintId = blueprintViewModel.BlueprintId; this.blueprintManager.ChangeBlueprintId(oldBlueprintId, newBlueprintId); blueprintViewModel.BlueprintId = newBlueprintId; // Validate new blueprint id again as it may be valid/invalid now. this.OnPropertyChanged("NewBlueprintId"); // Update parents. foreach ( var viewModel in this.Blueprints.Where(viewModel => oldBlueprintId.Equals(viewModel.Blueprint.ParentId)) ) { viewModel.Blueprint.ParentId = newBlueprintId; } }
/// <summary> /// Creates a new blueprint. /// </summary> /// <param name="blueprintId">Id for new blueprint.</param> /// <param name="original">Original blueprint to copy. Null if to create an empty blueprint.</param> /// <returns>New blueprint view model.</returns> public BlueprintViewModel CreateNewBlueprint(string blueprintId, BlueprintViewModel original = null) { if (this.CurrentBlueprintManager == null) { throw new InvalidOperationException("No blueprint file selected. Please select a blueprint file to add the new blueprint to!"); } // Update blueprint view models. var newBlueprint = new BlueprintViewModel( blueprintId, original != null ? new Blueprint(original.Blueprint) : new Blueprint()) { AssemblyComponents = this.assemblyComponents }; // Parent under same blueprint. if (original != null) { newBlueprint.Parent = original.Parent; } this.Blueprints.Add(newBlueprint); return(newBlueprint); }
private void OnBlueprintsChanged(object sender, NotifyCollectionChangedEventArgs e) { string undoMessage = null; if (e.Action == NotifyCollectionChangedAction.Add) { // Update blueprints in blueprint manager. foreach (BlueprintViewModel item in e.NewItems) { this.CurrentBlueprintManager.AddBlueprint(item.BlueprintId, item.Blueprint); } if (e.NewItems.Count == 1) { BlueprintViewModel item = (BlueprintViewModel)e.NewItems[0]; undoMessage = string.Format("Add blueprint '{0}'", item.BlueprintId); } else { undoMessage = "Add blueprints"; } foreach (BlueprintViewModel item in e.NewItems) { item.Root = this; // Setup correct parent hierarchy. if (item.Parent != null) { this.ReparentBlueprint(item.BlueprintId, item.Parent.BlueprintId, false); } } } else if (e.Action == NotifyCollectionChangedAction.Remove) { // Update blueprints in blueprint manager. foreach (BlueprintViewModel item in e.OldItems) { this.blueprintManager.RemoveBlueprint(item.BlueprintId); } if (e.OldItems.Count == 1) { BlueprintViewModel item = (BlueprintViewModel)e.OldItems[0]; undoMessage = string.Format("Remove blueprint '{0}'", item.BlueprintId); } else { undoMessage = "Remove blueprints"; } foreach (BlueprintViewModel item in e.OldItems) { item.Root = null; // Remove from parent's children list, if necessary. if (item.Parent != null) { item.Parent.DerivedBlueprints.Remove(item); } } } // This line will log the collection change with the undo framework. DefaultChangeFactory.Current.OnCollectionChanged( this, "Blueprints", this.Blueprints, e, undoMessage ?? "Collection of Blueprints changed"); // Validate new blueprint id again as it may be valid/invalid now. this.OnPropertyChanged("NewBlueprintId"); this.RefreshBlueprintView(); }