/// <summary> /// Initializes a new instance of the <see cref="AssetViewModelConstructionParameters"/> class with the parameter to forward to the <see cref="AssetViewModel"/> constructor. /// </summary> /// <param name="serviceProvider">The service provider to use for asset view model.</param> /// <param name="directory">The directory containing the asset.</param> /// <param name="package">The project containing the asset, or in which to add the asset if it's a new asset.</param> /// <param name="assetItem">The <see cref="AssetItem"/> instance containing the asset with its current location.</param> /// <param name="container">The <see cref="NodeContainer"/> used to store the graph of properties of this asset.</param> /// <param name="canUndoRedoCreation">Indicates whether the creation of this view model will create a transaction in the undo/redo service.</param> internal AssetViewModelConstructionParameters(IViewModelServiceProvider serviceProvider, DirectoryBaseViewModel directory, Package package, AssetItem assetItem, SessionNodeContainer container, bool canUndoRedoCreation) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } if (directory.Package == null) { throw new ArgumentException("The provided directory must be in a project when creating an asset."); } if (package == null) { throw new ArgumentNullException(nameof(package)); } if (assetItem == null) { throw new ArgumentNullException(nameof(assetItem)); } if (container == null) { throw new ArgumentNullException(nameof(container)); } Directory = directory; Package = package; AssetItem = assetItem; Container = container; ServiceProvider = serviceProvider; CanUndoRedoCreation = canUndoRedoCreation; }
public DirectoryBaseViewModel GetOrCreateDirectory(string path, bool canUndoRedoCreation) { if (path == null) { throw new ArgumentNullException(nameof(path)); } DirectoryBaseViewModel result = this; if (!string.IsNullOrEmpty(path)) { var directoryNames = path.Split(Separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); result = directoryNames.Aggregate(result, (current, next) => current.SubDirectories.FirstOrDefault(x => string.Equals(next, x.Name, StringComparison.InvariantCultureIgnoreCase)) ?? new DirectoryViewModel(next, current, canUndoRedoCreation)); } return(result); }
/// <summary> /// Set the parent of this directory and properly update <see cref="SubDirectories"/> collection of the previous and the new parent. /// </summary> /// <remarks>Should be invoked only by the setter of <see cref="Parent"/>.</remarks> /// <param name="oldParent">The old parent of this directory.</param> /// <param name="newParent">The nwe parent of this directory.</param> protected void SetParent(DirectoryBaseViewModel oldParent, DirectoryBaseViewModel newParent) { if (this is not DirectoryViewModel directory) { throw new InvalidOperationException("Can't change the parent of this folder"); } Dispatcher.Invoke(() => { oldParent?.subDirectories.Remove(directory); if (newParent != null) { newParent.subDirectories.Add(directory); UpdateAssetUrls(); } }); }
public DirectoryBaseViewModel GetDirectory(string path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } var directoryNames = path.Split(Separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); DirectoryBaseViewModel currentDirectory = this; foreach (var directoryName in directoryNames) { currentDirectory = currentDirectory.SubDirectories.FirstOrDefault(x => string.Equals(directoryName, x.Name, StringComparison.InvariantCultureIgnoreCase)); if (currentDirectory == null) { return(null); } } return(currentDirectory); }
/// <summary> /// Initializes a new instance of the <see cref="DirectoryViewModel"/> class with a parent directory. /// </summary> /// <param name="name">The name of this directory. Cannot be <c>null</c>.</param> /// <param name="parent">The parent of this directory. Cannot be <c>null</c>.</param> /// <param name="canUndoRedoCreation">Indicates if the creation of this view model should create a transaction in the undo/redo service.</param> public DirectoryViewModel(string name, DirectoryBaseViewModel parent, bool canUndoRedoCreation) : base(parent.SafeArgument(nameof(parent)).Package.Session) { this.name = name ?? throw new ArgumentNullException(nameof(name)); // Update property to make the directory dirty. The name must be already set here! if (canUndoRedoCreation) { Parent = parent; } else { this.parent = parent; SetParent(null, parent); } if (!IsValidName(name, out string errorMessage)) { throw new ArgumentException(errorMessage); } RenameCommand = new AnonymousCommand(ServiceProvider, () => IsEditing = true); }
private AssetViewModel CreateAsset(DirectoryBaseViewModel directory, AssetItem assetItem, bool canUndoRedoCreation, LoggerResult loggerResult, bool isLoading) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } if (assetItem == null) { throw new ArgumentNullException(nameof(assetItem)); } AssetCollectionItemIdHelper.GenerateMissingItemIds(assetItem.Asset); var parameters = new AssetViewModelConstructionParameters(ServiceProvider, directory, Package, assetItem, directory.Session.AssetNodeContainer, canUndoRedoCreation); Session.GraphContainer.InitializeAsset(assetItem, loggerResult); var assetType = assetItem.Asset.GetType(); var assetViewModelType = typeof(AssetViewModel <>); while (assetType != null) { if (Session.AssetViewModelTypes.TryGetValue(assetType, out assetViewModelType)) { break; } assetViewModelType = typeof(AssetViewModel <>); assetType = assetType.BaseType; } if (assetViewModelType.IsGenericType) { assetViewModelType = assetViewModelType.MakeGenericType(assetItem.Asset.GetType()); } var asset = (AssetViewModel)Activator.CreateInstance(assetViewModelType, parameters); if (!isLoading) { asset.Initialize(); } return(asset); }
public void MoveAsset(AssetViewModel asset, DirectoryBaseViewModel directory) { asset.MoveAsset(Package, directory); }
public AssetViewModel CreateAsset(DirectoryBaseViewModel directory, AssetItem assetItem, bool canUndoRedoCreation, LoggerResult loggerResult) { return(CreateAsset(directory, assetItem, canUndoRedoCreation, loggerResult, false)); }