public static string GenerateNewPropertyName(this SettingsStoreSubCollection subCollection) { ThreadHelper.ThrowIfNotOnUIThread(); var settingsStore = subCollection.Root.SettingsStore; for (int i = 1; i < 100; i++) { var name = Invariant($"New Value #{i}"); ErrorHandler.ThrowOnFailure(settingsStore.PropertyExists(subCollection.Path, name, out int exists)); if (exists == 0) { return(name); } } throw new InvalidOperationException("Could not find a unique name for the new value."); }
private void InPlaceEditSubCollectionName(SettingsStoreSubCollection subCollection, IVsWritableSettingsStore settingsStore) { _control.InPlaceEditTreeViewItem(subCollection, subCollection.Name, newName => { ThreadHelper.ThrowIfNotOnUIThread(); if (newName.Length == 0) { var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, Guid.Empty, "Error renaming collection", $"A collection name cannot be blank. Try again with a different name.", null, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_CRITICAL, 0, out int result)); return; } if (newName.IndexOfAny(s_invalidCollectionNameChars) >= 0) { var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, Guid.Empty, "Error renaming collection", $"A collection name cannot contain a backslash character (\\). Try again with a different name.", null, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_CRITICAL, 0, out int result)); return; } // Create a sibling and check for duplicate names. var parent = subCollection.Parent; var renamedSubCollection = new SettingsStoreSubCollection(parent, newName); if (settingsStore.CollectionExists(renamedSubCollection.Path)) { var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, Guid.Empty, "Error renaming collection", $"There is already a collection called '{newName}'. Try again with a different name.", null, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_CRITICAL, 0, out int result)); return; } // Clone and recreate the entire tree beneath this collection and then delete the original. ErrorHandler.ThrowOnFailure(settingsStore.CreateCollection(renamedSubCollection.Path)); settingsStore.CopyTree(subCollection, renamedSubCollection); ErrorHandler.ThrowOnFailure(settingsStore.DeleteCollection(subCollection.Path)); // Update the view model. subCollection.Rename(newName); // Select the newly-renamed sub-collection. _control.SetTreeViewSelection(subCollection); Telemetry.Client.TrackEvent("RenameSubCollection"); }); }
public static SettingsStoreSubCollection GenerateNewSubCollection(this SettingsStoreSubCollection subCollection) { ThreadHelper.ThrowIfNotOnUIThread(); var settingsStore = subCollection.Root.SettingsStore; for (int i = 1; i < 100; i++) { var name = Invariant($"New Collection #{i}"); var newCollection = new SettingsStoreSubCollection(subCollection, name); ErrorHandler.ThrowOnFailure(settingsStore.CollectionExists(newCollection.Path, out int exists)); if (exists == 0) { return(newCollection); } } throw new InvalidOperationException("Could not find a unique name for the new subcollection."); }
public void SetTreeViewSelection(SettingsStoreSubCollection subCollection) { var treeViewItem = treeView.ItemContainerGenerator.ContainerFromItemRecursive <TreeViewItem>(subCollection); if (treeViewItem == null) { // Try expanding the path to root. var parents = new Stack <SettingsStoreSubCollection>(); for (var parentCollection = subCollection.Parent; parentCollection != null; parentCollection = parentCollection.Parent) { parents.Push(parentCollection); } while (parents.Count != 0) { var parentCollection = parents.Pop(); var parentTreeViewItem = treeView.ItemContainerGenerator.ContainerFromItemRecursive <TreeViewItem>(parentCollection); if (parentTreeViewItem != null && !parentTreeViewItem.IsExpanded) { parentTreeViewItem.IsExpanded = true; // Force a re-layout and try again #pragma warning disable VSTHRD001 // Avoid legacy thread switching APIs Dispatcher.Invoke(() => SetTreeViewSelection(subCollection), DispatcherPriority.Render); #pragma warning restore VSTHRD001 // Avoid legacy thread switching APIs return; } } // Give up. return; } if (treeViewItem != null) { treeViewItem.BringIntoView(); treeViewItem.Focus(); } }
public static void CopyTree(this IVsWritableSettingsStore writableSettingsStore, SettingsStoreSubCollection from, SettingsStoreSubCollection to) { ThreadHelper.ThrowIfNotOnUIThread(); writableSettingsStore.CopyProperties(from, to); var fromStore = from.Root.SettingsStore; var fromPath = from.Path; for (uint index = 0; ; index++) { if (ErrorHandler.Failed(fromStore.GetSubCollectionName(fromPath, index, out var name))) { break; } var newSubCollection = new SettingsStoreSubCollection(to, name); ErrorHandler.ThrowOnFailure(writableSettingsStore.CreateCollection(newSubCollection.Name)); writableSettingsStore.CopyTree(new SettingsStoreSubCollection(from, name), newSubCollection); } }
public static void CopyProperties(this IVsWritableSettingsStore writableSettingsStore, SettingsStoreSubCollection from, SettingsStoreSubCollection to) { ThreadHelper.ThrowIfNotOnUIThread(); var fromStore = from.Root.SettingsStore; var fromPath = from.Path; var toPath = to.Path; for (uint index = 0; ; index++) { if (ErrorHandler.Failed(fromStore.GetPropertyName(fromPath, index, out var name))) { break; } if (ErrorHandler.Failed(fromStore.GetPropertyType(fromPath, name, out var type))) { break; } switch ((__VsSettingsType)type) { case __VsSettingsType.SettingsType_String: ErrorHandler.ThrowOnFailure(fromStore.GetString(fromPath, name, out var stringValue)); ErrorHandler.ThrowOnFailure(writableSettingsStore.SetString(toPath, name, stringValue)); break; case __VsSettingsType.SettingsType_Int: ErrorHandler.ThrowOnFailure(fromStore.GetInt(fromPath, name, out var intValue)); ErrorHandler.ThrowOnFailure(writableSettingsStore.SetInt(toPath, name, intValue)); break; case __VsSettingsType.SettingsType_Int64: ErrorHandler.ThrowOnFailure(fromStore.GetInt64(fromPath, name, out var longValue)); ErrorHandler.ThrowOnFailure(writableSettingsStore.SetInt64(toPath, name, longValue)); break; case __VsSettingsType.SettingsType_Binary: uint[] actualByteLength = { 0 }; ErrorHandler.ThrowOnFailure(fromStore.GetBinary(fromPath, name, 0, null, actualByteLength)); byte[] bytes = new byte[actualByteLength[0]]; ErrorHandler.ThrowOnFailure(fromStore.GetBinary(fromPath, name, actualByteLength[0], bytes, actualByteLength)); ErrorHandler.ThrowOnFailure(writableSettingsStore.SetBinary(toPath, name, actualByteLength[0], bytes)); break; } } }
protected SettingsStoreItem(SettingsStoreSubCollection parent, string name) { Parent = parent; _name = name; }